apiTableauFromClassement.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. require_once "libs/fnMain.php";
  3. require_once "vendor/autoload.php";
  4. use \Firebase\JWT\JWT;
  5. /**************************
  6. * Contrôle du token JWT
  7. **************************/
  8. $isClassementValid = isset($_GET["classement"]) && (int) $_GET["classement"] !== 0;
  9. if (!isset($_GET["jwt"], $_SETTINGS["jwt_secret"])) {
  10. echo "Manque d'arguments";
  11. die();
  12. }
  13. try {
  14. $jwtPayload = JWT::decode($_GET["jwt"], $_SETTINGS["jwt_secret"], ["HS256"]);
  15. } catch (\Firebase\JWT\SignatureInvalidException $e) {
  16. echo "Identité invalide";
  17. die();
  18. } catch (Exception $e) {
  19. // echo $e->getMessage();
  20. echo "Erreur lors de la vérification de l'identité";
  21. die();
  22. }
  23. /**************************
  24. * Contrôle du statut premium de l'utilisateur
  25. **************************/
  26. $curl = curl_init();
  27. curl_setopt($curl, CURLOPT_URL, "https://interface.asclepia.io/auth/user");
  28. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  29. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
  30. curl_setopt($curl, CURLOPT_TIMEOUT, 60);
  31. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
  32. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  33. curl_setopt($curl, CURLOPT_HTTPHEADER, [
  34. "authorization: JWT ".$_GET["jwt"],
  35. "accept: application/json, text/plain, */*"
  36. ]);
  37. $response = curl_exec($curl);
  38. // Vérification de la réponse
  39. $httpResponseCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
  40. if ($httpResponseCode === 200) {
  41. $userData = json_decode($response, true);
  42. if ($userData === NULL || !isset($userData["premium"])) {
  43. echo "Echec de récupération des données utilisateur";
  44. die();
  45. }
  46. // Vérification du statut premium de l'utilisateur
  47. if ($userData["premium"] === FALSE) {
  48. echo "Abonnement requis.";
  49. die();
  50. }
  51. } else {
  52. echo "Echec de l'authentification utilisateur";
  53. die();
  54. }
  55. /**************************
  56. * GENERATION DES CLASSEMENTS
  57. **************************/
  58. if ($isClassementValid) {
  59. $rangMoyen = (int) $_GET["classement"];
  60. // HTML renvoyé au site
  61. $html = <<<EOF
  62. <link rel="stylesheet" href="assets/bootstrap.min.css">
  63. <style>
  64. /* Tableau */
  65. table {
  66. font-size: 13px;
  67. text-align: center;
  68. border-collapse: collapse;
  69. white-space: nowrap;
  70. overflow: auto;
  71. }
  72. th, td {
  73. padding: 2px;
  74. border: 1px solid black;
  75. --text-color: lightgrey;
  76. }
  77. .all-choices {
  78. background-color: #28a745;
  79. /* color: #8fd19e; */
  80. color: white;
  81. }
  82. .half-choices {
  83. background-color: #8fd19e;
  84. color: black;
  85. }
  86. .last-choice {
  87. background-color: #ffdf7e;
  88. color: grey;
  89. }
  90. .no-choice {
  91. background-color: #ed969e;
  92. }
  93. .never-available {
  94. background-color: lightgrey;
  95. }
  96. </style>
  97. <h2>Tableau de classement pour le rang {$rangMoyen}</h2>
  98. <h3>Légende</h3>
  99. <table>
  100. <thead>
  101. <th colspan="5">Légende (sur toutes les années)</th>
  102. </thead>
  103. <tbody>
  104. <tr>
  105. <td>Jamais proposé</td>
  106. <td class="no-choice">Aucun choix</td>
  107. <td class="last-choice">Une seule année</td>
  108. <td class="half-choices">Plusieurs choix</td>
  109. <td class="all-choices">Tous les choix</td>
  110. </tr>
  111. </tbody>
  112. </table>
  113. <p>
  114. Chaque nombre dans chaque case correspond au nombre d'années où ce choix était dispo au classement rentré <br>
  115. </p>
  116. EOF;
  117. $html .= "<h3>Résultats</h3>";
  118. // On va récupérer tous les rangs limited dispo triés par idChoix
  119. $reqRL = $db->query("SELECT idChoix, annee, rangLimite FROM dataset");
  120. $rangLimites = $reqRL->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
  121. // Récupération des datasets
  122. $specialityDatasetAbrev = getCsvToArrayKeyValue($_SETTINGS["datasetFolder"]."/liste_specialites_abrev.csv");
  123. $specialityDataset = getCsvToArrayKeyValue($_SETTINGS["datasetFolder"]."/liste_specialites.csv");
  124. $cityDataset = getCsvToArrayKeyValue($_SETTINGS["datasetFolder"]."/liste_villes.csv");
  125. $inputClassement = $rangMoyen;
  126. // Calcul et affichage des données
  127. // Affichage de la 1ère ligne = Titre des colonnes
  128. $html .= "<table><thead><tr><th></th>";
  129. foreach ($specialityDatasetAbrev as $specialityName) {
  130. $html .= "<th scope='col'>".$specialityName."</th>";
  131. }
  132. $html .= "</tr></thead><tbody>";
  133. // Affichage de la suite du tableau avec le calcul
  134. foreach ($cityDataset as $cityId => $cityName) {
  135. // On gère la séparation des différentes régions entre elles (représentées dans le csv villes par les lignes XX_:XX)
  136. // La notation des lignes de séparation est sous forme XX_x car si on laissait XX, la fonction getCsvToArrayKeyValue
  137. // ne retourne la position que pour la 1ère occurence de XX;XX
  138. // La manière de fix ça de manière la plus opti et sale, c'est de faire un substr et de tester les 2 premiers charactères
  139. // si ils sont égaux à XX, on fait une séparation
  140. // TODO : Faire un parseur csv qui prend en compte chaque ligne du csv dans l'ordre du fichier
  141. if (strpos($cityId, "XX") !== FALSE) {
  142. $html .= "<tr><th></th>";
  143. foreach ($specialityDatasetAbrev as $specialityName) {
  144. $html .= "<th scope='col'>".$specialityName."</th>";
  145. }
  146. $html .= "</th>";
  147. continue;
  148. }
  149. $html .= "<tr><th scope='row'>".$cityName."</th>";
  150. foreach ($specialityDatasetAbrev AS $specialityId => $specialityName) {
  151. $idChoice = "0".$cityId.$specialityId;
  152. if (!isset($rangLimites[$idChoice])) {
  153. $nbAnneePropose = 0;
  154. $nbPossibleAnneePropose = 0;
  155. } else {
  156. $nbAnneePropose = count($rangLimites[$idChoice]);
  157. $nbPossibleAnneePropose = 0;
  158. foreach ($rangLimites[$idChoice] as $rangLimite) {
  159. if ((int) $rangLimite["rangLimite"] >= $inputClassement) {$nbPossibleAnneePropose++;}
  160. }
  161. }
  162. // Affichage de la cellule
  163. $html .= "<td class=\"";
  164. switch (TRUE) {
  165. case ($nbPossibleAnneePropose === 0 && $nbAnneePropose === 0):
  166. $html .= "never-available";
  167. break;
  168. case ($nbPossibleAnneePropose === 0):
  169. $html .= "no-choice";
  170. break;
  171. case ($nbAnneePropose > $nbPossibleAnneePropose && $nbPossibleAnneePropose === 1):
  172. $html .= "last-choice";
  173. break;
  174. case ($nbAnneePropose > $nbPossibleAnneePropose && $nbPossibleAnneePropose !== 1):
  175. $html .= "half-choices";
  176. break;
  177. case ($nbAnneePropose === $nbPossibleAnneePropose):
  178. $html .= "all-choices";
  179. break;
  180. }
  181. $html .= "\">";
  182. if ($nbPossibleAnneePropose > 0) {
  183. $html .= $nbPossibleAnneePropose;
  184. } else if ($nbPossibleAnneePropose === 0) {
  185. $html .= "0";
  186. }
  187. $html .= "</td>";
  188. }
  189. $html .= "</tr>";
  190. }
  191. $html .= "</tbody></table><br>";
  192. // Affichage du HTML demandé
  193. echo $html;
  194. } else {
  195. echo "Classement entré incorrect.";
  196. }