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