1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2024-12-26 19:02:21 +00:00
plateforme-corres2math/server_files/classes/Question.php

183 lines
3.9 KiB
PHP
Raw Normal View History

2019-09-20 12:57:06 +00:00
<?php
class Question
{
const DEFAULT_QUESTIONS = ["Comment avez-vous obtenu vos résultats ?",
"Peut-on rendre votre algorithme plus efficace ? Pourquoi ?",
"Comment élargir vos travaux ?",
null,
null,
null];
2019-09-20 12:57:06 +00:00
private $id;
private $from;
private $to;
private $problem;
private $number;
2019-09-20 12:57:06 +00:00
private $question;
private $attached_file;
2019-09-21 18:39:40 +00:00
private $answer;
private $attached_file_answer;
2019-09-20 12:57:06 +00:00
private function __construct()
{
}
public static function fromId($id)
{
global $DB;
$req = $DB->prepare("SELECT * FROM `questions` WHERE `id` = ?;");
$req->execute(htmlspecialchars($id));
$data = $req->fetch();
if ($data === false)
return null;
$question = new Question();
$question->fill($data);
return $question;
}
public static function fromAttachedFile($attached_file)
{
global $DB;
2019-09-21 18:39:40 +00:00
$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)
return null;
$question = new Question();
$question->fill($data);
return $question;
}
2019-09-20 12:57:06 +00:00
public function fill($data)
{
foreach ($data as $key => $value)
$this->$key = $value;
}
public static function getQuestions(Team $from, Team $to)
{
global $DB;
ensure($from->getProblem() == $to->getProblem(), "Les deux équipes doivent travailler sur le même problème.");
2019-09-23 22:12:33 +00:00
$req = $DB->prepare("SELECT * FROM `questions` WHERE `from` = ? AND `to` = ? ORDER BY `from`;");
2019-09-20 12:57:06 +00:00
$req->execute([$from->getId(), $to->getId()]);
$questions = [];
while (($data = $req->fetch()) !== false) {
$question = new Question();
$question->fill($data);
$questions[] = $question;
}
if (sizeof($questions) == 0) {
2019-09-27 17:18:54 +00:00
for ($i = 0; $i < 6; ++$i) {
$req = $DB->prepare("INSERT INTO `questions`(`from`, `to`, `problem`, `number`, `question`) VALUES (?, ?, ?, ?, ?);");
$req->execute([$from->getId(), $to->getId(), $from->getProblem(), $i, self::DEFAULT_QUESTIONS[$i]]);
2019-09-20 12:57:06 +00:00
}
return self::getQuestions($from, $to);
}
return $questions;
}
2019-09-21 12:51:58 +00:00
public static function getQuestionsTo(Team $to)
{
global $DB, $YEAR;
$id = $to->getId();
$req = $DB->query("SELECT `id` from `teams` WHERE (`video_team1` = $id OR `video_team2` = $id) AND `year` = $YEAR;");
$questions = [];
while (($data = $req->fetch()) !== false)
$questions[] = self::getQuestions(Team::fromId($data["id"]), $to);
return $questions;
}
2019-09-20 12:57:06 +00:00
public function getId()
{
return $this->id;
}
public function getFrom()
{
return $this->from;
}
public function getTo()
{
return $this->to;
}
public function getProblem()
{
return $this->problem;
}
public function getNumber()
{
return $this->number;
}
2019-09-20 12:57:06 +00:00
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]);
}
2019-09-21 18:39:40 +00:00
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]);
}
2019-09-20 12:57:06 +00:00
}