Add Theme class and some new functions in Exercice class

This commit is contained in:
Quentin Grosyeux 2013-10-10 03:44:46 +02:00
parent edd8eb45a5
commit dc0f544e2b
2 changed files with 164 additions and 0 deletions

View File

@ -0,0 +1,150 @@
<?php
if(!defined('ONYX')) exit;
class Exercice {
var $id=null;
var $theme;
var $require;
var $level;
var $points;
var $statement;
var $files;
var $keys;
function Exercice($id=null)
{
if (!empty($id))
{
$db = new BDD();
$res = $db->unique_query("SELECT `id`, `id_theme`, `require`, `level`, `points`, `statement`
FROM exercices
WHERE id=" . intval($id));
if (!empty($res))
{
$this->id = $res['id'];
$this->theme = new Theme($res['id_theme']);
$this->require = $res['require'];
$this->level = $res['level'];
$this->points = $res['points'];
$this->statement = $res['statement'];
$this->files = $db->query("SELECT `id`, `path`, `name`
FROM exercice_files
WHERE id_exercice = id");
$this->keys = $db->query("SELECT `id`, `format`, `value`
FROM exercice_keys
WHERE id_exercice = id");
}
$db->deconnexion();
}
}
function update($create)
{
$id = $this->id;
$theme = intval($this->theme->get_id());
$require = $this->require;
$level = intval($this->level);
$points = intval($this->points);
$statement = $this->statement;
$files = $this->files;
$keys = $this->keys;
$db = new BDD();
$db->escape($id);
$db->escape($theme);
$db->escape($require);
$db->escape($statement);
if ($create)
{
$db->query("INSERT INTO exercices
VALUES ('"$id"', '".$theme."', '".$require."', '".$level."', '".$points."','".$statement));
$aff = $db->affected();
}
else
{
$db->query("UPDATE exercices
SET `theme` = '".$theme."', `require` = '".$require."', `level` = '".$level."', `point` = '".$point."', `statement` = '"$statement"'
WHERE id = ".$id);
$aff = $db->affected();
}
foreach ($this->keys as $k => $key)
{
$format = $key['format'];
$value = $key['value'];
$kid = $key['id'];
$db->escape($format);
$db->escape($value);
$db->escape($kid);
if (!isset($kid))
{
$db->query("INSERT INTO exercice_keys
VALUES (NULL, '".$id."', '".$format."', '".$value);
$this->keys[$k]['id'] = $db->insert_id();
}
else
{
$db->query("UPDATE exercice_keys
SET `format` = '".$format."', `value` = '".$value."'
WHERE id = '".$kid);
}
}
foreach ($this->files as $k => $file)
{
$path = $file['path'];
$name = $file['name'];
$fid = $file['id'];
$db->escape($path);
$db->escape($name);
$db->escape($fid);
if (!isset($file['id']))
{
$db->query("INSERT INTO exercice_files
VALUES ('"$id"', '".$path."', '".$name);
$this->files[$k]['id'] = $db->insert_id();
}
else
{
$db->query("UPDATE exercice_files
SET `path` = '".$path."', `name` = '".$name."'
WHERE id = '".$fid);
}
}
$db->deconnexion();
return ($aff == 1);
}
function create()
{
update(true);
}
function add_key($format, $value)
{
$key = ["format" => $format, "value" => $value];
if (isset($key))
$this->keys[] = $key;
}
function add_file($path, $name)
{
$file = ["path" => $path, "name" => $name];
if (isset($file))
$this->files[] = $file;
}
}

View File

@ -53,4 +53,18 @@ class Theme {
{ {
return $this->title; return $this->title;
} }
function get_id()
{
return $this->id;
}
function add_exercice($exercice)
{
if (isset($exercice))
{
$exercice->theme = $this;
$exercice->update(true);
}
}
} }