plateforme-tfjm2/server_files/controllers/tournoi.php

160 lines
5.5 KiB
PHP
Raw Normal View History

2019-09-06 11:48:50 +00:00
<?php
$tournament_name = htmlspecialchars($_GET["name"]);
$tournament = Tournament::fromName($tournament_name);
2019-09-06 11:48:50 +00:00
if ($tournament === null)
require_once "server_files/404.php";
2019-09-06 11:48:50 +00:00
if (isset($_GET["modifier"]) && $_SESSION["role"] != Role::ADMIN && !$tournament->organize($_SESSION["user_id"]))
require_once "server_files/403.php";
2019-09-06 11:48:50 +00:00
$has_error = false;
$error_message = null;
2019-09-06 11:48:50 +00:00
if (isset($_POST["edit_tournament"])) {
$update_tournament = new UpdateTournament($_POST);
try {
$update_tournament->makeVerifications();
$update_tournament->updateTournament();
} catch (AssertionError $e) {
$has_error = true;
$error_message = $e->getMessage();
}
2019-09-06 11:48:50 +00:00
}
2019-09-07 23:35:05 +00:00
$orgas = $tournament->getOrganizers();
$teams = $tournament->getAllTeams();
2019-09-06 11:48:50 +00:00
class UpdateTournament
{
public $name;
public $organizers;
public $size;
public $place;
public $price;
public $date_start;
public $date_end;
public $date_inscription;
public $time_inscription;
public $date_solutions;
public $time_solutions;
public $date_syntheses;
public $time_syntheses;
public $description;
public $final;
public function __construct($data)
{
global $tournament;
foreach ($data as $key => $value)
$this->$key = ($key == "organizers" ? $value : htmlspecialchars($value));
if ($_SESSION["role"] != Role::ADMIN) {
$this->organizers = [];
/** @var User $organizer */
foreach ($tournament->getOrganizers() as $organizer)
$this->organizers[] = $organizer->getId();
}
}
2019-09-06 11:48:50 +00:00
public function makeVerifications()
{
global $tournament;
2019-09-06 11:48:50 +00:00
ensure($this->name != null && $this->name != "", "Le nom est invalide.");
ensure($this->name == $tournament->getName() || !tournamentExists($this->name), "Un tournoi existe déjà avec ce nom.");
ensure(sizeof($this->organizers) > 0, "Aucun organisateur n'a été choisi.");
2019-09-06 11:48:50 +00:00
$orgas = [];
foreach ($this->organizers as $orga_id) {
$orga = User::fromId($orga_id);
ensure($orga != null, "Un organisateur spécifié n'existe pas.");
ensure($orga->getRole() == Role::ORGANIZER || $orga->getRole() == Role::ADMIN, "Une personne indiquée ne peut pas organiser de tournoi.");
$orgas[] = $orga;
2019-09-06 11:48:50 +00:00
}
$this->organizers = $orgas;
ensure(preg_match("#[0-9]*#", $this->size), "Le nombre d'équipes indiqué n'est pas un nombre valide.");
$this->size = intval($this->size);
ensure($this->size >= 3 && $this->size <= 15, "Un tournoi doit avoir au moins 3 et au plus 15 équipes.");
ensure(preg_match("#[0-9]*#", $this->price), "Le tarif pour les participants n'est pas un entier valide.");
$this->price = intval($this->price);
ensure($this->price >= 0, "Le TFJM² ne va pas payer les élèves pour venir.");
ensure($this->price <= 50, "Soyons raisonnable sur le prix.");
ensure(dateWellFormed($this->date_start), "La date de début n'est pas valide.");
ensure(dateWellFormed($this->date_end), "La date de fin n'est pas valide.");
ensure(dateWellFormed($this->date_inscription . " " . $this->time_inscription), "La date de clôture des inscriptions n'est pas valide.");
ensure(dateWellFormed($this->date_solutions . " " . $this->time_solutions), "La date limite de remise des solutions n'est pas valide.");
ensure(dateWellFormed($this->date_syntheses . " " . $this->time_syntheses), "La date limite de remise des notes de synthèse n'est pas valide.");
2019-09-06 11:48:50 +00:00
}
public function updateTournament()
{
global $URL_BASE, $tournament;
$tournament->setName($this->name);
$tournament->setSize($this->size);
$tournament->setPlace($this->place);
$tournament->setPrice($this->price);
$tournament->setStartDate($this->date_start);
$tournament->setEndDate($this->date_end);
$tournament->setInscriptionDate("$this->date_inscription $this->time_inscription");
$tournament->setSolutionsDate("$this->date_solutions $this->time_solutions");
$tournament->setSynthesesDate("$this->date_syntheses $this->time_syntheses");
2020-01-21 11:43:13 +00:00
$tournament->setDescription($this->description);
foreach ($this->organizers as $organizer) {
if (!$tournament->organize($organizer->getId()))
Mailer::sendAddOrganizerForTournamentMail($organizer, $tournament);
}
2019-09-06 11:48:50 +00:00
$tournament->clearOrganizers();
/** @var User $organizer */
foreach ($this->organizers as $organizer)
$tournament->addOrganizer($organizer);
2019-09-06 11:48:50 +00:00
header("Location: $URL_BASE/tournoi/" . $this->name);
exit();
2019-09-06 11:48:50 +00:00
}
}
2020-04-27 12:25:40 +00:00
if ($_SESSION["role"] == Role::ORGANIZER || $_SESSION["role"] == Role::ADMIN) {
2020-04-12 22:35:22 +00:00
$emails = [];
2020-04-27 12:25:40 +00:00
$emails_validated = [];
foreach ($tournament->getOrganizers() as $organizer) {
$emails[] = $organizer->getEmail();
$emails_validated[] = $organizer->getEmail();
}
2020-04-12 22:35:22 +00:00
foreach ($teams as $team) {
foreach ($team->getEncadrants() as $encadrant_id) {
$encadrant = User::fromId($encadrant_id);
2020-04-27 12:25:40 +00:00
if ($encadrant != null) {
$emails[] = $encadrant->getEmail();
if ($team->getValidationStatus() == ValidationStatus::VALIDATED)
$emails_validated[] = $encadrant->getEmail();
}
2020-04-12 22:35:22 +00:00
}
foreach ($team->getParticipants() as $participant_id) {
$participant = User::fromId($participant_id);
if ($participant != null) {
$emails[] = $participant->getEmail();
2020-04-27 12:25:40 +00:00
if ($team->getValidationStatus() == ValidationStatus::VALIDATED)
$emails_validated[] = $participant->getEmail();
2020-04-12 22:35:22 +00:00
if ($participant->getResponsibleEmail() != null) {
$emails[] = $participant->getResponsibleEmail();
2020-04-27 12:25:40 +00:00
if ($team->getValidationStatus() == ValidationStatus::VALIDATED)
$emails_validated[] = $participant->getResponsibleEmail();
2020-04-12 22:35:22 +00:00
}
}
}
}
}
require_once "server_files/views/tournoi.php";