MOD MySQL est remplacé par PostgreSQL (fini)
[cl7.git] / php / pages / jeux_joues.php
index 6bf932d..a05a388 100644 (file)
@@ -2,7 +2,7 @@
 \r
 include_once("php/traitement_pre_affichage.php");\r
 \r
-if (!$participant->valide)\r
+if (!$participant->existe())\r
 {\r
    echo '<p><em>Remarque : </em>Il faut être inscrit pour pouvoir voter.</p>';\r
 }\r
@@ -12,30 +12,42 @@ echo '
 <form id="formulaireJeuxJoues" method="post" action="index.php?page=jeux_joues">\r
    <p><input type="hidden" name="set_jeux_joues" value="1" /></p>\r
    <table>\r
-      <tr>', ($participant->valide ? '<th></th>' : ''), '<th>Votes</th><th>Jeux</th></tr>';\r
-\r
-$jeux_query = mysql_query("\r
-   SELECT jeux.id, jeux.nom, jeux_choisis.participant_id, COUNT(*) + IF(participant_id is not null, 1, 0) - 1 AS nb_vote\r
-   FROM jeux LEFT JOIN jeux_choisis ON jeux.id = jeux_choisis.jeu_id\r
-   GROUP BY jeux.id\r
-   ORDER BY nb_vote DESC, nom \r
+      <tr>', ($participant->existe() ? '<th></th>' : ''), '<th>Votes</th><th>Jeux</th></tr>';\r
+
+# /!\ Attention, requête un poil tricky :
+# On souhait avoir une liste des jeux joués avec pour chaque tuple :
+#  - l'id du jeu et son nom
+#  - Un tableau des participants ayant voté pour ce jeu
+# De plus il faut que le résultat soit trié par le nombre de vote\r
+$jeux_query = pg_query("\r
+   SELECT 
+      jeux.id,
+      jeux.nom,
+      array_agg(jeux_choisis.participant_id) AS participants,
+      (COUNT(*) + (CASE WHEN bool_and(jeux_choisis.participant_id is null) THEN -1 ELSE 0 END)) as nb_vote
+   FROM 
+      jeux 
+      LEFT JOIN jeux_choisis ON jeux.id = jeux_choisis.jeu_id
+   GROUP BY jeux.id, jeux.nom  
+   ORDER BY nb_vote DESC, jeux.nom\r
 ");\r
    \r
-while ($jeu = mysql_fetch_object($jeux_query))\r
-{\r
+while ($jeu = pg_fetch_object($jeux_query))\r
+{
+   $a_vote = FALSE;
+      \r
    # est-ce que le participant courant à voté pour ce jeu ?\r
-   if ($participant->valide)\r
-   {\r
-      $a_vote = mysql_fetch_row(mysql_query("\r
-         SELECT COUNT(*) FROM jeux_choisis\r
-         WHERE participant_id = ".$participant->info->id." AND jeu_id = ".$jeu->id\r
-      )); $a_vote = $a_vote[0];\r
+   if ($participant->existe())\r
+   {
+      $participants = split(",", ereg_replace("[{-}]", "", $jeu->participants));
+      for($i = 0; $i < count($participants); $i++)
+         $participants[$i] = (int)$participants[$i];
+      
+      $a_vote = in_array($participant->info->id, $participants);\r
    }\r
-   else\r
-      $a_vote = FALSE;\r
    \r
    echo '<tr>',\r
-      $participant->valide ? '<td><input type="checkbox" name="votes[]" '. ($a_vote ? 'checked="checked"' : '') .' value="'.$jeu->id.'" /></td>' : '',\r
+      $participant->existe() ? '<td><input type="checkbox" name="votes[]" '. ($a_vote ? 'checked="checked"' : '') .' value="'.$jeu->id.'" /></td>' : '',\r
       '<td>' . $jeu->nb_vote . '</td>',\r
       '<td ' . ($a_vote ? 'class="aVote" ': '').'>' . traitement_pre_affichage($jeu->nom) . '</td></tr>';\r
 }\r
@@ -43,7 +55,7 @@ while ($jeu = mysql_fetch_object($jeux_query))
 echo '\r
    </table>';\r
 \r
-if ($participant->valide)\r
+if ($participant->existe())\r
    echo '\r
    <p>Autre : <input type="text" maxlength="50" name="jeu" /></p>\r
    <p><input type="submit" value="Voter" /></p>';\r
@@ -51,7 +63,7 @@ if ($participant->valide)
 echo '</form>';\r
 \r
 # affichage du nombre de vote restant\r
-if ($participant->valide)\r
+if ($participant->existe())\r
    echo '<p>Nombre de votes restant : ' . $participant->nb_vote_restant() . '</p>';\r
 \r
 ?>
\ No newline at end of file