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(); } } static function __set_state(array $array) { $tmp = new Theme(); $tmp->id = $array["id"]; $tmp->name = $array["name"]; return $tmp; } 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(); Cache::del("ordered_th".$this->id); return ($aff == 1); } function get_name() { return $this->name; } function get_name_url() { return preg_replace("#[^a-zA-Z0-9_]#", "_", $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() { $res = Cache::read("ordered_th".$this->id); if (!empty($res)) return $res; $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"]); Cache::set("ordered_th".$this->id, $res); return $res; } public static function get_themes() { $db = new BDD(); $ids = $db->query("SELECT `id` FROM `themes` ORDER BY `name`"); $db->deconnexion(); $array = array(); if ($ids) foreach ($ids as $id) $array[] = new Theme($id['id']); return $array; } } class ThemeNotFoundException extends Exception { }