From 42aa62aece57e737ddd1363d4984625b17e62d14 Mon Sep 17 00:00:00 2001 From: brongniart Date: Tue, 21 Jan 2025 15:09:38 +0100 Subject: [PATCH] =?UTF-8?q?Cr=C3=A9ation=20du=20Quiz=20random?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.php | 4 +- index.php | 4 +- src/Controleur/FrontControler.php | 3 +- src/Controleur/UserControler.php | 103 +++++++++++++++++++++----- src/Gateway/QuestionGateway.php | 67 +++++++++++++++++ src/Model/QuestionModel.php | 43 +++++++++++ src/Model/QuizModel.php | 10 +-- vue/endQuiz.php | 2 - vue/endQuizRandom.php | 17 +++++ vue/quiz.php | 5 +- vue/quizRandom.php | 20 +++++ vue/templates/accueilQuiz.html.twig | 11 +++ vue/templates/endQuiz.html.twig | 10 +-- vue/templates/endQuizRandom.html.twig | 12 +++ vue/templates/quiz.html.twig | 8 +- vue/templates/quizRandom.html.twig | 27 +++++++ 16 files changed, 306 insertions(+), 40 deletions(-) create mode 100644 vue/endQuizRandom.php create mode 100644 vue/quizRandom.php create mode 100644 vue/templates/endQuizRandom.html.twig create mode 100644 vue/templates/quizRandom.html.twig diff --git a/config/config.php b/config/config.php index 671ef53..8bb400c 100644 --- a/config/config.php +++ b/config/config.php @@ -13,7 +13,7 @@ $login = ''; $mdp = ''; //$racine='/~kemondejar/WF-Website'; // /~kekentin/WF/WF-Website /~lebeaulato/WF-Website /~kemondejar/WF-Website -$racine='/WF-Website'; +$racine='/~kekentin/WF/WF-Website'; //$repImg='/home/UCA/kemondejar/public_html/WF-Website/public/images/'; $repImg='/'; @@ -34,6 +34,8 @@ $vues['endQuiz'] = 'vue/endQuiz.php'; $vues['submitQuote'] = 'vue/submitQuote.php'; $vues['recap'] = 'vue/recap.php'; $vues['accueilQuiz'] = 'vue/accueilQuiz.php'; +$vues['quizRandom'] = 'vue/quizRandom.php'; +$vues['endQuizRandom'] = 'vue/endQuizRandom.php'; diff --git a/index.php b/index.php index a92283c..a4837a8 100644 --- a/index.php +++ b/index.php @@ -15,8 +15,8 @@ if(!isset($_SESSION['theme'])){ require_once __DIR__ . '/config/config.php'; require __DIR__ . '/vendor/autoload.php'; -$co = new \Gateway\Connection('pgsql:host=localhost;dbname=wikifantasy3;', 'postgres', 'sucepute'); -//$co = new \Gateway\Connection('pgsql:host=localhost;dbname=dbkekentin;', 'kekentin', 'passwd'); +//$co = new \Gateway\Connection('pgsql:host=localhost;dbname=wikifantasy3;', 'postgres', 'sucepute'); +$co = new \Gateway\Connection('pgsql:host=localhost;dbname=dbkekentin;', 'kekentin', 'passwd'); //twig $loader = new \Twig\Loader\FilesystemLoader('vue/templates'); diff --git a/src/Controleur/FrontControler.php b/src/Controleur/FrontControler.php index 826751a..cddf172 100644 --- a/src/Controleur/FrontControler.php +++ b/src/Controleur/FrontControler.php @@ -16,7 +16,7 @@ Class FrontControler{ global $twig,$racine; $this->listAction = ['visitor' => array('accueil','search','quote','login','signin','validlogin','validsignin'), - 'user' => array('quiz','commentary','logout','addComment','favorite','profil','addFav','supFav','changedata', 'submit', 'validsubmit', 'add', 'accueilQuiz'), + 'user' => array('quiz','randomQuiz','commentary','logout','addComment','favorite','profil','addFav','supFav','changedata', 'submit', 'validsubmit', 'add', 'accueilQuiz'), 'admin' => array('null')]; $dVueEreur = []; @@ -56,6 +56,7 @@ Class FrontControler{ $router->map('GET|POST', '/add', 'UserControler', 'add'); $router->map('GET|POST', '/accueilQuiz', 'UserControler', 'accueilQuiz'); $router->map('GET|POST', '/error', 'VisitorControler','error'); + $router->map('GET|POST', '/randomQuiz', 'UserControler','randomQuiz'); $match = $router->match(); diff --git a/src/Controleur/UserControler.php b/src/Controleur/UserControler.php index a7a5100..49b29e8 100644 --- a/src/Controleur/UserControler.php +++ b/src/Controleur/UserControler.php @@ -53,6 +53,90 @@ class UserControler { $quizs = $this->quizModel->listQuiz(); require_once $vues['accueilQuiz']; } + + public function randomQuiz() { + global $vues; + + $canswer = Verification::verifChar($_POST['canswer'] ?? null); + + /*---------------------------------------------*/ + + if(!isset($_SESSION["no_question"])){ + $no_question = 0; + } + else{ + $no_question = Verification::verifChar($_SESSION["no_question"]); + + if($canswer!=null){ + $no_question=$no_question+1; + } + + if($no_question < 1 ){ + $no_question = 0; + } + } + $_SESSION["no_question"]=$no_question; + + /*---------------------------------------------*/ + + if(!isset($_SESSION["nbVie"])){ + $vie = 3; + } + else{ + $vie = Verification::verifChar($_SESSION["nbVie"]) ; + if($vie > 3 ){ + $vie = 3; + } + elseif($vie < 1){ + $vie = 0; + } + } + + /*---------------------------------------------*/ + + $answera = Verification::verifChar($_POST['answera'] ?? null); + $answerb = Verification::verifChar($_POST['answerb'] ?? null); + $answerc = Verification::verifChar($_POST['answerc'] ?? null); + $answerd = Verification::verifChar($_POST['answerd'] ?? null); + + + $answer = null; + + if ($answera) { + $answer = $answera; + } elseif ($answerb) { + $answer = $answerb; + } elseif ($answerc) { + $answer = $answerc; + } elseif ($answerd) { + $answer = $answerd; + } + if($answer != $canswer){ + $vie=$vie-1; + } + + /*---------------------------------------------*/ + + $_SESSION["nbVie"]=$vie; + + if($vie<=0){ + $score=($no_question - 3); + $next=false; + $nbQuestion=$no_question; + + unset($_SESSION["no_question"]); + unset($_SESSION["nbVie"]); + + require_once $vues['endQuizRandom']; + } + else{ + /*---------------------------------------------*/ + + $question = $this->questMod->QuestionForQuoteToCarac('fr'); + + require_once $vues['quizRandom']; + } + } public function profil() { @@ -530,25 +614,6 @@ class UserControler { } } - // Quiz aléatoire - - // argument = Langue / type question / type reponse - public function quizAleatoire(){ - global $vues; - if(!isset($_POST["nbVie"])){ - $vie = 3; - } - $question = $questMod->getRandomQuestion([$_POST["questionType"],$_POST["awnserType"]],$_POST["langue"]); - addTab($question); - if($question == null){ - $erreur = [501,"Erreur serveur lors de l'obtention de la question"]; - $require_once($vues["erreur"]); - } - else{ - $require_once($vues['quiz']); - } - } - public function questionSuivantAleatoire(){ } diff --git a/src/Gateway/QuestionGateway.php b/src/Gateway/QuestionGateway.php index 946ee4e..54ab309 100644 --- a/src/Gateway/QuestionGateway.php +++ b/src/Gateway/QuestionGateway.php @@ -102,4 +102,71 @@ class QuestionGateway extends Gateway } + + /*--------------------------------------- Quiz Aléatoire ---------------------------------------*/ + + /*----------------------- Question + Reponce Corecte -----------------------*/ + public function QuestionToReponceForQuoteToCarac(string $lang) : array + { + $query = "Select q.content as Question ,c.caracter as Reponce + from Quote Q + join Caracter c on c.id_caracter=q.id_caracter + where q.langue=:lang and q.isValide = 'true' + ORDER BY RANDOM() + LIMIT 1;"; + $this -> co -> executeQuery($query, [':lang' => array($lang, PDO::PARAM_STR)]); + return $this -> co -> getResults(); + } + + + public function QuestionToReponceForQuoteToTitle(string $lang) : array + { + $query = "Select q.content as Question ,s.title as Reponce + from Quote Q + join Source s on s.id_source=q.id_source + where q.langue=:lang and q.isValide = 'true' + ORDER BY RANDOM() + LIMIT 1;"; + $this -> co -> executeQuery($query, [':lang' => array($lang, PDO::PARAM_STR)]); + return $this -> co -> getResults(); + } + + /*----------------------- Reponce Fausse -----------------------*/ + + public function FakeReponceForQuoteToCarac(string $lang,string $corecteReponce) : array + { + $query = "Select c.caracter as Reponce + from Quote Q + join Caracter c on c.id_caracter=q.id_caracter + where q.langue = :lang And c.caracter != :corecteReponce and q.isValide = 'true' + ORDER BY RANDOM() + LIMIT 3;"; + + $this -> co -> executeQuery($query, [ + ':lang' => array($lang, PDO::PARAM_STR), + ':corecteReponce' => array($lang, PDO::PARAM_STR) + ]); + return $this -> co -> getResults(); + } + + + + public function FakeReponceForQuoteToTitle(string $lang,string $corecteReponce) : array + { + $query = "Select s.title as Reponce + from Quote Q + join Source s on s.id_source=q.id_source + where q.langue = :lang And s.title != :corecteReponce and q.isValide = 'true' + ORDER BY RANDOM() + LIMIT 3;"; + + $this -> co -> executeQuery($query, [ + ':lang' => array($lang, PDO::PARAM_STR), + ':corecteReponce' => array($lang, PDO::PARAM_STR) + ]); + return $this -> co -> getResults(); + } + + + } diff --git a/src/Model/QuestionModel.php b/src/Model/QuestionModel.php index 1e6db0a..33fd767 100644 --- a/src/Model/QuestionModel.php +++ b/src/Model/QuestionModel.php @@ -114,4 +114,47 @@ class QuestionModel extends Model return null; } } + + + /*--------------------------------------- Quiz Aléatoire ---------------------------------------*/ + + /*----------------------- Question compléte -----------------------*/ + + public function QuestionForQuoteToCarac(string $lang) : QuestionEntity + { + $questRep = $this->gateway->QuestionToReponceForQuoteToCarac($lang); + + + $quest = $questRep[0]['question']; + $cAnswer = $questRep[0]['reponce']; + + $fake = $this->gateway->FakeReponceForQuoteToCarac($lang,$cAnswer); + + $fakeRep = [$cAnswer]; + foreach ($fake as $rep){ + $fakeRep[]=$rep['reponce']; + } + shuffle($fakeRep); + + + return new QuestionEntity(-1,$quest,$fakeRep[0],$fakeRep[1],$fakeRep[2],$fakeRep[3],$cAnswer); + } + + public function QuestionForQuoteToTitle(string $lang) : QuestionEntity + { + $questRep = $this->gateway->QuestionToReponceForQuoteToTitle($lang); + + $quest = $questRep['question']; + $cAnswer = $questRep['reponce']; + + $fake = $this->gateway->FakeReponceForQuoteToTitle($lang,$cAnswer); + + $fakeRep = [$cAnswer]; + foreach ($fake as $rep){ + $fakeRep[]=$rep['reponce']; + } + $fakeRep=array_rand($fakeRep); + + return new QuestionEntity(-1,$quest,$fakeRep[0],$fakeRep[1],$fakeRep[2],$fakeRep[3],$cAnswer); + } } \ No newline at end of file diff --git a/src/Model/QuizModel.php b/src/Model/QuizModel.php index 8cc3383..edb52f9 100644 --- a/src/Model/QuizModel.php +++ b/src/Model/QuizModel.php @@ -2,7 +2,7 @@ namespace Model; -use Entity\quizEntity; +use Entity\QuizEntity; use Gateway\QuizGateway; use Gateway\Gateway; @@ -13,14 +13,14 @@ class QuizModel extends Model{ return $this -> gateway -> create($id_quiz, $nb_questions, $id_img, $title); } - public function getQuiz(int $id_quiz): ?quizEntity + public function getQuiz(int $id_quiz): ?QuizEntity { $q = $this -> gateway -> findQuizById($id_quiz); if ($q) { - return new quizEntity( + return new QuizEntity( $q[0]['id_quiz'], $q[0]['nb_quest'], - $q[0]['id_img'], + $q[0]['img'], $q[0]['title'] ); } @@ -40,7 +40,7 @@ class QuizModel extends Model{ foreach ($res as $quiz) { - $quizs[] = new quizEntity( + $quizs[] = new QuizEntity( $quiz['id_quiz'], $quiz['nb_quest'], $quiz['imgpath'], diff --git a/vue/endQuiz.php b/vue/endQuiz.php index 2c09dee..64e46f4 100644 --- a/vue/endQuiz.php +++ b/vue/endQuiz.php @@ -8,14 +8,12 @@ echo $twig->render('head.html.twig', [ 'scripts' => array("../public/script/theme-toggle-double-param.js") ]); - // Rendu du bandeau echo $twig->render('bandeau.html.twig'); echo $twig->render('endQuiz.html.twig', ['score' => $score, 'isNextQuiz' => $next, - 'nextquiz' => $id_quiz + 1, 'nb' => $nbQuestion]); ?> diff --git a/vue/endQuizRandom.php b/vue/endQuizRandom.php new file mode 100644 index 0000000..1ca94de --- /dev/null +++ b/vue/endQuizRandom.php @@ -0,0 +1,17 @@ +render('head.html.twig', [ + 'title' => "Quiz", + 'style' => "public/styles/styleEndQuiz.css", + 'scripts' => array("public/script/theme-toggle.js") +]); + +// Rendu du bandeau +echo $twig->render('bandeau.html.twig'); + +echo $twig->render('endQuizRandom.html.twig', + ['score' => $score +]); +?> \ No newline at end of file diff --git a/vue/quiz.php b/vue/quiz.php index f522f41..10c59cd 100644 --- a/vue/quiz.php +++ b/vue/quiz.php @@ -11,5 +11,8 @@ echo $twig->render('head.html.twig', [ echo $twig->render('bandeau.html.twig'); echo $twig->render('quiz.html.twig', [ - 'quizList' => $quizs + 'question' => $question, + 'id'=>$idquestion, + 'nb' => $num + 1, + 'nbFinal' => $nbQuestion ]); diff --git a/vue/quizRandom.php b/vue/quizRandom.php new file mode 100644 index 0000000..a70093d --- /dev/null +++ b/vue/quizRandom.php @@ -0,0 +1,20 @@ +render('head.html.twig', [ + 'title' => "Random Quiz", + 'style' => "public/styles/styleQuiz.css", + 'scripts' => array("public/script/theme-toggle.js") +]); + +// Rendu du bandeau +echo $twig->render('bandeau.html.twig'); + + + + +echo $twig->render('quizRandom.html.twig', [ + 'vie' => $vie, + 'question' => $question, + 'nb' => $no_question + 1 +]); diff --git a/vue/templates/accueilQuiz.html.twig b/vue/templates/accueilQuiz.html.twig index 81eefa3..8e4bf2c 100644 --- a/vue/templates/accueilQuiz.html.twig +++ b/vue/templates/accueilQuiz.html.twig @@ -1,5 +1,13 @@

