2013-10-09 23:18:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if(!defined('ONYX')) exit;
|
|
|
|
|
2013-12-13 19:03:04 +00:00
|
|
|
function cmp_team_pts($i1, $i2)
|
2013-10-22 06:56:38 +00:00
|
|
|
{
|
|
|
|
if ($i1->get_pts() == $i2->get_pts()){
|
2013-11-25 22:06:26 +00:00
|
|
|
$db = new BDD();
|
|
|
|
|
|
|
|
$timestampi1 = $db->unique_query("SELECT MAX( s.time ) AS maxTime
|
|
|
|
FROM solved AS s
|
2013-11-26 19:25:25 +00:00
|
|
|
WHERE s.id_team =".$i1->get_id());
|
2013-11-25 22:06:26 +00:00
|
|
|
$timestampi2 = $db->unique_query("SELECT MAX( s.time ) AS maxTime
|
|
|
|
FROM solved AS s
|
2013-11-26 19:25:25 +00:00
|
|
|
WHERE s.id_team =".$i2->get_id());
|
2013-11-25 22:06:26 +00:00
|
|
|
|
|
|
|
$db->deconnexion();
|
|
|
|
|
2013-11-26 19:25:25 +00:00
|
|
|
if ($timestampi1['maxTime'] > $timestampi2['maxTime'])
|
|
|
|
return 1;
|
2013-11-25 22:06:26 +00:00
|
|
|
else
|
2013-11-26 19:25:25 +00:00
|
|
|
return -1;
|
2013-10-22 06:56:38 +00:00
|
|
|
}
|
|
|
|
else{
|
2013-12-13 19:03:04 +00:00
|
|
|
return ($i1->get_pts() < $i2->get_pts()) ? 1 : -1;
|
2013-10-22 06:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 05:58:39 +00:00
|
|
|
class Team
|
2013-10-10 03:52:48 +00:00
|
|
|
{
|
|
|
|
var $id = null;
|
2013-10-26 17:20:24 +00:00
|
|
|
var $team_name;
|
2013-10-23 19:32:17 +00:00
|
|
|
var $key_hash;
|
2013-10-10 03:52:48 +00:00
|
|
|
var $auth_level;
|
2013-10-26 16:17:25 +00:00
|
|
|
var $slogan;
|
|
|
|
var $members = array();
|
2013-10-10 03:52:48 +00:00
|
|
|
var $points = null;
|
2013-11-30 20:30:20 +00:00
|
|
|
var $revoked = 0;
|
2013-10-10 03:52:48 +00:00
|
|
|
|
2013-10-22 05:58:39 +00:00
|
|
|
// Constructor
|
|
|
|
function Team ($id=null)
|
2013-10-10 03:52:48 +00:00
|
|
|
{
|
|
|
|
if (!empty($id))
|
2013-10-09 23:18:49 +00:00
|
|
|
{
|
2013-10-10 03:52:48 +00:00
|
|
|
$db = new BDD();
|
2013-11-30 20:30:20 +00:00
|
|
|
$res = $db->unique_query("SELECT `id`, `team_name`, `key_hash`, `slogan`,
|
|
|
|
`auth_level`, `revoked`
|
2013-11-12 15:52:50 +00:00
|
|
|
FROM teams WHERE id = ".intval($id)) or die($db->erreur());
|
2013-10-10 03:52:48 +00:00
|
|
|
|
|
|
|
if (!empty($res))
|
|
|
|
{
|
2013-10-22 06:56:38 +00:00
|
|
|
$this->id = $res['id'];
|
2013-10-26 17:20:24 +00:00
|
|
|
$this->team_name = $res['team_name'];
|
2013-10-23 19:32:17 +00:00
|
|
|
$this->key_hash = $res['key_hash'];
|
2013-10-26 16:17:25 +00:00
|
|
|
$this->slogan = $res['slogan'];
|
2013-10-10 03:52:48 +00:00
|
|
|
$this->auth_level = $res['auth_level'];
|
2013-11-30 20:30:20 +00:00
|
|
|
$this->revoked = $res['revoked'];
|
2013-10-10 03:52:48 +00:00
|
|
|
}
|
|
|
|
$db->deconnexion();
|
2013-10-09 23:18:49 +00:00
|
|
|
}
|
2013-10-10 03:52:48 +00:00
|
|
|
}
|
|
|
|
|
2013-12-14 05:11:14 +00:00
|
|
|
static function __set_state(array $array)
|
|
|
|
{
|
|
|
|
$tmp = new Team();
|
|
|
|
|
|
|
|
$tmp->id = $array["id"];
|
|
|
|
$tmp->team_name = $array["team_name"];
|
|
|
|
$tmp->key_hash = $array["key_hash"];
|
|
|
|
$tmp->auth_level = $array["auth_level"];
|
|
|
|
$tmp->slogan = $array["slogan"];
|
|
|
|
$tmp->members = $array["members"];
|
|
|
|
$tmp->points = $array["points"];
|
|
|
|
$tmp->revoked = $array["revoked"];
|
|
|
|
|
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2013-10-22 05:58:39 +00:00
|
|
|
// Class methods
|
2013-10-10 03:52:48 +00:00
|
|
|
function update()
|
|
|
|
{
|
2013-10-23 19:32:17 +00:00
|
|
|
$key_hash = $this->key_hash;
|
2013-10-10 03:52:48 +00:00
|
|
|
$auth_level = intval($this->auth_level);
|
2013-10-26 16:17:25 +00:00
|
|
|
$slogan = $this->slogan;
|
2013-10-26 17:20:24 +00:00
|
|
|
$team_name = $this->team_name;
|
2013-11-30 20:30:20 +00:00
|
|
|
$revoked = $this->revoked;
|
2013-10-10 03:52:48 +00:00
|
|
|
|
|
|
|
$db = new BDD();
|
2013-10-23 19:32:17 +00:00
|
|
|
$db->escape($key_hash);
|
2013-10-26 16:17:25 +00:00
|
|
|
$db->escape($slogan);
|
2013-10-26 17:20:24 +00:00
|
|
|
$db->escape($team_name);
|
2013-10-10 03:52:48 +00:00
|
|
|
|
|
|
|
if (empty($this->id))
|
2013-10-09 23:18:49 +00:00
|
|
|
{
|
2013-10-23 19:32:17 +00:00
|
|
|
$db->query("INSERT INTO teams
|
2013-11-30 20:30:20 +00:00
|
|
|
VALUES (NULL, '".$team_name."', '".$key_hash."', ".$auth_level.", '".
|
|
|
|
$slogan."', ".$revoked.")");
|
2013-10-10 03:52:48 +00:00
|
|
|
$this->id = $db->insert_id();
|
|
|
|
$aff = ($this->id > 0);
|
2013-10-09 23:18:49 +00:00
|
|
|
}
|
2013-10-10 03:52:48 +00:00
|
|
|
else
|
2013-10-09 23:18:49 +00:00
|
|
|
{
|
2013-10-26 16:17:25 +00:00
|
|
|
$db->query("UPDATE teams
|
2013-11-30 20:30:20 +00:00
|
|
|
SET team_name = '".$team_name."', auth_level = ".$auth_level.
|
|
|
|
", key_hash = '".$key_hash."', slogan = '".
|
|
|
|
$slogan."', ".intval($revoked)."
|
2013-10-10 03:52:48 +00:00
|
|
|
WHERE id = ".intval($this->id));
|
|
|
|
$aff = $db->affected();
|
2013-10-09 23:18:49 +00:00
|
|
|
}
|
2013-10-10 03:52:48 +00:00
|
|
|
$db->deconnexion();
|
2013-10-09 23:18:49 +00:00
|
|
|
|
2013-10-10 03:52:48 +00:00
|
|
|
return ($aff == 1);
|
|
|
|
}
|
2013-10-09 23:18:49 +00:00
|
|
|
|
2013-11-30 14:43:53 +00:00
|
|
|
// TODO: Drop members ?
|
|
|
|
function drop()
|
|
|
|
{
|
|
|
|
$db = new BDD();
|
|
|
|
if (!empty($this->id))
|
|
|
|
$db->query("DELETE FROM teams WHERE id = ".intval($this->id));
|
|
|
|
$aff = $db->affected();
|
|
|
|
if ($aff == 1)
|
|
|
|
$this->id = 0;
|
|
|
|
$db->deconnexion();
|
|
|
|
return $aff;
|
|
|
|
}
|
|
|
|
|
2013-10-10 03:52:48 +00:00
|
|
|
function get_id() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
2013-10-09 23:18:49 +00:00
|
|
|
|
2013-10-26 16:17:25 +00:00
|
|
|
function get_slogan() {
|
|
|
|
return $this->slogan;
|
2013-10-10 03:52:48 +00:00
|
|
|
}
|
|
|
|
|
2013-10-26 17:20:24 +00:00
|
|
|
function get_name() {
|
|
|
|
return $this->team_name;
|
|
|
|
}
|
|
|
|
|
2013-12-13 17:48:13 +00:00
|
|
|
function get_name_url() {
|
|
|
|
return urlencode($this->team_name);
|
|
|
|
}
|
|
|
|
|
2013-10-10 03:52:48 +00:00
|
|
|
function get_auth_level() {
|
|
|
|
return $this->auth_level;
|
|
|
|
}
|
|
|
|
|
2013-11-30 20:30:20 +00:00
|
|
|
function get_revoked() {
|
|
|
|
return $this->revoked;
|
|
|
|
}
|
|
|
|
|
2013-10-23 19:32:17 +00:00
|
|
|
function get_members()
|
|
|
|
{
|
2013-10-26 16:17:25 +00:00
|
|
|
if(count($this->members) == 0)
|
2013-10-23 19:32:17 +00:00
|
|
|
{
|
|
|
|
$db = new BDD();
|
|
|
|
|
2013-11-12 15:52:50 +00:00
|
|
|
$res = $db->query("SELECT `id` FROM team_members
|
|
|
|
WHERE id_team = ".intval($this->id));
|
2013-10-23 19:32:17 +00:00
|
|
|
|
|
|
|
$db->deconnexion();
|
|
|
|
|
2013-10-26 16:17:25 +00:00
|
|
|
foreach($res as $member)
|
|
|
|
$this->members[] = new Member($member["id"], $this);
|
2013-10-23 19:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->members;
|
|
|
|
}
|
|
|
|
|
2013-12-13 19:03:04 +00:00
|
|
|
function get_pts()
|
2013-10-10 03:52:48 +00:00
|
|
|
{
|
2013-10-22 06:56:38 +00:00
|
|
|
if(!isset($this->points))
|
2013-10-09 23:18:49 +00:00
|
|
|
{
|
2013-10-10 03:52:48 +00:00
|
|
|
$db = new BDD();
|
|
|
|
|
2013-12-13 19:03:04 +00:00
|
|
|
$res = $db->unique_query("SELECT E.id, S.id_team, SUM(E.points) as sum_points
|
2013-11-12 15:52:50 +00:00
|
|
|
FROM exercices E
|
|
|
|
LEFT OUTER JOIN solved S ON E.id = S.id_exercice
|
|
|
|
WHERE S.id_team = ".$this->id."
|
|
|
|
GROUP BY S.id_team");
|
2013-10-22 06:56:38 +00:00
|
|
|
|
2013-10-10 03:52:48 +00:00
|
|
|
$db->deconnexion();
|
|
|
|
|
|
|
|
if (!empty($res))
|
|
|
|
$this->points = $res['sum_points'];
|
2013-10-26 16:17:25 +00:00
|
|
|
else
|
|
|
|
$this->points = 0;
|
2013-10-09 23:18:49 +00:00
|
|
|
}
|
2013-10-22 06:56:38 +00:00
|
|
|
|
|
|
|
return $this->points;
|
2013-10-10 03:52:48 +00:00
|
|
|
}
|
|
|
|
|
2013-11-12 15:52:50 +00:00
|
|
|
function get_group_ids()
|
2013-10-22 05:58:39 +00:00
|
|
|
{
|
|
|
|
$db = new BDD();
|
|
|
|
|
|
|
|
$res = $db->query("SELECT `id` FROM `users`");
|
|
|
|
$db->deconnexion();
|
|
|
|
|
|
|
|
return $res['id'];
|
|
|
|
}
|
|
|
|
|
2013-10-26 17:20:24 +00:00
|
|
|
function get_rank()
|
|
|
|
{
|
|
|
|
$teams = Team::get_top();
|
2013-12-13 17:48:13 +00:00
|
|
|
foreach($teams as $k => $t)
|
|
|
|
{
|
|
|
|
if ($t->get_id() == $this->id)
|
|
|
|
return $k + 1;
|
2013-10-26 17:20:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-12 15:52:50 +00:00
|
|
|
function get_nb_res_exercises_by_theme($id_theme)
|
2013-10-26 19:44:19 +00:00
|
|
|
{
|
|
|
|
$db = new BDD();
|
2013-11-12 15:52:50 +00:00
|
|
|
$res = $db->unique_query("SELECT E.id_theme AS theme, count(S.id) AS solved
|
|
|
|
FROM solved S
|
|
|
|
RIGHT OUTER JOIN exercices E ON E.id = S.id_exercice
|
|
|
|
AND S.id_team = ".$this->id."
|
|
|
|
AND E.id_theme = ".$id_theme);
|
2013-10-26 19:44:19 +00:00
|
|
|
$db->deconnexion();
|
|
|
|
|
|
|
|
return $res['solved'];
|
|
|
|
}
|
|
|
|
|
2013-11-12 15:52:50 +00:00
|
|
|
function get_solved_exercices($id_theme=-1)
|
2013-11-05 04:15:18 +00:00
|
|
|
{
|
2013-11-07 20:53:35 +00:00
|
|
|
if ($id_theme != -1)
|
|
|
|
{
|
2013-11-08 18:15:17 +00:00
|
|
|
$db = new BDD();
|
2013-11-12 15:52:50 +00:00
|
|
|
$ids = $db->query("SELECT `id_theme`, `id_exercice`
|
|
|
|
FROM solved S
|
|
|
|
LEFT OUTER JOIN exercices E ON S.id_exercice = E.id
|
2013-11-08 19:56:07 +00:00
|
|
|
WHERE id_team =".$this->id." AND id_theme =".$id_theme);
|
2013-11-07 20:53:35 +00:00
|
|
|
$db->deconnexion();
|
|
|
|
|
2013-11-12 15:52:50 +00:00
|
|
|
$array = array();
|
|
|
|
$i = 0;
|
|
|
|
if ($ids)
|
|
|
|
{
|
2013-12-13 17:48:13 +00:00
|
|
|
foreach ($ids as $id)
|
2013-11-12 15:52:50 +00:00
|
|
|
$array[] = new Exercice($id['id_exercice']);
|
2013-11-07 20:53:35 +00:00
|
|
|
}
|
|
|
|
|
2013-11-12 15:52:50 +00:00
|
|
|
return $array;
|
2013-11-07 20:53:35 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
2013-11-05 04:15:18 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 03:52:48 +00:00
|
|
|
function authenticate($certificate)
|
|
|
|
{
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
2013-10-22 05:58:39 +00:00
|
|
|
// Static methods
|
2013-11-30 20:30:20 +00:00
|
|
|
public static function set_revoked($bool, $name)
|
|
|
|
{
|
|
|
|
$db = new BDD();
|
|
|
|
$db->query("UPDATE teams SET revoked = ".intval($bool)." WHERE team_name = '".$name."'");
|
|
|
|
$aff = $db->affected();
|
|
|
|
$db->deconnexion();
|
|
|
|
|
|
|
|
return ($aff == 1);
|
|
|
|
}
|
|
|
|
|
2013-12-01 18:40:47 +00:00
|
|
|
public static function remove_team($id)
|
|
|
|
{
|
|
|
|
$db = new BDD();
|
|
|
|
$db->query("DELETE FROM team_members WHERE id_team = ".$id);
|
|
|
|
$db->query("DELETE FROM solved WHERE id_team = ".$id);
|
|
|
|
$db->query("DELETE FROM teams WHERE id = ".$id);
|
|
|
|
$db->deconnexion();
|
|
|
|
}
|
|
|
|
|
2013-10-22 05:58:39 +00:00
|
|
|
public static function get_teams()
|
|
|
|
{
|
|
|
|
$db = new BDD();
|
2013-10-23 19:32:17 +00:00
|
|
|
$ids = $db->query("SELECT `id` FROM `teams`");
|
2013-10-22 05:58:39 +00:00
|
|
|
$db->deconnexion();
|
|
|
|
|
|
|
|
$array = array();
|
2013-11-08 18:15:17 +00:00
|
|
|
if ($ids)
|
|
|
|
foreach ($ids as $id)
|
|
|
|
$array[] = new Team($id['id']);
|
2013-10-22 05:58:39 +00:00
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2013-12-13 19:03:04 +00:00
|
|
|
public static function get_top($nb=0)
|
2013-10-22 05:58:39 +00:00
|
|
|
{
|
2013-12-14 05:11:14 +00:00
|
|
|
$top = Cache::read("top");
|
|
|
|
if (empty($top))
|
|
|
|
{
|
|
|
|
$top = Team::get_teams();
|
|
|
|
|
|
|
|
usort($top, "cmp_team_pts");
|
2013-10-23 19:32:17 +00:00
|
|
|
|
2013-12-14 05:11:14 +00:00
|
|
|
Cache::set("top", $top);
|
|
|
|
}
|
2013-11-04 22:20:16 +00:00
|
|
|
|
|
|
|
if ($nb != 0)
|
2013-12-14 05:11:14 +00:00
|
|
|
$top = array_slice($top, 0, $nb);
|
2013-11-04 22:20:16 +00:00
|
|
|
|
2013-12-14 05:11:14 +00:00
|
|
|
return $top;
|
2013-10-22 05:58:39 +00:00
|
|
|
}
|
2013-10-09 23:18:49 +00:00
|
|
|
|
2013-11-12 15:52:50 +00:00
|
|
|
public static function get_nb_teams()
|
2013-10-26 17:20:24 +00:00
|
|
|
{
|
|
|
|
$db = new BDD();
|
|
|
|
$res = $db->unique_query("SELECT COUNT(id) as count_teams FROM teams");
|
|
|
|
$db->deconnexion();
|
|
|
|
|
|
|
|
return $res['count_teams'];
|
|
|
|
}
|
2013-10-23 19:32:17 +00:00
|
|
|
}
|