167 lines
3.1 KiB
PHP
167 lines
3.1 KiB
PHP
<?php
|
|
|
|
if(!defined('ONYX')) exit;
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
class Team
|
|
{
|
|
var $id = null;
|
|
var $key_hash;
|
|
var $auth_level;
|
|
var $slogan;
|
|
var $members = array();
|
|
var $points = null;
|
|
|
|
// Constructor
|
|
function Team ($id=null)
|
|
{
|
|
if (!empty($id))
|
|
{
|
|
$db = new BDD();
|
|
$res = $db->unique_query("SELECT id, key_hash, slogan, auth_level
|
|
FROM teams WHERE id=" . intval($id)) or die($db->erreur());
|
|
|
|
if (!empty($res))
|
|
{
|
|
$this->id = $res['id'];
|
|
$this->key_hash = $res['key_hash'];
|
|
$this->slogan = $res['slogan'];
|
|
$this->auth_level = $res['auth_level'];
|
|
}
|
|
$db->deconnexion();
|
|
}
|
|
}
|
|
|
|
// Class methods
|
|
function update()
|
|
{
|
|
$key_hash = $this->key_hash;
|
|
$auth_level = intval($this->auth_level);
|
|
$slogan = $this->slogan;
|
|
|
|
$db = new BDD();
|
|
$db->escape($key_hash);
|
|
$db->escape($slogan);
|
|
|
|
if (empty($this->id))
|
|
{
|
|
$db->query("INSERT INTO teams
|
|
VALUES (NULL, '".$key_hash."', ".$auth_level.", '".$slogan."')");
|
|
$this->id = $db->insert_id();
|
|
$aff = ($this->id > 0);
|
|
}
|
|
else
|
|
{
|
|
$db->query("UPDATE teams
|
|
SET auth_level = ".$auth_level.", key_hash = '".$key_hash."', slogan = '".$slogan."'
|
|
WHERE id = ".intval($this->id));
|
|
$aff = $db->affected();
|
|
}
|
|
$db->deconnexion();
|
|
|
|
return ($aff == 1);
|
|
}
|
|
|
|
function get_id() {
|
|
return $this->id;
|
|
}
|
|
|
|
function get_slogan() {
|
|
return $this->slogan;
|
|
}
|
|
|
|
function get_auth_level() {
|
|
return $this->auth_level;
|
|
}
|
|
|
|
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()
|
|
{
|
|
if(!isset($this->points))
|
|
{
|
|
$db = new BDD();
|
|
|
|
$res = $db->unique_query("SELECT e.id, s.id_user, SUM(e.points) as sum_points
|
|
FROM exercices e
|
|
LEFT OUTER JOIN solved s ON e.id = s.id_exercice
|
|
WHERE s.id_user = " . $this->id . "
|
|
GROUP BY s.id_user");
|
|
|
|
$db->deconnexion();
|
|
|
|
if (!empty($res))
|
|
$this->points = $res['sum_points'];
|
|
else
|
|
$this->points = 0;
|
|
}
|
|
|
|
return $this->points;
|
|
}
|
|
|
|
function get_groupIds()
|
|
{
|
|
$db = new BDD();
|
|
|
|
$res = $db->query("SELECT `id` FROM `users`");
|
|
$db->deconnexion();
|
|
|
|
return $res['id'];
|
|
}
|
|
|
|
function authenticate($certificate)
|
|
{
|
|
//TODO
|
|
}
|
|
|
|
// Static methods
|
|
public static function get_teams()
|
|
{
|
|
$db = new BDD();
|
|
$ids = $db->query("SELECT `id` FROM `teams`");
|
|
$db->deconnexion();
|
|
|
|
$array = array();
|
|
foreach ($ids as $id){
|
|
$array[] = new Team($id['id']);
|
|
}
|
|
|
|
return $array;
|
|
}
|
|
|
|
// TODO: Not tested, need feeding BDD
|
|
public static function get_top()
|
|
{
|
|
$teams = Team::get_teams();
|
|
|
|
usort($teams, "cmp_team_pts");
|
|
return $teams;
|
|
}
|
|
|
|
}
|