forked from tom.biard/ScienceQuest
parent
9a535b017e
commit
f6583259d2
@ -0,0 +1,134 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace controller;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use model\ConfigurationJeu;
|
||||||
|
use model\Connection;
|
||||||
|
use model\Joueur;
|
||||||
|
use model\MdlScienceQuizz;
|
||||||
|
use model\MdlScientifique;
|
||||||
|
use config\Validation;
|
||||||
|
use model\ScientifiqueGateway;
|
||||||
|
use model\ValidationException;
|
||||||
|
use Twig\Error\LoaderError;
|
||||||
|
use Twig\Error\RuntimeError;
|
||||||
|
use Twig\Error\SyntaxError;
|
||||||
|
|
||||||
|
class ScienceQuizzController
|
||||||
|
{
|
||||||
|
private array $dVue;
|
||||||
|
private array $dVueErreur;
|
||||||
|
private MdlScienceQuizz $scienceQuizz;
|
||||||
|
private Connection $con;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws RuntimeError
|
||||||
|
* @throws SyntaxError
|
||||||
|
* @throws LoaderError
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function __construct(Joueur $role, ConfigurationJeu $configJeu)
|
||||||
|
{
|
||||||
|
$this->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]);
|
||||||
|
?>
|
||||||
|
<script>
|
||||||
|
setTimeout(function() {
|
||||||
|
window.location.href = "/ScienceQuizzReponse.php";
|
||||||
|
}, 10000);
|
||||||
|
</script>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws RuntimeError
|
||||||
|
* @throws SyntaxError
|
||||||
|
* @throws LoaderError
|
||||||
|
*/
|
||||||
|
private function vueReponse()
|
||||||
|
{
|
||||||
|
global $twig, $config;
|
||||||
|
|
||||||
|
$dScientifique['nom'] = $this->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]);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,109 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace model;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class MdlScienceQuizz
|
||||||
|
{
|
||||||
|
private int $bonneReponse;
|
||||||
|
private string $nbPoints;
|
||||||
|
private int $numQuestion;
|
||||||
|
private array $reponses;
|
||||||
|
private array $questions;
|
||||||
|
private array $questionsPass;
|
||||||
|
private bool $partieTerminee;
|
||||||
|
|
||||||
|
private int $scientifique;
|
||||||
|
|
||||||
|
public function __construct(int $scientifique, array $questions)
|
||||||
|
{
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="fr">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Science Quizz</title>
|
||||||
|
<!-- CSS -->
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
height: 100vh;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.game-container {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.answer-button {
|
||||||
|
padding: 10px 20px;
|
||||||
|
font-size: 16px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div class="game-container">
|
||||||
|
<h1>Science Quizz</h1>
|
||||||
|
<div>
|
||||||
|
<p>Question {{ dVue.statJeu.numQuestion }}:</p>
|
||||||
|
<p>{{ dVue.statJeu.question.question }}</p>
|
||||||
|
|
||||||
|
<!-- Boutons des réponses -->
|
||||||
|
<button class="answer-button" type="submit" name="reponse" value="{{ dVue.statJeu.question.answer }}">
|
||||||
|
Show Answer
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Gestion des erreurs -->
|
||||||
|
{% if dVueErreur %}
|
||||||
|
<div style="color: red;">
|
||||||
|
<ul>
|
||||||
|
{% for error in dVueErreur %}
|
||||||
|
<li>{{ error }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
<!-- JS -->
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
@ -0,0 +1,10 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Science Quizz Recap</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,10 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Science Quizz 2</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in new issue