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-23 19:32:17 +00:00
|
|
|
var $key_hash;
|
2013-10-10 03:52:48 +00:00
|
|
|
var $auth_level;
|
2013-10-23 19:32:17 +00:00
|
|
|
var $company;
|
|
|
|
var $members = null;
|
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-23 19:32:17 +00:00
|
|
|
$res = $db->unique_query("SELECT id, key_hash, company, auth_level
|
|
|
|
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-23 19:32:17 +00:00
|
|
|
$this->key_hash = $res['key_hash'];
|
2013-10-10 03:52:48 +00:00
|
|
|
$this->company = $res['company'];
|
|
|
|
$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);
|
|
|
|
$company = $this->company;
|
|
|
|
|
|
|
|
$db = new BDD();
|
2013-10-23 19:32:17 +00:00
|
|
|
$db->escape($key_hash);
|
2013-10-10 03:52:48 +00:00
|
|
|
$db->escape($company);
|
|
|
|
|
|
|
|
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
|
|
|
|
VALUES (NULL, '".$key_hash."', ".$auth_level.", '".$company."')");
|
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-10 03:52:48 +00:00
|
|
|
$db->query("UPDATE users
|
2013-10-23 19:32:17 +00:00
|
|
|
SET auth_level = ".$auth_level.", key_hash = '".$key_hash."', company = '".$company."'
|
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-10 03:52:48 +00:00
|
|
|
function get_company() {
|
|
|
|
return $this->company;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_auth_level() {
|
|
|
|
return $this->auth_level;
|
|
|
|
}
|
|
|
|
|
2013-10-23 19:32:17 +00:00
|
|
|
function get_members()
|
|
|
|
{
|
|
|
|
if(!isset($this->members))
|
|
|
|
{
|
|
|
|
$db = new BDD();
|
|
|
|
|
|
|
|
$res = $db->query("SELECT id, firstname, lastname, nickname
|
|
|
|
FROM team_members
|
|
|
|
WHERE id_team = " . intval($this->id));
|
|
|
|
|
|
|
|
$db->deconnexion();
|
|
|
|
|
|
|
|
if (!empty($res))
|
|
|
|
$this->members = $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-22 06:56:38 +00:00
|
|
|
$res = $db->unique_query("SELECT e.id, s.id_user, 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-10-22 06:56:38 +00:00
|
|
|
WHERE s.id_user = " . $this->id . "
|
2013-10-10 03:52:48 +00:00
|
|
|
GROUP BY s.id_user");
|
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-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-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();
|
|
|
|
foreach ($ids as $id){
|
|
|
|
$array[] = new Team($id['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Not tested, need feeding BDD
|
|
|
|
public static function get_top()
|
|
|
|
{
|
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-10-22 05:58:39 +00:00
|
|
|
return $teams;
|
|
|
|
}
|
2013-10-09 23:18:49 +00:00
|
|
|
|
2013-10-23 19:32:17 +00:00
|
|
|
}
|