server/onyx/include/common/Theme.class.php

155 lines
2.9 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'];
}
else
{
$db->deconnexion();
throw new ThemeNotFoundException();
}
$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_name_url()
{
return urlencode($this->name);
}
function get_id()
{
return $this->id;
}
function get_nb_exercices()
{
$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;
}
function get_exercices_ordered()
{
$db = new BDD();
$res = $db->query("SELECT E.id, E.require FROM exercices E
INNER JOIN themes T ON T.id = E.id_theme
WHERE T.id = ".intval($this->id));
$db->deconnexion();
$size = count($res);
for ($i = 0; $i < $size; $i++)
{
for ($j = $i; $j < $size; $j++)
{
if ($i == 0)
{
if ($res[$j]['require'] == '')
{
$tmp = $res[0];
$res[0] = $res[$j];
$res[$j] = $tmp;
}
}
else
{
if ($res[$j]['require'] == $res[$i - 1]['id'])
{
$tmp = $res[$i];
$res[$i] = $res[$j];
$res[$j] = $tmp;
}
}
}
}
foreach($res as &$r)
$r = new Exercice($r["id"]);
return $res;
}
public static function get_themes()
{
$db = new BDD();
$ids = $db->query("SELECT `id` FROM `themes`");
$db->deconnexion();
$array = array();
if ($ids)
foreach ($ids as $id)
$array[] = new Theme($id['id']);
return $array;
}
}
class ThemeNotFoundException extends Exception
{
}