1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2025-01-06 01:02:20 +00:00

Phase 2 : pièces jointes dans les questions

This commit is contained in:
galaxyoyo 2019-09-21 13:37:48 +02:00
parent 80eeb71262
commit 41c4734624
4 changed files with 128 additions and 20 deletions

View File

@ -8,6 +8,7 @@ class Question
private $to;
private $problem;
private $question;
private $attached_file;
private function __construct()
{
@ -29,6 +30,22 @@ class Question
return $question;
}
public static function fromAttachedFile($attached_file)
{
global $DB;
$req = $DB->prepare("SELECT * FROM `questions` WHERE `attached_file` = ?;");
$req->execute([htmlspecialchars($attached_file)]);
$data = $req->fetch();
if ($data === false)
return null;
$question = new Question();
$question->fill($data);
return $question;
}
public function fill($data)
{
foreach ($data as $key => $value)
@ -88,4 +105,9 @@ class Question
{
return $this->question;
}
public function getAttachedFile()
{
return $this->attached_file;
}
}

View File

@ -13,7 +13,7 @@ $has_error = false;
$error_message = null;
if (isset($_POST["give_questions"])) {
$give_questions = new GiveQuestions($_POST);
$give_questions = new GiveQuestions($_POST, $_FILES);
try {
$give_questions->makeVerifications();
$give_questions->giveQuestions();
@ -36,38 +36,71 @@ class GiveQuestions
private $question_4;
private $question_5;
private $question_6;
private $no_drawing;
private $files;
public function __construct($data)
public function __construct($data, $files)
{
foreach ($data as $key => $value) {
$this->$key = $value;
}
$this->files = [];
for ($i = 1; $i <= 6; ++$i)
$this->files[] = strlen($files["file_$i"]["name"]) > 0 ? $files["file_$i"] : null;
}
public function makeVerifications()
{
global $team;
global $LOCAL_PATH, $team;
$this->to_team = Team::fromTrigram($this->to);
ensure($this->to_team, "L'équipe indiquée n'existe pas.");
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 == "",
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.");
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(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 giveQuestions()
{
global $DB, $team;
global $DB, $LOCAL_PATH, $team;
$attached_file = [];
for ($i = 0; $i < 5; ++$i)
{
if ($this->files[$i] == null)
$attached_file[] = null;
else {
do
$id = genRandomPhrase(64);
while (file_exists("$LOCAL_PATH/files/$id"));
if (!rename($this->files[$i]["tmp_name"], "$LOCAL_PATH/files/$id"))
throw new AssertionError("Une erreur est survenue lors de l'envoi du fichier.");
$attached_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`) VALUES "
. "(?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?);");
$req->execute([$team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_1,
$team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_2,
$team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_3,
$team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_4,
$team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_5,
$team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_6]);
$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

@ -25,10 +25,38 @@ if ($file !== null) {
$surname = $user->getSurname();
$first_name = $user->getFirstName();
$name = "Autorisation de droit à l'image de $first_name $surname.pdf";
} else
require_once "server_files/404.php";
header("Content-Type: application/pdf");
header("Content-Type: application/pdf");
}
else {
$question = Question::fromAttachedFile($id);
if ($question != null)
{
$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();
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;
}
header("Content-Type: " . $mime_type);
}
else
require_once "server_files/404.php";
}
header("Content-Disposition: inline; filename=\"$name\"");
readfile("$LOCAL_PATH/files/$id");

View File

@ -8,21 +8,46 @@ for ($i = 0; $i < 2; ++$i) {
Lien de la vidéo : <a href="<?= $video->getLink() ?>"><?= $video->getLink() ?></a><br/>
<?php displayVideo($video->getLink()) ?>
<br/>
<form method="POST">
<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="to" value="<?= $receiver->getTrigram() ?>"/>
<table style="width: 100%;">
<tbody>
<?php
for ($j = 0; $j < 6; ++$j) { ?>
for ($j = 0; $j < 6; ++$j) {
$question = $questions[$i][$j]; ?>
<tr>
<td style="width: 30%;">
<label for="question_<?= $j + 1 ?>">Question <?= $j + 1 ?> :</label>
<label for="question_<?= $j + 1 ?>">Question <?= $j + 1 ?> <?= $j >= 3 ? "(<em>facultatif</em>)" : "" ?> :</label>
</td>
<td style="width: 70%;">
<textarea style="width: 100%;" id="question_<?= $j + 1 ?>" name="question_<?= $j + 1 ?>"><?= $questions[$i][$j]->getQuestion() ?></textarea>
<textarea style="width: 100%;" id="question_<?= $j + 1 ?>" name="question_<?= $j + 1 ?>"><?= $question->getQuestion() ?></textarea>
</td>
</tr>
<?php
if ($question->getAttachedFile() != null) { ?>
<tr>
<td>
Pièce jointe :
</td>
<td>
<a href="<?= $URL_BASE . "/file/" . $question->getAttachedFile() ?>">Télécharger</a>
</td>
</tr>
<?php } ?>
<tr>
<td>
<label for="file_<?= $j + 1 ?>">Ajouter une pièce jointe (<em>facultatif</em>) :</label>
</td>
<td>
<input style="width: 100%;" type="file" name="file_<?= $j + 1 ?>" id="file_<?= $j + 1 ?>" />
</td>
</tr>
<?php } ?>
<tr>
<td style="text-align: center" colspan="2">
<input type="checkbox" name="no_drawing" id="no_drawing" /> <label for="no_drawing">Je confirme que mes pièces jointes ne contiennent pas de texte.</label>
</td>
</tr>
<tr>
<td colspan="2">
<input style="width: 100%;" type="submit" name="give_questions" value="Poser les questions" />