Amélioration du code de la page des solutions (vue équipe)
This commit is contained in:
parent
98fb682c66
commit
f5e73ae2ed
|
@ -10,8 +10,18 @@ if (!isset($_SESSION["team"]))
|
||||||
$team = $_SESSION["team"];
|
$team = $_SESSION["team"];
|
||||||
$tournament = Tournament::fromId($team->getTournamentId());
|
$tournament = Tournament::fromId($team->getTournamentId());
|
||||||
|
|
||||||
|
$has_error = false;
|
||||||
|
$error_message = null;
|
||||||
|
|
||||||
if (isset($_POST["send_solution"])) {
|
if (isset($_POST["send_solution"])) {
|
||||||
$error_message = saveSolution();
|
$save_solution = new SaveSolution();
|
||||||
|
try {
|
||||||
|
$save_solution->makeVerifications();
|
||||||
|
$save_solution->saveSolution();
|
||||||
|
} catch (AssertionError $e) {
|
||||||
|
$has_error = true;
|
||||||
|
$error_message = $e->getMessage();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$solutions = $tournament->getAllSolutions($team->getId());
|
$solutions = $tournament->getAllSolutions($team->getId());
|
||||||
|
@ -19,40 +29,43 @@ $solutions_final = null;
|
||||||
if ($team->isSelectedForFinal())
|
if ($team->isSelectedForFinal())
|
||||||
$solutions_final = $FINAL->getAllSolutions($team->getId());
|
$solutions_final = $FINAL->getAllSolutions($team->getId());
|
||||||
|
|
||||||
function saveSolution() {
|
class SaveSolution
|
||||||
|
{
|
||||||
|
private $problem;
|
||||||
|
private $file;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->file = $_FILES["document"];
|
||||||
|
$this->problem = htmlspecialchars($_POST["problem"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function makeVerifications()
|
||||||
|
{
|
||||||
|
global $LOCAL_PATH;
|
||||||
|
|
||||||
|
ensure(preg_match("#[1-9]#", $this->problem), "Le numéro du problème est invalide.");
|
||||||
|
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 saveSolution()
|
||||||
|
{
|
||||||
global $LOCAL_PATH, $DB, $team, $tournament, $FINAL;
|
global $LOCAL_PATH, $DB, $team, $tournament, $FINAL;
|
||||||
|
|
||||||
try {
|
|
||||||
$problem = $_POST["problem"];
|
|
||||||
if ($problem < 1 || $problem > 9)
|
|
||||||
return "Le numéro de problème est invalide.";
|
|
||||||
}
|
|
||||||
catch (Throwable $t) {
|
|
||||||
return "Le numéro de problème n'est pas valide. Merci de ne pas créer vos propres requêtes.";
|
|
||||||
}
|
|
||||||
|
|
||||||
$file = $_FILES["solution"];
|
|
||||||
|
|
||||||
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
|
do
|
||||||
$id = genRandomPhrase(64);
|
$id = genRandomPhrase(64);
|
||||||
while (file_exists("$LOCAL_PATH/files/$id"));
|
while (file_exists("$LOCAL_PATH/files/$id"));
|
||||||
|
|
||||||
if (!rename($file["tmp_name"], "$LOCAL_PATH/files/$id"))
|
if (!rename($this->file["tmp_name"], "$LOCAL_PATH/files/$id"))
|
||||||
return "Une erreur est survenue lors de l'envoi du fichier.";
|
throw new AssertionError("Une erreur est survenue lors de l'envoi du fichier.");
|
||||||
|
|
||||||
$req = $DB->prepare("INSERT INTO `solutions`(`file_id`, `team`, `tournament`, `problem`) VALUES (?, ?, ?, ?);");
|
$req = $DB->prepare("INSERT INTO `solutions`(`file_id`, `team`, `tournament`, `problem`) VALUES (?, ?, ?, ?);");
|
||||||
$req->execute([$id, $team->getId(), $team->isSelectedForFinal() ? $FINAL->getId() : $tournament->getId(), $problem]);
|
$req->execute([$id, $team->getId(), $team->isSelectedForFinal() ? $FINAL->getId() : $tournament->getId(), $this->problem]);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
require_once "server_files/views/solutions.php";
|
require_once "server_files/views/solutions.php";
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
<?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>";
|
||||||
} else {
|
} elseif (isset($save_solution)) {
|
||||||
echo "<h2>Le fichier a été correctement envoyé !</h2>";
|
echo "<h2>Le fichier a été correctement envoyé !</h2>";
|
||||||
}
|
}
|
||||||
} ?>
|
?>
|
||||||
|
|
||||||
<?php if (date("yyyy-mm-dd") < $tournament->getSolutionsDate()) { ?>
|
<?php if (date("yyyy-mm-dd") < $tournament->getSolutionsDate()) { ?>
|
||||||
<form method="post" enctype="multipart/form-data">
|
<form method="post" enctype="multipart/form-data">
|
||||||
|
|
Loading…
Reference in New Issue