1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-12-27 10:22:24 +00:00
plateforme-tfjm2/server_files/controllers/tournoi.php

125 lines
4.8 KiB
PHP
Raw Normal View History

2019-09-06 11:48:50 +00:00
<?php
$tournament_name = htmlspecialchars($_GET["name"]);
2019-09-06 11:48:50 +00:00
$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
$orgas_req = $DB->query("SELECT `users`.`id` AS `id` FROM `users` JOIN `organizers` ON `users`.`id` = `organizer` WHERE `tournament` = " . $tournament->getId() . ";");
2019-09-06 11:48:50 +00:00
$orgas = [];
$orgas_id = [];
while (($orga_data = $orgas_req->fetch()) !== false) {
$orgas[] = User::fromId($orga_data["id"]);
2019-09-06 11:48:50 +00:00
$orgas_id[] = $orga_data["id"];
}
if (isset($_GET["modifier"]) && $_SESSION["role"] != Role::ADMIN && !in_array($_SESSION["user_id"], $orgas_id))
require_once "server_files/403.php";
2019-09-06 11:48:50 +00:00
if (isset($_POST["edit_tournament"])) {
$error_message = updateTournament();
}
if ($tournament->isFinal())
2019-09-06 11:48:50 +00:00
$teams_response = $DB->query("SELECT `id`, `name`, `trigram`, `inscription_date`, `validation_status` FROM `teams` WHERE `final_selection` AND `year` = $YEAR;");
else
$teams_response = $DB->query("SELECT `id`, `name`, `trigram`, `inscription_date`, `validation_status` FROM `teams` WHERE `tournament` = " . $tournament->getId() . " AND `year` = $YEAR;");
2019-09-06 11:48:50 +00:00
$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;
2019-09-06 11:48:50 +00:00
$name = htmlspecialchars($_POST["name"]);
$result = $DB->query("SELECT `id` FROM `tournaments` WHERE `name` = '" . $name . "' AND `id` != " . $tournament->getId() . " AND `year` = '$YEAR';");
2019-09-06 11:48:50 +00:00
if ($result->fetch())
return "Un tournoi existe déjà avec ce nom.";
if (!isset($_POST["organizer"]) || sizeof($_POST["organizer"]) == 0)
return "Aucun organisateur n'a été choisi.";
if ($_SESSION["role"] == Role::ADMIN) {
2019-09-06 11:48:50 +00:00
$organizers = $_POST["organizer"];
$orga_mails = [];
foreach ($organizers as $orga_id) {
$orga = User::fromId($orga_id);
if ($orga === null)
2019-09-06 11:48:50 +00:00
return "L'organisateur spécifié n'existe pas.";
if ($orga->getRole() != Role::ORGANIZER && $orga->getRole() != Role::ADMIN)
2019-09-06 11:48:50 +00:00
return "L'organisateur indiqué ne peut pas organiser de tournoi.";
$orga_mails[] = $orga->getEmail();
2019-09-06 11:48:50 +00:00
}
}
try {
$size = intval(htmlspecialchars($_POST["size"]));
}
catch (Exception $ex) {
return "Le nombre d'équipes indiqué n'est pas un entier valide.";
}
if ($size < 3 || $size > 12)
return "Un tournoi doit comporter entre 3 et 12 équipes.";
$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() . ";");
2019-09-06 11:48:50 +00:00
$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() . ";");
2019-09-06 11:48:50 +00:00
foreach ($organizers as $orga) {
$req = $DB->prepare("INSERT INTO `organizers`(`organizer`, `tournament`) VALUES(?, ?);");
$req->execute([$orga->getId(), $tournament->getId()]);
2019-09-06 11:48:50 +00:00
}
}
header("Location: $URL_BASE/tournoi/" . $name);
exit();
}
require_once "server_files/views/tournoi.php";