ADD #143 (80%)
[cl7.git] / php / participant.php
1 <?php # coding: utf-8
2
3 /**
4 * Représente un participant.
5 */
6 class Participant
7 {
8 public $info; # Toute les infos du membre sous la forme d'un objet.
9
10 private $id = 0;
11
12 static private $NB_VOTES_PAR_PARTICIPANT = 3; # Concerne les votes des jeux joueés.
13
14 /**
15 * Constructeur, peut être appelé sous trois formes différentes :
16 * 1) $v1 = NULL, $v2 = NULL : participant non-valide
17 * 2) $v1 = id, $v2 = NULL : la participant existe et est chargé à partir de son id
18 * 3) $v1 = pseudo, $v2 = password : le participant existe et est chargé à partir de son pseudo et de son password
19 * Le mot de passe est donné en clair.
20 */
21 public function Participant($v1=NULL, $v2=NULL)
22 {
23 $this->authentifier($v1, $v2);
24 }
25
26 /**
27 * Est-ce que le participant existe ? C-à-d qu'il est inscrit.
28 */
29 public function existe()
30 {
31 return $this->id != 0;
32 }
33
34 private function authentifier($v1=NULL, $v2=NULL)
35 {
36 # Aucunes valeurs transmise => ce n'est pas un participant valide.
37 if ($v1 == NULL && $v2 == NULL)
38 return;
39
40 if (is_string($v1) && is_string($v2)) # Aucun des arguments n'est vide alors c'est le pseudo et le password qui ont été transmis
41 {
42 $res = pg_query_params("SELECT id FROM participants WHERE pseudo = $1 AND password = $2", array($v1, sha1($v2)));
43 }
44 else # Sinon c'est l'id
45 $res = pg_query_params("SELECT id FROM participants WHERE id = $1", array($v1));
46
47 if (pg_result_status($res) == PGSQL_TUPLES_OK && pg_num_rows($res) === 1)
48 {
49 $this->id = pg_fetch_object($res)->id;
50 $this->chargerInfos();
51 }
52 }
53
54 public function chargerInfos($v1=NULL, $v2=NULL)
55 {
56 $this->authentifier($v1, $v2);
57
58 if (!$this->existe())
59 return;
60
61 $res = pg_query_params("SELECT * FROM participants WHERE id = $1", array($this->id));
62
63 if (pg_result_status($res) == PGSQL_TUPLES_OK && pg_num_rows($res) === 1)
64 $this->info = pg_fetch_object($res);
65 else
66 $this->id = 0;
67 }
68
69 /**
70 * Renvoie le nombre de votes restant pour le participant.
71 */
72 function nb_vote_restant()
73 {
74 $nombre_de_vote = pg_fetch_array(pg_query_params("
75 SELECT COUNT(*) FROM participants RIGHT JOIN jeux_choisis ON participants.id = jeux_choisis.participant_id
76 WHERE participants.id = $1
77 GROUP BY participants.id
78 ", array($this->id)));
79
80 return Participant::$NB_VOTES_PAR_PARTICIPANT - $nombre_de_vote[0];
81 }
82
83 /**
84 * Renvois TRUE si le nombre de participant max est atteint.
85 */
86 static function nombre_participant_max_atteint()
87 {
88 return Participant::nombre_place_restante() <= 0;
89 }
90
91 /**
92 * Renvois le nombre de places restantes.
93 */
94 static function nombre_place_restante()
95 {
96 global $config;
97 $res_SQL = pg_query("SELECT COUNT(*) FROM participants");
98 $nb_participant = pg_fetch_row($res_SQL);
99
100 return $config->get('nb_max_participant') - $nb_participant[0];
101 }
102 }
103
104 ?>