plateforme-tfjm2/server_files/model.php

260 lines
8.4 KiB
PHP
Raw Normal View History

2019-09-06 11:48:50 +00:00
<?php
$FINAL = Tournament::getFinalTournament();
2019-09-09 21:28:03 +00:00
function loadUserValues()
{
$_SESSION["user"] = $_SESSION["team"] = $_SESSION["tournament"] = null;
unset($_SESSION["user"]);
unset($_SESSION["role"]);
unset($_SESSION["team"]);
unset($_SESSION["tournament"]);
2019-09-06 11:48:50 +00:00
if (isset($_SESSION["user_id"])) {
$user = $_SESSION["user"] = User::fromId($_SESSION["user_id"]);
$_SESSION["role"] = $user->getRole();
2019-09-06 11:48:50 +00:00
if ($user->getTeamId() !== null) {
$team = $_SESSION["team"] = Team::fromId($user->getTeamId());
$_SESSION["tournament"] = Tournament::fromId($team->getTournamentId());
2019-09-06 11:48:50 +00:00
}
2019-12-19 14:05:11 +00:00
if (isset($_GET["view-as-admin"])) {
if (isset($_SESSION["admin"])) {
$_SESSION["user_id"] = $_SESSION["admin"];
unset($_SESSION["admin"]);
}
header("Location: /");
exit();
}
2019-09-06 11:48:50 +00:00
}
}
2019-12-26 21:30:42 +00:00
function quitTeam($user_id = -1)
2019-09-09 21:28:03 +00:00
{
2019-09-06 11:48:50 +00:00
global $DB, $URL_BASE;
header("Location: $URL_BASE");
/** @var User $user */
$user = $_SESSION["user"];
2019-12-26 21:30:42 +00:00
if ($user_id == -1)
$user_id = $user->getId();
else
$user = User::fromId($user_id);
$role = $user->getRole();
if ($role == Role::ADMIN || $role == Role::ORGANIZER)
2019-09-06 11:48:50 +00:00
return;
2020-02-18 14:33:55 +00:00
for ($i = 1; $i <= ($role == Role::ENCADRANT ? 3 : 6); ++$i)
2019-09-06 11:48:50 +00:00
/** @noinspection SqlResolve */
$DB->exec("UPDATE `teams` SET `" . strtolower(Role::getName($role)) . "_$i` = NULL WHERE `" . strtolower(Role::getName($role)) . "_$i` = $user_id;");
$user->setTeamId(null);
2019-09-06 11:48:50 +00:00
$DB->exec("UPDATE `teams` SET `encadrant_1` = `encadrant_2`, `encadrant_2` = NULL WHERE `encadrant_1` IS NULL;");
2020-02-18 14:33:55 +00:00
$DB->exec("UPDATE `teams` SET `encadrant_2` = `encadrant_3`, `encadrant_3` = NULL WHERE `encadrant_2` IS NULL;");
2019-09-06 11:48:50 +00:00
for ($i = 1; $i <= 5; ++$i) {
/** @noinspection SqlResolve */
$DB->exec("UPDATE `teams` SET `participant_$i` = `participant_" . strval($i + 1) . "`, `participant_" . strval($i + 1) . "` = NULL WHERE `participant_$i` IS NULL;");
}
$req = $DB->query("SELECT `file_id` FROM `documents` WHERE `user` = $user_id;");
2019-09-06 11:48:50 +00:00
while (($data = $req->fetch()) !== false)
unlink("$URL_BASE/files/" . $data["file_id"]);
$DB->exec("DELETE FROM `documents` WHERE `user` = $user_id;");
2019-09-06 11:48:50 +00:00
if ($DB->exec("DELETE FROM `teams` WHERE `encadrant_1` IS NULL AND `participant_1` IS NULL;") > 0) {
$team_id = $user->getTeamId();
$req = $DB->query("SELECT `file_id` FROM `solutions` WHERE `team` = $team_id;");
2019-09-06 11:48:50 +00:00
while (($data = $req->fetch()) !== false)
unlink("$URL_BASE/files/" . $data["file_id"]);
$DB->exec("DELETE FROM `solutions` WHERE `team` = $team_id;");
2019-09-06 11:48:50 +00:00
$req = $DB->query("SELECT `file_id` FROM `syntheses` WHERE `team` = $team_id;");
2019-09-06 11:48:50 +00:00
while (($data = $req->fetch()) !== false)
unlink("$URL_BASE/files/" . $data["file_id"]);
$DB->exec("DELETE FROM `syntheses` WHERE `team` = $team_id;");
2019-09-06 11:48:50 +00:00
}
$_SESSION["team"] = null;
unset($_SESSION["team"]);
}
2019-09-09 21:28:03 +00:00
function userExists($email)
{
global $DB, $YEAR;
$req = $DB->prepare("SELECT `id` FROM `users` WHERE `email` = ? AND `year` = '$YEAR';");
$req->execute([$email]);
return $req->fetch();
}
2019-09-09 21:28:03 +00:00
function teamExists($name)
{
global $DB, $YEAR;
$req = $DB->prepare("SELECT `id` FROM `teams` WHERE `name` = ? AND `year` = '$YEAR';");
$req->execute([$name]);
return $req->fetch();
}
2019-09-09 21:28:03 +00:00
function trigramExists($trigram)
{
global $DB, $YEAR;
$req = $DB->prepare("SELECT `id` FROM `teams` WHERE `trigram` = ? AND `year` = '$YEAR';");
$req->execute([$trigram]);
return $req->fetch();
}
2019-09-09 21:28:03 +00:00
function tournamentExists($name)
{
global $DB, $YEAR;
$req = $DB->prepare("SELECT `id` FROM `tournaments` WHERE `name` = ? AND `year` = '$YEAR';");
$req->execute([$name]);
return $req->fetch();
2019-09-07 23:35:05 +00:00
}
2019-09-09 21:28:03 +00:00
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;
}
2020-01-18 13:43:42 +00:00
2019-09-09 21:28:03 +00:00
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;
$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;
2020-01-18 22:57:49 +00:00
$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;
2019-09-09 21:28:03 +00:00
}
}
2020-01-18 13:43:42 +00:00
$req = $DB->prepare("SELECT COUNT(*) AS `version` FROM `documents` WHERE `team` = ? AND `tournament` = ? AND `type` = ?;");
$req->execute([$team->getId(), $tournament->getId(), "MOTIVATION_LETTER"]);
$d = $req->fetch();
$can_validate &= $d["version"] > 0;
2019-09-09 21:28:03 +00:00
return $can_validate;
}
function printDocuments($documents)
{
2019-12-26 21:30:42 +00:00
if (sizeof($documents) == 0) {
echo "<div class=\"alert alert-warning\">\nPas de document envoyé pour le moment.\n</div>\n";
return;
}
2019-09-07 23:35:05 +00:00
2019-12-26 21:30:42 +00:00
echo "<div class=\"alert alert-info\">\n";
2019-09-07 23:35:05 +00:00
foreach ($documents as $document) {
$file_id = $document->getFileId();
2020-01-01 23:09:02 +00:00
$name = DocumentType::getTranslatedName($document->getType());
2019-09-07 23:35:05 +00:00
$version = $document->getVersion();
2020-01-18 13:43:42 +00:00
if ($document->getType() == DocumentType::MOTIVATION_LETTER) {
$team = Team::fromId($document->getTeamId());
echo "Lettre de motivation de l'équipe " . $team->getTrigram();
}
else {
$user = User::fromId($document->getUserId());
$surname = $user->getSurname();
$first_name = $user->getFirstName();
echo "$name de $first_name $surname";
}
echo " (version $version) : <a href=\"/file/$file_id\"><strong>Télécharger</strong></a><br />\n";
2019-09-07 23:35:05 +00:00
}
2019-12-26 21:30:42 +00:00
echo "</div>\n";
2019-09-09 10:15:48 +00:00
}
2019-09-09 21:28:03 +00:00
function getZipFile($document_type, $tournament_id, $team_id = -1)
{
2019-09-09 10:15:48 +00:00
global $LOCAL_PATH;
$tournament = Tournament::fromId($tournament_id);
$zip = new ZipArchive();
$file_name = tempnam("tmp", "tfjm-");
if ($zip->open($file_name, ZipArchive::CREATE) !== true) {
die("Impossible de créer le fichier zip.");
}
switch ($document_type) {
case DocumentType::SOLUTION:
$data = $tournament->getAllSolutions($team_id);
break;
case DocumentType::SYNTHESIS:
$data = $tournament->getAllSyntheses($team_id);
break;
default:
$data = $tournament->getAllDocuments($team_id);
break;
}
/** @var Document | Solution | Synthesis $file */
foreach ($data as $file) {
$file_id = $file->getFileId();
$team = Team::fromId($file->getTeamId());
switch ($document_type) {
case DocumentType::SOLUTION:
$name = "Problème " . $file->getProblem() . " " . $team->getTrigram() . ".pdf";
break;
case DocumentType::SYNTHESIS:
$name = "Note de synthèse " . $team->getTrigram() . " pour " . ($file->getDest() == DestType::OPPOSANT ? "l'opposant" : "le rapporteur") . ".pdf";
break;
default:
$user = User::fromId($file->getUserId());
switch ($file->getType()) {
case DocumentType::PARENTAL_CONSENT:
$name = "Autorisation parentale de " . $user->getFirstName() . " " . $user->getSurname() . ".pdf";
break;
case DocumentType::PHOTO_CONSENT:
$name = "Autorisation de droit à l'image de " . $user->getFirstName() . " " . $user->getSurname() . ".pdf";
break;
2020-01-18 13:43:42 +00:00
case DocumentType::SCHOLARSHIP:
$name = "Notification de bourse de " . $user->getFirstName() . " " . $user->getSurname() . ".pdf";
break;
case DocumentType::MOTIVATION_LETTER:
$name = "Lettre de motivation de l'équipe " . $team->getTrigram() . ".pdf";
break;
2019-09-09 10:15:48 +00:00
default:
$name = "Fiche sanitaire de " . $user->getFirstName() . " " . $user->getSurname() . ".pdf";
break;
}
break;
}
$zip->addFile("$LOCAL_PATH/files/$file_id", $name);
}
$zip->close();
return $file_name;
2019-09-06 11:48:50 +00:00
}