MOD MySQL est remplacé par PostgreSQL (fini)
[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 */
20 public function Participant($v1=NULL, $v2=NULL)
21 {
22 # Aucunes valeurs transmise => ce n'est pas un participant valide.
23 if ($v1 == NULL && $v2 == NULL)
24 return;
25
26 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
27 {
28 $res = pg_query_params("SELECT id FROM participants WHERE pseudo = $1 AND password = $2", array($v1, $v2));
29 }
30 else # Sinon c'est l'id
31 $res = pg_query_params("SELECT id FROM participants WHERE id = $1", array($v1));
32
33 if (pg_result_status($res) == PGSQL_TUPLES_OK && pg_num_rows($res) === 1)
34 {
35 $this->id = pg_fetch_object($res)->id;
36 $this->chargerInfos();
37 }
38 }
39
40 /**
41 * Est-ce que le participant existe ? C-à-d qu'il est inscrit.
42 */
43 public function existe()
44 {
45 return $this->id != 0;
46 }
47
48 public function chargerInfos()
49 {
50 if (!$this->existe())
51 return;
52
53 $res = pg_query_params("SELECT * FROM participants WHERE id = $1", array($this->id));
54
55 if (pg_result_status($res) == PGSQL_TUPLES_OK && pg_num_rows($res) === 1)
56 $this->info = pg_fetch_object($res);
57 else
58 $this->id = 0;
59 }
60
61 /**
62 * Renvoie le nombre de votes restant pour le participant.
63 */
64 function nb_vote_restant()
65 {
66 $nombre_de_vote = pg_fetch_array(pg_query_params("
67 SELECT COUNT(*) FROM participants RIGHT JOIN jeux_choisis ON participants.id = jeux_choisis.participant_id
68 WHERE participants.id = $1
69 GROUP BY participants.id
70 ", array($this->id)));
71
72 return Participant::$NB_VOTES_PAR_PARTICIPANT - $nombre_de_vote[0];
73 }
74
75 /**
76 * Renvois TRUE si le nombre de participant max est atteint.
77 */
78 static function nombre_participant_max_atteint()
79 {
80 return Participant::nombre_place_restante() <= 0;
81 }
82
83 /**
84 * Renvois le nombre de places restantes.
85 */
86 static function nombre_place_restante()
87 {
88 global $config;
89 $res_SQL = pg_query("SELECT COUNT(*) FROM participants");
90 $nb_participant = pg_fetch_row($res_SQL);
91
92 return $config->get('nb_max_participant') - $nb_participant[0];
93 }
94 }
95
96 ?>