1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2024-12-26 19:02:21 +00:00
plateforme-corres2math/server_files/controllers/ajouter_equipe.php

66 lines
2.3 KiB
PHP

<?php
if (!isset($_SESSION["role"]) || ($_SESSION["role"] != Role::PARTICIPANT && $_SESSION["role"] != Role::ENCADRANT))
require_once "server_files/403.php";
$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 $problem;
public $access_code;
public function __construct($data)
{
foreach ($data as $key => $value)
$this->$key = htmlspecialchars($value);
$this->trigram = strtoupper($this->trigram);
}
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, "Vous êtes déjà dans une équipe.");
ensure($this->name != null && $this->name != "", "Vous devez spécifier un nom d'équipe.");
ensure(preg_match("#^[\p{L} ]+$#ui", $this->name), "Le nom de l'équite ne doit pas comporter de caractères spéciaux.");
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.");
ensure(preg_match("#[1-4]#", $this->problem), "Le problème choisi n'a pas été reconnu.");
}
public function register() {
global $DB, $YEAR;
$this->access_code = genRandomPhrase(6);
$req = $DB->prepare("INSERT INTO `teams` (`name`, `trigram`, `problem`, `encadrant`, `participant_1`, `validation_status`, `access_code`, `year`)
VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
$req->execute([$this->name, $this->trigram, $this->problem, $_SESSION["role"] == Role::ENCADRANT ? $_SESSION["user_id"] : NULL,
$_SESSION["role"] == Role::PARTICIPANT ? $_SESSION["user_id"] : NULL, ValidationStatus::getName(ValidationStatus::NOT_READY), $this->access_code, $YEAR]);
$_SESSION["team"] = Team::fromTrigram($this->trigram);
$_SESSION["user"]->setTeamId($_SESSION["team"]->getId());
Mailer::sendAddTeamMail($_SESSION["user"], $_SESSION["team"]);
}
}
require_once "server_files/views/ajouter_equipe.php";