2013-10-09 23:18:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if(!defined('ONYX')) exit;
|
|
|
|
|
|
|
|
class User {
|
|
|
|
var $id = null;
|
|
|
|
var $firstname;
|
|
|
|
var $lastname;
|
|
|
|
var $username;
|
|
|
|
var $company;
|
|
|
|
var $auth_level;
|
|
|
|
var $points = null;
|
|
|
|
var $nb_themes = null;
|
|
|
|
|
|
|
|
function User ($id=null) {
|
|
|
|
if (!empty($id))
|
|
|
|
{
|
|
|
|
$db = new BDD();
|
|
|
|
$res = $db->unique_query("SELECT id, firstname, lastname, username, company, auth_level
|
|
|
|
FROM users WHERE id=" . intval($id));
|
|
|
|
|
|
|
|
if (!empty($res))
|
|
|
|
{
|
|
|
|
$this->firstname = $res['firstname'];
|
|
|
|
$this->lastname = $res['lastname'];
|
|
|
|
$this->username = $res['username'];
|
|
|
|
$this->company = $res['company'];
|
|
|
|
$this->auth_level = $res['auth_level'];
|
|
|
|
}
|
|
|
|
$db->deconnexion();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function update()
|
|
|
|
{
|
|
|
|
$username = $this->username;
|
|
|
|
$auth_level = intval($this->auth_level);
|
|
|
|
$firstname = $this->firstname;
|
|
|
|
$lastname = $this->lastname;
|
2013-10-10 02:47:02 +00:00
|
|
|
$company = $this->company;
|
|
|
|
|
2013-10-09 23:18:49 +00:00
|
|
|
$db = new BDD();
|
|
|
|
$db->escape($username);
|
|
|
|
$db->escape($firstname);
|
|
|
|
$db->escape($lastname);
|
|
|
|
$db->escape($company);
|
2013-10-10 02:47:02 +00:00
|
|
|
|
2013-10-09 23:18:49 +00:00
|
|
|
if (empty($this->id))
|
|
|
|
{
|
|
|
|
$db->query("INSERT INTO users
|
2013-10-10 02:47:02 +00:00
|
|
|
VALUES (NULL, '".$username."', 0x0, ".$auth_level.", '".$firstname."', '".$lastname."', '".$company."')");
|
2013-10-09 23:18:49 +00:00
|
|
|
$this->id = $db->insert_id();
|
|
|
|
$aff = ($this->id > 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$db->query("UPDATE users
|
|
|
|
SET username = '".$username."', auth_level = '".$auth_level."', firstname = '".$firstname."', lastname = '".$lastname."', company = '".$company."'
|
|
|
|
WHERE id = ".intval($this->id));
|
|
|
|
$aff = $db->affected();
|
|
|
|
}
|
|
|
|
$db->deconnexion();
|
2013-10-10 02:47:02 +00:00
|
|
|
|
2013-10-09 23:18:49 +00:00
|
|
|
return ($aff == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_id()
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_firstname()
|
|
|
|
{
|
|
|
|
return $this->firstname;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_lastname()
|
|
|
|
{
|
|
|
|
return $this->lastname;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_username()
|
|
|
|
{
|
|
|
|
return $this->username;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_company()
|
|
|
|
{
|
|
|
|
return $this->company;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_auth_level()
|
|
|
|
{
|
|
|
|
return $this->auth_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_pts()
|
|
|
|
{
|
|
|
|
if(isset($this->points))
|
|
|
|
{
|
|
|
|
$db = new BDD();
|
2013-10-10 02:47:02 +00:00
|
|
|
|
2013-10-09 23:18:49 +00:00
|
|
|
$res = $db->query("SELECT e.id, s.id_user, SUM(e.points) as sum_points
|
|
|
|
FROM exercices e
|
|
|
|
LEFT OUTER JOIN solved s ON e.id = s.id_exercice
|
|
|
|
WHERE s.id_user = " . intval($this->id) . "
|
|
|
|
GROUP BY s.id_user");
|
|
|
|
|
|
|
|
if (!empty($res))
|
|
|
|
{
|
2013-10-10 02:47:02 +00:00
|
|
|
$this->points = $res['sum_points'];
|
2013-10-09 23:18:49 +00:00
|
|
|
}
|
|
|
|
$db->deconnexion();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function authenticate($certificate)
|
|
|
|
{
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
// Points par theme (theme, user, sum_points)
|
|
|
|
//$res = $db->query("SELECT e.id_theme, s.id_user, SUM(e.points) as sum_points
|
|
|
|
//FROM exercices e
|
|
|
|
//LEFT OUTER JOIN solved s ON e.id = s.id_exercice
|
|
|
|
//WHERE s.id_user = " . intval($this->id) . "
|
|
|
|
//GROUP BY s.id_user, e.id_theme");
|
|
|
|
|
|
|
|
|
|
|
|
//SELECT e.id_theme,
|
|
|
|
// (select e.points from exercices where e.id=???),
|
|
|
|
// (select e.points from exercices where e.id=???),
|
|
|
|
// (select e.points from exercices where e.id=???),
|
|
|
|
// (select e.points from exercices where e.id=???),
|
|
|
|
// (select e.points from exercices where e.id=???)
|
|
|
|
// (select max(e.points) from exercices)
|
|
|
|
//FROM exercices e
|
|
|
|
//LEFT OUTER JOIN solved s ON e.id = s.id_exercice
|
|
|
|
//WHERE s.id_user = " . intval($this->id) . "
|
|
|
|
//GROUP BY s.id_user, e.id_theme");
|
|
|
|
|
|
|
|
}
|