Fichier "Mon équipe"

This commit is contained in:
galaxyoyo 2019-09-09 23:28:03 +02:00
parent fac2b29f4a
commit fffdaabe7c
4 changed files with 171 additions and 136 deletions

View File

@ -2,9 +2,9 @@
class User class User
{ {
public $id; private $id;
public $email; public $email;
public $pwd_hash; private $pwd_hash;
public $surname; public $surname;
public $first_name; public $first_name;
public $birth_date; public $birth_date;
@ -20,12 +20,12 @@ class User
public $responsible_phone; public $responsible_phone;
public $responsible_email; public $responsible_email;
public $description; public $description;
public $role; private $role;
public $team_id; private $team_id;
public $year; private $year;
public $confirm_email; private $confirm_email;
public $forgotten_password; private $forgotten_password;
public $inscription_date; private $inscription_date;
private function __construct() {} private function __construct() {}

View File

@ -7,12 +7,35 @@ if (isset($_POST["leave_team"])) {
$tournaments = Tournament::getAllTournaments(false, true); $tournaments = Tournament::getAllTournaments(false, true);
$has_error = false;
$error_message = null;
if (isset($_POST["send_document"])) { if (isset($_POST["send_document"])) {
$error_message = sendDocument(); $send_document = new SendDocument();
try {
$send_document->makeVerifications();
$send_document->sendDocument();
}
catch (AssertionError $e) {
$has_error = true;
$error_message = $e->getMessage();
}
}
if (isset($_POST["team_edit"])) {
$my_team = new MyTeam($_POST);
try {
$my_team->makeVerifications();
$my_team->updateTeam();
}
catch (AssertionError $e) {
$has_error = true;
$error_message = $e->getMessage();
}
} }
if (isset($_POST["request_validation"])) { if (isset($_POST["request_validation"])) {
if (!checkCanValidate()) if (!canValidate($team, $tournament))
$error_message = "Votre équipe ne peut pas demander la validation : il manque soit des participants, soit des documents."; $error_message = "Votre équipe ne peut pas demander la validation : il manque soit des participants, soit des documents.";
else else
$_SESSION["team"]->setValidationStatus(ValidationStatus::WAITING); $_SESSION["team"]->setValidationStatus(ValidationStatus::WAITING);
@ -32,127 +55,87 @@ if (isset($_SESSION["user_id"]) && isset($_SESSION["team"]) && $_SESSION["team"]
$documents_final = $user->getAllDocuments($FINAL->getId()); $documents_final = $user->getAllDocuments($FINAL->getId());
} }
else else
require_once "server_files/403.php"; require_once "server_files/403.php";
if (isset($_POST["team_edit"])) { class SendDocument
$error_message = updateTeam();
}
function sendDocument()
{ {
global $LOCAL_PATH, $DB, $FINAL; private $file;
private $type;
$type = strtoupper(htmlspecialchars($_POST["type"]));
if (!isset($type) || ($type != "PARENTAL_CONSENT" && $type != "PHOTO_CONSENT" && $type != "SANITARY_PLUG"))
return "Le type de document est invalide. Merci de ne pas formuler vos propres requêtes.";
$file = $_FILES["document"];
if ($file["size"] > 5000000 || $file["error"])
return "Une erreur est survenue. Merci de vérifier que le fichier pèse moins que 5 Mo.";
if (finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file["tmp_name"]) != 'application/pdf')
return "Le fichier doit être au format PDF.";
if (!is_dir("$LOCAL_PATH/files") && !mkdir("$LOCAL_PATH/files"))
return "Les droits sont insuffisants. Veuillez contacter l'administrateur du serveur.";
do public function __construct()
$id = genRandomPhrase(64); {
while (file_exists("$LOCAL_PATH/files/$id")); $this->file = $_FILES["document"];
$this->type = strtoupper(htmlspecialchars($_POST["type"]));
if (!rename($file["tmp_name"], "$LOCAL_PATH/files/$id")) }
return "Une erreur est survenue lors de l'envoi du fichier.";
public function makeVerifications()
$req = $DB->prepare("INSERT INTO `documents`(`file_id`, `user`, `team`, `tournament`, `type`) {
global $LOCAL_PATH;
ensure($this->file["size"] <= 2e6, "Le fichier doit peser moins que 2 Mo.");
ensure(!$this->file["error"], "Une erreur est survenue.");
ensure(finfo_file(finfo_open(FILEINFO_MIME_TYPE), $this->file["tmp_name"]) == "application/pdf", "Le fichier doit être au format PDF.");
ensure(is_dir("$LOCAL_PATH/files") || mkdir("$LOCAL_PATH/files"), "Un problème est survenue dans l'envoi du fichier. Veuillez contacter l'administrateur du serveur.");
}
public function sendDocument()
{
global $LOCAL_PATH, $DB, $FINAL;
do
$id = genRandomPhrase(64);
while (file_exists("$LOCAL_PATH/files/$id"));
if (!rename($this->file["tmp_name"], "$LOCAL_PATH/files/$id"))
throw new AssertionError("Une erreur est survenue lors de l'envoi du fichier.");
$req = $DB->prepare("INSERT INTO `documents`(`file_id`, `user`, `team`, `tournament`, `type`)
VALUES (?, ?, ?, ?, ?);"); VALUES (?, ?, ?, ?, ?);");
$req->execute([$id, $_SESSION["user_id"], $_SESSION["team"]->getId(), $_SESSION["team"]->isSelectedForFinal() ? $FINAL->getId() : $_SESSION["team"]->getTournamentId(), $type]); $req->execute([$id, $_SESSION["user_id"], $_SESSION["team"]->getId(), $_SESSION["team"]->isSelectedForFinal() ? $FINAL->getId() : $_SESSION["team"]->getTournamentId(), $this->type]);
}
return false;
} }
function updateTeam() class MyTeam
{ {
global $DB, $YEAR, $URL_BASE, $team; public $name;
public $trigram;
public $tournament_id;
private $team;
private $tournament;
$name = htmlspecialchars($_POST["name"]); public function __construct($data)
{
if (!isset($name) || $name == "") foreach ($data as $key => $value)
return "Vous devez spécifier un nom d'équipe."; $this->$key = htmlspecialchars($value);
$result = $DB->query("SELECT `id` FROM `teams` WHERE `name` = '" . $name . "' AND `id` != " . $team->getId() . " AND `year` = '$YEAR';"); $this->trigram = strtoupper($this->trigram);
if ($result->fetch()) $this->team = $_SESSION["team"];
return "Une équipe existe déjà avec ce nom."; $this->tournament = Tournament::fromId($this->tournament_id);
$trigram = strtoupper(htmlspecialchars($_POST["trigram"]));
if (!preg_match("#^[A-Z][A-Z][A-Z]$#", $trigram))
return "Le trigramme entré n'est pas valide.";
$result = $DB->query("SELECT `id` FROM `teams` WHERE `trigram` = '" . $trigram . "' AND `id` != '" . $team->getId() . "' AND `year` = '$YEAR';");
if ($result->fetch())
return "Une équipe a déjà choisi ce trigramme.";
$tournament_id = intval(htmlspecialchars($_POST["tournament"]));
$tournament = Tournament::fromId($tournament_id);
if ($tournament === null)
return "Le tournoi spécifié n'existe pas.";
$team->setName($name);
$team->setTrigram($trigram);
$team->setTournamentId($tournament_id);
$_SESSION["tournament"] = $tournament;
header("Location: $URL_BASE/mon_equipe");
return false;
}
function checkCanValidate()
{
global $DB, $team, $tournament, $YEAR;
$can_validate = $team->getValidationStatus() == ValidationStatus::NOT_READY;
$can_validate &= $team->getEncadrants()[0] != NULL;
$can_validate &= $team->getParticipants()[3] != NULL;
for ($i = 1; $i <= 2; ++$i) {
if ($team->getEncadrants()[$i - 1] === NULL)
continue;
$req = $DB->prepare("SELECT COUNT(`type`) AS `version` FROM `documents` WHERE `user` = ? AND `type` = ? GROUP BY `uploaded_at` ORDER BY `uploaded_at` DESC;");
$req->execute([$team->getEncadrants()[$i - 1], "PHOTO_CONSENT"]);
$d = $req->fetch();
$can_validate &= $d["version"] > 0;
$req = $DB->prepare("SELECT COUNT(`type`) AS `version` FROM `documents` WHERE `user` = ? AND `type` = ? GROUP BY `uploaded_at` ORDER BY `uploaded_at` DESC;");
$req->execute([$team->getEncadrants()[$i - 1], "SANITARY_PLUG"]);
$d = $req->fetch();
$can_validate &= $d["version"] > 0;
} }
for ($i = 1; $i <= 6; ++$i) {
if ($team->getParticipants()[$i] === NULL) public function makeVerifications()
continue; {
ensure($this->name != "" && $this->name != null, "Veuillez spécifier un nom d'équipe.");
$req = $DB->prepare("SELECT COUNT(`type`) AS `version` FROM `documents` WHERE `user` = ? AND `type` = ? GROUP BY `uploaded_at` ORDER BY `uploaded_at` DESC;"); ensure($this->name == $this->team->getName() || !teamExists($this->name), "Une équipe existe déjà avec ce nom.");
$req->execute([$team->getParticipants()[$i], "PHOTO_CONSENT"]); ensure(preg_match("#^[A-Z]{3}$#", $this->trigram), "Le trigramme n'est pas valide.");
$d = $req->fetch(); ensure($this->trigram == $this->team->getTrigram() || !trigramExists($this->trigram), "Une équipe a déjà choisi ce trigramme.");
$can_validate &= $d["version"] > 0; ensure($this->tournament != null, "Le tournoi indiqué n'existe pas.");
ensure(date("y-m-d H:i:s") <= $this->tournament->getInscriptionDate(), "Les inscriptions sont terminées.");
$req = $DB->prepare("SELECT COUNT(`type`) AS `version` FROM `documents` WHERE `user` = ? AND `type` = ? GROUP BY `uploaded_at` ORDER BY `uploaded_at` DESC;"); ensure($this->team->getValidationStatus() == ValidationStatus::NOT_READY, "Votre équipe est déjà validée ou en cours de validation.");
$req->execute([$team->getParticipants()[$i], "SANITARY_PLUG"]); }
$d = $req->fetch();
$can_validate &= $d["version"] > 0; public function updateTeam()
{
$birth_date = $DB->query("SELECT `birth_date` FROM `users` WHERE `id` = " . $team->getParticipants()[$i] . ";")->fetch()["birth_date"]; global $URL_BASE;
if ($birth_date > strval($YEAR - 18) . substr($tournament->getStartDate(), 4)) {
$req = $DB->prepare("SELECT COUNT(`type`) AS `version` FROM `documents` WHERE `user` = ? AND `type` = ? GROUP BY `uploaded_at` ORDER BY `uploaded_at` DESC;"); $this->team->setName($this->name);
$req->execute([$team->getParticipants()[$i], "PARENTAL_CONSENT"]); $this->team->setTrigram($this->trigram);
$d = $req->fetch(); $this->team->setTournamentId($this->tournament_id);
$can_validate &= $d["version"] > 0;
} $_SESSION["tournament"] = $this->tournament;
header("Location: $URL_BASE/mon_equipe");
} }
return $can_validate;
} }
require_once "server_files/views/mon_equipe.php"; require_once "server_files/views/mon_equipe.php";

View File

@ -2,7 +2,8 @@
$FINAL = Tournament::getFinalTournament(); $FINAL = Tournament::getFinalTournament();
function loadUserValues() { function loadUserValues()
{
$_SESSION["user"] = $_SESSION["team"] = $_SESSION["tournament"] = null; $_SESSION["user"] = $_SESSION["team"] = $_SESSION["tournament"] = null;
unset($_SESSION["user"]); unset($_SESSION["user"]);
unset($_SESSION["role"]); unset($_SESSION["role"]);
@ -44,7 +45,8 @@ function loadUserValues() {
} }
} }
function quitTeam() { function quitTeam()
{
global $DB, $URL_BASE; global $DB, $URL_BASE;
header("Location: $URL_BASE"); header("Location: $URL_BASE");
@ -89,7 +91,8 @@ function quitTeam() {
unset($_SESSION["team"]); unset($_SESSION["team"]);
} }
function userExists($email) { function userExists($email)
{
global $DB, $YEAR; global $DB, $YEAR;
$req = $DB->prepare("SELECT `id` FROM `users` WHERE `email` = ? AND `year` = '$YEAR';"); $req = $DB->prepare("SELECT `id` FROM `users` WHERE `email` = ? AND `year` = '$YEAR';");
@ -97,7 +100,8 @@ function userExists($email) {
return $req->fetch(); return $req->fetch();
} }
function teamExists($name) { function teamExists($name)
{
global $DB, $YEAR; global $DB, $YEAR;
$req = $DB->prepare("SELECT `id` FROM `teams` WHERE `name` = ? AND `year` = '$YEAR';"); $req = $DB->prepare("SELECT `id` FROM `teams` WHERE `name` = ? AND `year` = '$YEAR';");
@ -105,7 +109,8 @@ function teamExists($name) {
return $req->fetch(); return $req->fetch();
} }
function trigramExists($trigram) { function trigramExists($trigram)
{
global $DB, $YEAR; global $DB, $YEAR;
$req = $DB->prepare("SELECT `id` FROM `teams` WHERE `trigram` = ? AND `year` = '$YEAR';"); $req = $DB->prepare("SELECT `id` FROM `teams` WHERE `trigram` = ? AND `year` = '$YEAR';");
@ -113,7 +118,8 @@ function trigramExists($trigram) {
return $req->fetch(); return $req->fetch();
} }
function tournamentExists($name) { function tournamentExists($name)
{
global $DB, $YEAR; global $DB, $YEAR;
$req = $DB->prepare("SELECT `id` FROM `tournaments` WHERE `name` = ? AND `year` = '$YEAR';"); $req = $DB->prepare("SELECT `id` FROM `tournaments` WHERE `name` = ? AND `year` = '$YEAR';");
@ -121,7 +127,55 @@ function tournamentExists($name) {
return $req->fetch(); return $req->fetch();
} }
function printDocuments($documents) { function canValidate(Team $team, Tournament $tournament)
{
global $DB, $YEAR;
$can_validate = $team->getValidationStatus() == ValidationStatus::NOT_READY;
$can_validate &= $team->getEncadrants()[0] != NULL;
$can_validate &= $team->getParticipants()[3] != NULL;
for ($i = 1; $i <= 2; ++$i) {
if ($team->getEncadrants()[$i - 1] === NULL)
continue;
$req = $DB->prepare("SELECT COUNT(*) AS `version` FROM `documents` WHERE `user` = ? AND `tournament` = ? AND `type` = ?;");
$req->execute([$team->getEncadrants()[$i - 1], $tournament->getId(), "PHOTO_CONSENT"]);
$d = $req->fetch();
$can_validate &= $d["version"] > 0;
$req = $DB->prepare("SELECT COUNT(*) AS `version` FROM `documents` WHERE `user` = ? AND `tournament` = ? AND `type` = ?;");
$req->execute([$team->getEncadrants()[$i - 1], $tournament->getId(), "SANITARY_PLUG"]);
$d = $req->fetch();
$can_validate &= $d["version"] > 0;
}
for ($i = 1; $i <= 6; ++$i) {
if ($team->getParticipants()[$i] === NULL)
continue;
$req = $DB->prepare("SELECT COUNT(*) AS `version` FROM `documents` WHERE `user` = ? AND `tournament` = ? AND `type` = ?;");
$req->execute([$team->getParticipants()[$i], $tournament->getId(), "PHOTO_CONSENT"]);
$d = $req->fetch();
$can_validate &= $d["version"] > 0;
$req = $DB->prepare("SELECT COUNT(*) AS `version` FROM `documents` WHERE `user` = ? AND `tournament` = ? AND `type` = ?;");
$req->execute([$team->getParticipants()[$i], $tournament->getId(), "SANITARY_PLUG"]);
$d = $req->fetch();
$can_validate &= $d["version"] > 0;
$birth_date = $DB->query("SELECT `birth_date` FROM `users` WHERE `id` = " . $team->getParticipants()[$i] . ";")->fetch()["birth_date"];
if ($birth_date > strval($YEAR - 18) . substr($tournament->getStartDate(), 4)) {
$req = $DB->prepare("SELECT COUNT(*) AS `version` FROM `documents` WHERE `user` = ? AND `tournament` = ? AND `type` = ?;");
$req->execute([$team->getParticipants()[$i], $tournament->getId(), "PARENTAL_CONSENT"]);
$d = $req->fetch();
$can_validate &= $d["version"] > 0;
}
}
return $can_validate;
}
function printDocuments($documents)
{
global $URL_BASE; global $URL_BASE;
foreach ($documents as $document) { foreach ($documents as $document) {
@ -135,7 +189,8 @@ function printDocuments($documents) {
} }
} }
function getZipFile($document_type, $tournament_id, $team_id = -1) { function getZipFile($document_type, $tournament_id, $team_id = -1)
{
global $LOCAL_PATH; global $LOCAL_PATH;
$tournament = Tournament::fromId($tournament_id); $tournament = Tournament::fromId($tournament_id);

View File

@ -1,13 +1,10 @@
<?php <?php
require_once "header.php"; require_once "header.php";
if (isset($error_message)) { if ($has_error)
if ($error_message !== false) { echo "<h2>Erreur : " . $error_message . "</h2>";
echo "<h2>Erreur : " . $error_message . "</h2>"; elseif (isset($send_document))
} else {
echo "<h2>Le fichier a été correctement envoyé !</h2>"; echo "<h2>Le fichier a été correctement envoyé !</h2>";
}
}
?> ?>
<h2>Informations sur l'équipe</h2> <h2>Informations sur l'équipe</h2>
@ -65,7 +62,7 @@ for ($i = 1; $i <= 6; ++$i) {
<label for="tournament">Tournoi :</label> <label for="tournament">Tournoi :</label>
</td> </td>
<td> <td>
<select style="width: 100%;" id="tournament" name="tournament"> <select style="width: 100%;" id="tournament" name="tournament_id">
<?php <?php
foreach ($tournaments as $tournament) foreach ($tournaments as $tournament)
echo "<option value=\"" . $tournament->getId() . "\">" . $tournament->getName() . "</option>\n"; echo "<option value=\"" . $tournament->getId() . "\">" . $tournament->getName() . "</option>\n";
@ -147,7 +144,7 @@ for ($i = 1; $i <= 6; ++$i) {
</form> </form>
</td> </td>
<?php <?php
$can_validate = checkCanValidate(); $can_validate = canValidate($team, $tournament);
if ($can_validate) { ?> if ($can_validate) { ?>
<td style="width: 50%;"> <td style="width: 50%;">
<form method="post"> <form method="post">