From fa28d2a335b11265cc3302f2eff6a652d8d7eda2 Mon Sep 17 00:00:00 2001 From: tomivt Date: Tue, 22 Oct 2024 11:54:01 +0200 Subject: [PATCH] Add Quiz Entity, Gateway & Model --- src/questionEntity.php | 27 ++++++----- src/questionGateway.php | 2 +- src/questionModel.php | 4 +- src/quizEntity.php | 43 +++++++++++++++++ src/quizGateway.php | 100 ++++++++++++++++++++++++++++++++++++++++ src/quizModel.php | 60 ++++++++++++++++++++++++ 6 files changed, 218 insertions(+), 18 deletions(-) create mode 100644 src/quizEntity.php create mode 100644 src/quizGateway.php create mode 100644 src/quizModel.php diff --git a/src/questionEntity.php b/src/questionEntity.php index 6b3dcc9..76648b8 100644 --- a/src/questionEntity.php +++ b/src/questionEntity.php @@ -1,7 +1,6 @@ cAnswer; } - public function setQuestion(string $newQuestion) : void + public function setQuestion(string $new_question) : void { - $this -> question = $question; + $this -> question = $new_question; } - public function setAnswerA(string $newAnswerA) : void + public function setAnswerA(string $new_answera) : void { - $this -> answerA = $newAnswerA; + $this -> answerA = $new_answera; } - public function setAnswerB(string $newAnswerB) : void + public function setAnswerB(string $new_answerb) : void { - $this -> answerB = $newAnswerB; + $this -> answerB = $new_answerb; } - public function setAnswerC(string $newAnswerC) : void + public function setAnswerC(string $new_answerc) : void { - $this -> answerC = $newAnswerC; + $this -> answerC = $new_answerc; } - public function setAnswerD(string $newAnswerD) : void + public function setAnswerD(string $new_answerd) : void { - $this -> answerD = $newAnswerD; + $this -> answerD = $new_answerd; } - public function setCAnswer(string $newCAnswer) : void + public function setCAnswer(string $new_canswer) : void { - $this -> cAnswer = $newCAnswer; + $this -> cAnswer = $new_canswer; } } \ No newline at end of file diff --git a/src/questionGateway.php b/src/questionGateway.php index c0ff31b..f94dbe2 100644 --- a/src/questionGateway.php +++ b/src/questionGateway.php @@ -22,7 +22,7 @@ class QuestionGateway { $query = " INSERT INTO Question - VALUES(:id_q, :question, :answerA, :answerB, :answerC, :answerD, :cAnswer) + VALUES (:id_q, :question, :answerA, :answerB, :answerC, :answerD, :cAnswer) "; return $this -> co -> executeQuery($query, [ diff --git a/src/questionModel.php b/src/questionModel.php index d0dc225..cef27e1 100644 --- a/src/questionModel.php +++ b/src/questionModel.php @@ -3,8 +3,7 @@ require_once("questionEntity.php"); require_once("questionGateway.php"); -class questionModel -{ +class QuestionModel { private QuestionGateway $gateway; public function __construct(QuestionGateway $gateway) @@ -32,7 +31,6 @@ class questionModel $q -> setQuestion($question); return $this -> gateway -> updateText($q); } - return false; } diff --git a/src/quizEntity.php b/src/quizEntity.php new file mode 100644 index 0000000..170d868 --- /dev/null +++ b/src/quizEntity.php @@ -0,0 +1,43 @@ + id_quiz = $id_quiz; + $this -> level = $level; + $this -> timer = $timer; + } + + public function getIdQuiz(): int + { + return $this -> id_quiz; + } + + public function getLevel(): int + { + return $this -> level; + } + + public function getTimer(): int + { + return $this -> timer; + } + + public function setLevel(int $new_level): void + { + $this -> level = $new_level; + } + + public function setTimer(int $new_timer): void + { + $this -> timer = $new_timer; + } + +} \ No newline at end of file diff --git a/src/quizGateway.php b/src/quizGateway.php new file mode 100644 index 0000000..2238635 --- /dev/null +++ b/src/quizGateway.php @@ -0,0 +1,100 @@ + co = $co; + } + + public function create(QuizEntity $q) : bool + { + $query = " + INSERT INTO Quiz + VALUES (:id_quiz, :level, :timer) + "; + + return $this -> co -> executeQuery($query, [ + ':id_quiz' => array($q -> getIdQuiz(), PDO::PARAM_INT), + ':level' => array($q -> getLevel(), PDO::PARAM_INT), + ':timer' => array($q -> getTimer(), PDO::PARAM_INT) + ]); + } + + public function findQuizById(int $id) : ?QuizEntity + { + $query = "SELECT * FROM Quiz WHERE id_quiz = :id_q"; + + $this->co->executeQuery($query, [':id_q' => $id]); + + $res = $this->co->getResults(); + + if ($res) + return new QuizEntity( + $res['id_quiz'], + $res['level'], + $res['timer'] + ); + return null; + } + + public function updateLevel(QuizEntity $q) : bool + { + $query = " + UPDATE Quiz + SET level = :level + WHERE id_quiz = :id_q + "; + + return $this -> co -> executeQuery($query, [ + ':id_q' => array($q -> getIdQuiz(), PDO::PARAM_INT), + ':level' => array($q -> getLevel(), PDO::PARAM_INT), + ]); + } + + public function updateTimer(QuizEntity $q) : bool + { + $query = " + UPDATE Quiz + SET timer = :timer + WHERE id_quiz = :id_q + "; + + return $this -> co -> executeQuery($query, [ + ':id_q' => array($q -> getIdQuiz(), PDO::PARAM_INT), + ':timer' => array($q -> getTimer(), PDO::PARAM_INT), + ]); + } + + public function delete(int $id) : bool + { + $query = "DELETE FROM Quiz WHERE id_quiz = :id_q"; + + return $this -> co -> executeQuery($query, [':id_q' => $id]); + } + + public function findAll() : array + { + $query = "SELECT * FROM Quiz"; + + $this -> co -> executeQuery($query); + + $res = $this -> co -> getResults(); + + $quiz = []; + + foreach ($res as $q) + $quiz[] = new QuizEntity( + $q['id_quiz'], + $q['level'], + $q['timer'] + ); + + return $quiz; + } + +} \ No newline at end of file diff --git a/src/quizModel.php b/src/quizModel.php new file mode 100644 index 0000000..421724e --- /dev/null +++ b/src/quizModel.php @@ -0,0 +1,60 @@ + gateway = $gateway; + } + + public function createQuiz(int $id_quiz, int $level, int $timer) : bool + { + $q = new QuizEntity($id_quiz, $level, $timer); + return $this -> gateway -> create($q); + } + + public function getQuiz(int $id_quiz) : ?QuizEntity + { + return $this -> gateway -> findQuizById($id_quiz); + } + + public function updateLevelQuiz(int $id_quiz, int $level) : bool + { + $q = $this -> gateway -> findQuizById($id_quiz); + + if ($q) + { + $q -> setLevel($level); + return $this -> gateway -> updateLevel($q); + } + return false; + } + + public function updateTimerQuiz(int $id_quiz, int $timer) : bool + { + $q = $this -> gateway -> findQuizById($id_quiz); + + if ($q) + { + $q -> setTimer($timer); + return $this -> gateway -> updateTimer($q); + } + return false; + } + + public function deleteQuiz(int $id_quiz) : bool + { + return $this -> gateway -> delete($id_quiz); + } + + public function getAllQuiz() : array + { + return $this -> gateway -> findAll(); + } + +} \ No newline at end of file