1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-12-27 11:02:22 +00:00
plateforme-tfjm2/server_files/controllers/ajouter_equipe.php

64 lines
2.3 KiB
PHP
Raw Normal View History

2019-09-05 17:07:41 +00:00
<?php
if (!isset($_SESSION["role"]) || ($_SESSION["role"] != Role::PARTICIPANT && $_SESSION["role"] != Role::ENCADRANT))
require_once "server_files/403.php";
2019-12-19 12:02:01 +00:00
$tournaments_response = $DB->query("SELECT `id`, `name` FROM `tournaments` WHERE `date_inscription` > CURRENT_TIMESTAMP AND `year` = $YEAR;");
2019-09-05 17:07:41 +00:00
$has_error = false;
$error_message = null;
2019-12-19 12:02:01 +00:00
if (isset($_POST["add_team"])) {
$new_team = new NewTeam($_POST);
try {
$new_team->makeVerifications();
$new_team->register();
}
catch (AssertionError $e) {
$has_error = true;
$error_message = $e->getMessage();
}
2019-09-05 17:07:41 +00:00
}
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;
$this->access_code = genRandomPhrase(6);
$req = $DB->prepare("INSERT INTO `teams` (`name`, `trigram`, `tournament`, `encadrant_1`, `participant_1`, `validation_status`, `access_code`, `year`)
2019-09-05 17:07:41 +00:00
VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
$req->execute([$this->name, $this->trigram, $this->tournament_id, $_SESSION["role"] == Role::ENCADRANT ? $_SESSION["user_id"] : NULL,
2020-01-16 22:00:31 +00:00
$_SESSION["role"] == Role::PARTICIPANT ? $_SESSION["user_id"] : NULL, ValidationStatus::getName(ValidationStatus::NOT_READY), $this->access_code, $YEAR]);
2019-09-05 17:07:41 +00:00
$_SESSION["team"] = Team::fromTrigram($this->trigram);
$_SESSION["user"]->setTeamId($_SESSION["team"]->getId());
2019-09-05 17:07:41 +00:00
2019-09-08 10:45:48 +00:00
Mailer::sendAddTeamMail($_SESSION["user"], $_SESSION["team"], $this->tournament);
}
2019-09-05 17:07:41 +00:00
}
require_once "server_files/views/ajouter_equipe.php";