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.
202 lines
8.1 KiB
202 lines
8.1 KiB
<?php
|
|
namespace Gateway;
|
|
use Gateway\Connection;
|
|
use PDO;
|
|
|
|
class QuestionGateway extends Gateway
|
|
{
|
|
|
|
/**
|
|
* Creates a new question entry in the database.
|
|
*
|
|
* This method inserts a new question into the `Question` table, including the question text,
|
|
* multiple choice answers (A, B, C, D), and the correct answer.
|
|
*
|
|
* @param int $id_question The unique ID of the question.
|
|
* @param string $question The question text.
|
|
* @param string $answerA The first multiple choice answer.
|
|
* @param string $answerB The second multiple choice answer.
|
|
* @param string $answerC The third multiple choice answer.
|
|
* @param string $answerD The fourth multiple choice answer.
|
|
* @param string $cAnswer The correct answer among the options (A, B, C, D).
|
|
*
|
|
* @return bool Returns `true` if the question was successfully created, `false` otherwise.
|
|
*/
|
|
public function create(int $id_question, string $question, string $answerA, string $answerB, string $answerC, string $answerD, string $cAnswer): bool
|
|
{
|
|
// SQL query to insert a new question with its answers and correct answer
|
|
$query = "INSERT INTO Question
|
|
VALUES (:id_q, :question, :answerA, :answerB, :answerC, :answerD, :cAnswer)";
|
|
|
|
// Execute the query, binding the parameters securely to prevent SQL injection
|
|
return $this -> co -> executeQuery($query, [
|
|
'id_q' => array($id_question, PDO::PARAM_INT),
|
|
'question' => array($question, PDO::PARAM_STR),
|
|
'answerA' => array($answerA, PDO::PARAM_STR),
|
|
'answerB' => array($answerB, PDO::PARAM_STR),
|
|
'answerC' => array($answerC, PDO::PARAM_STR),
|
|
'answerD' => array($answerD, PDO::PARAM_STR),
|
|
'cAnswer' => array($cAnswer, PDO::PARAM_STR),
|
|
]);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Retrieves a question by its ID from the database.
|
|
*
|
|
* This method queries the `Question` table to find the question with the specified ID
|
|
* and returns all its details, including the question text, possible answers, and the correct answer.
|
|
*
|
|
* @param int $id The unique ID of the question to be retrieved.
|
|
*
|
|
* @return array An associative array containing the details of the question (if found).
|
|
* The keys correspond to the column names in the `Question` table.
|
|
* Returns an empty array if the question is not found.
|
|
*/
|
|
public function findById(int $id) : array
|
|
{
|
|
// SQL query to select the question with the specified ID
|
|
$query = "SELECT *
|
|
FROM Question
|
|
WHERE id_question = :id_q";
|
|
|
|
// Execute the query, binding the parameter securely to prevent SQL injection
|
|
$this -> co -> executeQuery($query, ['id_q' => array($id, PDO::PARAM_INT)]);
|
|
|
|
// Retrieve and return the results as an associative array
|
|
return $this -> co -> getResults();
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Updates the text of a question in the database.
|
|
*
|
|
* This method modifies the `question` field of a specific question in the `Question` table,
|
|
* identified by its unique ID.
|
|
*
|
|
* @param int $id_q The unique ID of the question to be updated.
|
|
* @param string $newQuestion The new text to replace the current question text.
|
|
*
|
|
* @return bool Returns `true` if the query was successfully executed, or `false` otherwise.
|
|
*/
|
|
public function updateText(int $id_q, string $newQuestion) : bool
|
|
{
|
|
// SQL query to update the question text for a specific ID
|
|
$query = "UPDATE Question
|
|
SET question = :question
|
|
WHERE id_question = :id_q";
|
|
|
|
// Execute the query with bound parameters
|
|
return $this -> co -> executeQuery($query, [
|
|
'id_q' => array($id_q, PDO::PARAM_INT), // Bind the ID of the question
|
|
'question' => array($newQuestion, PDO::PARAM_STR) // Bind the new question text
|
|
]);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Updates the answers for a specific question in the database.
|
|
*
|
|
* This method modifies the answer options (`answerA`, `answerB`, `answerC`, `answerD`)
|
|
* and the correct answer (`cAnswer`) of a specific question identified by its unique ID.
|
|
*
|
|
* @param int $id_q The unique ID of the question to be updated.
|
|
* @param string $newA The new value for the first answer option (A).
|
|
* @param string $newB The new value for the second answer option (B).
|
|
* @param string $newC The new value for the third answer option (C).
|
|
* @param string $newD The new value for the fourth answer option (D).
|
|
* @param string $newCorrect The new correct answer.
|
|
*
|
|
* @return bool Returns `true` if the query was successfully executed, or `false` otherwise.
|
|
*/
|
|
public function updateAnswers(int $id_q, string $newA, string $newB, string $newC, string $newD, string $newCorrect): bool
|
|
{
|
|
// SQL query to update the answers and the correct answer for a specific question
|
|
$query = "UPDATE Question
|
|
SET answerA = :answerA, answerB = :answerB,
|
|
answerC = :answerC, answerD = :answerD, cAnswer = :cAnswer
|
|
WHERE id_question = :id_q";
|
|
|
|
// Execute the query with bound parameters
|
|
return $this -> co -> executeQuery($query, [
|
|
'id_q' => array($id_q, PDO::PARAM_INT), // Bind the ID of the question
|
|
'answerA' => array($newA, PDO::PARAM_STR), // Bind the new answer A
|
|
'answerB' => array($newB, PDO::PARAM_STR), // Bind the new answer B
|
|
'answerC' => array($newC, PDO::PARAM_STR), // Bind the new answer C
|
|
'answerD' => array($newD, PDO::PARAM_STR), // Bind the new answer D
|
|
'cAnswer' => array($newCorrect, PDO::PARAM_STR) // Bind the new correct answer
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* Deletes a question from the database.
|
|
*
|
|
* This method removes a specific question from the `Question` table based on its unique ID.
|
|
*
|
|
* @param int $id The unique ID of the question to be deleted.
|
|
*
|
|
* @return bool Returns `true` if the query was successfully executed, or `false` otherwise.
|
|
*/
|
|
public function delete(int $id) : bool
|
|
{
|
|
// SQL query to delete a question by its ID
|
|
$query = "DELETE FROM Question
|
|
WHERE id_question = :id_q";
|
|
|
|
// Execute the query with the provided ID
|
|
return $this -> co -> executeQuery($query, [
|
|
'id_q' => array($id, PDO::PARAM_INT) // Bind the ID parameter to the query
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* Retrieves all questions from the database.
|
|
*
|
|
* This method fetches all records from the `Question` table, returning an array
|
|
* of questions with their associated details.
|
|
*
|
|
* @return array Returns an array containing all the questions. Each question is represented
|
|
* as an associative array with keys corresponding to the table columns.
|
|
*/
|
|
public function findAll() : array
|
|
{
|
|
// SQL query to select all questions
|
|
$query = "SELECT * FROM Question";
|
|
|
|
// Execute the query
|
|
$this -> co -> executeQuery($query);
|
|
|
|
// Return all results as an array
|
|
return $this -> co -> getResults();
|
|
}
|
|
|
|
|
|
/**
|
|
* Retrieves a random question from the database.
|
|
*
|
|
* This method selects one random record from the `Question` table.
|
|
*
|
|
* @return array Returns an array containing the details of a randomly selected question.
|
|
* The result is an associative array with keys corresponding to the table columns.
|
|
*/
|
|
public function findRdmQuestion() : array
|
|
{
|
|
// SQL query to fetch a random question
|
|
$query = "SELECT *
|
|
FROM Question
|
|
ORDER BY RANDOM()
|
|
LIMIT 1";
|
|
|
|
// Execute the query
|
|
$this -> co -> executeQuery($query);
|
|
|
|
// Return the randomly selected question
|
|
return $this -> co -> getResults();
|
|
}
|
|
}
|