plateforme-tfjm2/server_files/classes/Team.php

210 lines
4.8 KiB
PHP

<?php
class Team
{
private $id;
private $name;
private $trigram;
private $tournament;
private $encadrants;
private $participants;
private $inscription_date;
private $validation_status;
private $final_selection;
private $access_code;
private $year;
private function __construct() {}
public static function fromId($id)
{
global $DB;
$req = $DB->prepare("SELECT * FROM `teams` WHERE `id` = ?;");
$req->execute([htmlspecialchars($id)]);
$data = $req->fetch();
if ($data === false)
return null;
$team = new Team();
$team->fill($data);
return $team;
}
public static function fromTrigram($trigram)
{
global $DB, $YEAR;
$req = $DB->prepare("SELECT * FROM `teams` WHERE `trigram` = ? AND `year` = $YEAR;");
$req->execute([htmlspecialchars($trigram)]);
$data = $req->fetch();
if ($data === false)
return null;
$team = new Team();
$team->fill($data);
return $team;
}
public static function fromAccessCode($access_code)
{
global $DB, $YEAR;
$req = $DB->prepare("SELECT * FROM `teams` WHERE `access_code` = ? AND `year` = $YEAR;");
$req->execute([htmlspecialchars($access_code)]);
$data = $req->fetch();
if ($data === false)
return null;
$team = new Team();
$team->fill($data);
return $team;
}
private function fill($data)
{
$this->id = $data["id"];
$this->name = $data["name"];
$this->trigram = $data["trigram"];
$this->tournament = $data["tournament"];
$this->encadrants = [$data["encadrant_1"], $data["encadrant_2"], $data["encadrant_3"]];
$this->participants = [$data["participant_1"], $data["participant_2"], $data["participant_3"], $data["participant_4"], $data["participant_5"], $data["participant_6"]];
$this->inscription_date = $data["inscription_date"];
$this->validation_status = ValidationStatus::fromName($data["validation_status"]);
$this->final_selection = $data["final_selection"] == true;
$this->access_code = $data["access_code"];
$this->year = $data["year"];
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
global $DB;
$this->name = $name;
$DB->prepare("UPDATE `teams` SET `name` = ? WHERE `id` = ?;")->execute([$name, $this->id]);
}
public function getTrigram()
{
return $this->trigram;
}
public function setTrigram($trigram)
{
global $DB;
$this->trigram = $trigram;
$DB->prepare("UPDATE `teams` SET `trigram` = ? WHERE `id` = ?;")->execute([$trigram, $this->id]);
}
public function getTournamentId()
{
return $this->tournament;
}
/**
* @return Tournament
*/
public function getEffectiveTournament()
{
return $this->isSelectedForFinal() ? Tournament::getFinalTournament() : Tournament::fromId($this->getTournamentId());
}
public function setTournamentId($tournament)
{
global $DB;
$this->tournament = $tournament;
$DB->prepare("UPDATE `teams` SET `tournament` = ? WHERE `id` = ?;")->execute([$tournament, $this->id]);
}
public function getEncadrants()
{
return $this->encadrants;
}
public function setEncadrant($i, $encadrant)
{
global $DB;
$this->encadrants[$i - 1] = $encadrant;
/** @noinspection SqlResolve */
$DB->prepare("UPDATE `teams` SET `encadrant_$i` = ? WHERE `id` = ?;")->execute([$encadrant, $this->id]);
}
public function getParticipants()
{
return $this->participants;
}
public function setParticipant($i, $participant)
{
global $DB;
$this->participants[$i - 1] = $participant;
/** @noinspection SqlResolve */
$DB->prepare("UPDATE `teams` SET `participant_$i` = ? WHERE `id` = ?;")->execute([$participant, $this->id]);
}
public function getInscriptionDate()
{
return $this->inscription_date;
}
public function getValidationStatus()
{
return $this->validation_status;
}
public function setValidationStatus($status)
{
global $DB;
$this->validation_status = $status;
/** @noinspection PhpUndefinedMethodInspection */
$DB->prepare("UPDATE `teams` SET `validation_status` = ? WHERE `id` = ?;")->execute([ValidationStatus::getName($status), $this->id]);
}
public function isSelectedForFinal()
{
return $this->final_selection;
}
public function selectForFinal($selected)
{
global $DB;
$this->final_selection = $selected;
$DB->prepare("UPDATE `teams` SET `final_selection` = ? WHERE `id` = ?;")->execute([$selected, $this->id]);
}
public function getAccessCode()
{
return $this->access_code;
}
public function getYear()
{
return $this->year;
}
public static function getAllTeams($only_not_validated = false)
{
global $DB, $YEAR;
$req = $DB->query("SELECT * FROM `teams` WHERE " . ($only_not_validated ? "`validation_status` = 0 AND " : "") . "`year` = $YEAR;");
$teams = [];
while (($data = $req->fetch()) != false) {
$team = new Team();
$team->fill($data);
$teams[] = $team;
}
return $teams;
}
}