1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2024-12-25 19:02:27 +00:00
plateforme-corres2math/server_files/controllers/repondre_questions.php
2019-09-21 20:39:40 +02:00

115 lines
3.3 KiB
PHP

<?php
if (!isset($_SESSION["user_id"]) || $_SESSION["role"] != Role::PARTICIPANT && $_SESSION["role"] != Role::ENCADRANT || Phase::getCurrentPhase() != Phase::PHASE3)
require_once "server_files/403.php";
/** @var Team $team */
$team = $_SESSION["team"];
$has_error = false;
$error_message = null;
if (isset($_POST["answer"])) {
$answer_questions = new AnswerQuestions($_POST, $_FILES);
try {
$answer_questions->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";