2019-09-06 11:48:50 +00:00
|
|
|
<?php
|
|
|
|
|
2019-09-06 23:33:05 +00:00
|
|
|
if (!isset($_SESSION["role"]) || $_SESSION["role"] != Role::ADMIN)
|
2019-09-07 11:42:36 +00:00
|
|
|
require_once "server_files/403.php";
|
2019-09-06 11:48:50 +00:00
|
|
|
|
2019-09-07 16:08:40 +00:00
|
|
|
$has_error = false;
|
|
|
|
$error_message = null;
|
|
|
|
|
2020-01-16 21:04:29 +00:00
|
|
|
if (isset($_POST["add_tournament"])) {
|
2019-09-07 16:08:40 +00:00
|
|
|
$tournament = new NewTournament($_POST);
|
|
|
|
try {
|
|
|
|
$tournament->makeVerifications();
|
|
|
|
$tournament->register();
|
|
|
|
}
|
|
|
|
catch (AssertionError $e) {
|
|
|
|
$has_error = true;
|
|
|
|
$error_message = $e->getMessage();
|
|
|
|
}
|
2019-09-06 11:48:50 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 16:08:40 +00:00
|
|
|
class NewTournament {
|
|
|
|
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 $tournament;
|
|
|
|
|
|
|
|
public function __construct($data)
|
|
|
|
{
|
|
|
|
foreach ($data as $key => $value)
|
|
|
|
$this->$key = ($key == "organizers" ? $value : htmlspecialchars($value));
|
2019-09-06 11:48:50 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 16:08:40 +00:00
|
|
|
public function makeVerifications()
|
|
|
|
{
|
|
|
|
global $FINAL;
|
|
|
|
|
|
|
|
ensure($this->name != null && $this->name != "", "Le nom est invalide.");
|
|
|
|
ensure(!tournamentExists($this->name), "Un tournoi existe déjà avec ce nom.");
|
|
|
|
ensure(sizeof($this->organizers) > 0, "Aucun organisateur n'a été choisi.");
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
$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);
|
2019-09-09 23:34:41 +00:00
|
|
|
ensure($this->price >= 0, "Le TFJM² ne va pas payer les élèves pour venir.");
|
|
|
|
ensure($this->price <= 50, "Soyons raisonnable sur le prix.");
|
2019-09-07 16:08:40 +00:00
|
|
|
|
|
|
|
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.");
|
|
|
|
|
|
|
|
$this->final = $this->final ? 1 : 0;
|
|
|
|
|
|
|
|
ensure(!$this->final || $FINAL == NULL, "Une finale nationale est déjà enregistrée.");
|
|
|
|
}
|
2019-09-06 11:48:50 +00:00
|
|
|
|
2019-09-07 16:08:40 +00:00
|
|
|
public function register()
|
|
|
|
{
|
|
|
|
global $DB, $YEAR;
|
2019-09-06 11:48:50 +00:00
|
|
|
|
2019-09-07 16:08:40 +00:00
|
|
|
$req = $DB->prepare("INSERT INTO `tournaments` (`name`, `size`, `place`, `price`, `description`,
|
2019-09-06 11:48:50 +00:00
|
|
|
`date_start`, `date_end`, `date_inscription`, `date_solutions`, `date_syntheses`, `final`, `year`)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
|
2019-09-07 16:08:40 +00:00
|
|
|
$req->execute([$this->name, $this->size, $this->place, $this->price, $this->description, $this->date_start, $this->date_end,
|
|
|
|
"$this->date_inscription $this->time_inscription", "$this->date_solutions $this->time_solutions", "$this->date_syntheses $this->time_syntheses", $this->final ? 1 : 0, $YEAR]);
|
2019-09-06 11:48:50 +00:00
|
|
|
|
2019-09-07 16:08:40 +00:00
|
|
|
$this->tournament = Tournament::fromName($this->name);
|
2019-09-06 11:48:50 +00:00
|
|
|
|
2019-09-08 10:45:48 +00:00
|
|
|
/** @var User $organizer */
|
2019-09-07 16:08:40 +00:00
|
|
|
foreach ($this->organizers as $organizer) {
|
2019-09-09 23:34:41 +00:00
|
|
|
$this->tournament->addOrganizer($organizer);
|
2019-09-08 10:45:48 +00:00
|
|
|
Mailer::sendAddOrganizerForTournamentMail($organizer, $this->tournament);
|
2019-09-07 16:08:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-06 11:48:50 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 11:42:36 +00:00
|
|
|
require_once "server_files/views/ajouter_tournoi.php";
|