server/onyx/include/common/Theme.class.php
2013-10-26 21:44:49 +02:00

99 lines
1.7 KiB
PHP

<?php
if(!defined('ONYX')) exit;
class Theme
{
var $id = null;
var $name;
function Theme($id=null)
{
if (!empty($id))
{
$db = new BDD();
$res = $db->unique_query("SELECT id, name
FROM themes WHERE id=" . intval($id));
if (!empty($res))
{
$this->id = $res['id'];
$this->name = $res['name'];
}
$db->deconnexion();
}
}
function update()
{
$name = $this->name;
$db = new BDD();
$db->escape($name);
if (empty($this->id))
{
$db->query("INSERT INTO themes
VALUES (NULL, '".$name."');");
$this->id = $db->insert_id();
$aff = ($this->id > 0);
}
else
{
$db->query("UPDATE themes
SET name = '".$name."'
WHERE id = ".intval($this->id));
$aff = $db->affected();
}
$db->deconnexion();
return ($aff == 1);
}
function get_name()
{
return $this->name;
}
function get_id()
{
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))
{
$exercice->theme = $this;
return $exercice->update(true);
}
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;
}
}