plateforme-tfjm2/server_files/controllers/mon_equipe.php

142 lines
4.2 KiB
PHP
Raw Normal View History

2019-09-06 11:48:50 +00:00
<?php
if (isset($_POST["leave_team"])) {
quitTeam();
exit();
2019-09-06 11:48:50 +00:00
}
2019-09-07 23:35:05 +00:00
$tournaments = Tournament::getAllTournaments(false, true);
2019-09-06 11:48:50 +00:00
2019-09-09 21:28:03 +00:00
$has_error = false;
$error_message = null;
2019-09-06 11:48:50 +00:00
if (isset($_POST["send_document"])) {
2019-09-09 21:28:03 +00:00
$send_document = new SendDocument();
try {
$send_document->makeVerifications();
$send_document->sendDocument();
}
catch (AssertionError $e) {
$has_error = true;
$error_message = $e->getMessage();
}
}
if (isset($_POST["team_edit"])) {
$my_team = new MyTeam($_POST);
try {
$my_team->makeVerifications();
$my_team->updateTeam();
}
catch (AssertionError $e) {
$has_error = true;
$error_message = $e->getMessage();
}
2019-09-06 11:48:50 +00:00
}
if (isset($_POST["request_validation"])) {
2019-09-09 21:28:03 +00:00
if (!canValidate($team, $tournament))
2019-09-06 11:48:50 +00:00
$error_message = "Votre équipe ne peut pas demander la validation : il manque soit des participants, soit des documents.";
else
$_SESSION["team"]->setValidationStatus(ValidationStatus::WAITING);
2019-09-06 11:48:50 +00:00
}
if (isset($_SESSION["user_id"]) && isset($_SESSION["team"]) && $_SESSION["team"] !== null) {
2019-09-07 23:35:05 +00:00
/**
* @var User $user
* @var Team $team
*/
$user = $_SESSION["user"];
$team = $_SESSION["team"];
$tournament = Tournament::fromId($team->getTournamentId());
2019-09-07 23:35:05 +00:00
$documents = $user->getAllDocuments($team->getTournamentId());
if ($team->isSelectedForFinal())
$documents_final = $user->getAllDocuments($FINAL->getId());
2019-09-06 11:48:50 +00:00
}
else
2019-09-09 21:28:03 +00:00
require_once "server_files/403.php";
2019-09-06 11:48:50 +00:00
2019-09-09 21:28:03 +00:00
class SendDocument
2019-09-06 11:48:50 +00:00
{
2019-09-09 21:28:03 +00:00
private $file;
private $type;
public function __construct()
{
$this->file = $_FILES["document"];
$this->type = strtoupper(htmlspecialchars($_POST["type"]));
}
public function makeVerifications()
{
global $LOCAL_PATH;
ensure($this->file["size"] <= 2e6, "Le fichier doit peser moins que 2 Mo.");
ensure(!$this->file["error"], "Une erreur est survenue.");
ensure(finfo_file(finfo_open(FILEINFO_MIME_TYPE), $this->file["tmp_name"]) == "application/pdf", "Le fichier doit être au format PDF.");
ensure(is_dir("$LOCAL_PATH/files") || mkdir("$LOCAL_PATH/files"), "Un problème est survenue dans l'envoi du fichier. Veuillez contacter l'administrateur du serveur.");
}
public function sendDocument()
{
global $LOCAL_PATH, $DB, $FINAL;
do
$id = genRandomPhrase(64);
while (file_exists("$LOCAL_PATH/files/$id"));
if (!rename($this->file["tmp_name"], "$LOCAL_PATH/files/$id"))
throw new AssertionError("Une erreur est survenue lors de l'envoi du fichier.");
$req = $DB->prepare("INSERT INTO `documents`(`file_id`, `user`, `team`, `tournament`, `type`)
2019-09-06 11:48:50 +00:00
VALUES (?, ?, ?, ?, ?);");
2019-09-09 21:28:03 +00:00
$req->execute([$id, $_SESSION["user_id"], $_SESSION["team"]->getId(), $_SESSION["team"]->isSelectedForFinal() ? $FINAL->getId() : $_SESSION["team"]->getTournamentId(), $this->type]);
}
2019-09-06 11:48:50 +00:00
}
2019-09-09 21:28:03 +00:00
class MyTeam
2019-09-06 11:48:50 +00:00
{
2019-09-09 21:28:03 +00:00
public $name;
public $trigram;
public $tournament_id;
private $team;
private $tournament;
public function __construct($data)
{
foreach ($data as $key => $value)
$this->$key = htmlspecialchars($value);
$this->trigram = strtoupper($this->trigram);
$this->team = $_SESSION["team"];
$this->tournament = Tournament::fromId($this->tournament_id);
}
2019-09-06 11:48:50 +00:00
2019-09-09 21:28:03 +00:00
public function makeVerifications()
{
ensure($this->name != "" && $this->name != null, "Veuillez spécifier un nom d'équipe.");
ensure($this->name == $this->team->getName() || !teamExists($this->name), "Une équipe existe déjà avec ce nom.");
ensure(preg_match("#^[A-Z]{3}$#", $this->trigram), "Le trigramme n'est pas valide.");
ensure($this->trigram == $this->team->getTrigram() || !trigramExists($this->trigram), "Une équipe a déjà choisi ce trigramme.");
ensure($this->tournament != null, "Le tournoi indiqué n'existe pas.");
ensure(date("y-m-d H:i:s") <= $this->tournament->getInscriptionDate(), "Les inscriptions sont terminées.");
ensure($this->team->getValidationStatus() == ValidationStatus::NOT_READY, "Votre équipe est déjà validée ou en cours de validation.");
2019-09-06 11:48:50 +00:00
}
2019-09-09 21:28:03 +00:00
public function updateTeam()
{
global $URL_BASE;
$this->team->setName($this->name);
$this->team->setTrigram($this->trigram);
$this->team->setTournamentId($this->tournament_id);
$_SESSION["tournament"] = $this->tournament;
2019-12-19 12:02:01 +00:00
header("Location: $URL_BASE/mon-equipe");
2019-09-06 11:48:50 +00:00
}
}
require_once "server_files/views/mon_equipe.php";