2013-10-09 23:18:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if(!defined('ONYX')) exit;
|
|
|
|
|
2013-10-10 02:47:02 +00:00
|
|
|
class Theme
|
|
|
|
{
|
2013-10-10 03:52:48 +00:00
|
|
|
var $id = null;
|
2013-10-26 19:44:49 +00:00
|
|
|
var $name;
|
2013-10-09 23:18:49 +00:00
|
|
|
|
2013-10-10 02:47:02 +00:00
|
|
|
function Theme($id=null)
|
|
|
|
{
|
|
|
|
if (!empty($id))
|
|
|
|
{
|
|
|
|
$db = new BDD();
|
2013-11-12 15:52:50 +00:00
|
|
|
$res = $db->unique_query("SELECT `id`, `name`
|
2013-10-10 02:47:02 +00:00
|
|
|
FROM themes WHERE id=" . intval($id));
|
2013-10-09 23:18:49 +00:00
|
|
|
|
2013-10-10 02:47:02 +00:00
|
|
|
if (!empty($res))
|
|
|
|
{
|
2013-10-26 19:44:49 +00:00
|
|
|
$this->id = $res['id'];
|
|
|
|
$this->name = $res['name'];
|
2013-10-10 02:47:02 +00:00
|
|
|
}
|
2013-11-28 21:25:54 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$db->deconnexion();
|
|
|
|
throw new ThemeNotFoundException();
|
|
|
|
}
|
2013-10-10 02:47:02 +00:00
|
|
|
$db->deconnexion();
|
|
|
|
}
|
|
|
|
}
|
2013-10-09 23:18:49 +00:00
|
|
|
|
2013-12-14 05:11:14 +00:00
|
|
|
static function __set_state(array $array)
|
|
|
|
{
|
|
|
|
$tmp = new Theme();
|
|
|
|
|
|
|
|
$tmp->id = $array["id"];
|
|
|
|
$tmp->name = $array["name"];
|
|
|
|
|
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:47:02 +00:00
|
|
|
function update()
|
|
|
|
{
|
2013-10-26 19:44:49 +00:00
|
|
|
$name = $this->name;
|
2013-10-10 02:47:02 +00:00
|
|
|
|
|
|
|
$db = new BDD();
|
2013-10-26 19:44:49 +00:00
|
|
|
$db->escape($name);
|
2013-10-10 02:47:02 +00:00
|
|
|
|
|
|
|
if (empty($this->id))
|
|
|
|
{
|
|
|
|
$db->query("INSERT INTO themes
|
2013-10-26 19:44:49 +00:00
|
|
|
VALUES (NULL, '".$name."');");
|
2013-10-10 02:47:02 +00:00
|
|
|
$this->id = $db->insert_id();
|
|
|
|
$aff = ($this->id > 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$db->query("UPDATE themes
|
2013-11-12 15:52:50 +00:00
|
|
|
SET name = '".$name."'
|
|
|
|
WHERE id = ".intval($this->id));
|
2013-10-10 02:47:02 +00:00
|
|
|
$aff = $db->affected();
|
|
|
|
}
|
|
|
|
$db->deconnexion();
|
|
|
|
|
2013-12-14 05:11:14 +00:00
|
|
|
Cache::del("ordered_th".$this->id);
|
|
|
|
|
2013-10-10 02:47:02 +00:00
|
|
|
return ($aff == 1);
|
|
|
|
}
|
2013-10-09 23:18:49 +00:00
|
|
|
|
2013-10-26 19:44:49 +00:00
|
|
|
function get_name()
|
2013-10-10 02:47:02 +00:00
|
|
|
{
|
2013-10-26 19:44:49 +00:00
|
|
|
return $this->name;
|
2013-10-10 02:47:02 +00:00
|
|
|
}
|
2013-10-10 01:44:46 +00:00
|
|
|
|
2013-12-13 17:48:13 +00:00
|
|
|
function get_name_url()
|
|
|
|
{
|
|
|
|
return urlencode($this->name);
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:47:02 +00:00
|
|
|
function get_id()
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
2013-10-10 01:44:46 +00:00
|
|
|
|
2013-11-12 15:52:50 +00:00
|
|
|
function get_nb_exercices()
|
2013-10-26 19:44:49 +00:00
|
|
|
{
|
|
|
|
$db = new BDD();
|
2013-11-12 15:52:50 +00:00
|
|
|
$res = $db->unique_query("SELECT count(id) as nb_exercices FROM exercices
|
|
|
|
WHERE id_theme = ".$this->id."
|
|
|
|
GROUP BY id_theme");
|
2013-10-26 19:44:49 +00:00
|
|
|
$db->deconnexion();
|
|
|
|
|
|
|
|
return $res['nb_exercices'];
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:47:02 +00:00
|
|
|
function add_exercice($exercice)
|
|
|
|
{
|
|
|
|
if (isset($exercice))
|
|
|
|
{
|
|
|
|
$exercice->theme = $this;
|
|
|
|
return $exercice->update(true);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-26 19:44:49 +00:00
|
|
|
|
2013-11-12 15:52:50 +00:00
|
|
|
function get_exercices_ordered()
|
2013-11-05 02:45:48 +00:00
|
|
|
{
|
2013-12-14 05:11:14 +00:00
|
|
|
$res = Cache::read("ordered_th".$this->id);
|
|
|
|
if (!empty($res))
|
|
|
|
return $res;
|
|
|
|
|
2013-11-05 02:45:48 +00:00
|
|
|
$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++)
|
|
|
|
{
|
2013-11-05 03:42:47 +00:00
|
|
|
for ($j = $i; $j < $size; $j++)
|
2013-11-05 02:45:48 +00:00
|
|
|
{
|
|
|
|
if ($i == 0)
|
|
|
|
{
|
|
|
|
if ($res[$j]['require'] == '')
|
|
|
|
{
|
|
|
|
$tmp = $res[0];
|
|
|
|
$res[0] = $res[$j];
|
|
|
|
$res[$j] = $tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-05 03:42:47 +00:00
|
|
|
if ($res[$j]['require'] == $res[$i - 1]['id'])
|
2013-11-05 02:45:48 +00:00
|
|
|
{
|
|
|
|
$tmp = $res[$i];
|
|
|
|
$res[$i] = $res[$j];
|
|
|
|
$res[$j] = $tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-05 04:15:18 +00:00
|
|
|
foreach($res as &$r)
|
|
|
|
$r = new Exercice($r["id"]);
|
|
|
|
|
2013-12-14 05:11:14 +00:00
|
|
|
Cache::set("ordered_th".$this->id, $res);
|
2013-11-05 02:45:48 +00:00
|
|
|
return $res;
|
|
|
|
}
|
2013-11-08 14:47:55 +00:00
|
|
|
|
|
|
|
public static function get_themes()
|
|
|
|
{
|
|
|
|
$db = new BDD();
|
2013-12-14 05:11:14 +00:00
|
|
|
$ids = $db->query("SELECT `id` FROM `themes` ORDER BY `name`");
|
2013-11-08 14:47:55 +00:00
|
|
|
$db->deconnexion();
|
|
|
|
|
|
|
|
$array = array();
|
2013-11-08 19:46:48 +00:00
|
|
|
if ($ids)
|
|
|
|
foreach ($ids as $id)
|
|
|
|
$array[] = new Theme($id['id']);
|
2013-11-08 14:47:55 +00:00
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
2013-10-10 02:47:02 +00:00
|
|
|
}
|
2013-11-28 21:25:54 +00:00
|
|
|
|
|
|
|
class ThemeNotFoundException extends Exception
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|