Add classes to common includes

This commit is contained in:
Némunaire 2013-10-10 04:47:02 +02:00
parent e1a654359a
commit 0fdc0703f6
4 changed files with 198 additions and 189 deletions

View File

@ -6,6 +6,10 @@ if (empty($sess->values["connected"]) && !defined("xCSRF"))
require_once("functions.php"); //Inclusion des principales fonctions require_once("functions.php"); //Inclusion des principales fonctions
require_once("common/Exercice.class.php");
require_once("common/Theme.class.php");
require_once("common/User.class.php");
//On charge la session //On charge la session
$SESS = new Session(); $SESS = new Session();
@ -25,4 +29,3 @@ if ($SESS->level > 2 && !empty($_SERVER["HTTP_REFERER"]) && !(preg_match('#^http
unset($_POST, $_GET); unset($_POST, $_GET);
$_GET = $_POST = array(); $_GET = $_POST = array();
} }
?>

View File

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

View File

@ -2,69 +2,71 @@
if(!defined('ONYX')) exit; if(!defined('ONYX')) exit;
class Theme { class Theme
{
$id=null; var $id=null;
$title; var $title;
function Theme($id=null) function Theme($id=null)
{ {
if (!empty($id)) if (!empty($id))
{ {
$db = new BDD(); $db = new BDD();
$res = $db->unique_query("SELECT id, title $res = $db->unique_query("SELECT id, title
FROM themes WHERE id=" . intval($id)); FROM themes WHERE id=" . intval($id));
if (!empty($res)) if (!empty($res))
{ {
$this->title = $res['title']; $this->title = $res['title'];
} }
$db->deconnexion(); $db->deconnexion();
} }
} }
function update() function update()
{ {
$title = $this->title; $title = $this->title;
$db = new BDD(); $db = new BDD();
$db->escape($title); $db->escape($title);
if (empty($this->id)) if (empty($this->id))
{ {
$db->query("INSERT INTO themes $db->query("INSERT INTO themes
VALUES (NULL, '".$title); VALUES (NULL, '".$title."');");
$this->id = $db->insert_id(); $this->id = $db->insert_id();
$aff = ($this->id > 0); $aff = ($this->id > 0);
} }
else else
{ {
$db->query("UPDATE themes $db->query("UPDATE themes
SET title = '".$title."' SET title = '".$title."'
WHERE id = ".intval($this->id)); WHERE id = ".intval($this->id));
$aff = $db->affected(); $aff = $db->affected();
} }
$db->deconnexion(); $db->deconnexion();
return ($aff == 1); return ($aff == 1);
} }
function get_title() function get_title()
{ {
return $this->title; return $this->title;
} }
function get_id() function get_id()
{ {
return $this->id; return $this->id;
} }
function add_exercice($exercice) function add_exercice($exercice)
{ {
if (isset($exercice)) if (isset($exercice))
{ {
$exercice->theme = $this; $exercice->theme = $this;
$exercice->update(true); return $exercice->update(true);
} }
} return false;
}
} }

View File

@ -37,6 +37,7 @@ class User {
$auth_level = intval($this->auth_level); $auth_level = intval($this->auth_level);
$firstname = $this->firstname; $firstname = $this->firstname;
$lastname = $this->lastname; $lastname = $this->lastname;
$company = $this->company;
$db = new BDD(); $db = new BDD();
$db->escape($username); $db->escape($username);
@ -47,7 +48,7 @@ class User {
if (empty($this->id)) if (empty($this->id))
{ {
$db->query("INSERT INTO users $db->query("INSERT INTO users
VALUES (NULL, '".$username."', NULL , ".$auth_level." '".$firstname."', '".$lastname."', '".$company."')"); VALUES (NULL, '".$username."', 0x0, ".$auth_level.", '".$firstname."', '".$lastname."', '".$company."')");
$this->id = $db->insert_id(); $this->id = $db->insert_id();
$aff = ($this->id > 0); $aff = ($this->id > 0);
} }
@ -107,7 +108,7 @@ class User {
if (!empty($res)) if (!empty($res))
{ {
$this->points = res['sum_points']; $this->points = $res['sum_points'];
} }
$db->deconnexion(); $db->deconnexion();
} }