From 9b556e9fde7e1d1d48d3d75aca748753ed6074d6 Mon Sep 17 00:00:00 2001 From: tomivt Date: Tue, 15 Oct 2024 12:14:38 +0200 Subject: [PATCH] Add questionGateway.php --- src/questionGateway.php | 152 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 src/questionGateway.php diff --git a/src/questionGateway.php b/src/questionGateway.php new file mode 100644 index 0000000..77d964e --- /dev/null +++ b/src/questionGateway.php @@ -0,0 +1,152 @@ + 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; + } + +} \ No newline at end of file