diff --git a/dispatcher.php b/dispatcher.php index fa1de82..38cafc9 100644 --- a/dispatcher.php +++ b/dispatcher.php @@ -50,6 +50,7 @@ $ROUTES["^mon-equipe/?$"] = ["server_files/controllers/mon_equipe.php"]; $ROUTES["^poser-questions-2$"] = ["server_files/controllers/poser_questions.php"]; $ROUTES["^probleme/([1-4])/?$"] = ["server_files/controllers/probleme.php", "probleme"]; $ROUTES["^rejoindre_equipe/?$"] = ["server_files/controllers/rejoindre_equipe.php"]; +$ROUTES["^repondre-questions-3$"] = ["server_files/controllers/repondre_questions.php"]; $ROUTES["^videos-solutions/?$"] = ["server_files/controllers/videos_solutions.php"]; # Assets files diff --git a/server_files/classes/Question.php b/server_files/classes/Question.php index 78490b7..cac707c 100644 --- a/server_files/classes/Question.php +++ b/server_files/classes/Question.php @@ -10,6 +10,8 @@ class Question private $number; private $question; private $attached_file; + private $answer; + private $attached_file_answer; private function __construct() { @@ -35,8 +37,8 @@ class Question { global $DB; - $req = $DB->prepare("SELECT * FROM `questions` WHERE `attached_file` = ?;"); - $req->execute([htmlspecialchars($attached_file)]); + $req = $DB->prepare("SELECT * FROM `questions` WHERE `attached_file` = ? OR `attached_file_answer` = ?;"); + $req->execute([htmlspecialchars($attached_file), htmlspecialchars($attached_file)]); $data = $req->fetch(); if ($data === false) @@ -151,4 +153,30 @@ class Question $req = $DB->prepare("UPDATE `questions` SET `attached_file` = ? WHERE `id` = ?;"); $req->execute([$attached_file, $this->id]); } + + public function getAnswer() + { + return $this->answer; + } + + public function setAnswer($answer) + { + global $DB; + $this->answer = $answer; + $req = $DB->prepare("UPDATE `questions` SET `answer` = ? WHERE `id` = ?;"); + $req->execute([$answer, $this->id]); + } + + public function getAttachedFileAnswer() + { + return $this->attached_file_answer; + } + + public function setAttachedFileAnswer($attached_file) + { + global $DB; + $this->attached_file_answer = $attached_file; + $req = $DB->prepare("UPDATE `questions` SET `attached_file_answer` = ? 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 7a346f7..170bbcf 100644 --- a/server_files/controllers/poser_questions.php +++ b/server_files/controllers/poser_questions.php @@ -1,6 +1,6 @@ makeVerifications(); + $answer_questions->answerQuestions(); + } catch (AssertionError $e) { + $has_error = true; + $error_message = $e->getMessage(); + } +} + +class AnswerQuestions +{ + private $from; + /** + * @var Team $to_team + */ + private $from_team; + private $answer_1; + private $answer_2; + private $answer_3; + private $answer_4; + private $answer_5; + private $answer_6; + private $answers; + private $no_drawing; + private $has_files; + private $files; + + public function __construct($data, $files) + { + foreach ($data as $key => $value) { + $this->$key = $value; + } + + $this->files = []; + $this->has_files = false; + + 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->answers = [$this->answer_1, $this->answer_2, $this->answer_3, $this->answer_4, $this->answer_5, $this->answer_6]; + } + + public function makeVerifications() + { + global $LOCAL_PATH, $team; + + $this->from_team = Team::fromTrigram($this->from); + ensure($this->from_team != null, "L'équipe indiquée n'existe pas."); + ensure($team->getProblem() == $this->from_team->getProblem(), "Les équipes ne travaillent pas sur le même problème."); + ensure(!$this->has_files || $this->no_drawing, "Vous devez confirmer ne pas avoir inclus de texte dans vos pièces jointes."); + + for ($i = 0; $i < 6; ++$i) { + if ($this->answers[$i] == "") + $this->answers[$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. 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."); + } + + public function answerQuestions() + { + global $LOCAL_PATH, $team; + + /** @var Question[] $questions */ + $questions = Question::getQuestions($this->from_team, $team); + for ($i = 0; $i < 6; ++$i) { + $question = $questions[$i]; + if ($question->getAnswer() != $this->answers[$i] && $question->getAttachedFileAnswer() != null) { + unlink("$LOCAL_PATH/files/" . $question->getAttachedFileAnswer()); + $question->setAttachedFileAnswer(null); + } + + $question->setAnswer($this->answers[$i]); + + if ($this->files[$i] != null) { + do + $file_id = genRandomPhrase(64); + while (file_exists("$LOCAL_PATH/files/$file_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."); + + $question->setAttachedFileAnswer($file_id); + } + } + } +} + +$questions = Question::getQuestionsTo($team); +$video = Video::getVideo(Reason::SOLUTION, $team); + +require_once "server_files/views/repondre_questions.php"; \ No newline at end of file diff --git a/server_files/controllers/view_file.php b/server_files/controllers/view_file.php index b19cc43..17cc264 100644 --- a/server_files/controllers/view_file.php +++ b/server_files/controllers/view_file.php @@ -36,21 +36,7 @@ else { $to = Team::fromId($question->getTo()); $mime_type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), "$LOCAL_PATH/files/$id"); $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"; - break; - case "image/png": - $name .= ".png"; - break; - case "image/jpg": - case "image/jpeg": - $name .= ".jpg"; - break; - case "application/zip": - $name .= ".zip"; - break; - } + $name .= getExtFromMimeType($mime_type); header("Content-Type: " . $mime_type); } else diff --git a/server_files/utils.php b/server_files/utils.php index e0a26f5..6363e82 100644 --- a/server_files/utils.php +++ b/server_files/utils.php @@ -29,4 +29,20 @@ function genRandomPhrase($size, $uppercase = false) } return $phrase; +} + +function getExtFromMimeType($mime_type) +{ + switch ($mime_type) { + case "application/pdf": + return ".pdf"; + case "image/png": + return ".png"; + case "image/jpg": + case "image/jpeg": + return ".jpg"; + case "application/zip": + return ".zip"; + } + return ""; } \ No newline at end of file diff --git a/server_files/views/header.php b/server_files/views/header.php index c32dfd5..b65b279 100644 --- a/server_files/views/header.php +++ b/server_files/views/header.php @@ -60,9 +60,12 @@ case Phase::PHASE1: ?>
  • Envoyer ma vidéo (phase 1)
  • + case Phase::PHASE2: ?>
  • Poser des questions (phase 2)
  • - +
  • Répondre aux questions (phase 3)
  • + diff --git a/server_files/views/repondre_questions.php b/server_files/views/repondre_questions.php new file mode 100644 index 0000000..6f69ab1 --- /dev/null +++ b/server_files/views/repondre_questions.php @@ -0,0 +1,87 @@ + + + Lien de la vidéo : getLink() ?> + +getLink()); ?> + +getFrom()); + echo "

    Questions de l'équipe " . $from->getName() . " (" . $from->getTrigram() . ") :

    \n"; + ?> +
    + + + + getQuestion() == null) + continue; + ?> + + + + + getAttachedFile() != null) { ?> + + + + + + + + + + getAttachedFileAnswer() != null) { ?> + + + + + + + + + + + + + + + + + +
    + Question : + + getQuestion() ?> +
    + Pièce jointe : + + getAttachedFile() ?>">Télécharger +
    + + + +
    + Pièce jointe : + + getAttachedFileAnswer() ?>">Télécharger +
    + + + +
    + +
    + +
    +
    + + +getQuestion() . "
    \n"; - if ($question->getAttachedFile() != null) { + if ($question->getAttachedFile() != null) echo "Pièce jointe attachée : getAttachedFile() . "\">Télécharger
    \n"; - } + if ($question->getAnswer() != null) + echo "Réponse apportée : " . $question->getAnswer() . "
    \n"; + if ($question->getAttachedFileAnswer() != null) + echo "Pièce jointe attachée pour la réponse : getAttachedFileAnswer() . "\">Télécharger
    \n"; } } }