Add Theme and User class

This commit is contained in:
Quentin Grosyeux 2013-10-10 01:18:49 +02:00
parent 87f2521ba1
commit a5893d468f
3 changed files with 197 additions and 0 deletions

View File

@ -0,0 +1,56 @@
<?php
if(!defined('ONYX')) exit;
class Theme {
$id=null;
$title;
function Theme($id=null)
{
if (!empty($id))
{
$db = new BDD();
$res = $db->unique_query("SELECT id, title
FROM themes WHERE id=" . intval($id));
if (!empty($res))
{
$this->title = $res['title'];
}
$db->deconnexion();
}
}
function update()
{
$title = $this->title;
$db = new BDD();
$db->escape($title);
if (empty($this->id))
{
$db->query("INSERT INTO themes
VALUES (NULL, '".$title);
$this->id = $db->insert_id();
$aff = ($this->id > 0);
}
else
{
$db->query("UPDATE themes
SET title = '".$title."'
WHERE id = ".intval($this->id));
$aff = $db->affected();
}
$db->deconnexion();
return ($aff == 1);
}
function get_title()
{
return $this->title;
}
}

View File

@ -0,0 +1,141 @@
<?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;
$db = new BDD();
$db->escape($username);
$db->escape($firstname);
$db->escape($lastname);
$db->escape($company);
if (empty($this->id))
{
$db->query("INSERT INTO users
VALUES (NULL, '".$username."', NULL , ".$auth_level." '".$firstname."', '".$lastname."', '".$company."')");
$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();
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();
$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))
{
$this->points = res['sum_points'];
}
$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");
}

View File