You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3.01-QCM_MuscuMaths/Website/controllers/ControllerUser.php

239 lines
8.2 KiB

<?php
class ControllerUser
{
private $mdQuestion;
private $mdChapter;
private $mdAnswer;
private $mdPlayer;
private $mdLobby;
private $mdAdministrator;
private $twig;
private $vues;
function __construct()
{
global $vues, $twig;
session_start();
try {
$this->twig = $twig;
$this->vues = $vues;
$this->mdQuestion = new ModelQuestion();
$this->mdAnswer = new ModelAnswer();
$this->mdChapter = new ModelChapter();
$this->mdPlayer = new ModelPlayer();
$this->mdLobby = new ModelLobby();
$this->mdAdministrator = new ModelAdministrator();
} catch (PDOException $e) {
// $dataVueEreur[] = "Erreur inattendue!!! ";
// require(__DIR__.'/../vues/erreur.php');
} catch (Exception $e2) {
// $dataVueEreur[] = "Erreur inattendue!!! ";
// require ($rep.$vues['erreur']);
}
}
function home()
{
echo $this->twig->render($this->vues["home"]);
}
function error()
{
echo $this->twig->render($this->vues["error"]);
}
function themeChoice()
{
$chapters = $this->mdChapter->getChapters();
echo $this->twig->render($this->vues["themeChoice"], [
'chapters' => $chapters,
]);
}
function singleplayer()
{
echo $this->twig->render($this->vues["singleplayer"]);
}
function multiplayer()
{
echo $this->twig->render($this->vues["multiplayer"]);
}
function loginAdmin()
{
echo $this->twig->render($this->vues["loginAdmin"], [
'error' => $_SESSION["error"],
]);
$_SESSION["error"] = "";
}
function loginPlayer()
{
echo $this->twig->render($this->vues["loginAdmin"], [
'error' => $_SESSION["error"],
]);
$_SESSION["error"] = "";
}
function verifyAdmin()
{
$username = $_POST['username'];
$password = $_POST['password'];
$Administrator = [
'username' => $username,
'password' => $password,
];
$AdministratorIsOk = $this->mdAdministrator->verifyAdministrator($Administrator);
if ($AdministratorIsOk != null) {
$_SESSION["idAdminConnected"] = $AdministratorIsOk;
header("Location:/admin/administrators");
} else {
$_SESSION["error"] = "utilisateur introuvable.";
header("Location:/login");
}
}
function verifySingleplayer()
{
$_SESSION["Score"] = 0;
$difficulty = $_POST['difficulty'];
$chapter = $_POST['chapter'];
$difficultyIsOk = TRUE;
$chapterIsOk = TRUE;
if (!($difficulty == 1 or $difficulty == 2 or $difficulty == 3)) {
$_SESSION["error"] = "Valeur de difficulté invalide";
$difficultyIsOk = FALSE;
}
if ($this->mdChapter->verifyChapter($chapter) == NULL) {
$_SESSION["error"] = "Valeur de chapitre invalide";
$chapterIsOk = FALSE;
}
if ($difficultyIsOk and $chapterIsOk) {
$_SESSION["PrevTime"] = new DateTime('now');
$_SESSION["Questions"] = $this->mdQuestion->getQuestionsByChapterAndDifficulty($chapter, $difficulty);
$_SESSION["Answers"] = array();
foreach ($_SESSION["Questions"] as $question) {
$answers = $this->mdAnswer->getAnswersByIDQuestions($question->getId());
$_SESSION["Answers"][] = $answers;
}
echo $this->twig->render($this->vues["singleplayer"], [
'questions' => $_SESSION["Questions"],
'numQuestion' => 0,
'answerss' => $_SESSION["Answers"],
]);
} else {
$_SESSION["error"] = "Valeur de choix de thème invalide";
header("Location:/themeChoice");
}
}
function verifQuestion()
//Only Handdle solo game
{
$_SESSION["CurrTime"] = new DateTime('now');
$answerNumber = $_POST["answer"];
$numQuestion = $_POST["numQuestion"];
if (!($answerNumber == 0 or $answerNumber == 1 or $answerNumber == 2 or $answerNumber == 3)) {
$_SESSION["error"] = "Valeur de choix de réponse invalide";
echo $this->twig->render($this->vues["singleplayer"], [
'questions' => $_SESSION["Questions"],
'numQuestion' => $numQuestion,
'answerss' => $_SESSION["Answers"],
]);
} else {
$answerContent = $_SESSION["Answers"][$numQuestion][$answerNumber]->getContent();
$_SESSION["playerAnswersContent"][$numQuestion] = $answerContent;
if ($_SESSION["Questions"][$numQuestion]->getIdAnswerGood() == $_SESSION["Answers"][$numQuestion][$answerNumber]->getId()) {
$time = $_SESSION["PrevTime"]->diff($_SESSION["CurrTime"]);
$_SESSION["Score"] = $_SESSION["Score"] + 80 + 40 * ((30 - $time->s) / 100 * 10 / 3);
} else {
$_SESSION["Questions"][$numQuestion]->setNbFails($_SESSION["Questions"][$numQuestion]->getNbFails() + 1);
}
if ($numQuestion <= 8) {
$_SESSION["PrevTime"] = $_SESSION["CurrTime"];
echo $this->twig->render($this->vues["singleplayer"], [
'questions' => $_SESSION["Questions"],
'numQuestion' => $numQuestion + 1,
'answerss' => $_SESSION["Answers"],
]);
} else {
$Final = array();
$Final[]["Question"] = array();
$Final[]["goodAnswer"] = array();
$Final[]["PlayerAnswer"] = array();
$c = 0;
foreach ($_SESSION["Questions"] as &$question) {
$answer = $this->mdAnswer->getAnswerByID($question->getIdAnswerGood());
$Final[$c]["goodAnswer"] = $answer->getContent();
$c = $c + 1;
}
$c = 0;
foreach ($_SESSION["Questions"] as $question) {
$Final[$c]["Question"] = $question->getContent();
$c = $c + 1;
}
$c = 0;
foreach ($_SESSION["playerAnswersContent"] as $answer) {
$Final[$c]["PlayerAnswer"] = $answer;
$c = $c + 1;
}
echo $this->twig->render($this->vues["viewScore"], [
'score' => $_SESSION["Score"],
'Final' => $Final,
]);
}
}
}
function passer()
{
$numQuestion = $_POST["numQuestion"];
$_SESSION["playerAnswersContent"][$numQuestion] = "Pas de réponse";
$_SESSION["Questions"][$numQuestion]->setNbFails($_SESSION["Questions"][$numQuestion]->getNbFails() + 1);
if ($numQuestion <= 8) {
$_SESSION["PrevTime"] = $_SESSION["CurrTime"];
echo $this->twig->render($this->vues["singleplayer"], [
'questions' => $_SESSION["Questions"],
'numQuestion' => $numQuestion + 1,
'answerss' => $_SESSION["Answers"],
]);
} else {
$Final = array();
$Final[]["Question"] = array();
$Final[]["goodAnswer"] = array();
$Final[]["PlayerAnswer"] = array();
$c = 0;
foreach ($_SESSION["Questions"] as &$question) {
$answer = $this->mdAnswer->getAnswerByID($question->getIdAnswerGood());
$Final[$c]["goodAnswer"] = $answer->getContent();
$c = $c + 1;
}
$c = 0;
foreach ($_SESSION["Questions"] as $question) {
$Final[$c]["Question"] = $question->getContent();
$c = $c + 1;
}
$c = 0;
foreach ($_SESSION["playerAnswersContent"] as $answer) {
$Final[$c]["PlayerAnswer"] = $answer;
$c = $c + 1;
}
echo $this->twig->render($this->vues["viewScore"], [
'score' => $_SESSION["Score"],
'Final' => $Final,
]);
}
}
}