1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2025-01-05 23:42:21 +00:00

Phase 2 : amélioration du dépôt des questions

This commit is contained in:
galaxyoyo 2019-09-21 14:31:31 +02:00
parent 41c4734624
commit 81acf09fbf
3 changed files with 60 additions and 27 deletions

View File

@ -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]);
}
}

View File

@ -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]]);
}
}

View File

@ -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 " . $question->getNumber() . " de l'équipe " . $from->getTrigram() . " pour l'équipe " . $from->getTrigram() . " (problème " . $question->getProblem() . ")";
switch ($mime_type) {
case "application/pdf":
$name .= "pdf";