You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
WF-Website/src/Gateway/QuestionGateway.php

102 lines
3.2 KiB

<?php
namespace Gateway;
use Gateway\Connection;
use PDO;
class QuestionGateway extends Gateway
{
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();
}
}