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

Phase 3 : réponse aux questions

This commit is contained in:
galaxyoyo 2019-09-21 20:39:40 +02:00
parent ac48412f7e
commit 5355ba87b1
9 changed files with 261 additions and 22 deletions

View File

@ -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

View File

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

View File

@ -1,6 +1,6 @@
<?php
if (!isset($_SESSION["user_id"]) || $_SESSION["role"] != Role::PARTICIPANT && $_SESSION["role"] != Role::ENCADRANT)
if (!isset($_SESSION["user_id"]) || $_SESSION["role"] != Role::PARTICIPANT && $_SESSION["role"] != Role::ENCADRANT || Phase::getCurrentPhase() != Phase::PHASE2)
require_once "server_files/403.php";
/** @var Team $team */

View File

@ -0,0 +1,115 @@
<?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";

View File

@ -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

View File

@ -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 "";
}

View File

@ -60,9 +60,12 @@
case Phase::PHASE1: ?>
<li><a href="<?= $URL_BASE ?>/envoyer-video-1">Envoyer ma vidéo (phase 1)</a></li>
<?php break;
case Phase::PHASE2: ?>
case Phase::PHASE2: ?>
<li><a href="<?= $URL_BASE ?>/poser-questions-2">Poser des questions (phase 2)</a></li>
<?php break;
<?php break;
case Phase::PHASE3: ?>
<li><a href="<?= $URL_BASE ?>/repondre-questions-3">Répondre aux questions (phase 3)</a></li>
<?php break;
}
} ?>
<?php } ?>

View File

@ -0,0 +1,87 @@
<?php
require_once "header.php";
?>
Lien de la vidéo : <a href="<?= $video->getLink() ?>"><?= $video->getLink() ?></a>
<?php displayVideo($video->getLink()); ?>
<?php
foreach ($questions as $questions_team) {
$from = Team::fromId($questions_team[0]->getFrom());
echo "<h4>Questions de l'équipe " . $from->getName() . " (" . $from->getTrigram() . ") :</h4>\n";
?>
<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="from" value="<?= $from->getTrigram() ?>" />
<table style="width: 100%;">
<tbody>
<?php
for ($i = 0; $i < sizeof($questions_team); ++$i) {
$question = $questions_team[$i];
if ($question->getQuestion() == null)
continue;
?>
<tr>
<td>
Question <?= $i + 1 ?> :
</td>
<td>
<?= $question->getQuestion() ?>
</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="answer_<?= $i + 1 ?>">Réponse :</label>
</td>
<td>
<textarea style="width: 100%;" id="answer_<?= $i + 1 ?>" name="answer_<?= $i + 1 ?>"><?= $question->getAnswer() ?></textarea>
</td>
</tr>
<?php
if ($question->getAttachedFileAnswer() != null) { ?>
<tr>
<td>
Pièce jointe :
</td>
<td>
<a href="<?= $URL_BASE . "/file/" . $question->getAttachedFileAnswer() ?>">Télécharger</a>
</td>
</tr>
<?php } ?>
<tr>
<td>
<label for="file_<?= $i + 1 ?>">Ajouter une pièce jointe (<em>facultatif</em>) :</label>
</td>
<td>
<input style="width: 100%;" type="file" name="file_<?= $i + 1 ?>" id="file_<?= $i + 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="answer" value="Répondre aux questions posées" />
</td>
</tr>
</tbody>
</table>
</form>
<?php } ?>
<?php
require_once "footer.php";

View File

@ -42,9 +42,12 @@ for ($problem = 1; $problem <= 4; ++$problem) {
continue;
echo "Question " . ($i + 1) . " : " . $question->getQuestion() . "<br />\n";
if ($question->getAttachedFile() != null) {
if ($question->getAttachedFile() != null)
echo "<em>Pièce jointe attachée :</em> <a href=\"$URL_BASE/file/" . $question->getAttachedFile() . "\">Télécharger</a><br />\n";
}
if ($question->getAnswer() != null)
echo "Réponse apportée : " . $question->getAnswer() . "<br />\n";
if ($question->getAttachedFileAnswer() != null)
echo "<em>Pièce jointe attachée pour la réponse :</em> <a href=\"$URL_BASE/file/" . $question->getAttachedFileAnswer() . "\">Télécharger</a><br />\n";
}
}
}