Add Quiz Entity, Gateway & Model

pull/19/head
tomivt 6 months ago
parent a74a9faa72
commit fa28d2a335

@ -1,7 +1,6 @@
<?php
class QuestionEntity
{
class QuestionEntity {
private int $id_question;
private string $question;
private string $answerA;
@ -56,33 +55,33 @@
return $this -> 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;
}
}

@ -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, [

@ -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;
}

@ -0,0 +1,43 @@
<?php
class QuizEntity {
private int $id_quiz;
private int $level;
private int $timer;
public function __construct(int $id_quiz, int $level, int $timer)
{
$this -> 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;
}
}

@ -0,0 +1,100 @@
<?php
require_once("../public/script/Connection.php");
require_once("quizEntity.php");
class QuizGateway {
private Connection $co;
public function __construct(Connection $co)
{
$this -> 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;
}
}

@ -0,0 +1,60 @@
<?php
require_once("quizEntity.php");
require_once("quizGateway.php");
class QuizModel {
private QuizGateway $gateway;
public function __construct(QuizGateway $gateway)
{
$this -> 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();
}
}
Loading…
Cancel
Save