parent
a74a9faa72
commit
fa28d2a335
@ -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…
Reference in new issue