Liste des Quiz

+ + + {% for quiz in quizs %} diff --git a/vue/templates/endQuiz.html.twig b/vue/templates/endQuiz.html.twig index 3944b2e..60b1435 100644 --- a/vue/templates/endQuiz.html.twig +++ b/vue/templates/endQuiz.html.twig @@ -3,11 +3,11 @@

nombre de réponse juste : {{ score }}

nombre de réponse fausse : {{ nb - score }}

pourcentage de réusite : {{ (100 * score) / nb }}%

- {% if nextquiz %} - - - - {% endif %} + + + + +

diff --git a/vue/templates/endQuizRandom.html.twig b/vue/templates/endQuizRandom.html.twig new file mode 100644 index 0000000..455a783 --- /dev/null +++ b/vue/templates/endQuizRandom.html.twig @@ -0,0 +1,12 @@ +

Résultat

+
+ +

nombre de réponse juste : {{ score }}

+ + + + + +
+ + \ No newline at end of file diff --git a/vue/templates/quiz.html.twig b/vue/templates/quiz.html.twig index 8615c60..6833145 100644 --- a/vue/templates/quiz.html.twig +++ b/vue/templates/quiz.html.twig @@ -4,16 +4,16 @@
- - - -
diff --git a/vue/templates/quizRandom.html.twig b/vue/templates/quizRandom.html.twig new file mode 100644 index 0000000..c4f3786 --- /dev/null +++ b/vue/templates/quizRandom.html.twig @@ -0,0 +1,27 @@ +

Quiz Random

+
+

{{ question.question }}

+ +

Vie restante : {{ vie }}

+ + +
+ + + + +
+ + + {{ nb }}/∞ +
+ +