mirror of
https://gitlab.com/animath/si/plateforme-corres2math.git
synced 2024-12-25 19:02:27 +00:00
69 lines
2.4 KiB
PHP
69 lines
2.4 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["add_team"])) {
|
|
$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 $allow_publish;
|
|
public $access_code;
|
|
|
|
public function __construct($data)
|
|
{
|
|
foreach ($data as $key => $value)
|
|
$this->$key = htmlspecialchars($value);
|
|
|
|
$this->trigram = strtoupper($this->trigram);
|
|
|
|
$this->allow_publish = $this->allow_publish == "on";
|
|
}
|
|
|
|
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'équipe 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`, `allow_publish`, `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, $this->allow_publish, 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";
|