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->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(); $db = new BDD();
} $db->escape($id);
else $db->escape($theme);
{ $db->escape($require);
$db->query("UPDATE exercices $db->escape($statement);
SET `theme` = '".$theme."', `require` = '".$require."', `level` = '".$level."', `point` = '".$point."', `statement` = '"$statement"'
WHERE id = ".$id);
$aff = $db->affected(); if ($create)
} {
$db->query("INSERT INTO exercices
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);
foreach ($this->keys as $k => $key) $aff = $db->affected();
{ }
$format = $key['format'];
$value = $key['value'];
$kid = $key['id'];
$db->escape($format); foreach ($this->keys as $k => $key)
$db->escape($value); {
$db->escape($kid); $format = $key['format'];
$value = $key['value'];
if (isset($key['id']))
$kid = $key['id'];
else
$kid = NULL;
if (!isset($kid)) $db->escape($format);
{ $db->escape($value);
$db->query("INSERT INTO exercice_keys $db->escape($kid);
VALUES (NULL, '".$id."', '".$format."', '".$value);
$this->keys[$k]['id'] = $db->insert_id(); if (empty($kid))
} {
else $db->query("INSERT INTO exercice_keys
{ VALUES (NULL, '".$this->id."', '".$format."', UNHEX('".strhex($value)."'))");
$db->query("UPDATE exercice_keys
SET `format` = '".$format."', `value` = '".$value."'
WHERE id = '".$kid);
}
}
foreach ($this->files as $k => $file) $this->keys[$k]['id'] = $db->insert_id();
{ }
$path = $file['path']; else
$name = $file['name']; {
$fid = $file['id']; $db->query("UPDATE exercice_keys
SET `format` = '".$format."', `value` = UNHEX('".strhex($value).")')
WHERE id = ".$kid);
}
}
$db->escape($path); foreach ($this->files as $k => $file)
$db->escape($name); {
$db->escape($fid); $path = $file['path'];
$name = $file['name'];
if (isset($file['id']))
$fid = intval($file['id']);
if (!isset($file['id'])) $db->escape($path);
{ $db->escape($name);
$db->query("INSERT INTO exercice_files
VALUES ('"$id"', '".$path."', '".$name);
$this->files[$k]['id'] = $db->insert_id(); if (!isset($file['id']))
} {
else $db->query("INSERT INTO exercice_files
{ VALUES (NULL, '".$id."', '".$path."', '".$name."');");
$db->query("UPDATE exercice_files
SET `path` = '".$path."', `name` = '".$name."'
WHERE id = '".$fid);
}
}
$db->deconnexion(); $this->files[$k]['id'] = $db->insert_id();
}
return ($aff == 1); else
} {
$db->query("UPDATE exercice_files
SET `path` = '".$path."', `name` = '".$name."'
WHERE id = ".$fid);
}
}
function create() $db->deconnexion();
{
update(true);
}
function add_key($format, $value) //FIXME
{ return 1;
$key = ["format" => $format, "value" => $value]; }
if (isset($key))
$this->keys[] = $key;
}
function add_file($path, $name) function create()
{ {
$file = ["path" => $path, "name" => $name]; update(true);
if (isset($file)) }
$this->files[] = $file;
} 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

@ -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);
}
function get_title() return ($aff == 1);
{ }
return $this->title;
}
function get_id() function get_title()
{ {
return $this->id; return $this->title;
} }
function add_exercice($exercice) function get_id()
{ {
if (isset($exercice)) return $this->id;
{ }
$exercice->theme = $this;
$exercice->update(true); function add_exercice($exercice)
} {
} if (isset($exercice))
} {
$exercice->theme = $this;
return $exercice->update(true);
}
return false;
}
}

View File

@ -37,17 +37,18 @@ 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);
$db->escape($firstname); $db->escape($firstname);
$db->escape($lastname); $db->escape($lastname);
$db->escape($company); $db->escape($company);
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);
} }
@ -59,7 +60,7 @@ class User {
$aff = $db->affected(); $aff = $db->affected();
} }
$db->deconnexion(); $db->deconnexion();
return ($aff == 1); return ($aff == 1);
} }
@ -98,7 +99,7 @@ class User {
if(isset($this->points)) if(isset($this->points))
{ {
$db = new BDD(); $db = new BDD();
$res = $db->query("SELECT e.id, s.id_user, SUM(e.points) as sum_points $res = $db->query("SELECT e.id, s.id_user, SUM(e.points) as sum_points
FROM exercices e FROM exercices e
LEFT OUTER JOIN solved s ON e.id = s.id_exercice LEFT OUTER JOIN solved s ON e.id = s.id_exercice
@ -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();
} }