lottery.class.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. class lottery
  3. {
  4. public static function getLotterys()
  5. {
  6. db::query("SELECT "
  7. . "" . DB_T_LOTTERY . ".id, "
  8. . "" . DB_T_LOTTERY . ".md5, "
  9. . "" . DB_T_LOTTERY . ".titre, "
  10. . "" . DB_T_LOTTERY . ".description, "
  11. . "" . DB_T_LOTTERY . ".sortDate, "
  12. . "COUNT(" . DB_T_LOTTERY_INSCRITS . ".login) AS m_inscrits, "
  13. . "SUM(IF(" . DB_T_LOTTERY_INSCRITS . ".valide = 1, 1, 0)) AS m_eligible, "
  14. . "SUM(IF(" . DB_T_LOTTERY_INSCRITS . ".valide = 0, 1, 0)) AS m_ineligible, "
  15. . "COUNT(" . DB_T_LOTTERY_INSCRITS . ".selected) AS m_selected, "
  16. . "" . DB_T_LOTTERY . ".cree, "
  17. . "" . DB_T_LOTTERY . ".maj, "
  18. . "CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'user' "
  19. . "FROM " . DB_T_LOTTERY . " "
  20. . "LEFT JOIN " . DB_T_LOTTERY_INSCRITS . " ON " . DB_T_LOTTERY_INSCRITS . ".id_lottery = " . DB_T_LOTTERY . ".id "
  21. . "INNER JOIN " . DB_T_USER . " ON " . DB_T_LOTTERY . ".id_user = " . DB_T_USER . ".id "
  22. . "GROUP BY 1");
  23. return db::resultset();
  24. }
  25. public static function getInscription(int $_id){
  26. db::query("SELECT "
  27. . "" . DB_T_LOTTERY_INSCRITS . ".id_lottery, "
  28. . "" . DB_T_LOTTERY_INSCRITS . ".id_salarie, "
  29. . "" . DB_T_LOTTERY_INSCRITS . ".id_presta, "
  30. . "" . DB_T_LOTTERY_INSCRITS . ".id_dossier, "
  31. . "IF(" . DB_T_SALARIES . ".actif = 1, 'ACTIF', IF(" . DB_T_SALARIES . ".actif = 0, 'INACTIF', 'ABSENT')) AS actif , "
  32. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".login) AS login, "
  33. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".prenom) AS prenom, "
  34. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".nom) AS nom, "
  35. . "UCASE(" . DB_T_SALARIES . ".lieu) AS lieu, "
  36. . "IF(" . DB_T_LOTTERY_INSCRITS . ".valide = 1, 'OUI', 'NON') AS valide, "
  37. . "" . DB_T_LOTTERY_INSCRITS . ".selected, "
  38. . "" . DB_T_LOTTERY_INSCRITS . ".cree, "
  39. . "" . DB_T_LOTTERY_INSCRITS . ".id_user, "
  40. . "CONCAT(" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom ) AS admin, "
  41. . "" . DB_T_LOTTERY_INSCRITS . ".cree "
  42. . "FROM " . DB_T_LOTTERY_INSCRITS . " "
  43. . "INNER JOIN " . DB_T_USER . " ON " . DB_T_LOTTERY_INSCRITS . ".id_user = " . DB_T_USER . ".id "
  44. . "LEFT JOIN " . DB_T_SALARIES . " ON " . DB_T_LOTTERY_INSCRITS . ".id_salarie = " . DB_T_SALARIES . ".id "
  45. . "WHERE " . DB_T_LOTTERY_INSCRITS . ".id_lottery = :id_lottery");
  46. db::bind(':id_lottery', $_id);
  47. return db::resultset();
  48. }
  49. public static function getWinners(int $_id){
  50. db::query("SELECT "
  51. . "" . DB_T_LOTTERY_INSCRITS . ".id_salarie, "
  52. . "" . DB_T_LOTTERY_INSCRITS . ".id_dossier, "
  53. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".login) AS login, "
  54. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".prenom) AS prenom, "
  55. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".nom) AS nom, "
  56. . "" . DB_T_LOTTERY_INSCRITS . ".selected "
  57. . "FROM " . DB_T_LOTTERY_INSCRITS . " "
  58. . "WHERE " . DB_T_LOTTERY_INSCRITS . ".id_lottery = :id_lottery AND " . DB_T_LOTTERY_INSCRITS . ".selected IS NOT NULL");
  59. db::bind(':id_lottery', $_id);
  60. return db::resultset();
  61. }
  62. public static function getCloture(int $_id){
  63. db::query("SELECT "
  64. . DB_T_LOTTERY . ".sortDate, "
  65. . DB_T_LOTTERY . ".sortNb, "
  66. . "CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'sortBy' "
  67. . "FROM " . DB_T_LOTTERY . " "
  68. . "INNER JOIN " . DB_T_USER . " ON " . DB_T_LOTTERY . ".id_user = " . DB_T_USER . ".id "
  69. . "WHERE " . DB_T_LOTTERY . ".id = :id");
  70. db::bind(':id', $_id);
  71. return db::single();
  72. }
  73. public static function getSortInscription(int $_id, int $_sort = 0)
  74. {
  75. if($_sort == 0){
  76. $sqlSort = "";
  77. } else {
  78. $sqlSort = " ORDER BY RAND() LIMIT " . $_sort;
  79. }
  80. db::query("SELECT "
  81. . "" . DB_T_LOTTERY_INSCRITS . ".id_lottery, "
  82. . "" . DB_T_LOTTERY_INSCRITS . ".id_salarie, "
  83. . "" . DB_T_LOTTERY_INSCRITS . ".id_dossier, "
  84. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".login) AS login, "
  85. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".prenom) AS prenom, "
  86. . "UCASE(" . DB_T_LOTTERY_INSCRITS . ".nom) AS nom "
  87. . "FROM " . DB_T_LOTTERY_INSCRITS . " "
  88. . "INNER JOIN " . DB_T_SALARIES . " ON " . DB_T_LOTTERY_INSCRITS . ".id_salarie = " . DB_T_SALARIES . ".id "
  89. . "WHERE " . DB_T_LOTTERY_INSCRITS . ".id_lottery = :id_lottery AND " . DB_T_SALARIES . ".actif = 1" . $sqlSort);
  90. db::bind(':id_lottery', $_id);
  91. return db::resultset();
  92. }
  93. public static function setSortLottery(int $_idDossier)
  94. {
  95. db::query("UPDATE " . DB_T_LOTTERY_INSCRITS . " "
  96. . "SET " . DB_T_LOTTERY_INSCRITS . ".selected = CURRENT_TIMESTAMP() "
  97. . "WHERE " . DB_T_LOTTERY_INSCRITS . ".id_dossier = :id_dossier");
  98. db::bind(':id_dossier', $_idDossier);
  99. try {
  100. db::execute();
  101. return TRUE;
  102. } catch (Exception $ex) {
  103. alert::recError("ERREUR TECHNIQUE : Au niveau de l'écriture en base de données lors du tirage au sort");
  104. return FALSE;
  105. }
  106. }
  107. public static function deleteLottery(int $_id)
  108. {
  109. db::query("DELETE FROM " . DB_T_LOTTERY . " WHERE id = :id");
  110. db::bind(':id', $_id);
  111. return db::execute();
  112. }
  113. public static function deleteInscriptionLottery(int $_id)
  114. {
  115. db::query("DELETE FROM " . DB_T_LOTTERY_INSCRITS . " WHERE id_lottery = :id_lottery");
  116. db::bind(':id_lottery', $_id);
  117. return db::execute();
  118. }
  119. public static function setDateLottery(int $_id, int $_nb)
  120. {
  121. db::query("UPDATE " . DB_T_LOTTERY . " "
  122. . "SET " . DB_T_LOTTERY . ".sortDate = CURRENT_TIMESTAMP(), "
  123. . DB_T_LOTTERY . ".sortNb = :sortNb, "
  124. . DB_T_LOTTERY . ".sortBy = :sortBy "
  125. . "WHERE " . DB_T_LOTTERY . ".id = :id");
  126. db::bind(':sortNb', $_nb);
  127. db::bind(':sortBy', session::getId());
  128. db::bind(':id', $_id);
  129. try {
  130. db::execute();
  131. return TRUE;
  132. } catch (Exception $ex) {
  133. alert::recError("ERREUR TECHNIQUE : Au niveau de l'écriture en base de données lors de la datation");
  134. return FALSE;
  135. }
  136. }
  137. public static function getInscriptionData(int $_id)
  138. {
  139. db::query("SELECT "
  140. . "COUNT(" . DB_T_LOTTERY_INSCRITS . ".login) AS inscrits, "
  141. . "SUM(IF(" . DB_T_LOTTERY_INSCRITS . ".valide = 1, 1, 0)) AS eligible, "
  142. . "SUM(IF(" . DB_T_LOTTERY_INSCRITS . ".valide = 0, 1, 0)) AS ineligible "
  143. . "FROM " . DB_T_LOTTERY_INSCRITS . " "
  144. . "WHERE " . DB_T_LOTTERY_INSCRITS . ".id_lottery = :id_lottery");
  145. db::bind(':id_lottery', $_id);
  146. return db::single();
  147. }
  148. public static function getFiche(int $_id)
  149. {
  150. db::query("SELECT * FROM " . DB_T_LOTTERY . " WHERE id = :id");
  151. db::bind(':id', $_id);
  152. return db::single();
  153. }
  154. public static function lastLottery()
  155. {
  156. db::query("SELECT MAX(id) AS id FROM " . DB_T_LOTTERY);
  157. return db::single()["id"];
  158. }
  159. }