Amélioration de la classe d'ajout d'équipe
This commit is contained in:
parent
e5e197dd38
commit
70cece8694
|
@ -5,59 +5,62 @@ if (!isset($_SESSION["role"]) || ($_SESSION["role"] != Role::PARTICIPANT && $_SE
|
|||
|
||||
$tournaments_response = $DB->query("SELECT `id`, `name` FROM `tournaments` WHERE `date_inscription` > CURRENT_DATE AND `year` = '$YEAR';");
|
||||
|
||||
$has_error = false;
|
||||
$error_message = null;
|
||||
|
||||
if (isset($_POST["submitted"])) {
|
||||
$error_message = registerTeam();
|
||||
$new_team = new NewTeam($_POST);
|
||||
try {
|
||||
$new_team->makeVerifications();
|
||||
$new_team->register();
|
||||
}
|
||||
catch (AssertionError $e) {
|
||||
$has_error = true;
|
||||
$error_message = $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
function registerTeam() {
|
||||
global $DB, $YEAR, $MAIL_ADDRESS, $access_code;
|
||||
class NewTeam {
|
||||
public $name;
|
||||
public $trigram;
|
||||
public $tournament_id;
|
||||
public $tournament;
|
||||
public $access_code;
|
||||
|
||||
if ($_SESSION["team"] != NULL)
|
||||
return "Vous êtes déjà dans une équipe.";
|
||||
public function __construct($data)
|
||||
{
|
||||
foreach ($data as $key => $value)
|
||||
$this->$key = htmlspecialchars($value);
|
||||
}
|
||||
|
||||
$name = htmlspecialchars($_POST["name"]);
|
||||
public function makeVerifications() {
|
||||
ensure($_SESSION["team"] == null, "Vous êtes déjà dans une équipe.");
|
||||
ensure($this->name != null && $this->name != "", "Vous devez spécifier un nom d'équipe.");
|
||||
ensure(preg_match("#^[A-Z]{3}$#", $this->trigram), "Le trigramme entré n'est pas valide.");
|
||||
ensure(!teamExists($this->name), "Une équipe existe déjà avec ce nom.");
|
||||
ensure(!trigramExists($this->trigram), "Une équipe a déjà choisi ce trigramme.");
|
||||
$this->tournament = Tournament::fromId($this->tournament_id);
|
||||
ensure($this->tournament != null, "Le tournoi spécifié n'existe pas.");
|
||||
}
|
||||
|
||||
if (!isset($name) || $name == "")
|
||||
return "Vous devez spécifier un nom d'équipe.";
|
||||
public function register() {
|
||||
global $DB, $YEAR;
|
||||
|
||||
$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.";
|
||||
$alphabet = "0123456789abcdefghijkmnopqrstuvwxyz0123456789";
|
||||
$this->access_code = "";
|
||||
for ($i = 0; $i < 6; ++$i)
|
||||
$this->access_code .= $alphabet[rand(0, strlen($alphabet) - 1)];
|
||||
|
||||
$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"]));
|
||||
$tournament = Tournament::fromId($tournament_id);
|
||||
if ($tournament === null)
|
||||
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`)
|
||||
$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"] == Role::ENCADRANT ? $_SESSION["user_id"] : NULL,
|
||||
$_SESSION["role"] == Role::PARTICIPANT ? $_SESSION["user_id"] : NULL, ValidationStatus::NOT_READY, $access_code, $YEAR]);
|
||||
$req->execute([$this->name, $this->trigram, $this->tournament_id, $_SESSION["role"] == Role::ENCADRANT ? $_SESSION["user_id"] : NULL,
|
||||
$_SESSION["role"] == Role::PARTICIPANT ? $_SESSION["user_id"] : NULL, ValidationStatus::NOT_READY, $this->access_code, $YEAR]);
|
||||
|
||||
$_SESSION["team"] = Team::fromTrigram($trigram);
|
||||
$_SESSION["user"]->setTeamId($_SESSION["team"]->getId());
|
||||
$_SESSION["team"] = Team::fromTrigram($this->trigram);
|
||||
$_SESSION["user"]->setTeamId($_SESSION["team"]->getId());
|
||||
|
||||
$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. ";
|
||||
$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["user"]->getEmail(), "Nouvelle équipe TFJM² $YEAR", $msg, "From: $MAIL_ADDRESS\r\n");
|
||||
|
||||
return false;
|
||||
sendAddTeam($_SESSION["user"], $_SESSION["team"], $this->tournament);
|
||||
}
|
||||
}
|
||||
|
||||
require_once "server_files/views/ajouter_equipe.php";
|
||||
|
|
|
@ -92,7 +92,23 @@ function quitTeam() {
|
|||
function userExists($email) {
|
||||
global $DB, $YEAR;
|
||||
|
||||
$req = $DB->prepare("SELECT `email` FROM `users` WHERE `email` = ? AND `year` = '$YEAR';");
|
||||
$req = $DB->prepare("SELECT `id` FROM `users` WHERE `email` = ? AND `year` = '$YEAR';");
|
||||
$req->execute([$email]);
|
||||
return !$req->fetch();
|
||||
return $req->fetch();
|
||||
}
|
||||
|
||||
function teamExists($name) {
|
||||
global $DB, $YEAR;
|
||||
|
||||
$req = $DB->prepare("SELECT `id` FROM `teams` WHERE `name` = ? AND `year` = '$YEAR';");
|
||||
$req->execute([$name]);
|
||||
return $req->fetch();
|
||||
}
|
||||
|
||||
function trigramExists($trigram) {
|
||||
global $DB, $YEAR;
|
||||
|
||||
$req = $DB->prepare("SELECT `id` FROM `teams` WHERE `trigram` = ? AND `year` = '$YEAR';");
|
||||
$req->execute([$trigram]);
|
||||
return $req->fetch();
|
||||
}
|
|
@ -28,3 +28,21 @@ function sendRegisterMail($new_user)
|
|||
|
||||
sendMail($new_user->email, "Inscription au TFJM² $YEAR", $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $user User
|
||||
* @param $team Team
|
||||
* @param $tournament Tournament
|
||||
*/
|
||||
function sendAddTeam($user, $team, $tournament)
|
||||
{
|
||||
global $LOCAL_PATH, $YEAR;
|
||||
|
||||
$content = file_get_contents("$LOCAL_PATH/server_files/services/mail_templates/add_team.html");
|
||||
$content = preg_replace("#{FIRST_NAME}#", $user->getFirstName(), $content);
|
||||
$content = preg_replace("#{SURNAME}#", $user->getSurname(), $content);
|
||||
$content = preg_replace("#{TEAM_NAME}#", $team->getName(), $content);
|
||||
$content = preg_replace("#{TRIGRAM}#", $team->getTrigram(), $content);
|
||||
$content = preg_replace("#{TOURNAMENT_NAME}#", $tournament->getName(), $content);
|
||||
$content = preg_replace("#{ACCESS_CODE}#", $team->getAccessCode(), $content);
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Nouvelle équipe TFJM² {YEAR}</title>
|
||||
</head>
|
||||
<body>
|
||||
Bonjour {FIRST_NAME} {SURNAME},<br />
|
||||
<br />
|
||||
Vous venez de créer l'équipe « {TEAM_NAME} » ({TRIGRAM}) pour le TFJM<sup>2</sup> de {TOURNAMENT_NAME} et nous vous en remercions.<br />
|
||||
"Afin de permettre aux autres membres de votre équipe de vous rejoindre, veuillez leur transmettre le code d'accès : {ACCESS_CODE}<br />
|
||||
<br />
|
||||
Le comité national d'organisation du TFJM<sup>2</sup>
|
||||
</body>
|
||||
</html>
|
|
@ -1,10 +1,10 @@
|
|||
<?php
|
||||
require_once "header.php";
|
||||
|
||||
if ($_SESSION["team"] != NULL) { ?>
|
||||
if (isset($new_team) && !$has_error) { ?>
|
||||
Votre équipe a bien été créée ! Voici le code d'accès à transmettre aux autres membres de votre équipe : <strong><?= $new_team->access_code ?></strong>
|
||||
<?php } elseif ($_SESSION["team"] != 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><?= $access_code ?></strong>
|
||||
<?php } else { ?>
|
||||
|
||||
<?php if (isset($error_message) && $error_message) echo "<h2>Erreur : " . $error_message . "</h2>"; ?>
|
||||
|
@ -31,10 +31,10 @@ if ($_SESSION["team"] != NULL) { ?>
|
|||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<label for="tournament">Tournoi :</label>
|
||||
<label for="tournament_id">Tournoi :</label>
|
||||
</td>
|
||||
<td>
|
||||
<select style="width: 100%;" id="tournament" name="tournament">
|
||||
<select style="width: 100%;" id="tournament_id" name="tournament_id">
|
||||
<?php
|
||||
while (($data = $tournaments_response->fetch()) !== FALSE) {
|
||||
echo "<option value=\"" . $data["id"] . "\">" . $data["name"] . "</option>\n";
|
||||
|
|
Loading…
Reference in New Issue