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/gateway/questionGateway.php

152 lines
4.4 KiB

<?php
require_once("../script/Connection.php");
require_once("questionEntity.php");
class QuestionGateway
{
private Connection $co;
public function __construct(Connection $co)
{
$this -> co = $co;
}
/**
* Inserts a new question into the database
*
* @param QuestionEntity $q The `Question` object to insert
* @return bool True if the question was successfully inserted, false otherwise
*/
public function create(QuestionEntity $q) : bool
{
$query = "
INSERT INTO Question
VALUES(:id_q, :question, :answerA, :answerB, :answerC, :answerD, :cAnswer)
";
$stmt = $this -> co -> prepare($query);
return $stmt -> execute([
':id_q' => $q -> getIdquestion(),
':question' => $q -> getQuestion(),
':answerA' => $q -> getAnswerA(),
':answerB' => $q -> getAnswerB(),
':answerC' => $q -> getAnswerC(),
':answerD' => $q -> getAnswerD(),
':cAnswer' => $q -> getCAnswer()
]);
}
/**
* Retrieves a question from the database by its ID
*
* @param int $id The ID of the question to retrieve
* @return QuestionEntity|null The `Question` object if found, null otherwise
*/
public function findById(int $id) : ?QuestionEntity
{
$query = "SELECT * FROM Question WHERE id_question = :id_q";
$stmt = $this -> co -> prepare($query);
$stmt -> execute([':id' => $id]);
$res = $stmt -> fetch(PDO::FETCH_ASSOC);
if ($res)
return new QuestionEntity(
$res['id_q'],
$res['question'],
$res['answerA'],
$res['answerB'],
$res['answerC'],
$res['answerD'],
$res['cAnswer'],
);
return null;
}
/**
* Updates the text of and an existing question in the database
*
* @param QuestionEntity $q The `Question` object with updated information
* @return bool True if the text of the question was successfully updated, false otherwise
*/
public function updateText(QuestionEntity $q) : bool
{
$query = "
UPDATE Question
SET question = :question
WHERE id_question = :id_q
";
$stmt = $this -> co -> prepare($query);
return $stmt -> execute([':question' => $q -> getQuestion()]);
}
/**
* Updates the answers of an existing question in the database
*
* @param QuestionEntity $q The `Question` object with updated information
* @return bool True if the answers of the question was successfully updated, false otherwise
*/
public function updateAnswers(QuestionEntity $q) : bool
{
$query = "
UPDATE Question
SET answerA = :answerA, answerB = :answerB,
answerC = :answerC, answerD = :answerD, cAnswer = :cAnswer
WHERE id_question = :id_q
";
$stmt = $this -> co -> prepare($query);
return $stmt -> execute([
':id_q' => $q -> getIdQuestion(),
':answerA' => $q -> getAnswerA(),
':answerB' => $q -> getAnswerB(),
':answerC' => $q -> getAnswerC(),
':answerD' => $q -> getAnswerD(),
':cAnswer' => $q -> getCAnswer(),
]);
}
/**
* 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";
$stmt = $this -> co -> prepare($query);
return $stmt -> execute([':id_q' => $id]);
}
/**
* Retrieves all quizzes from the database
*
* @return QuestionEntity[] An array of `Question` objects
*/
public function findAll() : array
{
$query = "SELECT * FROM Question";
$stmt = $this -> co -> prepare($query);
$stmt->execute();
$res = $stmt -> fetchAll(PDO::FETCH_ASSOC);
$questions = [];
foreach ($res as $q)
$questions[] = new QuestionEntity(
$q['id_question'],
$q['question'],
$q['answerA'],
$q['answerB'],
$q['answerC'],
$q['answerD'],
$q['cAnswer']
);
return $questions;
}
}