1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-12-26 19:42:22 +00:00
plateforme-tfjm2/server_files/controllers/ajouter_equipe.php
2019-09-07 17:26:30 +02:00

67 lines
2.4 KiB
PHP

<?php
if (!isset($_SESSION["role"]) || ($_SESSION["role"] != Role::PARTICIPANT && $_SESSION["role"] != Role::ENCADRANT))
require_once "server_files/403.php";
$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"])) {
$new_team = new NewTeam($_POST);
try {
$new_team->makeVerifications();
$new_team->register();
}
catch (AssertionError $e) {
$has_error = true;
$error_message = $e->getMessage();
}
}
class NewTeam {
public $name;
public $trigram;
public $tournament_id;
public $tournament;
public $access_code;
public function __construct($data)
{
foreach ($data as $key => $value)
$this->$key = htmlspecialchars($value);
}
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.");
}
public function register() {
global $DB, $YEAR;
$alphabet = "0123456789abcdefghijkmnopqrstuvwxyz0123456789";
$this->access_code = "";
for ($i = 0; $i < 6; ++$i)
$this->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 (?, ?, ?, ?, ?, ?, ?, ?);");
$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($this->trigram);
$_SESSION["user"]->setTeamId($_SESSION["team"]->getId());
sendAddTeamMail($_SESSION["user"], $_SESSION["team"], $this->tournament);
}
}
require_once "server_files/views/ajouter_equipe.php";