2019-08-21 20:56:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
include 'config.php';
|
|
|
|
|
|
|
|
$tournaments_response = $DB->query("SELECT `id`, `name` FROM `tournaments` WHERE `year` = '$YEAR';");
|
|
|
|
|
|
|
|
if (isset($_POST["submitted"])) {
|
2019-08-26 10:16:39 +00:00
|
|
|
$error_message = registerTeam();
|
2019-08-21 20:56:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 10:16:39 +00:00
|
|
|
function registerTeam() {
|
2019-08-21 20:56:46 +00:00
|
|
|
global $DB, $YEAR, $MAIL_ADDRESS, $access_code;
|
2019-08-26 10:16:39 +00:00
|
|
|
|
2019-08-21 20:56:46 +00:00
|
|
|
if ($_SESSION["team_id"] != NULL)
|
|
|
|
return "Vous êtes déjà dans une équipe.";
|
|
|
|
|
|
|
|
$name = htmlspecialchars($_POST["name"]);
|
2019-08-26 10:16:39 +00:00
|
|
|
|
2019-08-21 20:56:46 +00:00
|
|
|
if (!isset($name) || $name == "")
|
|
|
|
return "Vous devez spécifier un nom d'équipe.";
|
2019-08-26 10:16:39 +00:00
|
|
|
|
2019-08-21 20:56:46 +00:00
|
|
|
$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.";
|
|
|
|
|
2019-08-26 10:16:39 +00:00
|
|
|
$trigram = strtoupper(htmlspecialchars($_POST["trigram"]));
|
|
|
|
|
|
|
|
if (!preg_match("#^[A-Z][A-Z][A-Z]$#", $trigram))
|
2019-08-21 20:56:46 +00:00
|
|
|
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"]));
|
|
|
|
|
|
|
|
$result = $DB->query("SELECT `id`, `name` FROM `tournaments` WHERE `id` = '" . $tournament_id . "' AND `year` = '$YEAR';");
|
|
|
|
$data = $result->fetch();
|
|
|
|
if ($data === FALSE)
|
|
|
|
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)];
|
|
|
|
|
2019-08-26 10:16:39 +00:00
|
|
|
$req = $DB->prepare("INSERT INTO `teams` (`name`, `trigram`, `tournament`, `encadrant_1`, `participant_1`, `validation_status`, `access_code`, `year`)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
|
|
|
|
$req->execute([$name, $trigram, $tournament_id, $_SESSION["role"] == "ENCADRANT" ? $_SESSION["user_id"] : NULL,
|
2019-08-21 20:56:46 +00:00
|
|
|
$_SESSION["role"] == "PARTICIPANT" ? $_SESSION["user_id"] : NULL, "NOT_READY", $access_code, $YEAR]);
|
2019-08-26 10:16:39 +00:00
|
|
|
|
2019-08-21 20:56:46 +00:00
|
|
|
$result = $DB->query("SELECT `id` FROM `teams` WHERE `name` = '" . $name . "' AND `year` = '$YEAR';");
|
|
|
|
$data_team = $result->fetch();
|
|
|
|
$DB->prepare("UPDATE `users` SET `team_id` = ? WHERE `id` = " . $_SESSION["user_id"] . ";")->execute([$data_team["id"]]);
|
2019-08-26 10:16:39 +00:00
|
|
|
|
2019-08-21 20:56:46 +00:00
|
|
|
$msg = "Bonjour " . $_SESSION["first_name"] . " " . $_SESSION["surname"] . ",\r\n\r\n";
|
|
|
|
$msg .= "Vous venez de créer l'équipe « $name » ($trigram) pour le TFJM² de " . $data["name"] . " et nous vous en remercions. ";
|
|
|
|
$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";
|
|
|
|
mail($_SESSION["email"], "Nouvelle équipe TFJM² $YEAR", $msg, "From: $MAIL_ADDRESS\r\n");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|
|
|
|
|
|
|
|
<?php include "header.php" ?>
|
|
|
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
if (!isset($_SESSION["role"]) or ($_SESSION["role"] != "PARTICIPANT" && $_SESSION["role"] != "ENCADRANT")) {
|
|
|
|
?>
|
|
|
|
<h2>Vous devez être participant ou encadrant pour pouvoir ajouter une équipe.</h2>
|
|
|
|
<?php } else if ($_SESSION["team_id"] != NULL) { ?>
|
|
|
|
<h2>Vous êtes déjà dans une équipe.</h2>
|
|
|
|
<?php } else if (isset($access_code)) { ?>
|
|
|
|
Votre équipe a bien été créée ! Voici le code d'accès à transmettre aux autres membres de votre équipe : <strong><?php echo $access_code ?></strong>
|
|
|
|
<?php } else { ?>
|
|
|
|
|
|
|
|
<?php if (isset($error_message) && $error_message) echo "<h2>Erreur : " . $error_message . "</h2>"; ?>
|
|
|
|
|
|
|
|
<form method="POST">
|
|
|
|
<input type="hidden" name="submitted" value="true" />
|
2019-08-26 10:16:39 +00:00
|
|
|
<table style="width: 100%;">
|
2019-08-21 20:56:46 +00:00
|
|
|
<tbody>
|
|
|
|
<tr>
|
2019-08-26 10:16:39 +00:00
|
|
|
<td style="width: 30%;">
|
2019-08-21 20:56:46 +00:00
|
|
|
<label for="name">Nom :</label>
|
|
|
|
</td>
|
2019-08-26 10:16:39 +00:00
|
|
|
<td style="width: 70%;">
|
|
|
|
<input style="width: 100%;" type="text" id="name" name="name" />
|
2019-08-21 20:56:46 +00:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
<label for="trigram">Trigramme :</label>
|
|
|
|
</td>
|
|
|
|
<td>
|
2019-08-26 10:16:39 +00:00
|
|
|
<input style="width: 100%;" type="text" id="trigram" name="trigram" />
|
2019-08-21 20:56:46 +00:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
<label for="tournament">Tournoi :</label>
|
|
|
|
</td>
|
|
|
|
<td>
|
2019-08-26 10:16:39 +00:00
|
|
|
<select style="width: 100%;" id="tournament" name="tournament">
|
2019-08-21 20:56:46 +00:00
|
|
|
<?php
|
|
|
|
while (($data = $tournaments_response->fetch()) !== FALSE) {
|
|
|
|
echo "<option value=\"" . $data["id"] . "\">" . $data["name"] . "</option>\n";
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
</select>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
2019-08-26 10:16:39 +00:00
|
|
|
<td colspan="2">
|
|
|
|
<input style="width: 100%;" type="submit" value="Ajouter une équipe" />
|
2019-08-21 20:56:46 +00:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</form>
|
|
|
|
|
|
|
|
<?php include "footer.php" ?>
|
|
|
|
|
|
|
|
<?php } ?>
|