2013-10-09 23:18:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if(!defined('ONYX')) exit;
|
|
|
|
|
2013-10-22 06:56:38 +00:00
|
|
|
function cmp_team_pts($i1, $i2)
|
|
|
|
{
|
|
|
|
if ($i1->get_pts() == $i2->get_pts()){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return ($i1->get_pts() < $i2->get_pts()) ? 1 : -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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-10-26 17:20:24 +00:00
|
|
|
$res = $db->unique_query("SELECT id, team_name, key_hash, slogan, auth_level
|
2013-10-23 19:32:17 +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'];
|
|
|
|
}
|
|
|
|
$db->deconnexion();
|
2013-10-09 23:18:49 +00:00
|
|
|
}
|
2013-10-10 03:52:48 +00:00
|
|
|
}
|
|
|
|
|
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-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-08 18:15:17 +00:00
|
|
|
VALUES (NULL, '".$team_name."', '".$key_hash."', ".$auth_level.", '".$slogan."')");
|
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-08 18:15:17 +00:00
|
|
|
SET team_name = '".$team_name."', auth_level = ".$auth_level.", key_hash = '".$key_hash."', slogan = '".$slogan."'
|
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-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-10-10 03:52:48 +00:00
|
|
|
function get_auth_level() {
|
|
|
|
return $this->auth_level;
|
|
|
|
}
|
|
|
|
|
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-10-26 16:17:25 +00:00
|
|
|
$res = $db->query("SELECT id FROM team_members
|
2013-10-23 19:32:17 +00:00
|
|
|
WHERE id_team = " . intval($this->id));
|
|
|
|
|
|
|
|
$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-10-10 03:52:48 +00:00
|
|
|
function get_pts()
|
|
|
|
{
|
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-11-08 19:56:07 +00:00
|
|
|
$res = $db->unique_query("SELECT e.id, s.id_team, SUM(e.points) as sum_points
|
2013-10-10 03:52:48 +00:00
|
|
|
FROM exercices e
|
|
|
|
LEFT OUTER JOIN solved s ON e.id = s.id_exercice
|
2013-11-08 19:56:07 +00:00
|
|
|
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-10-22 05:58:39 +00:00
|
|
|
function get_groupIds()
|
|
|
|
{
|
|
|
|
$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();
|
|
|
|
for ($i = 0; $i < 10; $i++){
|
|
|
|
$tid = $teams[$i]->get_id();
|
|
|
|
if ($tid == $this->id)
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-26 19:44:19 +00:00
|
|
|
function get_nbResExercisesByTheme($id_theme)
|
|
|
|
{
|
|
|
|
$db = new BDD();
|
|
|
|
$res = $db->unique_query("SELECT e.id_theme AS theme, count( s.id ) AS solved
|
|
|
|
FROM solved AS s
|
|
|
|
RIGHT OUTER JOIN exercices AS e ON e.id = s.id_exercice
|
2013-11-08 19:56:07 +00:00
|
|
|
AND s.id_team = ".$this->id."
|
2013-10-26 19:44:19 +00:00
|
|
|
AND e.id_theme = ".$id_theme);
|
|
|
|
|
|
|
|
$db->deconnexion();
|
|
|
|
|
|
|
|
return $res['solved'];
|
|
|
|
}
|
|
|
|
|
2013-11-07 20:53:35 +00:00
|
|
|
function get_solvedExercices($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-07 20:53:35 +00:00
|
|
|
$ids = $db->query("SELECT id_theme, id_exercice
|
|
|
|
FROM `solved`
|
|
|
|
LEFT OUTER JOIN exercices ON `solved`.id_exercice = `exercices`.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();
|
|
|
|
|
|
|
|
$array = array();
|
|
|
|
$i = 0;
|
|
|
|
if ($ids)
|
|
|
|
{
|
|
|
|
foreach ($ids as $id){
|
|
|
|
$array[] = new Exercice($id['id_exercice']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
|
|
|
|
}
|
|
|
|
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
|
|
|
|
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-11-04 22:20:16 +00:00
|
|
|
public static function get_top($nb=0)
|
2013-10-22 05:58:39 +00:00
|
|
|
{
|
2013-10-22 06:56:38 +00:00
|
|
|
$teams = Team::get_teams();
|
2013-10-23 19:32:17 +00:00
|
|
|
|
2013-10-22 06:56:38 +00:00
|
|
|
usort($teams, "cmp_team_pts");
|
2013-11-04 22:20:16 +00:00
|
|
|
|
|
|
|
if ($nb != 0)
|
|
|
|
$teams = array_slice($teams, 0, $nb);
|
|
|
|
|
2013-10-22 05:58:39 +00:00
|
|
|
return $teams;
|
|
|
|
}
|
2013-10-09 23:18:49 +00:00
|
|
|
|
2013-10-26 17:20:24 +00:00
|
|
|
public static function get_nbTeams()
|
|
|
|
{
|
|
|
|
$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
|
|
|
}
|