From 81acf09fbf95bcea9fb24cc3fe55faab9be41133 Mon Sep 17 00:00:00 2001 From: galaxyoyo Date: Sat, 21 Sep 2019 14:31:31 +0200 Subject: [PATCH] =?UTF-8?q?Phase=202=20:=20am=C3=A9lioration=20du=20d?= =?UTF-8?q?=C3=A9p=C3=B4t=20des=20questions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server_files/classes/Question.php | 22 +++++++ server_files/controllers/poser_questions.php | 63 ++++++++++++-------- server_files/controllers/view_file.php | 2 +- 3 files changed, 60 insertions(+), 27 deletions(-) diff --git a/server_files/classes/Question.php b/server_files/classes/Question.php index 251b821..06a6362 100644 --- a/server_files/classes/Question.php +++ b/server_files/classes/Question.php @@ -7,6 +7,7 @@ class Question private $from; private $to; private $problem; + private $number; private $question; private $attached_file; @@ -101,13 +102,34 @@ class Question return $this->problem; } + public function getNumber() + { + return $this->number; + } + public function getQuestion() { return $this->question; } + public function setQuestion($question) + { + global $DB; + $this->question = $question; + $req = $DB->prepare("UPDATE `questions` SET `question` = ? WHERE `id` = ?;"); + $req->execute([$question, $this->id]); + } + public function getAttachedFile() { return $this->attached_file; } + + public function setAttachedFile($attached_file) + { + global $DB; + $this->attached_file = $attached_file; + $req = $DB->prepare("UPDATE `questions` SET `attached_file` = ? WHERE `id` = ?;"); + $req->execute([$attached_file, $this->id]); + } } \ No newline at end of file diff --git a/server_files/controllers/poser_questions.php b/server_files/controllers/poser_questions.php index daf3613..7a346f7 100644 --- a/server_files/controllers/poser_questions.php +++ b/server_files/controllers/poser_questions.php @@ -36,7 +36,9 @@ class GiveQuestions private $question_4; private $question_5; private $question_6; + private $questions; private $no_drawing; + private $has_files; private $files; public function __construct($data, $files) @@ -46,9 +48,18 @@ class GiveQuestions } $this->files = []; + $this->has_files = false; - for ($i = 1; $i <= 6; ++$i) - $this->files[] = strlen($files["file_$i"]["name"]) > 0 ? $files["file_$i"] : null; + for ($i = 1; $i <= 6; ++$i) { + if (strlen($files["file_$i"]["name"]) > 0) { + $this->files[] = $files["file_$i"]; + $this->has_files = true; + } + else + $this->files[] = null; + } + + $this->questions = [$this->question_1, $this->question_2, $this->question_3, $this->question_4, $this->question_5, $this->question_6]; } public function makeVerifications() @@ -60,12 +71,16 @@ class GiveQuestions ensure($team->getProblem() == $this->to_team->getProblem(), "Les équipes ne travaillent pas sur le même problème."); ensure($this->question_1 != null && $this->question_1 != "" && $this->question_2 != null && $this->question_2 != "" && $this->question_3 != null && $this->question_3 != "", "Vous devez poser au moins 3 questions."); - ensure(sizeof($_FILES) == 0 || $this->no_drawing, "Vous devez confirmer ne pas avoir inclus de texte dans vos pièces jointes."); + ensure(!$this->has_files || $this->no_drawing, "Vous devez confirmer ne pas avoir inclus de texte dans vos pièces jointes."); + + for ($i = 3; $i < 6; ++$i) { + if ($this->questions[$i] == "") + $this->questions[$i] = null; + } for ($i = 0; $i < 6; ++$i) { ensure($this->files[$i]["size"] <= 2e6, "Le fichier doit peser moins que 2 Mo."); - ensure(!$this->files[$i]["error"], "Une erreur est survenue."); - //ensure(finfo_file(finfo_open(FILEINFO_MIME_TYPE), $this->files[$i]["tmp_name"]) == "application/pdf", "Le fichier doit être au format PDF."); + ensure(!$this->files[$i]["error"], "Une erreur est survenue. Veuillez vérifier vos pièces jointes. Elles ne doivent pas peser plus de 2 Mo chacune."); } 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."); @@ -73,34 +88,30 @@ class GiveQuestions public function giveQuestions() { - global $DB, $LOCAL_PATH, $team; + global $LOCAL_PATH, $team; - $attached_file = []; - for ($i = 0; $i < 5; ++$i) - { - if ($this->files[$i] == null) - $attached_file[] = null; - else { + /** @var Question[] $questions */ + $questions = Question::getQuestions($team, $this->to_team); + for ($i = 0; $i < 6; ++$i) { + $question = $questions[$i]; + if ($question->getQuestion() != $this->questions[$i] && $question->getAttachedFile() != null) { + unlink("$LOCAL_PATH/files/" . $question->getAttachedFile()); + $question->setAttachedFile(null); + } + + $question->setQuestion($this->questions[$i]); + + if ($this->files[$i] != null) { do - $id = genRandomPhrase(64); - while (file_exists("$LOCAL_PATH/files/$id")); + $file_id = genRandomPhrase(64); + while (file_exists("$LOCAL_PATH/files/$file_id")); - if (!rename($this->files[$i]["tmp_name"], "$LOCAL_PATH/files/$id")) + if (!rename($this->files[$i]["tmp_name"], "$LOCAL_PATH/files/$file_id")) throw new AssertionError("Une erreur est survenue lors de l'envoi du fichier."); - $attached_file[] = $id; + $question->setAttachedFile($file_id); } } - - $DB->exec("DELETE FROM `questions` WHERE `from` = " . $team->getId() . " AND `to` = " . $this->to_team->getId() . ";"); - $req = $DB->prepare("INSERT INTO `questions`(`from`, `to`, `problem`, `question`, `attached_file`) VALUES " - . "(?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?);"); - $req->execute([$team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_1, $attached_file[0], - $team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_2, $attached_file[1], - $team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_3, $attached_file[2], - $team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_4, $attached_file[3], - $team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_5, $attached_file[4], - $team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_6, $attached_file[5]]); } } diff --git a/server_files/controllers/view_file.php b/server_files/controllers/view_file.php index e6c180d..b19cc43 100644 --- a/server_files/controllers/view_file.php +++ b/server_files/controllers/view_file.php @@ -35,7 +35,7 @@ else { $from = Team::fromId($question->getFrom()); $to = Team::fromId($question->getTo()); $mime_type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), "$LOCAL_PATH/files/$id"); - $name = "Pièce jointe de l'équipe " . $from->getTrigram() . " pour l'équipe " . $from->getTrigram(); + $name = "Pièce jointe n°" . $question->getNumber() . " de l'équipe " . $from->getTrigram() . " pour l'équipe " . $from->getTrigram() . " (problème " . $question->getProblem() . ")"; switch ($mime_type) { case "application/pdf": $name .= "pdf";