plateforme-tfjm2/server_files/controllers/equipe.php

174 lines
5.6 KiB
PHP

<?php
if (!isset($_SESSION["user_id"]) || $_SESSION["role"] != Role::ORGANIZER && $_SESSION["role"] != Role::ADMIN)
require_once "server_files/403.php";
$trigram = htmlspecialchars($_GET["trigram"]);
$team = Team::fromTrigram($trigram);
$tournament = Tournament::fromId($team->getTournamentId());
if ($_SESSION["role"] == Role::ORGANIZER && !$tournament->organize($_SESSION["user_id"]))
require_once "server_files/403.php";
if ($team === null)
require_once "server_files/404.php";
if (isset($_POST["team_edit"])) {
$edit_team = new EditTeam($_POST);
try {
$edit_team->makeVerifications();
$edit_team->updateTeam();
}
catch (AssertionError $e) {
$has_error = true;
$error_message = $e->getMessage();
}
}
if (isset($_POST["validate"])) {
$team->setValidationStatus(ValidationStatus::VALIDATED);
Mailer::sendValidateTeam($team, $_POST["message"]);
}
elseif (isset($_POST["unvalidate"])) {
$team->setValidationStatus(ValidationStatus::NOT_READY);
Mailer::sendUnvalidateTeam($team, $_POST["message"]);
}
if (isset($_POST["select"])) {
$team->selectForFinal(true);
# $team->setValidationStatus(ValidationStatus::NOT_READY);
$sols = $tournament->getAllSolutions($team->getId());
/** @var Solution $sol */
foreach ($sols as $sol) {
$old_id = $sol->getFileId();
do
$id = genRandomPhrase(64);
while (file_exists("$LOCAL_PATH/files/$id"));
copy("$LOCAL_PATH/files/$old_id", "$LOCAL_PATH/files/$id");
$req = $DB->prepare("INSERT INTO `solutions`(`file_id`, `team`, `tournament`, `problem`) VALUES (?, ?, ?, ?);");
$req->execute([$id, $team->getId(), $FINAL->getId(), $sol->getProblem()]);
}
}
if (isset($_POST["download_zip"])) {
$final = isset($_POST["final"]);
$tournament_dest = $final ? $FINAL : $tournament;
$file_name = getZipFile(DocumentType::PARENTAL_CONSENT, $tournament_dest->getId(), $team->getId());
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"Documents de l'équipe " . $team->getTrigram() . ".zip\"");
header("Content-Length: " . strval(filesize($file_name)));
readfile($file_name);
exit();
}
if (isset($_POST["select_tournament"])) {
$new_tournament = Tournament::fromId($_POST["select_tournament"]);
ensure($new_tournament != null, "Le tournoi indiqué n'existe pas.");
$team->setTournamentId($new_tournament->getId());
$DB->prepare("UPDATE `documents` SET `tournament` = ? WHERE `team` = ?;")->execute([$tournament->getId(), $team->getId()]);
$DB->prepare("UPDATE `solutions` SET `tournament` = ? WHERE `team` = ?;")->execute([$tournament->getId(), $team->getId()]);
$DB->prepare("UPDATE `syntheses` SET `tournament` = ? WHERE `team` = ?;")->execute([$tournament->getId(), $team->getId()]);
foreach ($team->getParticipants() as $user) {
if ($user != null)
$DB->prepare("UPDATE `payments` SET `tournament` = ? WHERE `user` = ?;")->execute([$tournament->getId(), $user]);
}
foreach ($team->getEncadrants() as $user) {
if ($user != null)
$DB->prepare("UPDATE `payments` SET `tournament` = ? WHERE `user` = ?;")->execute([$tournament->getId(), $user]);
}
$tournament = $new_tournament;
}
if (isset($_POST["delete_team"])) {
foreach ($team->getEncadrants() as $encadrant_id) {
quitTeam($encadrant_id);
}
foreach ($team->getParticipants() as $participant_id) {
quitTeam($participant_id);
}
header("Location: /");
return;
}
class EditTeam
{
public $name;
public $trigram;
public $tournament_id;
private $team;
private $tournament;
public function __construct($data)
{
global $team;
foreach ($data as $key => $value)
$this->$key = htmlspecialchars($value);
$this->trigram = strtoupper($this->trigram);
$this->team = $team;
$this->tournament = Tournament::fromId($this->tournament_id);
}
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($this->tournament_id == $this->team->getTournamentId() || $_SESSION["role"] == Role::ADMIN, "Vous n'avez pas la permission pour changer cette équipe de tournoi.");
}
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;
header("Location: $URL_BASE/equipe/$this->trigram");
}
}
$documents = $tournament->getAllDocuments($team->getId());
$documents_final = null;
if ($team->isSelectedForFinal())
$documents_final = $FINAL->getAllDocuments($team->getId());
$emails = [];
if ($_SESSION["role"] == Role::ORGANIZER || $_SESSION["role"] == Role::ADMIN) {
foreach ($team->getEncadrants() as $encadrant_id) {
$encadrant = User::fromId($encadrant_id);
if ($encadrant != null) {
$emails[] = $encadrant->getEmail();
}
}
foreach ($team->getParticipants() as $participant_id) {
$participant = User::fromId($participant_id);
if ($participant != null) {
$emails[] = $participant->getEmail();
if ($participant->getResponsibleEmail() != null) {
$emails[] = $participant->getResponsibleEmail();
}
}
}
}
require_once "server_files/views/equipe.php";