Add some classes and fix some bugs in class Theme

This commit is contained in:
Quentin Grosyeux 2013-10-26 21:44:49 +02:00
parent eac6118532
commit 99f86c6b28

View file

@ -5,19 +5,20 @@ if(!defined('ONYX')) exit;
class Theme
{
var $id = null;
var $title;
var $name;
function Theme($id=null)
{
if (!empty($id))
{
$db = new BDD();
$res = $db->unique_query("SELECT id, title
$res = $db->unique_query("SELECT id, name
FROM themes WHERE id=" . intval($id));
if (!empty($res))
{
$this->title = $res['title'];
$this->id = $res['id'];
$this->name = $res['name'];
}
$db->deconnexion();
}
@ -25,22 +26,22 @@ class Theme
function update()
{
$title = $this->title;
$name = $this->name;
$db = new BDD();
$db->escape($title);
$db->escape($name);
if (empty($this->id))
{
$db->query("INSERT INTO themes
VALUES (NULL, '".$title."');");
VALUES (NULL, '".$name."');");
$this->id = $db->insert_id();
$aff = ($this->id > 0);
}
else
{
$db->query("UPDATE themes
SET title = '".$title."'
SET name = '".$name."'
WHERE id = ".intval($this->id));
$aff = $db->affected();
}
@ -49,9 +50,9 @@ class Theme
return ($aff == 1);
}
function get_title()
function get_name()
{
return $this->title;
return $this->name;
}
function get_id()
@ -59,6 +60,17 @@ class Theme
return $this->id;
}
function get_nbExercices()
{
$db = new BDD();
$res = $db->unique_query("SELECT count( id ) as nb_exercices FROM exercices
WHERE id_theme = ".$this->id."
GROUP BY id_theme");
$db->deconnexion();
return $res['nb_exercices'];
}
function add_exercice($exercice)
{
if (isset($exercice))
@ -68,4 +80,19 @@ class Theme
}
return false;
}
public static function get_themes()
{
$db = new BDD();
$ids = $db->query("SELECT `id` FROM `themes`");
$db->deconnexion();
$array = array();
foreach ($ids as $id){
$array[] = new Theme($id['id']);
}
return $array;
}
}