From 504c7975d24ce2f1cf9c308c2ff644fa61d49ded Mon Sep 17 00:00:00 2001 From: Alexis Date: Tue, 31 Jan 2023 23:54:48 +0100 Subject: [PATCH] test Model methods --- Source/API/script/Config/config.php | 6 +- Source/API/script/Gateway/GatewayForm.php | 13 ++-- Source/API/script/Gateway/GatewayKeyword.php | 2 - Source/API/script/Gateway/GatewayQuestion.php | 61 +++++++++++-------- Source/Controller/ControllerAdmin.php | 2 - Source/Controller/ControllerCandidate.php | 1 + Source/Controller/FrontController.php | 4 +- Source/Model/ModelAdmin.php | 1 - Source/Model/ModelCandidate.php | 3 + Source/index.php | 4 +- 10 files changed, 54 insertions(+), 43 deletions(-) diff --git a/Source/API/script/Config/config.php b/Source/API/script/Config/config.php index 9515c1e..c577855 100644 --- a/Source/API/script/Config/config.php +++ b/Source/API/script/Config/config.php @@ -7,9 +7,9 @@ require_once __DIR__ ."/Connection.php"; function connect() { - $dsn = "mysql:host=".$_ENV["HOST"].";dbname=".$_ENV["DATABASE"].";"; - $login = $_ENV["USER"]; - $password = $_ENV["PASSWORD"]; + $dsn = "mysql:host=localhost;dbname=formulaire;charset=utf8"; + $login = "root"; + $password = "root"; try { diff --git a/Source/API/script/Gateway/GatewayForm.php b/Source/API/script/Gateway/GatewayForm.php index b639723..738dd7b 100644 --- a/Source/API/script/Gateway/GatewayForm.php +++ b/Source/API/script/Gateway/GatewayForm.php @@ -2,8 +2,6 @@ namespace API\script\Gateway; -include(__DIR__ . "/../Config/config.php"); - use API\script\Config\Connection; use BusinessClass\Form; use BusinessClass\Keyword; @@ -39,25 +37,26 @@ class GatewayForm return $this->connection->getResults(); } - public function assignKeywordToQuestion(Keyword $keyword, string $response, int $idQuestion) + public function assignKeywordToQuestion(string $keyword, string $response, int $idQuestion) { - $query = "SELECT pr.id FROM Propose p, PossibleResponse r WHERE p.question = :id AND p.possibleResponse = pr.id AND pr.content = :response"; + echo $keyword; + $query = "SELECT pr.id FROM Propose p, PossibleResponse pr WHERE p.question = :id AND p.possibleResponse = pr.id AND pr.content = :response"; $this->connection->executeQuery($query, array( ':id' => array($idQuestion, PDO::PARAM_STR), ':response' => array($response, PDO::PARAM_STR) )); - $idPossibleResponse = $this->connection->getResults()[0][0]; + $idPossibleResponse = $this->connection->getResults()[0][0]; $query = "INSERT INTO Reference(possibleResponse, keyword) VALUES(:possibleResponse, :keyword)"; $this->connection->executeQuery($query, array( ':possibleResponse' => array($idPossibleResponse, PDO::PARAM_INT), - ':keyword' => array($keyword->getId(), PDO::PARAM_INT) + ':keyword' => array($keyword, PDO::PARAM_STR) )); } - public function deleteKeywordFromQuestion(Keyword $keyword, string $response, Question $question) + public function deleteKeywordFromQuestion(string $keyword, string $response, Question $question) { $query = "SELECT pr.id FROM Propose p, PossibleResponse r WHERE p.question = :id AND p.possibleResponse = pr.id AND pr.content = :response"; $this->connection->executeQuery($query, array( diff --git a/Source/API/script/Gateway/GatewayKeyword.php b/Source/API/script/Gateway/GatewayKeyword.php index ea86e93..5068a8d 100644 --- a/Source/API/script/Gateway/GatewayKeyword.php +++ b/Source/API/script/Gateway/GatewayKeyword.php @@ -2,8 +2,6 @@ namespace API\script\Gateway; -include(__DIR__ . "/../Config/config.php"); - use API\script\Config\Connection; use BusinessClass\Keyword; diff --git a/Source/API/script/Gateway/GatewayQuestion.php b/Source/API/script/Gateway/GatewayQuestion.php index f5df47c..14bc153 100644 --- a/Source/API/script/Gateway/GatewayQuestion.php +++ b/Source/API/script/Gateway/GatewayQuestion.php @@ -2,11 +2,10 @@ namespace API\script\Gateway; -include(__DIR__ . "/../Config/config.php"); - use API\script\Config\Connection; use BusinessClass\BoxQuestion; use BusinessClass\Question; +use BusinessClass\TextQuestion; use PDO; class GatewayQuestion @@ -29,7 +28,7 @@ class GatewayQuestion $idQuestion = $this->connection->lastInsertId(); - if(get_class($question) == BoxQuestion::class){ + if(get_class($question) != TextQuestion::class){ $listPossibleResponse = $question->getPossibleResponses(); for($i = 0; $i < count($listPossibleResponse); $i++){ @@ -49,11 +48,12 @@ class GatewayQuestion foreach ($question->getCategories()[$i] as $keyword){ - $gatewayForm = new GatewayForm($this->connection); + $gatewayForm = new GatewayForm(); $gatewayForm->assignKeywordToQuestion($keyword, $listPossibleResponse[$i], $idQuestion); } } } + } public function deleteQuestion(Question $question): void @@ -97,40 +97,51 @@ class GatewayQuestion public function getAllQuestions(string $idForm): array //revoie un array contenant trois qui pour chaque indice commun de ces 3 array une question, sa liste de reponse possible et sa liste de keyword associer au réponse. les deux autres sont null si c'est une textBox { - $query = "SELECT * FROM Question WHERE from = :form"; + $query = "SELECT * FROM Question WHERE form = :form"; $this->connection->executeQuery($query, array( ':form' => array($idForm, PDO::PARAM_STR) )); + + $resultQuestion = $this->connection->getResults(); $possibleResponse = []; $keywordResponse = []; - for ($i=0; $i < count($resultQuestion); $i++){ - if($resultQuestion[$i]["type"]!="TextQuestion"){ - $query = "SELECT pr.* FROM Propose p, PossibleResponse pr - WHERE p.question = :questionId AND p.possibleResponse = pr.id"; - $this->connection->executeQuery($query, array( - ':id' => array($resultQuestion[$i]["id"], PDO::PARAM_STR) - )); - $possibleResponse[] = $this->connection->getResults(); - $tmpTab = []; - foreach ($possibleResponse[$i] as $row){ - $query = "SELECT k.* FROM Keyword k, Reference r - WHERE k.id = r.keyword AND r.response = :id"; + if(!empty($resultQuestion)) + { + for ($i=0; $i < count($resultQuestion); $i++) + { + if($resultQuestion[$i]["type"]!="TextQuestion") + { + $query = "SELECT pr.* FROM Propose p, PossibleResponse pr + WHERE p.question = :questionId AND p.possibleResponse = pr.id"; $this->connection->executeQuery($query, array( - ':id' => array($row["id"], PDO::PARAM_STR) + ':questionId' => array($resultQuestion[$i]["id"], PDO::PARAM_INT) )); - $tmpTab[] = $this->connection->getResults(); + $possibleResponse[] = $this->connection->getResults(); + + $tmpTab = []; + foreach ($possibleResponse[$i] as $row){ + var_dump($row); + $query = "SELECT k.* FROM Keyword k, Reference r + WHERE k.word = r.keyword AND r.possibleResponse = :id"; + $this->connection->executeQuery($query, array( + ':id' => array($row["id"], PDO::PARAM_INT) + )); + + $tmpTab[] = $this->connection->getResults(); + } + $keywordResponse[] = $tmpTab; + } + else{ + $possibleResponse[] = null; + $keywordResponse[] = null; } - $keywordResponse[] = $tmpTab; - } - else{ - $possibleResponse[] = null; - $keywordResponse[] = null; } + return array($resultQuestion, $possibleResponse, $keywordResponse); } - return array($resultQuestion, $possibleResponse, $keywordResponse); + return array(); } } \ No newline at end of file diff --git a/Source/Controller/ControllerAdmin.php b/Source/Controller/ControllerAdmin.php index a1b6aab..0ac775d 100644 --- a/Source/Controller/ControllerAdmin.php +++ b/Source/Controller/ControllerAdmin.php @@ -2,8 +2,6 @@ namespace Controller; -use API\Gateway\GatewayQuestion; -use BusinessClass\ListBoxQuestion; use Model\ModelAdmin; class ControllerAdmin diff --git a/Source/Controller/ControllerCandidate.php b/Source/Controller/ControllerCandidate.php index c8631eb..7a8a0f0 100644 --- a/Source/Controller/ControllerCandidate.php +++ b/Source/Controller/ControllerCandidate.php @@ -10,6 +10,7 @@ class ControllerCandidate { global $rep, $views; $html = (new ModelCandidate())->getForm(); + require($rep.$views['form']); } diff --git a/Source/Controller/FrontController.php b/Source/Controller/FrontController.php index 5213e35..7e5155e 100644 --- a/Source/Controller/FrontController.php +++ b/Source/Controller/FrontController.php @@ -18,7 +18,7 @@ class FrontController try { /* Si l'action est NULL on appelle goToTestimony(), sinon on nettoie l'action */ - $action = $_REQUEST['action'] ? $action = $_REQUEST['action'] : (new ControllerCandidate)->goToForm(); + $action = $_REQUEST['action'] ? $action = $_REQUEST['action'] : (new ControllerCandidate())->goToForm(); foreach ($listControllers as $controller) // Pour chaque Controller { @@ -31,7 +31,7 @@ class FrontController } catch (PDOException|Exception $e) { $dVueError[] = "Erreur innatendue !"; // Ecriture du message d'erreur - require ($rep.$views['error']); // Affichage de la vue d'erreur + echo "ERREUUUUUR"; } exit(0); diff --git a/Source/Model/ModelAdmin.php b/Source/Model/ModelAdmin.php index 6ff29a9..bb718e2 100644 --- a/Source/Model/ModelAdmin.php +++ b/Source/Model/ModelAdmin.php @@ -24,7 +24,6 @@ class ModelAdmin public function createForm(): void { - if(empty((new GatewayForm())->getForm())) { $form = new Form(0, "Votre avis nous intéresse !!!", "Description de notrer formulaire", array()); diff --git a/Source/Model/ModelCandidate.php b/Source/Model/ModelCandidate.php index 0ff4df1..591b068 100644 --- a/Source/Model/ModelCandidate.php +++ b/Source/Model/ModelCandidate.php @@ -34,6 +34,7 @@ class ModelCandidate return "PAS DE FORMULAIRE\n"; } + $title = $form[0]['title']; $description = $form[0]['description']; $questionsTab = (new GatewayQuestion())->getAllQuestions($form[0]['id']); @@ -51,6 +52,7 @@ class ModelCandidate $possiblesResponses = $questionsTab[1][$i]; $content = $questionsTab[0][$i]['content']; $categories = $questionsTab[2][$i]; + $id = $questionsTab[0][$i]['id']; $questions[] = new $questionsTab[0][$i]['type']($possiblesResponses, $content, $categories, $id); @@ -63,6 +65,7 @@ class ModelCandidate
\n
\n"; + foreach ($questions as $question) { $html.= $question->printStrategy()."\n"; diff --git a/Source/index.php b/Source/index.php index b18efe1..ae15593 100644 --- a/Source/index.php +++ b/Source/index.php @@ -3,6 +3,8 @@ use Controller\FrontController; + +require_once(__DIR__ . "/API/script/Config/config.php"); require_once(__DIR__.'/Config/config.php'); require_once(__DIR__.'/Config/Autoload.php'); Autoload::charger(); @@ -11,4 +13,4 @@ session_start(); $frontController = new FrontController(); -?> \ No newline at end of file +?>