diff --git a/server_files/classes/Tournament.php b/server_files/classes/Tournament.php index 102fb10..e404e7d 100644 --- a/server_files/classes/Tournament.php +++ b/server_files/classes/Tournament.php @@ -279,6 +279,26 @@ class Tournament return false; } + public function addOrganizer(User $user) + { + global $DB; + + $this->organizers[] = $user; + + $req = $DB->prepare("INSERT INTO `organizers`(`organizer`, `tournament`) VALUES(?, ?);"); + $req->execute([$user->getId(), $this->id]); + } + + public function clearOrganizers() + { + global $DB; + + $this->organizers = []; + + $req = $DB->prepare("DELETE FROM `organizers` WHERE `tournament` = ?;"); + $req->execute([$this->id]); + } + public function getYear() { return $this->year; diff --git a/server_files/controllers/ajouter_tournoi.php b/server_files/controllers/ajouter_tournoi.php index dcc437d..b855ea2 100644 --- a/server_files/controllers/ajouter_tournoi.php +++ b/server_files/controllers/ajouter_tournoi.php @@ -67,8 +67,8 @@ class NewTournament { 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->size >= 0, "Le TFJM² ne va pas payer les élèves pour venir."); - ensure($this->size <= 50, "Soyons raisonnable sur le prix."); + 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."); @@ -95,8 +95,7 @@ class NewTournament { /** @var User $organizer */ foreach ($this->organizers as $organizer) { - $req = $DB->prepare("INSERT INTO `organizers`(`organizer`, `tournament`) VALUES(?, ?);"); - $req->execute([$organizer->getId(), $this->tournament->getId()]); + $this->tournament->addOrganizer($organizer); Mailer::sendAddOrganizerForTournamentMail($organizer, $this->tournament); } } diff --git a/server_files/controllers/tournoi.php b/server_files/controllers/tournoi.php index 5d516eb..ce716cc 100644 --- a/server_files/controllers/tournoi.php +++ b/server_files/controllers/tournoi.php @@ -9,103 +9,117 @@ if ($tournament === null) if (isset($_GET["modifier"]) && $_SESSION["role"] != Role::ADMIN && !$tournament->organize($_SESSION["user_id"])) require_once "server_files/403.php"; +$has_error = false; +$error_message = null; + if (isset($_POST["edit_tournament"])) { - $error_message = updateTournament(); + $update_tournament = new UpdateTournament($_POST); + try { + $update_tournament->makeVerifications(); + $update_tournament->updateTournament(); + } catch (AssertionError $e) { + $has_error = true; + $error_message = $e->getMessage(); + } } + $orgas = $tournament->getOrganizers(); $teams = $tournament->getAllTeams(); $orgas_response = $DB->query("SELECT `id`, `surname`, `first_name` FROM `users` WHERE (`role` = 'ORGANIZER' OR `role` = 'ADMIN') AND `year` = '$YEAR';"); -function updateTournament() { - global $DB, $URL_BASE, $YEAR, $tournament, $orgas; +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; - $name = htmlspecialchars($_POST["name"]); + public function __construct($data) + { + global $tournament; - $result = $DB->query("SELECT `id` FROM `tournaments` WHERE `name` = '" . $name . "' AND `id` != " . $tournament->getId() . " AND `year` = '$YEAR';"); - if ($result->fetch()) - return "Un tournoi existe déjà avec ce nom."; + foreach ($data as $key => $value) + $this->$key = ($key == "organizers" ? $value : htmlspecialchars($value)); - if (!isset($_POST["organizer"]) || sizeof($_POST["organizer"]) == 0) - return "Aucun organisateur n'a été choisi."; + if ($_SESSION["role"] != Role::ADMIN) { + $this->organizers = []; + /** @var User $organizer */ + foreach ($tournament->getOrganizers() as $organizer) + $this->organizers[] = $organizer->getId(); + } + } + + public function makeVerifications() + { + global $tournament; + + 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."); - if ($_SESSION["role"] == Role::ADMIN) { - $organizers = $_POST["organizer"]; $orgas = []; - - foreach ($organizers as $orga_id) { + foreach ($this->organizers as $orga_id) { $orga = User::fromId($orga_id); - if ($orga === null) - return "L'organisateur spécifié n'existe pas."; - if ($orga->getRole() != Role::ORGANIZER && $orga->getRole() != Role::ADMIN) - return "L'organisateur indiqué ne peut pas organiser de tournoi."; + 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; } + $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."); } - try { - $size = intval(htmlspecialchars($_POST["size"])); - } - catch (Exception $ex) { - return "Le nombre d'équipes indiqué n'est pas un entier valide."; - } + public function updateTournament() + { + global $URL_BASE, $tournament; - if ($size < 3 || $size > 12) - return "Un tournoi doit comporter entre 3 et 12 équipes."; + $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"); - $place = htmlspecialchars($_POST["place"]); - - try { - $price = intval(htmlspecialchars($_POST["price"])); - } - catch (Throwable $t) { - return "Le tarif pour les participants n'est pas un nombre valide."; - } - - if ($price < 0) - return "Le TFJM² ne va pas payer les élèves pour venir."; - - if ($price > 50) - return "Soyons raisonnable sur le prix."; - - $date_start = htmlspecialchars($_POST["date_start"]); - $date_start_parsed = date_parse_from_format("yyyy-mm-dd", $date_start); - - $date_end = htmlspecialchars($_POST["date_end"]); - $date_end_parsed = date_parse_from_format("yyyy-mm-dd", $date_end); - - $date_inscription = htmlspecialchars($_POST["date_inscription"]); - $time_inscription = htmlspecialchars($_POST["time_inscription"]); - $date_inscription_parsed = date_parse_from_format("yyyy-mm-dd", $date_inscription . ' ' . $time_inscription); - - $date_solutions = htmlspecialchars($_POST["date_solutions"]); - $time_solutions = htmlspecialchars($_POST["time_solutions"]); - $date_solutions_parsed = date_parse_from_format("yyyy-mm-dd", $date_solutions . ' ' . $time_solutions); - - $date_syntheses = htmlspecialchars($_POST["date_syntheses"]); - $time_syntheses = htmlspecialchars($_POST["time_syntheses"]); - $date_syntheses_parsed = date_parse_from_format("yyyy-mm-dd", $date_syntheses . ' ' . $time_syntheses); - - if (!$date_start_parsed || !$date_end_parsed || !$date_inscription_parsed || !$date_solutions_parsed || !$date_syntheses_parsed) - return "Une date est mal formée."; - - $description = htmlspecialchars($_POST["description"]); - - $req = $DB->prepare("UPDATE `tournaments` SET `name` = ?, `size` = ?, `place` = ?, `price` = ?, `description` = ?, - `date_start` = ?, `date_end` = ?, `date_inscription` = ?, `date_solutions` = ?, `date_syntheses` = ? - WHERE `id` = " . $tournament->getId() . ";"); - $req->execute([$name, $size, $place, $price, $description, $date_start, $date_end, - "$date_inscription $time_inscription", "$date_solutions $time_solutions", "$date_syntheses $time_syntheses"]); - - if ($_SESSION["role"] == Role::ADMIN) { - $DB->exec("DELETE FROM `organizers` WHERE `tournament` = " . $tournament->getId() . ";"); - foreach ($orgas as $orga) { - $req = $DB->prepare("INSERT INTO `organizers`(`organizer`, `tournament`) VALUES(?, ?);"); - $req->execute([$orga->getId(), $tournament->getId()]); + foreach ($this->organizers as $organizer) { + if (!$tournament->organize($organizer->getId())) + Mailer::sendAddOrganizerForTournamentMail($organizer, $tournament); } - } - header("Location: $URL_BASE/tournoi/" . $name); - exit(); + $tournament->clearOrganizers(); + /** @var User $organizer */ + foreach ($this->organizers as $organizer) + $tournament->addOrganizer($organizer); + + header("Location: $URL_BASE/tournoi/" . $this->name); + exit(); + } } require_once "server_files/views/tournoi.php"; diff --git a/server_files/views/tournoi.php b/server_files/views/tournoi.php index 73257cd..e0ebc5c 100644 --- a/server_files/views/tournoi.php +++ b/server_files/views/tournoi.php @@ -1,5 +1,10 @@ +Erreur : $error_message"; +?> +

Tournoi de getName() ?>

Organisateur= 2 ? 's' : '' ?> : @@ -117,10 +122,10 @@ else { - + - fetch()) !== FALSE) { echo "