parent
0f022322d6
commit
0188e2b3d1
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Entity;
|
||||
class QuizEntity
|
||||
{
|
||||
|
||||
private int $id_quiz;
|
||||
|
||||
private int $nb_questions;
|
||||
|
||||
public function __construct(int $id_quiz, int $nb_questions)
|
||||
{
|
||||
$this -> id_quiz = $id_quiz;
|
||||
$this -> nb_questions = $nb_questions;
|
||||
}
|
||||
|
||||
public function getIdQuiz(): int
|
||||
{
|
||||
return $this -> id_quiz;
|
||||
}
|
||||
|
||||
public function getNbQuestions(): int
|
||||
{
|
||||
return $this -> nb_questions;
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Gateway;
|
||||
use Connection;
|
||||
use PDO;
|
||||
|
||||
require_once("../Entity/QuizEntity.php");
|
||||
|
||||
class QuizGateway
|
||||
{
|
||||
private Connection $co;
|
||||
|
||||
public function __construct(Connection $co)
|
||||
{
|
||||
$this -> co = $co;
|
||||
}
|
||||
|
||||
public function create(int $id_quiz, int $nb_questions) : bool
|
||||
{
|
||||
$query = "
|
||||
INSERT INTO Quiz
|
||||
VALUES (:id_q, :nb_q)
|
||||
";
|
||||
|
||||
return $this -> co -> executeQuery($query, [
|
||||
':id_q' => array($id_quiz, PDO::PARAM_INT),
|
||||
':nb_q' => array($nb_questions, PDO::PARAM_INT)
|
||||
]);
|
||||
}
|
||||
|
||||
public function findQuizById(int $id) : array
|
||||
{
|
||||
$query = "SELECT * FROM Quiz WHERE id_quiz = :id_q";
|
||||
$this -> co -> executeQuery($query, [':id_q' => array($id, PDO::PARAM_INT)]);
|
||||
return $this -> co -> getResults();
|
||||
}
|
||||
|
||||
public function delete(int $id) : bool
|
||||
{
|
||||
$query = "DELETE FROM Quiz WHERE id_quiz = :id_q";
|
||||
return $this -> co -> executeQuery($query, [':id_q' => array($id, PDO::PARAM_INT)]);
|
||||
}
|
||||
|
||||
public function findAll() : array
|
||||
{
|
||||
$query = "SELECT * FROM Quiz";
|
||||
$this -> co -> executeQuery($query);
|
||||
return $this -> co -> getResults();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Model;
|
||||
|
||||
use Entity\QuizEntity;
|
||||
use Gateway\QuizGateway;
|
||||
|
||||
class QuizModel {
|
||||
|
||||
private QuizGateway $gateway;
|
||||
|
||||
public function __construct(QuizGateway $gw)
|
||||
{
|
||||
$this -> gateway = $gw;
|
||||
}
|
||||
|
||||
public function createQuiz(int $id_quiz, int $nb_questions) : bool
|
||||
{
|
||||
return $this -> gateway -> create($id_quiz, $nb_questions);
|
||||
}
|
||||
|
||||
public function getQuiz(int $id_quiz): ?QuizEntity
|
||||
{
|
||||
$q = $this -> gateway -> findQuizById($id_quiz);
|
||||
if ($q) {
|
||||
return new QuizEntity(
|
||||
$q['id_quiz'],
|
||||
$q['nb_questions'],
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function deleteQuiz(int $id_quiz) : bool
|
||||
{
|
||||
return $this -> gateway -> delete($id_quiz);
|
||||
}
|
||||
|
||||
public function getAllQuiz() : array
|
||||
{
|
||||
$q = $this -> gateway -> findAll();
|
||||
|
||||
$quizzes = [];
|
||||
|
||||
foreach ($q as $quiz) {
|
||||
$quizzes[] = new QuizEntity(
|
||||
$quiz['id_quiz'],
|
||||
$quiz['nb_questions']
|
||||
);
|
||||
}
|
||||
return $quizzes;
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue