1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2024-12-25 17:42:22 +00:00
plateforme-corres2math/server_files/model.php
2019-10-06 13:29:20 +02:00

169 lines
5.1 KiB
PHP

<?php
function loadUserValues()
{
$_SESSION["user"] = $_SESSION["team"] = null;
unset($_SESSION["user"]);
unset($_SESSION["role"]);
unset($_SESSION["team"]);
if (isset($_SESSION["user_id"])) {
$user = $_SESSION["user"] = User::fromId($_SESSION["user_id"]);
if ($user == null) {
unset($_SESSION["user_id"]);
return;
}
$_SESSION["role"] = $user->getRole();
if ($user->getTeamId() !== null)
$_SESSION["team"] = Team::fromId($user->getTeamId());
}
}
function quitTeam($user_id = -1)
{
global $DB, $URL_BASE;
if ($user_id == -1)
header("Location: $URL_BASE");
$user_id = $user_id >= 0 ? $user_id : $_SESSION["user_id"];
/** @var User $user */
$user = User::fromId($user_id);
$role = $user->getRole();
if ($role == Role::ADMIN)
return;
if ($role == Role::PARTICIPANT)
for ($i = 1; $i <= 5; ++$i)
/** @noinspection SqlResolve */
$DB->exec("UPDATE `teams` SET `participant_$i` = NULL WHERE `participant_$i` = $user_id;");
else
$DB->exec("UPDATE `teams` SET `encadrant` = NULL WHERE `encadrant` = $user_id;");
$user->setTeamId(null);
for ($i = 1; $i <= 4; ++$i) {
/** @noinspection SqlResolve */
$DB->exec("UPDATE `teams` SET `participant_$i` = `participant_" . strval($i + 1) . "`, `participant_" . strval($i + 1) . "` = NULL WHERE `participant_$i` IS NULL;");
}
$DB->exec("DELETE FROM `teams` WHERE `encadrant` IS NULL AND `participant_1` IS NULL;");
$req = $DB->query("SELECT `file_id` FROM `documents` WHERE `user` = $user_id;");
while (($data = $req->fetch()) !== false)
unlink("$URL_BASE/files/" . $data["file_id"]);
$DB->exec("DELETE FROM `documents` WHERE `user` = $user_id;");
$_SESSION["team"] = null;
unset($_SESSION["team"]);
}
function userExists($email)
{
global $DB, $YEAR;
$req = $DB->prepare("SELECT `id` FROM `users` WHERE `email` = ? AND `year` = '$YEAR';");
$req->execute([$email]);
return $req->fetch();
}
function teamExists($name)
{
global $DB, $YEAR;
$req = $DB->prepare("SELECT `id` FROM `teams` WHERE `name` = ? AND `year` = '$YEAR';");
$req->execute([$name]);
return $req->fetch();
}
function trigramExists($trigram)
{
global $DB, $YEAR;
$req = $DB->prepare("SELECT `id` FROM `teams` WHERE `trigram` = ? AND `year` = '$YEAR';");
$req->execute([$trigram]);
return $req->fetch();
}
function canValidate(Team $team)
{
global $DB, $CONFIG;
$can_validate = date("Y-m-d H:i:s") < $CONFIG->getInscriptionDate();
$can_validate &= $team->getValidationStatus() == ValidationStatus::NOT_READY;
$can_validate &= $team->getEncadrantId() != null;
$can_validate &= $team->getParticipants()[2] != null;
$can_validate &= preg_match("#[1-4]#", $team->getProblem());
for ($i = 1; $i <= 5; ++$i) {
if ($team->getParticipants()[$i] === NULL)
continue;
$req = $DB->prepare("SELECT COUNT(*) AS `version` FROM `documents` WHERE `user` = ? AND `problem` = ?;");
$req->execute([$team->getParticipants()[$i], $team->getProblem()]);
$d = $req->fetch();
$can_validate &= $d["version"] > 0;
}
return $can_validate;
}
function printDocuments($documents)
{
echo "<div class=\"alert alert-info\">\n";
foreach ($documents as $document) {
$file_id = $document->getFileId();
$user = User::fromId($document->getUserId());
$surname = $user->getSurname();
$first_name = $user->getFirstName();
$name = "Autorisation de droit à l'image";
$version = $document->getVersion();
echo "$name de $first_name $surname (version $version) : <a href=\"/file/$file_id\"><strong>Télécharger</strong></a><br />\n";
}
echo "</div>\n";
}
function getZipFile($problem, $team_id = -1)
{
global $LOCAL_PATH;
$zip = new ZipArchive();
$file_name = tempnam("tmp", "corres2math-");
if ($zip->open($file_name, ZipArchive::CREATE) !== true) {
die("Impossible de créer le fichier zip.");
}
$data = Document::getAllDocuments($problem, $team_id);
/** @var Document $file */
foreach ($data as $file) {
$file_id = $file->getFileId();
$user = User::fromId($file->getUserId());
$name = "Autorisation de droit à l'image de " . $user->getFirstName() . " " . $user->getSurname() . ".pdf";
$zip->addFile("$LOCAL_PATH/files/$file_id", $name);
}
$zip->close();
return $file_name;
}
function displayVideo($link)
{
if (preg_match("#(https?\://|)(www\.|)youtube\.com\/watch\?v=(.*)#", $link, $matches)) {
$code = $matches[3];
echo "<center><iframe width=\"854px\" height=\"480px\" src=\"https://www.youtube.com/embed/$code\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></center><br />\n";
}
elseif (preg_match("#(https?\://|)(www\.|)vimeo\.com/([0-9]*)#", $link, $matches)) {
$code = $matches[3];
echo "<center><iframe src=\"https://player.vimeo.com/video/$code?color=3be6c3\" width=\"853\" height=\"480\" frameborder=\"0\" allow=\"autoplay; fullscreen\" allowfullscreen></iframe></center>";
}
elseif (preg_match("#(https?\://|)(www\.|)dailymotion\.com/video/(.*)#", $link, $matches)) {
$code = $matches[3];
echo "<center><iframe frameborder=\"0\" width=\"853\" height=\"480\" src=\"https://www.dailymotion.com/embed/video/$code\" allowfullscreen allow=\"autoplay\"></iframe></center>";
}
}