ADD #143 (80%)
[cl7.git] / php / pages / jeux_joues.php
1 <?php # coding: utf-8
2
3 include_once("php/traitement_pre_affichage.php");
4
5 if (!$participant->existe())
6 {
7 echo '<p><em>Remarque : </em>Il faut être inscrit pour pouvoir voter.</p>';
8 }
9 else if ($config->get("inscription_terminees"))
10 {
11 echo '<p><em>Remarque : </em>Les inscriptions sont terminées, il n\'est plus possible de voter.</p>';
12 }
13
14 $vote_possible = !$config->get("inscription_terminees") && $participant->existe();
15
16 echo '
17 <form id="formulaireJeuxJoues" method="post" action="index.php?page=jeux_joues">
18 <p><input type="hidden" name="set_jeux_joues" value="1" /></p>
19 <table>
20 <tr>', ($vote_possible ? '<th></th>' : ''), '<th>Votes</th><th>Nom</th><th>Type</th><th></th></tr>';
21
22 # /!\ Attention, requête un poil tricky :
23 # On souhait avoir une liste des jeux joués avec pour chaque tuple :
24 # - l'id du jeu et son nom
25 # - Un tableau des participants ayant voté pour ce jeu
26 # De plus il faut que le résultat soit trié par le nombre de vote
27 $jeux_query = pg_query("
28 SELECT
29 jeux.id,
30 jeux.nom,
31 jeux.type,
32 jeux.url,
33 array_agg(jeux_choisis.participant_id) AS participants,
34 (COUNT(*) + (CASE WHEN bool_and(jeux_choisis.participant_id is null) THEN -1 ELSE 0 END)) as nb_vote
35 FROM
36 jeux
37 LEFT JOIN jeux_choisis ON jeux.id = jeux_choisis.jeu_id
38 GROUP BY jeux.id, jeux.nom, jeux.type, jeux.url
39 ORDER BY nb_vote DESC, jeux.nom
40 ");
41
42 while ($jeu = pg_fetch_object($jeux_query))
43 {
44 $a_vote = FALSE;
45
46 # est-ce que le participant courant à voté pour ce jeu ?
47 if ($vote_possible)
48 {
49 $participants = split(",", ereg_replace("[{-}]", "", $jeu->participants));
50 for($i = 0; $i < count($participants); $i++)
51 $participants[$i] = (int)$participants[$i];
52
53 $a_vote = in_array($participant->info->id, $participants);
54 }
55
56 $a_url = preg_match('/^https?:\/\/.+$/', $jeu->url);
57
58 echo '<tr>',
59 $vote_possible ? '<td><input type="checkbox" name="votes[]" '. ($a_vote ? 'checked="checked"' : '') .' value="'.$jeu->id.'" /></td>' : '',
60 '<td>' . $jeu->nb_vote . '</td>',
61 '<td ' . ($a_vote ? 'class="aVote" ': '').'>' . ($a_url ? '<a href="'. htmlentities($jeu->url) .'">' : '') . traitement_pre_affichage($jeu->nom) . ($a_url ? '</a>' : '') . '</td>',
62 '<td ' . ($a_vote ? 'class="aVote" ': '').'>' . traitement_pre_affichage($jeu->type) . '</td>',
63 '<td>x</td></tr>';
64 }
65
66 echo '
67 </table>';
68
69 if ($vote_possible)
70 echo '
71 <p>Ajouter :
72 <input class="info" type="text" size="20" maxlength="50" name="jeu" value="<nom>" >
73 <input class="info" type="text" size="10" maxlength="100" name="type" value="<type>" />
74 <input class="info" type="text" size="20" maxlength="200" name="url" value="<URL>" />
75 </p>
76 <p><input type="submit" value="Voter" /></p>';
77
78 echo '</form>';
79
80 # affichage du nombre de vote restant
81 if ($vote_possible)
82 echo '<p>Nombre de votes restant : ' . $participant->nb_vote_restant() . '</p>';
83
84 ?>