diff --git a/project/src/config/Validation.php b/project/src/config/Validation.php index 7648117..597c0a8 100755 --- a/project/src/config/Validation.php +++ b/project/src/config/Validation.php @@ -5,6 +5,7 @@ namespace config; use model\ConfigurationJeu; use model\Joueur; use model\MdlPendu; +use model\MdlScienceQuizz; use model\ValidationException; class Validation @@ -90,4 +91,14 @@ class Validation return $pendu; } + public static function valMdlScienceQuizz($scienceQuizz, array &$dVueErreur): MdlScienceQuizz + { + if(! $scienceQuizz instanceof MdlScienceQuizz){ + $role = NULL; + $dVueErreur[] = 'Erreur, mauvais jeu en utilisation'; + throw new ValidationException('Erreur, mauvais jeu en utilisation'); + } + return $scienceQuizz; + } + } diff --git a/project/src/config/config.php b/project/src/config/config.php index 4194993..a4d2110 100755 --- a/project/src/config/config.php +++ b/project/src/config/config.php @@ -4,12 +4,15 @@ namespace config; $config = [ "rep" => __DIR__.'/../', - "db" => ["dsn" => 'pgsql:host=localhost;dbname=dbrebeuret', - "login" => 'rebeuret', - "mdp" => 'achanger'], + "db" => ["dsn" => 'pgsql:host=localhost;dbname=postgres', + "login" => 'postgres', + "mdp" => 'root'], "templates" => ["index" => 'vues/index.php', "pseudo" => 'pseudo.html', "jouer" => "jouer.html", "pendu" => "pendu.html", - "penduScore" => 'penduScore.html'] + "penduScore" => 'penduScore.html', + "scienceQuizz" => 'scienceQuizz.html', + "scienceQuizzReponse" => 'scienceQuizzReponse.html', + "scienceQuizzRecap" => 'scienceQuizzRecap.html',] ]; \ No newline at end of file diff --git a/project/src/controller/FrontController.php b/project/src/controller/FrontController.php index 4ea36d4..4e04c55 100755 --- a/project/src/controller/FrontController.php +++ b/project/src/controller/FrontController.php @@ -39,7 +39,11 @@ class FrontController $router->map('GET|POST','/admin/[a:action]','AdminController'); $router->map('GET|POST','/[a:action]?','UserController'); - session_start(); + try { + session_start(); + } catch (Exception $e) { + die('Session start failed: ' . $e->getMessage()); + } if(isset($_SESSION['pseudo'])) $dVue['pseudo'] = $_SESSION['pseudo']; diff --git a/project/src/controller/JouerController.php b/project/src/controller/JouerController.php index 22922f3..4b502ed 100755 --- a/project/src/controller/JouerController.php +++ b/project/src/controller/JouerController.php @@ -29,6 +29,9 @@ class JouerController{ if(count($dVueErreur) == 0){ $idJeu = $configurationJeu->getJeu()->getId(); switch($idJeu){ + case 2: + new ScienceQuizzController($role, $configurationJeu); + break; case 3: new PenduController($role, $configurationJeu); break; diff --git a/project/src/controller/ScienceQuizzController.php b/project/src/controller/ScienceQuizzController.php new file mode 100644 index 0000000..c325bbe --- /dev/null +++ b/project/src/controller/ScienceQuizzController.php @@ -0,0 +1,134 @@ +dVue = []; + $this->dVueErreur = []; + if (isset($_SESSION['scienceQuizz']) && Validation::valMdlScienceQuizz($_SESSION['scienceQuizz'], $this->dVueErreur)) { + $this->scienceQuizz = $_SESSION['scienceQuizz']; + } else { + $this->reInit(); + } + + if ($this->scienceQuizz->partieTerminee()) { + $this->vueRecap(); + } + else { + $this->vueJeu(); + } + } + + /** + * @throws Exception + */ + private function reInit() + { + $mdlScientifique = new MdlScientifique(); + $scientifique = $mdlScientifique->getRandom(); + $idScientifique = $scientifique->getId(); + $questions = $mdlScientifique->getQuestions($idScientifique); + $this->scienceQuizz = new MdlScienceQuizz($idScientifique,$questions); + $_SESSION['scienceQuizz'] = $this->scienceQuizz; + } + + /** + * @throws SyntaxError + * @throws RuntimeError + * @throws LoaderError + */ + private function vueJeu() + { + global $twig, $config; + + $dStatJeu['numQuestion'] = $this->scienceQuizz->getNumQuestion(); + + $questions = $this->scienceQuizz->getQuestions(); + + if ($questions) { + $dStatJeu['question'] = $this->scienceQuizz->getRandomQuestion($questions); + } else { + // Gérer le cas où aucune question n'est disponible + $this->dVueErreur[] = "Aucune question disponible."; + echo $twig->render('erreur.html', ['dVueErreur' => $this->dVueErreur]); + return; + } + + $this->dVue['statJeu'] = $dStatJeu; + + $this->dVue['Jeu'] = $this->scienceQuizz->getJeu(); + + echo $twig->render($config['templates']['scienceQuizz'], ['dVue' => $this->dVue, 'dVueErreur' => $this->dVueErreur]); + ?> + + scienceQuizz->getNom(); + $dScientifique['prenom'] = $this->scienceQuizz->getPrenom(); + $dScientifique['dateNaissance'] = $this->scienceQuizz->getDateNaissance(); + + + $this->dVue['scientifique'] = $dScientifique; + + echo $twig->render($config['templates']['scienceQuizzReponse'], ["dVue" => $this->dVue]); + } + + /** + * @throws RuntimeError + * @throws SyntaxError + * @throws LoaderError + */ + private function vueRecap() + { + global $twig, $config; + + $dStatJoueur ['bonneReponse'] = $this->scienceQuizz->getBonneReponse(); + $dStatJoueur ['nbPoints'] = $this->scienceQuizz->getNbPoints(); + + $this->dVue['statJoueur'] = $dStatJoueur; + unset($_SESSION['scienceQuizz']); + + echo $twig->render($config['templates']['scienceQuizzRecap'], ["dVue" => $this->dVue]); + } +} \ No newline at end of file diff --git a/project/src/controller/UserController.php b/project/src/controller/UserController.php index 6eea4d8..2cd9e34 100755 --- a/project/src/controller/UserController.php +++ b/project/src/controller/UserController.php @@ -53,6 +53,9 @@ class UserController { if(count($dVueErreur) == 0){ $idJeu = $configurationJeu->getJeu()->getId(); switch($idJeu){ + case 2: + new ScienceQuizzController($role, $configurationJeu); + break; case 3: new PenduController($role, $configurationJeu); break; diff --git a/project/src/model/gateways/Connection.php b/project/src/model/gateways/Connection.php index 2e8572d..d6fc0b1 100755 --- a/project/src/model/gateways/Connection.php +++ b/project/src/model/gateways/Connection.php @@ -2,7 +2,9 @@ namespace model; +use Exception; use PDO; +use PDOException; use PDOStatement; class Connection extends PDO { @@ -10,24 +12,44 @@ class Connection extends PDO { private PDOStatement $stmt; public function __construct(string $dsn, string $username, string $password) { - - parent::__construct($dsn,$username,$password); - $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + try { + parent::__construct($dsn, $username, $password); + $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } catch (PDOException $e) { + die("Connection failed: " . $e->getMessage()); + } } - /** * @param string $query - * @param array $parameters * - * @return bool Returns `true` on success, `false` otherwise + /** * + * @param string $query + * @param array $params + * @return bool|null Returns `true` on success, `false` otherwise */ - public function executeQuery(string $query, array $parameters = []) : bool{ - $this->stmt = parent::prepare($query); - foreach ($parameters as $name => $value) { - $this->stmt->bindValue($name, $value[0], $value[1]); - } + /** + * @param string $query + * @param array $params + * @return PDOStatement|false Returns `PDOStatement` on success, `false` otherwise + * @throws Exception + */ + public function executeQuery(string $query, array $params = []) + { + try { + $stmt = $this->prepare($query); + + foreach ($params as $param => $value) { + $stmt->bindValue($param, $value[0], $value[1]); + } - return $this->stmt->execute(); + $stmt->execute(); + $this->stmt = $stmt; + + return $stmt; + + } catch (PDOException $e) { + throw new Exception($e->getMessage()); + } } public function getResults() : array { @@ -37,4 +59,5 @@ class Connection extends PDO { public function getOneResult() { return $this->stmt->fetch(); } + } \ No newline at end of file diff --git a/project/src/model/gateways/ScientifiqueGateway.php b/project/src/model/gateways/ScientifiqueGateway.php index 5590529..37c2595 100755 --- a/project/src/model/gateways/ScientifiqueGateway.php +++ b/project/src/model/gateways/ScientifiqueGateway.php @@ -2,6 +2,8 @@ namespace model; +use Exception; + class ScientifiqueGateway { private Connection $con; @@ -18,6 +20,7 @@ class ScientifiqueGateway /** * @return array|bool + * @throws Exception */ public function getRandom() { // PHP 7.4 $this->con->executeQuery( @@ -26,6 +29,9 @@ class ScientifiqueGateway return $this->con->getOneResult(); } + /** + * @throws Exception + */ public function addScientifique(Scientifique $sci): bool{ return $this->con->executeQuery( "INSERT INTO Scientifique(nom, prenom, photo, dateNaissance, descriptif, ratioTrouvee, idThematique, idDifficulte, idSexe) VALUES (:nom, :prenom, :photo, :dateNaissance, :descriptif, :ratioTrouvee, :idThematique, :idDifficulte, :idSexe);" @@ -41,6 +47,10 @@ class ScientifiqueGateway ":idSexe"=>[$sci->getSexe()->getId(),$this->con::PARAM_STR] ]); } + + /** + * @throws Exception + */ public function editScientifique(Scientifique $sci): bool{ return $this->con->executeQuery( "UPDATE Scientifique SET nom = :nom, prenom = :prenom, photo = :photo, dateNaissance = :dateNaissance, descriptif = :descriptif, ratioTrouvee = :ratioTrouvee, idThematique = :idThematique, idDifficulte = :idDifficulte, idSexe = :idSexe WHERE id=:id;" @@ -57,6 +67,10 @@ class ScientifiqueGateway ":id"=>[$sci->getId(),$this->con::PARAM_INT] ]); } + + /** + * @throws Exception + */ public function deleteScientifique(int $id): bool{ return $this->con->executeQuery( "DELETE FROM Scientifique WHERE id=:id;" @@ -65,6 +79,9 @@ class ScientifiqueGateway ]); } + /** + * @throws Exception + */ public function getScientifique(int $id) { $this->con->executeQuery( "SELECT id, nom, prenom, photo, dateNaissance, descriptif, ratioTrouvee, idThematique, idDifficulte, idSexe FROM Scientifique WHERE id=:id;" @@ -73,4 +90,21 @@ class ScientifiqueGateway ]); return $this->con->getOneResult(); } + + /** + * @throws Exception + */ + public function getQuestions(int $idScientifique): array + { + $query = "SELECT q.* FROM Question q + JOIN Reponse r ON q.id = r.idQuestion + WHERE r.idScientifique = :idScientifique + ORDER BY RANDOM() LIMIT 5"; + + $params = [":idScientifique" => [$idScientifique, $this->con::PARAM_INT]]; + + $this->con->executeQuery($query, $params); + + return $this->con->getResults(); + } } \ No newline at end of file diff --git a/project/src/model/mdl/MdlScienceQuizz.php b/project/src/model/mdl/MdlScienceQuizz.php new file mode 100644 index 0000000..8638b67 --- /dev/null +++ b/project/src/model/mdl/MdlScienceQuizz.php @@ -0,0 +1,109 @@ +bonneReponse = 0; + $this->nbPoints = 0; + $this->numQuestion = 0; + $this->reponses = []; + $this->scientifique = $scientifique; + $this->questions = $questions; + $this->questionsPass = []; + $this->partieTerminee = false; + } + + /** + * @return array + */ + public function getQuestions(): array + { + var_dump($this->questions); + return $this->questions; + } + + public function getScientifique(): int + { + return $this->scientifique; + } + + /** + * @return int + */ + public function getBonneReponse(): int + { + return $this->bonneReponse; + } + + /** + * @return string + */ + public function getNbPoints(): string + { + return $this->nbPoints; + } + + /** + * @return int + */ + public function getNumQuestion(): int + { + return $this->numQuestion; + } + + /** + * @return array + */ + public function getReponses(): array + { + return $this->reponses; + } + + /** + * @param int $bonneReponse + */ + public function setBonneReponse(int $bonneReponse): void + { + $this->bonneReponse = $bonneReponse; + } + + // Sélectionne une question aléatoire + + /** + * @throws Exception + */ + public function getRandomQuestion(array $questions): array + { + $randomNum=random_int(0, count($questions)-1); + $question=$questions[$randomNum]; + $this->questionsPass[]=$question; + if (count($this->questionsPass)==count($questions)) + { + $this->partieTerminee=true; + } + return $question; + } + + public function partieTerminee(): bool + { + return $this->partieTerminee; + } + +} \ No newline at end of file diff --git a/project/src/model/mdl/MdlScientifique.php b/project/src/model/mdl/MdlScientifique.php index 22a67ce..33d6378 100755 --- a/project/src/model/mdl/MdlScientifique.php +++ b/project/src/model/mdl/MdlScientifique.php @@ -41,14 +41,28 @@ class MdlScientifique extends MdlBase{ $difficulte, $sexe); } - public function addScientifique(Scientifique $s){ + + /** + * @throws Exception + */ + public function addScientifique(Scientifique $s): bool + { return $this->gw->addScientifique($s); } - public function editScientifique(Scientifique $s){ + + /** + * @throws Exception + */ + public function editScientifique(Scientifique $s): bool + { return $this->gw->editScientifique($s); } - public function getScientifique(int $id){ + /** + * @throws Exception + */ + public function getScientifique(int $id): Scientifique + { $t=$this->gw->getScientifique($id); if(gettype($t)!="array"){ throw new Exception("Scientifique non trouvé"); @@ -75,7 +89,24 @@ class MdlScientifique extends MdlBase{ $diff, $sexe ); - - } + + /** + * @throws Exception + */ + public function getQuestions(int $id): array + { + $t = $this->gw->getQuestions($id); + + if (gettype($t) != "array") { + throw new Exception("Scientifique non trouvé"); + } + + $questions = []; + + foreach ($t as $question) { + $questions[] = $question["libelle"]; + } + return $questions; + } } \ No newline at end of file diff --git a/project/src/model/metier/Scientifique.php b/project/src/model/metier/Scientifique.php index 2f1715f..3cc7d5b 100755 --- a/project/src/model/metier/Scientifique.php +++ b/project/src/model/metier/Scientifique.php @@ -16,6 +16,7 @@ class Scientifique private Thematique $thematique; private Difficulte $difficulte; private Sexe $sexe; + private array $questions; /** * @param int $id @@ -28,6 +29,7 @@ class Scientifique * @param Thematique $thematique * @param Difficulte $difficulte * @param Sexe $sexe + * @param array $questions */ public function __construct(int $id, string $nom, @@ -38,7 +40,8 @@ class Scientifique float $ratioTrouvee, Thematique $thematique, Difficulte $difficulte, - Sexe $sexe) + Sexe $sexe, + array $questions = []) { $this->id = $id; $this->nom = $nom; @@ -50,6 +53,7 @@ class Scientifique $this->thematique = $thematique; $this->difficulte = $difficulte; $this->sexe = $sexe; + $this->questions = $questions; } /** diff --git a/project/src/templates/scienceQuizz.html b/project/src/templates/scienceQuizz.html new file mode 100644 index 0000000..bdb191d --- /dev/null +++ b/project/src/templates/scienceQuizz.html @@ -0,0 +1,57 @@ + + + +
+ + +Question {{ dVue.statJeu.numQuestion }}:
+{{ dVue.statJeu.question.question }}
+ + + +