Amélioration du code de la page pour rejoindre une équipe

This commit is contained in:
galaxyoyo 2019-09-10 00:49:55 +02:00
parent fffdaabe7c
commit 98fb682c66
3 changed files with 52 additions and 36 deletions

View File

@ -3,48 +3,64 @@
if (isset($_SESSION["team"]) || !isset($_SESSION["user"]) || ($_SESSION["role"] != Role::PARTICIPANT && $_SESSION["role"] != Role::ENCADRANT)) if (isset($_SESSION["team"]) || !isset($_SESSION["user"]) || ($_SESSION["role"] != Role::PARTICIPANT && $_SESSION["role"] != Role::ENCADRANT))
require_once "server_files/403.php"; require_once "server_files/403.php";
$has_error = false;
$error_message = null;
if (isset($_POST["submitted"])) { if (isset($_POST["submitted"])) {
$error_message = joinTeam(); $join_team = new JoinTeam($_POST);
try {
$join_team->makeVerifications();
$join_team->joinTeam();
} catch (AssertionError $e) {
$has_error = true;
$error_message = $e->getMessage();
}
} }
function joinTeam() { class JoinTeam
global $access_code; {
private $access_code;
private $team;
private $min_null_index;
$access_code = htmlspecialchars($_POST["access_code"]); public function __construct($data)
{
$this->access_code = strtolower(htmlspecialchars($data["access_code"]));
$this->team = Team::fromAccessCode($this->access_code);
}
if (!isset($access_code) || strlen($access_code) != 6) public function makeVerifications()
return "Le code d'accès doit comporter 6 caractères."; {
ensure(preg_match("#[a-z0-9]{6}#", $this->access_code), "Le code d'accès doit comporter 6 caractères alphanumériques.");
/** @var User $user */ ensure($this->team != null, "Ce code d'accès est invalide.");
$user = $_SESSION["user"]; ensure($this->team->getValidationStatus() == ValidationStatus::NOT_READY, "Cette équipe est déjà validée ou en cours de validation, vous ne pouvez pas la rejoindre.");
$team = Team::fromAccessCode($access_code);
if ($team === null)
return "Ce code d'accès est invalide.";
if ($team->getValidationStatus() != ValidationStatus::NOT_READY)
return "Cette équipe est déjà en cours de validation ou validée, vous ne pouvez pas la rejoindre.";
for ($i = 1; $i <= $_SESSION["role"] == Role::PARTICIPANT ? 6 : 2; ++$i) { for ($i = 1; $i <= $_SESSION["role"] == Role::PARTICIPANT ? 6 : 2; ++$i) {
if (($_SESSION["role"] == Role::PARTICIPANT ? $team->getParticipants()[$i - 1] : $team->getEncadrants()[$i - 1]) == NULL) if (($_SESSION["role"] == Role::PARTICIPANT ? $this->team->getParticipants()[$i - 1] : $this->team->getEncadrants()[$i - 1]) == NULL)
break; break;
} }
if ($_SESSION["role"] == Role::PARTICIPANT && $i == 7 || $_SESSION["role"] == Role::ENCADRANT && $i == 3) $this->min_null_index = $i;
return "Il n'y a plus de place pour vous dans l'équipe.";
$user->setTeamId($team->getId()); ensure($_SESSION["role"] == Role::PARTICIPANT && $this->min_null_index <= 6 || $_SESSION["role"] == Role::ENCADRANT && $this->min_null_index <= 2, "Il n'y a plus de place pour vous dans l'équipe.");
}
public function joinTeam()
{
$user = $_SESSION["user"];
$user->setTeamId($this->team->getId());
if ($_SESSION["role"] == Role::ENCADRANT) if ($_SESSION["role"] == Role::ENCADRANT)
$team->setEncadrant($i, $user->getId()); $this->team->setEncadrant($this->min_null_index, $user->getId());
else else
$team->setParticipant($i, $user->getId()); $this->team->setParticipant($this->min_null_index, $user->getId());
$_SESSION["team"] = $team; $_SESSION["team"] = $this->team;
$tournament = $_SESSION["tournament"] = Tournament::fromId($team->getTournamentId()); $tournament = $_SESSION["tournament"] = Tournament::fromId($this->team->getTournamentId());
Mailer::sendJoinTeamMail($user, $team, $tournament); Mailer::sendJoinTeamMail($user, $this->team, $tournament);
}
return false;
} }
require_once "server_files/views/rejoindre_equipe.php"; require_once "server_files/views/rejoindre_equipe.php";

View File

@ -7,7 +7,7 @@
Tournoi : <a href="<?= $URL_BASE . "/tournoi/" . $tournament->getName() ?>"><?= $tournament->getName() ?></a><br/> Tournoi : <a href="<?= $URL_BASE . "/tournoi/" . $tournament->getName() ?>"><?= $tournament->getName() ?></a><br/>
<?php <?php
for ($i = 1; $i <= 2; ++$i) { for ($i = 1; $i <= 2; ++$i) {
if ($team->getEncadrants()[$i] == NULL) if ($team->getEncadrants()[$i - 1] == NULL)
continue; continue;
$encadrant = User::fromId($team->getEncadrants()[$i - 1]); $encadrant = User::fromId($team->getEncadrants()[$i - 1]);
$id = $encadrant->getId(); $id = $encadrant->getId();

View File

@ -1,11 +1,11 @@
<?php <?php
require_once "header.php" ; require_once "header.php" ;
if (isset($error_message) && $error_message === false) { ?> if (isset($join_team) && !$has_error) { ?>
Vous avez bien rejoint l'équipe <?= $_SESSION["team"]->getName() ?> ! Vous avez bien rejoint l'équipe <?= $_SESSION["team"]->getName() ?> !
<?php } else { ?> <?php } else { ?>
<?php if (isset($error_message) && $error_message) echo "<h2>Erreur : " . $error_message . "</h2>"; ?> <?php if ($has_error) echo "<h2>Erreur : " . $error_message . "</h2>"; ?>
<form method="POST"> <form method="POST">
<input type="hidden" name="submitted" value="true" /> <input type="hidden" name="submitted" value="true" />