72 lines
2.3 KiB
PHP
72 lines
2.3 KiB
PHP
<?php
|
|
|
|
if ((isset($_SESSION["team"]) && $_SESSION["role"] == Role::PARTICIPANT) || !isset($_SESSION["user"]) || ($_SESSION["role"] != Role::PARTICIPANT && $_SESSION["role"] != Role::ENCADRANT))
|
|
require_once "server_files/403.php";
|
|
|
|
$has_error = false;
|
|
$error_message = null;
|
|
|
|
if (isset($_POST["join_team"])) {
|
|
$join_team = new JoinTeam($_POST);
|
|
try {
|
|
$join_team->makeVerifications();
|
|
$join_team->joinTeam();
|
|
} catch (AssertionError $e) {
|
|
$has_error = true;
|
|
$error_message = $e->getMessage();
|
|
}
|
|
}
|
|
|
|
class JoinTeam
|
|
{
|
|
private $access_code;
|
|
private $team;
|
|
private $min_null_index;
|
|
|
|
public function __construct($data)
|
|
{
|
|
$this->access_code = strtolower(htmlspecialchars($data["access_code"]));
|
|
$this->team = Team::fromAccessCode($this->access_code);
|
|
}
|
|
|
|
public function makeVerifications()
|
|
{
|
|
global $CONFIG;
|
|
|
|
ensure(date("Y-m-d H:i:s") < $CONFIG->getInscriptionDate(), "La date limite d'inscription est dépassée.");
|
|
ensure($_SESSION["team"] == null || $_SESSION["role"] == Role::ENCADRANT, "Vous êtes déjà dans une équipe.");
|
|
ensure(preg_match("#[a-z0-9]{6}#", $this->access_code), "Le code d'accès doit comporter 6 caractères alphanumériques.");
|
|
ensure($this->team != null, "Ce code d'accès est invalide.");
|
|
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.");
|
|
|
|
for ($i = 1; $i <= ($_SESSION["role"] == Role::PARTICIPANT ? 5 : 1); ++$i) {
|
|
if (($_SESSION["role"] == Role::PARTICIPANT ? $this->team->getParticipants()[$i - 1] : $this->team->getEncadrantId()) == NULL)
|
|
break;
|
|
}
|
|
|
|
$this->min_null_index = $i;
|
|
|
|
ensure($_SESSION["role"] == Role::PARTICIPANT && $this->min_null_index <= 5 || $_SESSION["role"] == Role::ENCADRANT && $this->min_null_index <= 1, "Il n'y a plus de place pour vous dans l'équipe.");
|
|
}
|
|
|
|
public function joinTeam()
|
|
{
|
|
global $team;
|
|
|
|
$user = $_SESSION["user"];
|
|
|
|
$user->setTeamId($this->team->getId());
|
|
|
|
if ($_SESSION["role"] == Role::ENCADRANT)
|
|
$this->team->setEncadrant($user->getId());
|
|
else
|
|
$this->team->setParticipant($this->min_null_index, $user->getId());
|
|
|
|
$team = $_SESSION["team"] = $this->team;
|
|
|
|
Mailer::sendJoinTeamMail($user, $this->team);
|
|
}
|
|
}
|
|
|
|
require_once "server_files/views/rejoindre_equipe.php";
|