MOD Passage de MySQL à un vrai SGBDR : PostgreSQL
[cl7.git] / php / participant.php
index 7e549e1..9f718b0 100644 (file)
@@ -6,7 +6,8 @@
 class Participant
 {
    public $info; # Toute les infos du membre sous la forme d'un objet.
-       public $valide; # Savoir si le participant existe.
+   
+   private $id = 0;
 
    static private $NB_VOTES_PAR_PARTICIPANT = 3; # Concerne les votes des jeux joueés.
        
@@ -16,41 +17,54 @@ class Participant
      * 2) $v1 = id, $v2 = NULL : la participant existe et est chargé à partir de son id
      * 3) $v1 = pseudo, $v2 = password : le participant existe et est chargé à partir de son pseudo et de son password
      */
-   function Participant($v1=NULL, $v2=NULL)
+   public function Participant($v1=NULL, $v2=NULL)
    {      
       # Aucunes valeurs transmise => ce n'est pas un participant valide.
           if ($v1 == NULL && $v2 == NULL) 
-      {
-         $this->valide = 0;
          return;
-      }
             
                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
-                       $res = mysql_query("SELECT * FROM participants WHERE pseudo = '" . addslashes($v1) . "' AND password = '" . addslashes($v2) . "'");
+                       $res = pg_query_params("SELECT id FROM participants WHERE pseudo = $1 AND password = $2", array($v1, $v2));
                else # Sinon c'est l'id
-                       $res = mysql_query("SELECT * FROM participants WHERE id = " . addslashes($v1));
+                       $res = pg_query_params("SELECT id FROM participants WHERE id = $1", array($v1));
    
-               if (mysql_error() || mysql_num_rows($res) == 0)
-      {
-         $this->valide = FALSE;
-      }
-      else
+               if (pg_result_status($res) == PGSQL_COMMAND_OK && pg_num_rows($res) === 1)
       {
-         $this->info = mysql_fetch_object($res);
-         $this->valide = TRUE; 
+         $this->id = pg_fetch_object($res)->id;
       }
        }
    
+   /**
+     * Est-ce que le participant existe ? C-à-d qu'il est inscrit.
+     */
+   public function existe()
+   {
+      return $this->id != 0;
+   }
+   
+   public function chargerInfos()
+   {
+      if (!$this->existe())
+         return;
+         
+      $res = pg_query_params("SELECT * FROM participants WHERE id = $1", array($this->id));
+   
+      if (pg_result_status($res) == PGSQL_COMMAND_OK && pg_num_rows($res) === 1)
+         $this->info = pg_fetch_object($res);
+      else
+         $this->id = 0;
+   }
+   
    /**
      * Renvoie le nombre de votes restant pour le participant.
      */
    function nb_vote_restant()
    {
-      $nombre_de_vote = mysql_fetch_array(mysql_query("
+      $nombre_de_vote = pg_fetch_array(pg_query_params("
          SELECT COUNT(*) FROM participants RIGHT JOIN jeux_choisis ON participants.id = jeux_choisis.participant_id
-         WHERE participants.id = " . $this->info->id . "
+         WHERE participants.id = $1
          GROUP BY participants.id
-      "));
+      ", array($this->id)));
       
       return Participant::$NB_VOTES_PAR_PARTICIPANT - $nombre_de_vote[0];
    }
@@ -60,11 +74,7 @@ class Participant
      */
    static function nombre_participant_max_atteint()
    {
-      global $config;
-      $res_SQL = mysql_query("SELECT COUNT(*) FROM participants");
-      $nb_participant = mysql_fetch_row($res_SQL);
-
-      return $nb_participant[0] >= $config->get('nb_max_participant');
+      return $this->nombre_place_restante() <= 0;
    }
    
    /**
@@ -73,8 +83,8 @@ class Participant
    static function nombre_place_restante()
    {
       global $config;
-      $res_SQL = mysql_query("SELECT COUNT(*) FROM participants");
-      $nb_participant = mysql_fetch_row($res_SQL);
+      $res_SQL = pg_query("SELECT COUNT(*) FROM participants");
+      $nb_participant = pg_fetch_row($res_SQL);
       
       return $config->get('nb_max_participant') - $nb_participant[0];
    }