322 lines
7.3 KiB
PHP
322 lines
7.3 KiB
PHP
<?php
|
|
|
|
if(!defined('ONYX')) exit;
|
|
|
|
function cmp_team_pts($i1, $i2, $idTheme)
|
|
{
|
|
if ($i1->get_pts() == $i2->get_pts()){
|
|
$db = new BDD();
|
|
|
|
$timestampi1 = $db->unique_query("SELECT MAX( s.time ) AS maxTime
|
|
FROM solved AS s
|
|
WHERE s.id_team =".$i1->get_id());
|
|
$timestampi2 = $db->unique_query("SELECT MAX( s.time ) AS maxTime
|
|
FROM solved AS s
|
|
WHERE s.id_team =".$i2->get_id());
|
|
|
|
$db->deconnexion();
|
|
|
|
if ($timestampi1['maxTime'] > $timestampi2['maxTime'])
|
|
return 1;
|
|
else
|
|
return -1;
|
|
}
|
|
else{
|
|
return ($i1->get_pts($idTheme) < $i2->get_pts($idTheme)) ? 1 : -1;
|
|
}
|
|
}
|
|
|
|
class Team
|
|
{
|
|
var $id = null;
|
|
var $team_name;
|
|
var $key_hash;
|
|
var $auth_level;
|
|
var $slogan;
|
|
var $members = array();
|
|
var $points = null;
|
|
var $revoked = 0;
|
|
|
|
// Constructor
|
|
function Team ($id=null)
|
|
{
|
|
if (!empty($id))
|
|
{
|
|
$db = new BDD();
|
|
$res = $db->unique_query("SELECT `id`, `team_name`, `key_hash`, `slogan`,
|
|
`auth_level`, `revoked`
|
|
FROM teams WHERE id = ".intval($id)) or die($db->erreur());
|
|
|
|
if (!empty($res))
|
|
{
|
|
$this->id = $res['id'];
|
|
$this->team_name = $res['team_name'];
|
|
$this->key_hash = $res['key_hash'];
|
|
$this->slogan = $res['slogan'];
|
|
$this->auth_level = $res['auth_level'];
|
|
$this->revoked = $res['revoked'];
|
|
}
|
|
$db->deconnexion();
|
|
}
|
|
}
|
|
|
|
// Class methods
|
|
function update()
|
|
{
|
|
$key_hash = $this->key_hash;
|
|
$auth_level = intval($this->auth_level);
|
|
$slogan = $this->slogan;
|
|
$team_name = $this->team_name;
|
|
$revoked = $this->revoked;
|
|
|
|
$db = new BDD();
|
|
$db->escape($key_hash);
|
|
$db->escape($slogan);
|
|
$db->escape($team_name);
|
|
|
|
if (empty($this->id))
|
|
{
|
|
$db->query("INSERT INTO teams
|
|
VALUES (NULL, '".$team_name."', '".$key_hash."', ".$auth_level.", '".
|
|
$slogan."', ".$revoked.")");
|
|
$this->id = $db->insert_id();
|
|
$aff = ($this->id > 0);
|
|
}
|
|
else
|
|
{
|
|
$db->query("UPDATE teams
|
|
SET team_name = '".$team_name."', auth_level = ".$auth_level.
|
|
", key_hash = '".$key_hash."', slogan = '".
|
|
$slogan."', ".intval($revoked)."
|
|
WHERE id = ".intval($this->id));
|
|
$aff = $db->affected();
|
|
}
|
|
$db->deconnexion();
|
|
|
|
return ($aff == 1);
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
function get_id() {
|
|
return $this->id;
|
|
}
|
|
|
|
function get_slogan() {
|
|
return $this->slogan;
|
|
}
|
|
|
|
function get_name() {
|
|
return $this->team_name;
|
|
}
|
|
|
|
function get_auth_level() {
|
|
return $this->auth_level;
|
|
}
|
|
|
|
function get_revoked() {
|
|
return $this->revoked;
|
|
}
|
|
|
|
function get_members()
|
|
{
|
|
if(count($this->members) == 0)
|
|
{
|
|
$db = new BDD();
|
|
|
|
$res = $db->query("SELECT `id` FROM team_members
|
|
WHERE id_team = ".intval($this->id));
|
|
|
|
$db->deconnexion();
|
|
|
|
foreach($res as $member)
|
|
$this->members[] = new Member($member["id"], $this);
|
|
}
|
|
|
|
return $this->members;
|
|
}
|
|
|
|
function get_pts($themeID=-1)
|
|
{
|
|
if(!isset($this->points))
|
|
{
|
|
$db = new BDD();
|
|
$res = null;
|
|
|
|
if ($themeID != -1)
|
|
{
|
|
$res = $db->unique_query("SELECT E.id, S.id_team, SUM(E.points) as sum_points
|
|
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");
|
|
}
|
|
else
|
|
{
|
|
$res = $db->unique_query("SELECT E.id, S.id_team, SUM(E.points) as sum_points
|
|
FROM exercices E
|
|
LEFT OUTER JOIN solved S ON E.id = S.id_exercice
|
|
WHERE S.id_team = ".$this->id." AND E.id_theme = ".$themeID."
|
|
GROUP BY S.id_team");
|
|
}
|
|
|
|
$db->deconnexion();
|
|
|
|
if (!empty($res))
|
|
$this->points = $res['sum_points'];
|
|
else
|
|
$this->points = 0;
|
|
}
|
|
|
|
return $this->points;
|
|
}
|
|
|
|
function get_group_ids()
|
|
{
|
|
$db = new BDD();
|
|
|
|
$res = $db->query("SELECT `id` FROM `users`");
|
|
$db->deconnexion();
|
|
|
|
return $res['id'];
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
function get_nb_res_exercises_by_theme($id_theme)
|
|
{
|
|
$db = new BDD();
|
|
$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);
|
|
$db->deconnexion();
|
|
|
|
return $res['solved'];
|
|
}
|
|
|
|
function get_solved_exercices($id_theme=-1)
|
|
{
|
|
if ($id_theme != -1)
|
|
{
|
|
$db = new BDD();
|
|
$ids = $db->query("SELECT `id_theme`, `id_exercice`
|
|
FROM solved S
|
|
LEFT OUTER JOIN exercices E ON S.id_exercice = E.id
|
|
WHERE id_team =".$this->id." AND id_theme =".$id_theme);
|
|
$db->deconnexion();
|
|
|
|
$array = array();
|
|
$i = 0;
|
|
if ($ids)
|
|
{
|
|
foreach ($ids as $id){
|
|
$array[] = new Exercice($id['id_exercice']);
|
|
}
|
|
}
|
|
|
|
return $array;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
function authenticate($certificate)
|
|
{
|
|
//TODO
|
|
}
|
|
|
|
// Static methods
|
|
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);
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
public static function get_teams()
|
|
{
|
|
$db = new BDD();
|
|
$ids = $db->query("SELECT `id` FROM `teams`");
|
|
$db->deconnexion();
|
|
|
|
$array = array();
|
|
if ($ids)
|
|
foreach ($ids as $id)
|
|
$array[] = new Team($id['id']);
|
|
|
|
return $array;
|
|
}
|
|
|
|
public static function get_top($nb=0, $idTheme=-1)
|
|
{
|
|
$teams = Team::get_teams();
|
|
|
|
usort($teams, "cmp_team_pts", $idTheme);
|
|
|
|
if ($nb != 0)
|
|
$teams = array_slice($teams, 0, $nb);
|
|
|
|
return $teams;
|
|
}
|
|
|
|
public static function get_nb_teams()
|
|
{
|
|
$db = new BDD();
|
|
$res = $db->unique_query("SELECT COUNT(id) as count_teams FROM teams");
|
|
$db->deconnexion();
|
|
|
|
return $res['count_teams'];
|
|
}
|
|
|
|
public static function first_to_solve_exercice($id_exercice)
|
|
{
|
|
$db = new BDD();
|
|
$res = $db->unique_query("SELECT t3.team_name as result
|
|
FROM solved AS t1
|
|
INNER JOIN (
|
|
SELECT MIN(s.time) AS minTime
|
|
FROM solved AS s
|
|
WHERE s.id_exercice = ".$id_exercice."
|
|
) AS t2
|
|
INNER JOIN teams AS t3 ON t1.id_team = t3.id
|
|
WHERE t1.time = t2.minTime");
|
|
$db->deconnexion();
|
|
|
|
return $res['result'];
|
|
}
|
|
}
|