parent
d94c88a86d
commit
0f022322d6
@ -0,0 +1,111 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Gateway;
|
||||||
|
use Connection;
|
||||||
|
use PDO;
|
||||||
|
use QuestionEntity;
|
||||||
|
|
||||||
|
require_once("../Gateway/Connection.php");
|
||||||
|
|
||||||
|
class QuestionGateway
|
||||||
|
{
|
||||||
|
private Connection $co;
|
||||||
|
|
||||||
|
public function __construct(Connection $co)
|
||||||
|
{
|
||||||
|
$this -> co = $co;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create(int $id_question, string $question, string $answerA, string $answerB, string $answerC, string $answerD, string $cAnswer): bool
|
||||||
|
{
|
||||||
|
$query = "
|
||||||
|
INSERT INTO Question
|
||||||
|
VALUES (:id_q, :question, :answerA, :answerB, :answerC, :answerD, :cAnswer)
|
||||||
|
";
|
||||||
|
|
||||||
|
return $this -> co -> executeQuery($query, [
|
||||||
|
array('id_q' => $id_question, PDO::PARAM_INT),
|
||||||
|
array('question' => $question, PDO::PARAM_STR),
|
||||||
|
array('answerA' => $answerA, PDO::PARAM_STR),
|
||||||
|
array('answerB' => $answerB, PDO::PARAM_STR),
|
||||||
|
array('answerC' => $answerC, PDO::PARAM_STR),
|
||||||
|
array('answerD' => $answerD, PDO::PARAM_STR),
|
||||||
|
array('cAnswer' => $cAnswer, PDO::PARAM_STR),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function findById(int $id) : array
|
||||||
|
{
|
||||||
|
$query = "SELECT * FROM Question WHERE id_question = :id_q";
|
||||||
|
$this -> co -> executeQuery($query, ['id_q' => array($id, PDO::PARAM_INT)]);
|
||||||
|
return $this -> co -> getResults();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function updateText(int $id_q, string $newQuestion) : bool
|
||||||
|
{
|
||||||
|
$query = "
|
||||||
|
UPDATE Question
|
||||||
|
SET question = :question
|
||||||
|
WHERE id_question = :id_q
|
||||||
|
";
|
||||||
|
|
||||||
|
return $this -> co -> executeQuery($query, [
|
||||||
|
'id_q' => array($id_q, PDO::PARAM_INT),
|
||||||
|
'question' => array($newQuestion, PDO::PARAM_STR)
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateAnswers(int $id_q, string $newA, string $newB, string $newC, string $newD, string $newCorrect): bool
|
||||||
|
{
|
||||||
|
$query = "
|
||||||
|
UPDATE Question
|
||||||
|
SET answerA = :answerA, answerB = :answerB,
|
||||||
|
answerC = :answerC, answerD = :answerD, cAnswer = :cAnswer
|
||||||
|
WHERE id_question = :id_q
|
||||||
|
";
|
||||||
|
|
||||||
|
return $this -> co -> executeQuery($query, [
|
||||||
|
'id_q' => array($id_q, PDO::PARAM_INT),
|
||||||
|
'answerA' => array($newA, PDO::PARAM_STR),
|
||||||
|
'answerB' => array($newB, PDO::PARAM_STR),
|
||||||
|
'answerC' => array($newC, PDO::PARAM_STR),
|
||||||
|
'answerD' => array($newD, PDO::PARAM_STR),
|
||||||
|
'cAnswer' => array($newCorrect, PDO::PARAM_STR)
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a question from the database by its ID
|
||||||
|
*
|
||||||
|
* @param int $id The ID of the question
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function delete(int $id) : bool
|
||||||
|
{
|
||||||
|
$query = "DELETE FROM Question WHERE id_question = :id_q";
|
||||||
|
|
||||||
|
return $this -> co -> executeQuery($query, ['id_q' => array($id, PDO::PARAM_INT)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves all quizzes from the database
|
||||||
|
*
|
||||||
|
* @return QuestionEntity[] An array of `Question` objects
|
||||||
|
*/
|
||||||
|
public function findAll() : array
|
||||||
|
{
|
||||||
|
$query = "SELECT * FROM Question";
|
||||||
|
$this -> co -> executeQuery($query);
|
||||||
|
return $this -> co -> getResults();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findRdmQuestion() : array
|
||||||
|
{
|
||||||
|
$query = "SELECT * FROM Question ORDER BY RANDOM() LIMIT 1";
|
||||||
|
$this -> co -> executeQuery($query);
|
||||||
|
return $this -> co -> getResults();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Model;
|
||||||
|
use Gateway\QuestionGateway;
|
||||||
|
use QuestionEntity;
|
||||||
|
|
||||||
|
require_once("../Entity/questionEntity.php");
|
||||||
|
require_once("../Gateway/QuestionGateway.php");
|
||||||
|
|
||||||
|
class QuestionModel
|
||||||
|
{
|
||||||
|
private QuestionGateway $gateway;
|
||||||
|
|
||||||
|
public function __construct(QuestionGateway $gw)
|
||||||
|
{
|
||||||
|
$this -> gateway = $gw;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createQuestion(int $id_question, string $question, string $answerA, string $answerB, string $answerC, string $answerD, string $cAnswer): bool
|
||||||
|
{
|
||||||
|
return $this -> gateway -> create($id_question, $question, $answerA, $answerB, $answerC, $answerD, $cAnswer);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getQuestion(int $id_question) : ?QuestionEntity
|
||||||
|
{
|
||||||
|
$q = $this -> gateway -> findById($id_question);
|
||||||
|
if ($q)
|
||||||
|
return new QuestionEntity(
|
||||||
|
$q['id_question'],
|
||||||
|
$q['question'],
|
||||||
|
$q['answerA'],
|
||||||
|
$q['answerB'],
|
||||||
|
$q['answerC'],
|
||||||
|
$q['answerD'],
|
||||||
|
$q['cAnswer']
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateTextQuestion(int $id_question, string $question) : bool
|
||||||
|
{
|
||||||
|
return $this -> gateway -> updateText($id_question, $question);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateAnswersQuestion(int $id_question, string $answerA, string $answerB, string $answerC, string $answerD, string $cAnswer): bool
|
||||||
|
{
|
||||||
|
return $this -> gateway -> updateAnswers($id_question, $answerA, $answerB, $answerC, $answerD, $cAnswer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteQuestion(int $id_question) : bool
|
||||||
|
{
|
||||||
|
return $this -> gateway -> delete($id_question);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAllQuestions() : array
|
||||||
|
{
|
||||||
|
$q = $this -> gateway -> findAll();
|
||||||
|
|
||||||
|
$questions = [];
|
||||||
|
|
||||||
|
foreach ($q as $question) {
|
||||||
|
$questions[] = new QuestionEntity(
|
||||||
|
$question['id_question'],
|
||||||
|
$question['question'],
|
||||||
|
$question['answerA'],
|
||||||
|
$question['answerB'],
|
||||||
|
$question['answerC'],
|
||||||
|
$question['answerD'],
|
||||||
|
$question['cAnswer']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return $questions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRdmQuestion() : ?QuestionEntity
|
||||||
|
{
|
||||||
|
$q = $this -> gateway -> findRdmQuestion();
|
||||||
|
if ($q)
|
||||||
|
return new QuestionEntity(
|
||||||
|
$q['id_question'],
|
||||||
|
$q['question'],
|
||||||
|
$q['answerA'],
|
||||||
|
$q['answerB'],
|
||||||
|
$q['answerC'],
|
||||||
|
$q['answerD'],
|
||||||
|
$q['cAnswer']
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue