plateforme-corres2math/server_files/controllers/repondre_questions.php

126 lines
3.7 KiB
PHP
Raw Normal View History

2019-09-21 18:39:40 +00:00
<?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 User $user
* @var Team $team
*/
$user = $_SESSION["user"];
$team = $user->getRole() == Role::PARTICIPANT ? $_SESSION["team"] : Team::fromTrigram($_GET["trigram"]);
if ($team == null)
require_once "server_files/404.php";
if ($team->getEncadrantId() != $user->getId() || $team->getValidationStatus() != ValidationStatus::VALIDATED)
require_once "server_files/403.php";
2019-09-21 18:39:40 +00:00
$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_0;
2019-09-21 18:39:40 +00:00
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 = 0; $i <= 6; ++$i) {
2019-09-21 18:39:40 +00:00
if (strlen($files["file_$i"]["name"]) > 0) {
$this->files[] = $files["file_$i"];
$this->has_files = true;
}
else
$this->files[] = null;
}
$this->answers = [$this->answer_0, $this->answer_1, $this->answer_2, $this->answer_3, $this->answer_4, $this->answer_5, $this->answer_6];
2019-09-21 18:39:40 +00:00
}
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 = 1; $i <= 6; ++$i) {
2019-09-21 18:39:40 +00:00
if ($this->answers[$i] == "")
$this->answers[$i] = null;
}
for ($i = 0; $i <= 6; ++$i) {
2019-09-21 18:39:40 +00:00
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) {
2019-09-21 18:39:40 +00:00
$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";