<?php

class User extends Model {

   // ===========
   // = Statics =
   // ===========
   protected static $table_name = 'USER';

   // load all users from Db
   public static function getList() {
      $stm = parent::exec('USER_LIST');
      return $stm->fetchAll();
   }

   // load one user from the Db
   public static function getUserByID($id) {
      $stm = parent::exec('USER_BY_ID',array(':user_id' => $id));
      return $stm->fetchAll();
   }

   // update one user from the Db
   public static function updateUser($values) {
      $stm = parent::exec('USER_UPDATE',$values);
      return "User updated";
   }

   // Create a new user in the Db
   public static function createUser($values) {
      $stm = parent::exec('USER_CREATE',$values);
      return "User created";
   }

   // Delete a user from the Db
   public static function deleteUser($id) {
      $stm = parent::exec('USER_DELETE',array(':id' => $id));
      return "User deleted";
   }

   // Retrieve all users having this login
   public static function getListWithLogin($login) {
      $stm = parent::exec('USER_GET_WITH_LOGIN',array(':login' => $login));
      return $stm->fetchAll();
   }

   // load the user with the login $login
   public static function tryLogin($login)
   {
      $users = static::getListWithLogin($login);
      return $users[0];
   }
   
   public static function getGames($id)
   {
      $stm = parent::exec('USER_GET_GAMES', array(':USER_ID' => $id));
      return $stm->fetchAll();
   }

}