lottery.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. /**
  3. * Classe lottery
  4. *
  5. * Cette classe gère les opérations liées aux loteries, telles que la récupération des données, l'inscription, la mise à jour et la suppression.
  6. */
  7. class lottery
  8. {
  9. /**
  10. * Récupère les informations des loteries.
  11. *
  12. * @return array Résultats des loteries.
  13. */
  14. public static function getLotterys()
  15. {
  16. db::query("SELECT "
  17. . "" . DB_T_LOTTERY . ".id, "
  18. . "" . DB_T_LOTTERY . ".md5, "
  19. . "" . DB_T_LOTTERY . ".titre, "
  20. . "" . DB_T_LOTTERY . ".description, "
  21. . "" . DB_T_LOTTERY . ".sortDate, "
  22. . "COUNT(" . DB_T_LOTTERY_INSCRITS . ".login) AS m_inscrits, "
  23. . "SUM(IF(" . DB_T_LOTTERY_INSCRITS . ".valide = 1, 1, 0)) AS m_eligible, "
  24. . "SUM(IF(" . DB_T_LOTTERY_INSCRITS . ".valide = 0, 1, 0)) AS m_ineligible, "
  25. . "COUNT(" . DB_T_LOTTERY_INSCRITS . ".selected) AS m_selected, "
  26. . "" . DB_T_LOTTERY . ".cree, "
  27. . "" . DB_T_LOTTERY . ".maj, "
  28. . "CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'user' "
  29. . "FROM " . DB_T_LOTTERY . " "
  30. . "LEFT JOIN " . DB_T_LOTTERY_INSCRITS . " ON " . DB_T_LOTTERY_INSCRITS . ".id_lottery = " . DB_T_LOTTERY . ".id "
  31. . "INNER JOIN " . DB_T_USER . " ON " . DB_T_LOTTERY . ".id_user = " . DB_T_USER . ".id "
  32. . "GROUP BY 1");
  33. return db::resultset();
  34. }
  35. /**
  36. * Récupère les inscriptions pour une loterie donnée.
  37. *
  38. * @param int $_id Identifiant de la loterie.
  39. * @return array Résultats des inscriptions.
  40. */
  41. public static function getInscription(int $_id){
  42. db::query("SELECT "
  43. . "" . DB_T_LOTTERY_INSCRITS . ".id_lottery, "
  44. . "" . DB_T_LOTTERY_INSCRITS . ".id_salarie, "
  45. . "" . DB_T_LOTTERY_INSCRITS . ".id_presta, "
  46. . "" . DB_T_LOTTERY_INSCRITS . ".id_dossier, "
  47. . "IF(" . DB_T_SALARIES . ".actif = 1, 'ACTIF', IF(" . DB_T_SALARIES . ".actif = 0, 'INACTIF', 'ABSENT')) AS actif , "
  48. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".login) AS login, "
  49. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".prenom) AS prenom, "
  50. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".nom) AS nom, "
  51. . "UCASE(" . DB_T_SALARIES . ".lieu) AS lieu, "
  52. . "IF(" . DB_T_LOTTERY_INSCRITS . ".valide = 1, 'OUI', 'NON') AS valide, "
  53. . "" . DB_T_LOTTERY_INSCRITS . ".selected, "
  54. . "" . DB_T_LOTTERY_INSCRITS . ".cree, "
  55. . "" . DB_T_LOTTERY_INSCRITS . ".id_user, "
  56. . "CONCAT(" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom ) AS admin, "
  57. . "" . DB_T_LOTTERY_INSCRITS . ".cree "
  58. . "FROM " . DB_T_LOTTERY_INSCRITS . " "
  59. . "INNER JOIN " . DB_T_USER . " ON " . DB_T_LOTTERY_INSCRITS . ".id_user = " . DB_T_USER . ".id "
  60. . "LEFT JOIN " . DB_T_SALARIES . " ON " . DB_T_LOTTERY_INSCRITS . ".id_salarie = " . DB_T_SALARIES . ".id "
  61. . "WHERE " . DB_T_LOTTERY_INSCRITS . ".id_lottery = :id_lottery");
  62. db::bind(':id_lottery', $_id);
  63. return db::resultset();
  64. }
  65. /**
  66. * Récupère les gagnants d'une loterie donnée.
  67. *
  68. * @param int $_id Identifiant de la loterie.
  69. * @return array Résultats des gagnants.
  70. */
  71. public static function getWinners(int $_id){
  72. db::query("SELECT "
  73. . "" . DB_T_LOTTERY_INSCRITS . ".id_salarie, "
  74. . "" . DB_T_LOTTERY_INSCRITS . ".id_dossier, "
  75. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".login) AS login, "
  76. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".prenom) AS prenom, "
  77. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".nom) AS nom, "
  78. . "" . DB_T_LOTTERY_INSCRITS . ".selected "
  79. . "FROM " . DB_T_LOTTERY_INSCRITS . " "
  80. . "WHERE " . DB_T_LOTTERY_INSCRITS . ".id_lottery = :id_lottery AND " . DB_T_LOTTERY_INSCRITS . ".selected IS NOT NULL");
  81. db::bind(':id_lottery', $_id);
  82. return db::resultset();
  83. }
  84. /**
  85. * Récupère les informations de clôture d'une loterie.
  86. *
  87. * @param int $_id Identifiant de la loterie.
  88. * @return array Informations de clôture.
  89. */
  90. public static function getCloture(int $_id){
  91. db::query("SELECT "
  92. . DB_T_LOTTERY . ".sortDate, "
  93. . DB_T_LOTTERY . ".sortNb, "
  94. . "CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'sortBy' "
  95. . "FROM " . DB_T_LOTTERY . " "
  96. . "INNER JOIN " . DB_T_USER . " ON " . DB_T_LOTTERY . ".id_user = " . DB_T_USER . ".id "
  97. . "WHERE " . DB_T_LOTTERY . ".id = :id");
  98. db::bind(':id', $_id);
  99. return db::single();
  100. }
  101. /**
  102. * Récupère les inscriptions triées pour une loterie donnée.
  103. *
  104. * @param int $_id Identifiant de la loterie.
  105. * @param int $_sort Nombre d'inscriptions à trier (0 pour tout).
  106. * @return array Résultats triés.
  107. */
  108. public static function getSortInscription(int $_id, int $_sort = 0)
  109. {
  110. if($_sort == 0){
  111. $sqlSort = "";
  112. } else {
  113. $sqlSort = " ORDER BY RAND() LIMIT " . $_sort;
  114. }
  115. db::query("SELECT "
  116. . "" . DB_T_LOTTERY_INSCRITS . ".id_lottery, "
  117. . "" . DB_T_LOTTERY_INSCRITS . ".id_salarie, "
  118. . "" . DB_T_LOTTERY_INSCRITS . ".id_dossier, "
  119. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".login) AS login, "
  120. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".prenom) AS prenom, "
  121. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".nom) AS nom "
  122. . "FROM " . DB_T_LOTTERY_INSCRITS . " "
  123. . "INNER JOIN " . DB_T_SALARIES . " ON " . DB_T_LOTTERY_INSCRITS . ".id_salarie = " . DB_T_SALARIES . ".id "
  124. . "WHERE " . DB_T_LOTTERY_INSCRITS . ".id_lottery = :id_lottery AND " . DB_T_SALARIES . ".actif = 1" . $sqlSort);
  125. db::bind(':id_lottery', $_id);
  126. return db::resultset();
  127. }
  128. /**
  129. * Insère une nouvelle loterie dans la base de données.
  130. *
  131. * @return bool Succès ou échec de l'insertion.
  132. */
  133. public static function insert()
  134. {
  135. db::query("INSERT INTO " . DB_T_LOTTERY . " (md5, titre, description, id_user) VALUES (:md5, :titre, :description, :id_user)");
  136. db::bind(':md5', md5(time().rand(1000000000, 9999999999)));
  137. db::bind(':titre', core::getPost("titre"));
  138. db::bind(':description', core::getPost("description"));
  139. db::bind(':id_user', session::getId());
  140. try {
  141. db::execute();
  142. return TRUE;
  143. } catch (Exception $ex) {
  144. return FALSE;
  145. }
  146. }
  147. /**
  148. * Met à jour une loterie existante.
  149. *
  150. * @return bool Succès ou échec de la mise à jour.
  151. */
  152. public static function update()
  153. {
  154. db::query("UPDATE " . DB_T_LOTTERY . " SET "
  155. . "titre = :titre, "
  156. . "description = :description, "
  157. . "sortDate = :sortDate, "
  158. . "id_user = :id_user "
  159. . "WHERE id = :id");
  160. db::bind(':titre', core::getPost("titre"));
  161. db::bind(':description', core::getPost("description"));
  162. db::bind(':sortDate', core::getPost("sortDate"));
  163. db::bind(':id_user', session::getId());
  164. db::bind(':id', core::getPost("id"));
  165. try {
  166. db::execute();
  167. return TRUE;
  168. } catch (Exception $ex) {
  169. return FALSE;
  170. }
  171. }
  172. /**
  173. * Insère une inscription pour une loterie.
  174. *
  175. * @param array $_lottery Données de l'inscription.
  176. * @return bool Succès ou échec de l'insertion.
  177. */
  178. public static function insertInscription(array $_lottery)
  179. {
  180. db::query("INSERT INTO " . DB_T_LOTTERY_INSCRITS . "
  181. (
  182. id_lottery,
  183. id_salarie,
  184. id_presta,
  185. id_dossier,
  186. login,
  187. prenom,
  188. nom,
  189. valide,
  190. id_user
  191. )
  192. VALUES
  193. (
  194. :id_lottery,
  195. :id_salarie,
  196. :id_presta,
  197. :id_dossier,
  198. :login,
  199. :prenom,
  200. :nom,
  201. :valide,
  202. :id_user
  203. )");
  204. db::bind(':id_lottery', core::getPost("lottery"));
  205. db::bind(':id_salarie', $_lottery["id_salarie"]);
  206. db::bind(':id_presta', $_lottery["id_presta"]);
  207. db::bind(':id_dossier', $_lottery["id_dossier"]);
  208. db::bind(':login', $_lottery["login"]);
  209. db::bind(':prenom', $_lottery["prenom"]);
  210. db::bind(':nom', $_lottery["nom"]);
  211. db::bind(':valide', $_lottery["valide"]);
  212. db::bind(':id_user', session::getId());
  213. try {
  214. db::execute();
  215. return TRUE;
  216. } catch (Exception $ex) {
  217. return FALSE;
  218. }
  219. }
  220. /**
  221. * Met à jour une inscription pour une loterie.
  222. *
  223. * @param array $_lottery Données de l'inscription.
  224. * @return bool Succès ou échec de la mise à jour.
  225. */
  226. public static function updateInscription(array $_lottery)
  227. {
  228. db::query("UPDATE " . DB_T_LOTTERY_INSCRITS . " "
  229. . "SET " . DB_T_LOTTERY_INSCRITS . ".login = :login, "
  230. . DB_T_LOTTERY_INSCRITS . ".prenom = :prenom, "
  231. . DB_T_LOTTERY_INSCRITS . ".nom = :nom, "
  232. . DB_T_LOTTERY_INSCRITS . ".valide = :valide, "
  233. . DB_T_LOTTERY_INSCRITS . ".id_user = :id_user, "
  234. . DB_T_LOTTERY_INSCRITS . ".id_salarie = :id_salarie "
  235. . "WHERE " . DB_T_LOTTERY_INSCRITS . ".id_dossier = :id_dossier AND " . DB_T_LOTTERY_INSCRITS . ".id_lottery = :id_lottery");
  236. db::bind(':login', $_lottery["login"]);
  237. db::bind(':prenom', $_lottery["prenom"]);
  238. db::bind(':nom', $_lottery["nom"]);
  239. db::bind(':valide', $_lottery["valide"]);
  240. db::bind(':id_user', session::getId());
  241. db::bind(':id_salarie', $_lottery["id_salarie"]);
  242. db::bind(':id_dossier', $_lottery["id_dossier"]);
  243. db::bind(':id_lottery', core::getPost("lottery"));
  244. db::bind(':id_user', session::getId());
  245. try {
  246. db::execute();
  247. return TRUE;
  248. } catch (Exception $ex) {
  249. return FALSE;
  250. }
  251. }
  252. /**
  253. * Définit un tirage au sort pour une loterie.
  254. *
  255. * @param int $_idDossier Identifiant du dossier.
  256. * @return bool Succès ou échec de l'opération.
  257. */
  258. public static function setSortLottery(int $_idDossier)
  259. {
  260. db::query("UPDATE " . DB_T_LOTTERY_INSCRITS . " "
  261. . "SET " . DB_T_LOTTERY_INSCRITS . ".selected = CURRENT_TIMESTAMP() "
  262. . "WHERE " . DB_T_LOTTERY_INSCRITS . ".id_dossier = :id_dossier");
  263. db::bind(':id_dossier', $_idDossier);
  264. try {
  265. db::execute();
  266. return TRUE;
  267. } catch (Exception $ex) {
  268. alert::recError("ERREUR TECHNIQUE : Au niveau de l'écriture en base de données lors du tirage au sort");
  269. return FALSE;
  270. }
  271. }
  272. /**
  273. * Supprime une loterie.
  274. *
  275. * @param int $_id Identifiant de la loterie.
  276. * @return bool Succès ou échec de la suppression.
  277. */
  278. public static function deleteLottery(int $_id)
  279. {
  280. db::query("DELETE FROM " . DB_T_LOTTERY . " WHERE id = :id");
  281. db::bind(':id', $_id);
  282. return db::execute();
  283. }
  284. /**
  285. * Supprime les inscriptions d'une loterie.
  286. *
  287. * @param int $_id Identifiant de la loterie.
  288. * @return bool Succès ou échec de la suppression.
  289. */
  290. public static function deleteInscriptionLottery(int $_id)
  291. {
  292. db::query("DELETE FROM " . DB_T_LOTTERY_INSCRITS . " WHERE id_lottery = :id_lottery");
  293. db::bind(':id_lottery', $_id);
  294. return db::execute();
  295. }
  296. /**
  297. * Définit la date et le nombre de tirages pour une loterie.
  298. *
  299. * @param int $_id Identifiant de la loterie.
  300. * @param int $_nb Nombre de tirages.
  301. * @return bool Succès ou échec de l'opération.
  302. */
  303. public static function setDateLottery(int $_id, int $_nb)
  304. {
  305. db::query("UPDATE " . DB_T_LOTTERY . " "
  306. . "SET " . DB_T_LOTTERY . ".sortDate = CURRENT_TIMESTAMP(), "
  307. . DB_T_LOTTERY . ".sortNb = :sortNb, "
  308. . DB_T_LOTTERY . ".sortBy = :sortBy "
  309. . "WHERE " . DB_T_LOTTERY . ".id = :id");
  310. db::bind(':sortNb', $_nb);
  311. db::bind(':sortBy', session::getId());
  312. db::bind(':id', $_id);
  313. try {
  314. db::execute();
  315. return TRUE;
  316. } catch (Exception $ex) {
  317. alert::recError("ERREUR TECHNIQUE : Au niveau de l'écriture en base de données lors de la datation");
  318. return FALSE;
  319. }
  320. }
  321. /**
  322. * Récupère les données d'inscription pour une loterie.
  323. *
  324. * @param int $_id Identifiant de la loterie.
  325. * @return array Données d'inscription.
  326. */
  327. public static function getInscriptionData(int $_id)
  328. {
  329. db::query("SELECT "
  330. . "COUNT(" . DB_T_LOTTERY_INSCRITS . ".login) AS inscrits, "
  331. . "SUM(IF(" . DB_T_LOTTERY_INSCRITS . ".valide = 1, 1, 0)) AS eligible, "
  332. . "SUM(IF(" . DB_T_LOTTERY_INSCRITS . ".valide = 0, 1, 0)) AS ineligible "
  333. . "FROM " . DB_T_LOTTERY_INSCRITS . " "
  334. . "WHERE " . DB_T_LOTTERY_INSCRITS . ".id_lottery = :id_lottery");
  335. db::bind(':id_lottery', $_id);
  336. return db::single();
  337. }
  338. /**
  339. * Récupère les informations d'une loterie spécifique.
  340. *
  341. * @param int $_id Identifiant de la loterie.
  342. * @return array Informations de la loterie.
  343. */
  344. public static function getFiche(int $_id)
  345. {
  346. db::query("SELECT * FROM " . DB_T_LOTTERY . " WHERE id = :id");
  347. db::bind(':id', $_id);
  348. return db::single();
  349. }
  350. /**
  351. * Récupère l'identifiant de la dernière loterie.
  352. *
  353. * @return int Identifiant de la dernière loterie.
  354. */
  355. public static function lastLottery()
  356. {
  357. db::query("SELECT MAX(id) AS id FROM " . DB_T_LOTTERY);
  358. return db::single()["id"];
  359. }
  360. /**
  361. * Recherche une loterie par dossier.
  362. *
  363. * @param int $_id Identifiant du dossier.
  364. * @return int|null Identifiant de la loterie ou NULL si non trouvé.
  365. */
  366. public static function searchDossier(int $_id)
  367. {
  368. db::query("SELECT id_lottery FROM " . DB_T_LOTTERY_INSCRITS. " WHERE id_dossier = :id_dossier");
  369. db::bind(':id_dossier', $_id);
  370. $return = db::single();
  371. if($return == FALSE){
  372. return NULL;
  373. } else {
  374. return $return["id_lottery"];
  375. }
  376. }
  377. }