From 0f022322d68cf447b70bd2334b7b495a2e7d4829 Mon Sep 17 00:00:00 2001 From: tomivt Date: Wed, 23 Oct 2024 16:06:11 +0200 Subject: [PATCH] Update Question Entity, Gateway & Model + Fix issues --- src/Entity/questionEntity.php | 2 +- src/Gateway/QuestionGateway.php | 111 ++++++++++++++++++++++++++++++++ src/Model/QuestionModel.php | 91 ++++++++++++++++++++++++++ 3 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 src/Gateway/QuestionGateway.php create mode 100644 src/Model/QuestionModel.php diff --git a/src/Entity/questionEntity.php b/src/Entity/questionEntity.php index 6b3dcc9..fed1e6f 100644 --- a/src/Entity/questionEntity.php +++ b/src/Entity/questionEntity.php @@ -58,7 +58,7 @@ public function setQuestion(string $newQuestion) : void { - $this -> question = $question; + $this -> question = $newQuestion; } public function setAnswerA(string $newAnswerA) : void diff --git a/src/Gateway/QuestionGateway.php b/src/Gateway/QuestionGateway.php new file mode 100644 index 0000000..f18c545 --- /dev/null +++ b/src/Gateway/QuestionGateway.php @@ -0,0 +1,111 @@ + 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(); + } + +} \ No newline at end of file diff --git a/src/Model/QuestionModel.php b/src/Model/QuestionModel.php new file mode 100644 index 0000000..6d6b39c --- /dev/null +++ b/src/Model/QuestionModel.php @@ -0,0 +1,91 @@ + 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; + } +} \ No newline at end of file