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

140 lines
2.7 KiB
PHP
Raw Normal View History

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;
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))
{
$this->id = $res['id'];
$this->name = $res['name'];
2013-10-10 02:47:02 +00:00
}
$db->deconnexion();
}
}
2013-10-09 23:18:49 +00:00
2013-10-10 02:47:02 +00:00
function update()
{
$name = $this->name;
2013-10-10 02:47:02 +00:00
$db = new BDD();
$db->escape($name);
2013-10-10 02:47:02 +00:00
if (empty($this->id))
{
$db->query("INSERT INTO themes
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();
return ($aff == 1);
}
2013-10-09 23:18:49 +00:00
function get_name()
2013-10-10 02:47:02 +00:00
{
return $this->name;
2013-10-10 02:47:02 +00:00
}
2013-10-10 02:47:02 +00:00
function get_id()
{
return $this->id;
}
2013-11-12 15:52:50 +00:00
function get_nb_exercices()
{
$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");
$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-11-12 15:52:50 +00:00
function get_exercices_ordered()
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++)
{
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
{
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-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();
$ids = $db->query("SELECT `id` FROM `themes`");
$db->deconnexion();
$array = array();
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
}