diff --git a/Website/class/Answer.php b/Website/class/Answer.php index 518beba..c5b7d8f 100644 --- a/Website/class/Answer.php +++ b/Website/class/Answer.php @@ -6,12 +6,17 @@ class Answer private string $content; private int $idQuestion; - public function __construct(string $content, int $idQuestion) + public function __construct(int $id, string $content, int $idQuestion) { + $this->id = $id; $this->content = $content; $this->idQuestion = $idQuestion; } + public function getId(): int + { + return $this->id; + } public function getContent() { return $this->content; diff --git a/Website/class/Question.php b/Website/class/Question.php index 7631fe6..aa19632 100644 --- a/Website/class/Question.php +++ b/Website/class/Question.php @@ -9,59 +9,69 @@ class Question private int $nbFails; private int $idAnswerGood; - public function __construct(string $content, int $idChapter, int $idAnswerGood = -1, int $difficulty = 1, int $nbFails = 0) - { + public function __construct( + int $id, + string $content, + int $idChapter, + int $idAnswerGood = -1, + int $difficulty = 1, + int $nbFails = 0 + ) { + $this->id = $id; $this->content = $content; $this->idChapter = $idChapter; - if ($idAnswerGood != -1) { - $this->idAnswerGood = $idAnswerGood; - } + $this->idAnswerGood = ($idAnswerGood !== -1) ? $idAnswerGood : -1; $this->difficulty = $difficulty; $this->nbFails = $nbFails; } - public function getContent() + public function getId(): int + { + return $this->id; + } + + public function getContent(): string { return $this->content; } - public function getIdChapter() + public function getIdChapter(): int { return $this->idChapter; } - public function getIdAnswerGood() + public function getIdAnswerGood(): int { return $this->idAnswerGood; } - public function setContent(string $content) + public function setContent(string $content): void { $this->content = $content; } - public function setIdAnswerGood(int $idAnswerGood) + public function setIdAnswerGood(int $idAnswerGood): void { $this->idAnswerGood = $idAnswerGood; } - public function getDifficulty() + public function getDifficulty(): int { return $this->difficulty; } - - public function setDifficulty(int $difficulty) + + public function setDifficulty(int $difficulty): void { $this->difficulty = $difficulty; } - public function getNbFails() + public function getNbFails(): int { return $this->nbFails; } - public function setNbFails(int $nbFails) + public function setNbFails(int $nbFails): void { $this->nbFails = $nbFails; } -} +} \ No newline at end of file diff --git a/Website/controllers/ControllerAdminQuestions.php b/Website/controllers/ControllerAdminQuestions.php index baf4193..a86ac1f 100644 --- a/Website/controllers/ControllerAdminQuestions.php +++ b/Website/controllers/ControllerAdminQuestions.php @@ -14,8 +14,8 @@ class ControllerAdminQuestions global $dns, $user, $pass, $vues, $twig; session_start(); try { - if($_SESSION["idAdminConnected"] != null){ - $this->twig =$twig; + if ($_SESSION["idAdminConnected"] != null) { + $this->twig = $twig; $this->vues = $vues; $this->mdQuestion = new ModelQuestion(); @@ -29,8 +29,7 @@ class ControllerAdminQuestions 'questions' => $questions, 'chapters' => $chapters, ]); - } - else { + } else { header("Location:/login"); } } catch (PDOException $e) { @@ -40,12 +39,14 @@ class ControllerAdminQuestions } } - function delete($param) { + function delete($param) + { $this->mdQuestion->deleteQuestionByID($param["id"]); header("Location:/admin/questions"); } - function add($param) { + function add($param) + { $content = $_POST['content']; $idChapter = intval($_POST['idChapter']); $AnswersPost = array(); @@ -58,6 +59,7 @@ class ControllerAdminQuestions $Question = [ 'content' => $content, 'idchapter' => $idChapter, + 'idanswergood' => $correctAnswer, 'difficulty' => 1, 'nbfails' => 0, ]; @@ -85,33 +87,43 @@ class ControllerAdminQuestions 'idanswergood' => $answersId[$correctAnswer], ]; - $this->mdQuestion->updateQuestion($idquestion,$Question); - + $this->mdQuestion->updateQuestion($idquestion, $Question); + header("Location:/admin/questions"); } - function updatemodal($param) { - + function updatemodal($param) + { $question = $this->mdQuestion->getQuestionByID($param["id"]); - $answers = array(); - $answers = $this->mdAnswer->getAnswersByIDQuestions($param["id"]); + $answersIDArray = $this->mdAnswer->getAnswersByIDQuestions($param["id"]); $chapters = $this->mdChapter->getChapters(); + $answers = array(); + $i = 0; + foreach ($answersIDArray as $answerIDArray) { + $answers["{$i}"] = $answerIDArray; + $i++; + } + echo $this->twig->render($this->vues["adminQuestionsModal"], [ 'question' => $question, 'chapters' => $chapters, - 'answers' => $answers, + 'answerZero' => $answers[0], + 'answerOne' => $answers[1], + 'answerTwo' => $answers[2], + 'answerThree' => $answers[3], ]); } - function update($param) { + function update($param) + { $id = $_POST['id']; $content = $_POST['content']; $idChapter = intval($_POST['idChapter']); - + var_dump($idChapter); $correctAnswer = intval($_POST['correctAnswer']); $answersId = array(); @@ -126,23 +138,25 @@ class ControllerAdminQuestions $answers[2] = $_POST['answer3']; $answers[3] = $_POST['answer4']; - $Question = [ + + + $questionDataArray = [ 'content' => $content, 'idchapter' => $idChapter, 'idanswergood' => $answersId[$correctAnswer], ]; - $this->mdQuestion->updateQuestion($id, $Question); + $this->mdQuestion->updateQuestion($id, $questionDataArray); for ($i = 0; $i <= 3; $i++) { - $Answers[] = [ + $answersDataArray[] = [ 'content' => $answers[$i], 'id' => $id, ]; } - + var_dump($answersId, $answersDataArray); for ($i = 0; $i <= 3; $i++) { - $this->mdAnswer->updateAnswer($answersId[$i],$Answers[$i]); + $this->mdAnswer->updateAnswer($answersId[$i], $answersDataArray[$i]); } header("Location:/admin/questions"); diff --git a/Website/controllers/ControllerUser.php b/Website/controllers/ControllerUser.php index c8589ed..1f76947 100644 --- a/Website/controllers/ControllerUser.php +++ b/Website/controllers/ControllerUser.php @@ -89,12 +89,10 @@ class ControllerUser function verifySingleplayer() { $_SESSION["Score"] = 0; - $_SESSION["PrevTime"] = new DateTime('now'); $difficulty = $_POST['difficulty']; $chapter = $_POST['chapter']; $difficultyIsOk = TRUE; $chapterIsOk = TRUE; - if (!($difficulty == 0 or $difficulty == 1 or $difficulty == 2)) { $_SESSION["error"] = "Valeur de difficulté invalide"; $difficultyIsOk = FALSE; @@ -105,16 +103,18 @@ class ControllerUser $chapterIsOk = FALSE; } if ($difficultyIsOk and $chapterIsOk) { + $_SESSION["PrevTime"] = new DateTime('now'); $_SESSION["Questions"] = $this->mdQuestion->getQuestionsByChapterAndDifficulty($chapter, $difficulty); - foreach ($_SESSION["Questions"] as &$question) { + $answerss = array(); + foreach ($_SESSION["Questions"] as $question) { $answers = $this->mdAnswer->getAnswersByIDQuestions($question['id']); - $question['answers'] = $answers; - var_dump("i"); + $answerss[] = $answers; + var_dump("t"); } - echo $this->twig->render($this->vues["singleplayer"], [ - 'questions' => $_SESSION["Questions"], - 'numQuestion' => 0, - ]); + // echo $this->twig->render($this->vues["singleplayer"], [ + // 'questions' => $_SESSION["Questions"], + // 'numQuestion' => 0, + // ]); } else { $_SESSION["error"] = "Valeur de choix de thème invalide"; header("Location:/themeChoice"); diff --git a/Website/gateways/GatewayAdministrator.php b/Website/gateways/GatewayAdministrator.php index 6fa15eb..46374e2 100755 --- a/Website/gateways/GatewayAdministrator.php +++ b/Website/gateways/GatewayAdministrator.php @@ -30,7 +30,7 @@ class GatewayAdministrator if ($results == NULL) { return false; } - return new Administrator($results[0]['id'], $results[0]['username'], $results[0]['hashedPassword']); + return $results[0]; } public function getAdministratorByID(int $id) @@ -47,11 +47,11 @@ class GatewayAdministrator $query = "SELECT * FROM administrators"; $this->con->executeQuery($query); $results = $this->con->getResults(); - + return $results; } - public function updateAdministrator($id,$administrator) + public function updateAdministrator($id, $administrator) { $query = "UPDATE administrators SET username = :username, password = :password WHERE id = :id;"; $this->con->executeQuery( @@ -81,7 +81,7 @@ class GatewayAdministrator ) ); $results = $this->con->getResults(); - + return $results[0]; } } diff --git a/Website/gateways/GatewayAnswer.php b/Website/gateways/GatewayAnswer.php index 10187df..4dba011 100755 --- a/Website/gateways/GatewayAnswer.php +++ b/Website/gateways/GatewayAnswer.php @@ -47,7 +47,6 @@ class GatewayAnswer ) ); $results = $this->con->getResults(); - return $results; } diff --git a/Website/gateways/GatewayChapter.php b/Website/gateways/GatewayChapter.php index 1f08039..1b0079a 100755 --- a/Website/gateways/GatewayChapter.php +++ b/Website/gateways/GatewayChapter.php @@ -57,17 +57,16 @@ class GatewayChapter $this->con->executeQuery($query, array(':id' => array($id, PDO::PARAM_INT))); } - public function verifyChapter($idChapter) + public function verifyChapter($name) { - $query = "SELECT chapters.id FROM chapters WHERE id = :id"; + $query = "SELECT chapters.name FROM chapters WHERE name = :name"; $this->con->executeQuery( $query, array( - ':id' => array($idChapter, PDO::PARAM_STR), + ':name' => array($name, PDO::PARAM_STR), ) ); $results = $this->con->getResults(); - return $results[0]; } } diff --git a/Website/gateways/GatewayLobby.php b/Website/gateways/GatewayLobby.php index d54eef0..848d730 100755 --- a/Website/gateways/GatewayLobby.php +++ b/Website/gateways/GatewayLobby.php @@ -24,7 +24,7 @@ class GatewayLobby ); } - public function getLobbys() + public function getLobbies() { $query = "SELECT * FROM Lobbies;"; $this->con->executeQuery($query); @@ -32,11 +32,7 @@ class GatewayLobby if ($results == NULL) { return false; } - $lobbys = array(); - foreach ($results as $row) { - $lobbys[] = new Lobby($row['id'], $row['name'], $row['password'], $row['nbPlayer']); - } - return $lobbys; + return $results; } public function getLobbyByName($name) @@ -47,7 +43,7 @@ class GatewayLobby if ($results == NULL) { return false; } - return new Lobby($results[0]['id'], $results[0]['name'], $results[0]['password'], $results[0]['nbPlayer']); + return $results[0]; } diff --git a/Website/gateways/GatewayPlayer.php b/Website/gateways/GatewayPlayer.php index 57a61cb..e323adb 100755 --- a/Website/gateways/GatewayPlayer.php +++ b/Website/gateways/GatewayPlayer.php @@ -31,7 +31,7 @@ class GatewayPlayer if ($results == NULL) { return false; } - return new Player($results[0]['id'], $results[0]['nickname'], $results[0]['hashedPassword']); + return $results[0]; } public function getPlayerByID(int $id) @@ -42,7 +42,7 @@ class GatewayPlayer if ($results == NULL) { return false; } - return new Player($results[0]['id'], $results[0]['nickname'], $results[0]['hashedPassword']); + return $results[0]; } public function getPlayers() @@ -50,11 +50,11 @@ class GatewayPlayer $query = "SELECT * FROM players"; $this->con->executeQuery($query); $results = $this->con->getResults(); - + return $results; } - public function updatePlayer($player) + public function updatePlayer($id, $player) { $query = "UPDATE players SET nickname = :nickname, hashedPassword = :hashedPassword WHERE id = :id;"; $this->con->executeQuery( @@ -89,7 +89,7 @@ class GatewayPlayer ) ); $results = $this->con->getResults(); - + return $results[0]; } } diff --git a/Website/gateways/GatewayQuestion.php b/Website/gateways/GatewayQuestion.php index aba3ed5..39b709d 100755 --- a/Website/gateways/GatewayQuestion.php +++ b/Website/gateways/GatewayQuestion.php @@ -40,19 +40,18 @@ class GatewayQuestion $query = "SELECT * FROM questions"; $this->con->executeQuery($query); $results = $this->con->getResults(); - return $results; } - public function updateQuestion($id, $question) + public function updateQuestion($id, $questionDataArray) { $query = "UPDATE questions SET content = :content, idchapter = :idchapter, idanswergood = :idanswergood WHERE id = :id;"; $this->con->executeQuery( $query, array( - ':content' => array($question['content'], PDO::PARAM_STR), - ':idchapter' => array($question['idchapter'], PDO::PARAM_INT), - ':idanswergood' => array($question['idanswergood'], PDO::PARAM_INT), + ':content' => array($questionDataArray['content'], PDO::PARAM_STR), + ':idchapter' => array($questionDataArray['idchapter'], PDO::PARAM_INT), + ':idanswergood' => array($questionDataArray['idanswergood'], PDO::PARAM_INT), ':id' => array($id, PDO::PARAM_INT), ) ); diff --git a/Website/models/ModelAdministrator.php b/Website/models/ModelAdministrator.php index d4f16e8..5e7796a 100644 --- a/Website/models/ModelAdministrator.php +++ b/Website/models/ModelAdministrator.php @@ -16,19 +16,25 @@ class ModelAdministrator public function getAdministratorByID($id) { - $administrator = $this->gwAdministrator->getAdministratorByID($id); + $administratorDataArray = $this->gwAdministrator->getAdministratorByID($id); + $administrator = new Administrator($administratorDataArray["username"], $administratorDataArray["password"]); return $administrator; } public function getAdministrators() { - $administrators = $this->gwAdministrator->getAdministrators(); + $administratorsDataArray = $this->gwAdministrator->getAdministrators(); + $administrators = array(); + foreach ($administratorsDataArray as $administratorDataArray) { + $administrator = new Administrator($administratorDataArray["username"], $administratorDataArray["password"]); + $administrators[$administratorDataArray['id']] = $administrator; + } return $administrators; } - public function updateAdministrator($id,$administrator) + public function updateAdministrator($id, $administrator) { - $this->gwAdministrator->updateAdministrator($id,$administrator); + $this->gwAdministrator->updateAdministrator($id, $administrator); } public function deleteAdministratorByID($id) diff --git a/Website/models/ModelAnswer.php b/Website/models/ModelAnswer.php index fc85d3a..5aa9f6e 100644 --- a/Website/models/ModelAnswer.php +++ b/Website/models/ModelAnswer.php @@ -17,13 +17,19 @@ class ModelAnswer function getAnswerByID($id) { - $answer = $this->gwAnswer->getAnswerByID($id); + $answerDataArray = $this->gwAnswer->getAnswerByID($id); + $answer = new Answer($id, $answerDataArray['content'], $answerDataArray['idquestion']); return $answer; } function getAnswersByIDQuestions($idQuestion) { - $answers = $this->gwAnswer->getAnswersByIDQuestions($idQuestion); + $answersDataArray = $this->gwAnswer->getAnswersByIDQuestions($idQuestion); + $answers = array(); + foreach ($answersDataArray as $answerDataArray) { + $answer = new Answer($answerDataArray['id'], $answerDataArray['content'], $idQuestion); + $answers[$answerDataArray['id']] = $answer; + } return $answers; } diff --git a/Website/models/ModelChapter.php b/Website/models/ModelChapter.php index 08c44c8..af2da98 100644 --- a/Website/models/ModelChapter.php +++ b/Website/models/ModelChapter.php @@ -11,29 +11,35 @@ class ModelChapter function getChapters() { - $chapters = $this->gwChapter->getChapters(); + $chaptersDataArray = $this->gwChapter->getChapters(); + $chapters = array(); + foreach ($chaptersDataArray as $chapterDataArray) { + $chapter = new Chapter($chapterDataArray["name"]); + $chapters[$chapterDataArray['id']] = $chapter; + } return $chapters; } - function deleteChapter($id) + function deleteChapter($id) { $this->gwChapter->deleteChapter($id); } - function addChapter($chapter) + function addChapter($chapter) { $this->gwChapter->addChapter($chapter); } - function getChapterByID($id) + function getChapterByID($id) { - $chapter = $this->gwChapter->getChapterByID($id); + $chapterDataArray = $this->gwChapter->getChapterByID($id); + $chapter = new Chapter($chapterDataArray['name']); return $chapter; } - function updateChapter($id,$chapter) + function updateChapter($id, $chapter) { - $this->gwChapter->updateChapter($id,$chapter); + $this->gwChapter->updateChapter($id, $chapter); } public function verifyChapter($chapter) { diff --git a/Website/models/ModelLobby.php b/Website/models/ModelLobby.php index 46202fc..e01a16b 100644 --- a/Website/models/ModelLobby.php +++ b/Website/models/ModelLobby.php @@ -14,9 +14,19 @@ class ModelLobby $this->gwLobby->addLobby($lobby); } - public function getlobbies() + public function getLobbies() { - $lobbies = $this->gwLobby->getlobbies(); - return $lobbies; + $lobbiesDataArray = $this->gwLobby->getLobbies(); + $lobbies = array(); + foreach ($lobbiesDataArray as $lobbyDataArray) { + $lobby = new Lobby( + intval($lobbyDataArray['id']), + $lobbyDataArray['name'], + $lobbyDataArray['password'], + intval($lobbyDataArray['nbPlayer']) + ); + $lobbies[$lobbyDataArray] = $lobby; + return $lobbies; + } } -} +} \ No newline at end of file diff --git a/Website/models/ModelPlayer.php b/Website/models/ModelPlayer.php index e8ac028..fe9effe 100644 --- a/Website/models/ModelPlayer.php +++ b/Website/models/ModelPlayer.php @@ -16,13 +16,13 @@ class ModelPlayer public function getPlayerByID($id) { - $player = $this->gwPlayer->getPlayerByID($id); - return $player; + $playerDataArray = $this->gwPlayer->getPlayerByID($id); + $player = new Player($playerDataArray["id"], $playerDataArray["nickname"], $playerDataArray["password"]); } - public function updatePlayer($id,$player) + public function updatePlayer($id, $player) { - $this->gwPlayer->updatePlayer($id,$player); + $this->gwPlayer->updatePlayer($id, $player); } public function deletePlayerByID($id) diff --git a/Website/models/ModelQuestion.php b/Website/models/ModelQuestion.php index 16fa822..a24074f 100644 --- a/Website/models/ModelQuestion.php +++ b/Website/models/ModelQuestion.php @@ -12,35 +12,70 @@ class ModelQuestion function getQuestions() { - $questions = $this->gwQuestion->getQuestions(); + $questionsDataArray = $this->gwQuestion->getQuestions(); + $questions = array(); + foreach ($questionsDataArray as $questionDataArray) { + $question = new Question( + intval($questionDataArray['id']), + $questionDataArray['content'], + intval($questionDataArray['idchapter']), + intval($questionDataArray['idanswergood']), + intval($questionDataArray['difficulty']), + intval($questionDataArray['nbfails']) + ); + $questions[$questionDataArray['id']] = $question; + } return $questions; } + + + function deleteQuestionByID($id) { $this->gwQuestion->deleteQuestionByID($id); } - function addQuestion($Question) + function addQuestion($questionsDataArray) { - $questionId = $this->gwQuestion->addQuestion($Question); + $questionId = $this->gwQuestion->addQuestion($questionsDataArray); return $questionId; } function getQuestionByID($id) { - $question = $this->gwQuestion->getQuestionByID($id); + $questionDataArray = $this->gwQuestion->getQuestionByID($id); + $question = new Question( + intval($questionDataArray['id']), + $questionDataArray['content'], + intval($questionDataArray['idchapter']), + intval($questionDataArray['idanswergood']), + intval($questionDataArray['difficulty']), + intval($questionDataArray['nbfails']) + ); return $question; } - function updateQuestion($id, $Question) + function updateQuestion($id, $questionDataArray) { - $this->gwQuestion->updateQuestion($id, $Question); + $this->gwQuestion->updateQuestion($id, $questionDataArray); } function getQuestionsByChapterAndDifficulty($chapter, $difficulty) { - $this->questions = $this->gwQuestion->getQuestionsByChapterAndDifficulty($chapter, $difficulty); + $questionsDataArray = $this->gwQuestion->getQuestionsByChapterAndDifficulty($chapter, $difficulty); + $this->questions = array(); + foreach ($questionsDataArray as $questionDataArray) { + $question = new Question( + intval($questionDataArray['id']), + $questionDataArray['content'], + intval($questionDataArray['idchapter']), + intval($questionDataArray['idanswergood']), + intval($questionDataArray['difficulty']), + intval($questionDataArray['nbfails']) + ); + $questions[$questionDataArray['id']] = $question; + } return $this->questions; } } diff --git a/Website/templates/adminQuestionsModal.twig b/Website/templates/adminQuestionsModal.twig index 0c54c15..edbb329 100644 --- a/Website/templates/adminQuestionsModal.twig +++ b/Website/templates/adminQuestionsModal.twig @@ -37,25 +37,25 @@ - Correct + Correct