2019-09-05 17:07:41 +00:00
|
|
|
<?php
|
|
|
|
|
2019-09-06 23:33:05 +00:00
|
|
|
if (!isset($_SESSION["role"]) || ($_SESSION["role"] != Role::PARTICIPANT && $_SESSION["role"] != Role::ENCADRANT))
|
2019-09-07 11:42:36 +00:00
|
|
|
require_once "server_files/403.php";
|
2019-09-06 23:33:05 +00:00
|
|
|
|
2019-09-05 17:07:41 +00:00
|
|
|
$tournaments_response = $DB->query("SELECT `id`, `name` FROM `tournaments` WHERE `date_inscription` > CURRENT_DATE AND `year` = '$YEAR';");
|
|
|
|
|
|
|
|
if (isset($_POST["submitted"])) {
|
|
|
|
$error_message = registerTeam();
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerTeam() {
|
|
|
|
global $DB, $YEAR, $MAIL_ADDRESS, $access_code;
|
|
|
|
|
2019-09-06 23:33:05 +00:00
|
|
|
if ($_SESSION["team"] != NULL)
|
2019-09-05 17:07:41 +00:00
|
|
|
return "Vous êtes déjà dans une équipe.";
|
|
|
|
|
|
|
|
$name = htmlspecialchars($_POST["name"]);
|
|
|
|
|
|
|
|
if (!isset($name) || $name == "")
|
|
|
|
return "Vous devez spécifier un nom d'équipe.";
|
|
|
|
|
|
|
|
$result = $DB->query("SELECT `id` FROM `teams` WHERE `name` = '" . $name . "' AND `year` = '$YEAR';");
|
|
|
|
if ($result->fetch())
|
|
|
|
return "Une équipe existe déjà avec ce nom.";
|
|
|
|
|
|
|
|
$trigram = strtoupper(htmlspecialchars($_POST["trigram"]));
|
|
|
|
|
|
|
|
if (!preg_match("#^[A-Z][A-Z][A-Z]$#", $trigram))
|
|
|
|
return "Le trigramme entré n'est pas valide.";
|
|
|
|
|
|
|
|
$result = $DB->query("SELECT `id` FROM `teams` WHERE `trigram` = '" . $trigram . "' AND `year` = '$YEAR';");
|
|
|
|
if ($result->fetch())
|
|
|
|
return "Une équipe a déjà choisi ce trigramme.";
|
|
|
|
|
|
|
|
$tournament_id = intval(htmlspecialchars($_POST["tournament"]));
|
2019-09-06 23:33:05 +00:00
|
|
|
$tournament = Tournament::fromId($tournament_id);
|
|
|
|
if ($tournament === null)
|
2019-09-05 17:07:41 +00:00
|
|
|
return "Le tournoi spécifié n'existe pas.";
|
|
|
|
|
|
|
|
$alphabet = "0123456789abcdefghijkmnopqrstuvwxyz0123456789";
|
|
|
|
$access_code = "";
|
|
|
|
for ($i = 0; $i < 6; ++$i)
|
|
|
|
$access_code .= $alphabet[rand(0, strlen($alphabet) - 1)];
|
|
|
|
|
|
|
|
$req = $DB->prepare("INSERT INTO `teams` (`name`, `trigram`, `tournament`, `encadrant_1`, `participant_1`, `validation_status`, `access_code`, `year`)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
|
2019-09-06 23:33:05 +00:00
|
|
|
$req->execute([$name, $trigram, $tournament_id, $_SESSION["role"] == Role::ENCADRANT ? $_SESSION["user_id"] : NULL,
|
|
|
|
$_SESSION["role"] == Role::PARTICIPANT ? $_SESSION["user_id"] : NULL, ValidationStatus::NOT_READY, $access_code, $YEAR]);
|
2019-09-05 17:07:41 +00:00
|
|
|
|
2019-09-06 23:33:05 +00:00
|
|
|
$_SESSION["team"] = Team::fromTrigram($trigram);
|
|
|
|
$_SESSION["user"]->setTeamId($_SESSION["team"]->getId());
|
2019-09-05 17:07:41 +00:00
|
|
|
|
2019-09-06 23:33:05 +00:00
|
|
|
$msg = "Bonjour " . $_SESSION["user"]->getFirstName() . " " . $_SESSION["user"]->getSurname() . ",\r\n\r\n";
|
|
|
|
$msg .= "Vous venez de créer l'équipe « $name » ($trigram) pour le TFJM² de " . $tournament->getName() . " et nous vous en remercions. ";
|
2019-09-05 17:07:41 +00:00
|
|
|
$msg .= "Afin de permettre aux autres membres de votre équipe de vous rejoindre, veuillez leur transmettre le code d'accès : " . $access_code . "\r\n\r\n";
|
|
|
|
$msg .= "Cordialement,\r\n\r\nL'organisation du TFJM² $YEAR";
|
2019-09-06 23:33:05 +00:00
|
|
|
mail($_SESSION["user"]->getEmail(), "Nouvelle équipe TFJM² $YEAR", $msg, "From: $MAIL_ADDRESS\r\n");
|
2019-09-05 17:07:41 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-07 11:42:36 +00:00
|
|
|
require_once "server_files/views/ajouter_equipe.php";
|