server/onyx/include/common/Team.class.php
2013-10-22 08:16:02 +02:00

164 lines
3.2 KiB
PHP

<?php
if(!defined('ONYX')) exit;
class Team
{
var $id = null;
var $firstname;
var $lastname;
var $username;
var $company;
var $auth_level;
var $points = null;
var $nb_themes = null;
// Constructor
function Team ($id=null)
{
if (!empty($id))
{
$db = new BDD();
$res = $db->unique_query("SELECT id, firstname, lastname, username, company, auth_level
FROM users WHERE id=" . intval($id));
if (!empty($res))
{
$this->firstname = $res['firstname'];
$this->lastname = $res['lastname'];
$this->username = $res['username'];
$this->company = $res['company'];
$this->auth_level = $res['auth_level'];
}
$db->deconnexion();
}
}
// Class methods
function update()
{
$username = $this->username;
$auth_level = intval($this->auth_level);
$firstname = $this->firstname;
$lastname = $this->lastname;
$company = $this->company;
$db = new BDD();
$db->escape($username);
$db->escape($firstname);
$db->escape($lastname);
$db->escape($company);
if (empty($this->id))
{
$db->query("INSERT INTO users
VALUES (NULL, '".$username."', 0x0, ".$auth_level.", '".$firstname."', '".$lastname."', '".$company."')");
$this->id = $db->insert_id();
$aff = ($this->id > 0);
}
else
{
$db->query("UPDATE users
SET username = '".$username."', auth_level = '".$auth_level."', firstname = '".$firstname."', lastname = '".$lastname."', company = '".$company."'
WHERE id = ".intval($this->id));
$aff = $db->affected();
}
$db->deconnexion();
return ($aff == 1);
}
function get_id() {
return $this->id;
}
function get_firstname() {
return $this->firstname;
}
function get_lastname() {
return $this->lastname;
}
function get_username() {
return $this->username;
}
function get_company() {
return $this->company;
}
function get_auth_level() {
return $this->auth_level;
}
function get_pts()
{
if(isset($this->points))
{
$db = new BDD();
$res = $db->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 = " . intval($this->id) . "
GROUP BY s.id_user");
$db->deconnexion();
if (!empty($res))
{
$this->points = $res['sum_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 `users`");
$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()
{
function cmp($i1, $i2)
{
if ($i1->get_pts() == $i2->get_pts()){
return 0;
}
else{
return ($i1->get_pts() < $i2->get_pts) ? -1 : 1;
}
}
$teams = $this->get_teams();
usort($teams, "cmp");
return $teams;
}
}