lottery.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 insert()
  94. {
  95. db::query("INSERT INTO " . DB_T_LOTTERY . " (md5, titre, description, id_user) VALUES (:md5, :titre, :description, :id_user)");
  96. db::bind(':md5', md5(time().rand(1000000000, 9999999999)));
  97. db::bind(':titre', core::getPost("titre"));
  98. db::bind(':description', core::getPost("description"));
  99. db::bind(':id_user', session::getId());
  100. try {
  101. db::execute();
  102. return TRUE;
  103. } catch (Exception $ex) {
  104. return FALSE;
  105. }
  106. }
  107. public static function update()
  108. {
  109. db::query("UPDATE " . DB_T_LOTTERY . " SET "
  110. . "titre = :titre, "
  111. . "description = :description, "
  112. . "sortDate = :sortDate, "
  113. . "id_user = :id_user "
  114. . "WHERE id = :id");
  115. db::bind(':titre', core::getPost("titre"));
  116. db::bind(':description', core::getPost("description"));
  117. db::bind(':sortDate', core::getPost("sortDate"));
  118. db::bind(':id_user', session::getId());
  119. db::bind(':id', core::getPost("id"));
  120. try {
  121. db::execute();
  122. return TRUE;
  123. } catch (Exception $ex) {
  124. return FALSE;
  125. }
  126. }
  127. public static function insertInscription(array $_lottery)
  128. {
  129. db::query("INSERT INTO " . DB_T_LOTTERY_INSCRITS . "
  130. (
  131. id_lottery,
  132. id_salarie,
  133. id_presta,
  134. id_dossier,
  135. login,
  136. prenom,
  137. nom,
  138. valide,
  139. id_user
  140. )
  141. VALUES
  142. (
  143. :id_lottery,
  144. :id_salarie,
  145. :id_presta,
  146. :id_dossier,
  147. :login,
  148. :prenom,
  149. :nom,
  150. :valide,
  151. :id_user
  152. )");
  153. db::bind(':id_lottery', core::getPost("lottery"));
  154. db::bind(':id_salarie', $_lottery["id_salarie"]);
  155. db::bind(':id_presta', $_lottery["id_presta"]);
  156. db::bind(':id_dossier', $_lottery["id_dossier"]);
  157. db::bind(':login', $_lottery["login"]);
  158. db::bind(':prenom', $_lottery["prenom"]);
  159. db::bind(':nom', $_lottery["nom"]);
  160. db::bind(':valide', $_lottery["valide"]);
  161. db::bind(':id_user', session::getId());
  162. try {
  163. db::execute();
  164. return TRUE;
  165. } catch (Exception $ex) {
  166. return FALSE;
  167. }
  168. }
  169. public static function updateInscription(array $_lottery)
  170. {
  171. db::query("UPDATE " . DB_T_LOTTERY_INSCRITS . " "
  172. . "SET " . DB_T_LOTTERY_INSCRITS . ".login = :login, "
  173. . DB_T_LOTTERY_INSCRITS . ".prenom = :prenom, "
  174. . DB_T_LOTTERY_INSCRITS . ".nom = :nom, "
  175. . DB_T_LOTTERY_INSCRITS . ".valide = :valide, "
  176. . DB_T_LOTTERY_INSCRITS . ".id_user = :id_user, "
  177. . DB_T_LOTTERY_INSCRITS . ".id_salarie = :id_salarie "
  178. . "WHERE " . DB_T_LOTTERY_INSCRITS . ".id_dossier = :id_dossier AND " . DB_T_LOTTERY_INSCRITS . ".id_lottery = :id_lottery");
  179. db::bind(':login', $_lottery["login"]);
  180. db::bind(':prenom', $_lottery["prenom"]);
  181. db::bind(':nom', $_lottery["nom"]);
  182. db::bind(':valide', $_lottery["valide"]);
  183. db::bind(':id_user', session::getId());
  184. db::bind(':id_salarie', $_lottery["id_salarie"]);
  185. db::bind(':id_dossier', $_lottery["id_dossier"]);
  186. db::bind(':id_lottery', core::getPost("lottery"));
  187. db::bind(':id_user', session::getId());
  188. try {
  189. db::execute();
  190. return TRUE;
  191. } catch (Exception $ex) {
  192. return FALSE;
  193. }
  194. }
  195. public static function setSortLottery(int $_idDossier)
  196. {
  197. db::query("UPDATE " . DB_T_LOTTERY_INSCRITS . " "
  198. . "SET " . DB_T_LOTTERY_INSCRITS . ".selected = CURRENT_TIMESTAMP() "
  199. . "WHERE " . DB_T_LOTTERY_INSCRITS . ".id_dossier = :id_dossier");
  200. db::bind(':id_dossier', $_idDossier);
  201. try {
  202. db::execute();
  203. return TRUE;
  204. } catch (Exception $ex) {
  205. alert::recError("ERREUR TECHNIQUE : Au niveau de l'écriture en base de données lors du tirage au sort");
  206. return FALSE;
  207. }
  208. }
  209. public static function deleteLottery(int $_id)
  210. {
  211. db::query("DELETE FROM " . DB_T_LOTTERY . " WHERE id = :id");
  212. db::bind(':id', $_id);
  213. return db::execute();
  214. }
  215. public static function deleteInscriptionLottery(int $_id)
  216. {
  217. db::query("DELETE FROM " . DB_T_LOTTERY_INSCRITS . " WHERE id_lottery = :id_lottery");
  218. db::bind(':id_lottery', $_id);
  219. return db::execute();
  220. }
  221. public static function setDateLottery(int $_id, int $_nb)
  222. {
  223. db::query("UPDATE " . DB_T_LOTTERY . " "
  224. . "SET " . DB_T_LOTTERY . ".sortDate = CURRENT_TIMESTAMP(), "
  225. . DB_T_LOTTERY . ".sortNb = :sortNb, "
  226. . DB_T_LOTTERY . ".sortBy = :sortBy "
  227. . "WHERE " . DB_T_LOTTERY . ".id = :id");
  228. db::bind(':sortNb', $_nb);
  229. db::bind(':sortBy', session::getId());
  230. db::bind(':id', $_id);
  231. try {
  232. db::execute();
  233. return TRUE;
  234. } catch (Exception $ex) {
  235. alert::recError("ERREUR TECHNIQUE : Au niveau de l'écriture en base de données lors de la datation");
  236. return FALSE;
  237. }
  238. }
  239. public static function getInscriptionData(int $_id)
  240. {
  241. db::query("SELECT "
  242. . "COUNT(" . DB_T_LOTTERY_INSCRITS . ".login) AS inscrits, "
  243. . "SUM(IF(" . DB_T_LOTTERY_INSCRITS . ".valide = 1, 1, 0)) AS eligible, "
  244. . "SUM(IF(" . DB_T_LOTTERY_INSCRITS . ".valide = 0, 1, 0)) AS ineligible "
  245. . "FROM " . DB_T_LOTTERY_INSCRITS . " "
  246. . "WHERE " . DB_T_LOTTERY_INSCRITS . ".id_lottery = :id_lottery");
  247. db::bind(':id_lottery', $_id);
  248. return db::single();
  249. }
  250. public static function getFiche(int $_id)
  251. {
  252. db::query("SELECT * FROM " . DB_T_LOTTERY . " WHERE id = :id");
  253. db::bind(':id', $_id);
  254. return db::single();
  255. }
  256. public static function lastLottery()
  257. {
  258. db::query("SELECT MAX(id) AS id FROM " . DB_T_LOTTERY);
  259. return db::single()["id"];
  260. }
  261. public static function searchDossier(int $_id)
  262. {
  263. db::query("SELECT id_lottery FROM " . DB_T_LOTTERY_INSCRITS. " WHERE id_dossier = :id_dossier");
  264. db::bind(':id_dossier', $_id);
  265. $return = db::single();
  266. if($return == FALSE){
  267. return NULL;
  268. } else {
  269. return $return["id_lottery"];
  270. }
  271. }
  272. }