2019-09-09 23:48:52 +00:00
|
|
|
<?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;
|
|
|
|
|
2019-09-24 21:18:32 +00:00
|
|
|
if (isset($_POST["add_team"])) {
|
2019-09-09 23:48:52 +00:00
|
|
|
$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;
|
2019-09-10 23:17:05 +00:00
|
|
|
public $problem;
|
2019-09-24 08:08:53 +00:00
|
|
|
public $allow_publish;
|
2019-09-09 23:48:52 +00:00
|
|
|
public $access_code;
|
|
|
|
|
|
|
|
public function __construct($data)
|
|
|
|
{
|
|
|
|
foreach ($data as $key => $value)
|
|
|
|
$this->$key = htmlspecialchars($value);
|
2019-09-12 10:18:15 +00:00
|
|
|
|
|
|
|
$this->trigram = strtoupper($this->trigram);
|
2019-09-24 08:08:53 +00:00
|
|
|
|
|
|
|
$this->allow_publish = $this->allow_publish == "on";
|
2019-09-09 23:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function makeVerifications() {
|
2019-09-12 16:47:57 +00:00
|
|
|
global $CONFIG;
|
|
|
|
|
|
|
|
ensure(date("Y-m-d H:i:s") < $CONFIG->getInscriptionDate(), "La date limite d'inscription est dépassée.");
|
2019-09-09 23:48:52 +00:00
|
|
|
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.");
|
2019-09-12 10:18:15 +00:00
|
|
|
ensure(preg_match("#^[\p{L} ]+$#ui", $this->name), "Le nom de l'équite ne doit pas comporter de caractères spéciaux.");
|
2019-09-09 23:48:52 +00:00
|
|
|
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.");
|
2019-09-10 23:17:05 +00:00
|
|
|
ensure(preg_match("#[1-4]#", $this->problem), "Le problème choisi n'a pas été reconnu.");
|
2019-09-09 23:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function register() {
|
|
|
|
global $DB, $YEAR;
|
|
|
|
|
|
|
|
$this->access_code = genRandomPhrase(6);
|
|
|
|
|
2019-09-24 08:08:53 +00:00
|
|
|
$req = $DB->prepare("INSERT INTO `teams` (`name`, `trigram`, `problem`, `encadrant`, `participant_1`, `allow_publish`, `validation_status`, `access_code`, `year`)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);");
|
2019-09-10 23:17:05 +00:00
|
|
|
$req->execute([$this->name, $this->trigram, $this->problem, $_SESSION["role"] == Role::ENCADRANT ? $_SESSION["user_id"] : NULL,
|
2019-09-24 08:08:53 +00:00
|
|
|
$_SESSION["role"] == Role::PARTICIPANT ? $_SESSION["user_id"] : NULL, $this->allow_publish, ValidationStatus::getName(ValidationStatus::NOT_READY), $this->access_code, $YEAR]);
|
2019-09-09 23:48:52 +00:00
|
|
|
|
|
|
|
$_SESSION["team"] = Team::fromTrigram($this->trigram);
|
|
|
|
$_SESSION["user"]->setTeamId($_SESSION["team"]->getId());
|
|
|
|
|
2019-09-11 16:41:45 +00:00
|
|
|
Mailer::sendAddTeamMail($_SESSION["user"], $_SESSION["team"]);
|
2019-09-09 23:48:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once "server_files/views/ajouter_equipe.php";
|