From 7a90d0aba2e518b3164752100d3f3242fdaef411 Mon Sep 17 00:00:00 2001 From: Ness Gaster Date: Mon, 30 Jan 2023 17:01:45 +0100 Subject: [PATCH 1/6] Gateway Question +insertQuestion(question: Question, idForm: string) --- .idea/php.xml | 2 +- Source/API/a | 0 Source/API/script/Gateway/Connection.php | 37 +++++++++++ Source/API/script/Gateway/GatewayQuestion.php | 61 +++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) delete mode 100644 Source/API/a create mode 100644 Source/API/script/Gateway/Connection.php create mode 100644 Source/API/script/Gateway/GatewayQuestion.php diff --git a/.idea/php.xml b/.idea/php.xml index b2ca53c..639f74d 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -9,7 +9,7 @@ - + diff --git a/Source/API/a b/Source/API/a deleted file mode 100644 index e69de29..0000000 diff --git a/Source/API/script/Gateway/Connection.php b/Source/API/script/Gateway/Connection.php new file mode 100644 index 0000000..768245b --- /dev/null +++ b/Source/API/script/Gateway/Connection.php @@ -0,0 +1,37 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } + + + /** * @param string $query + * @param array $parameters * + * @return bool Returns `true` on success, `false` otherwise + */ + public function executeQuery(string $query, array $parameters = []): bool + { + $this->stmt = parent::prepare($query); + foreach ($parameters as $name => $value) { + $this->stmt->bindValue($name, $value[0], $value[1]); + } + + return $this->stmt->execute(); + } + + public function getResults(): array + { + return $this->stmt->fetchall(); + + } +} \ No newline at end of file diff --git a/Source/API/script/Gateway/GatewayQuestion.php b/Source/API/script/Gateway/GatewayQuestion.php new file mode 100644 index 0000000..45f7bbe --- /dev/null +++ b/Source/API/script/Gateway/GatewayQuestion.php @@ -0,0 +1,61 @@ +connection = $connection; + } + + public function insertQuestion(Question $question, string $idForm) + { + $query = "INSERT INTO Question(content, type, form) VALUES(:content, :type, :form)"; + $this->connection->executeQuery($query, array( + ':content' => array($question->getContent(), PDO::PARAM_STR), + ':type' => array(get_class($question), PDO::PARAM_STR), + ':form' => array($idForm, PDO::PARAM_STR) + )); + + $this->connection->executeQuery(); + + $idQuestion = $this->connection->lastInsertId(); + + if(get_class($question) == BoxQuestion::class){ + $listPossibleResponse = $question->getPossibleResponses(); + + for($i = 0; $i < count($listPossibleResponse); $i++){ + + $query = "INSERT INTO PossibleResponse(content) VALUES(:content)"; + $this->connection->executeQuery($query, array( + ':content' => array($listPossibleResponse[$i], PDO::PARAM_STR) + )); + + $this->connection->executeQuery(); + + $idPossibleReponse = $this->connection->lastInsertId(); + + $query = "INSERT INTO Propose(question, possibleResponse) VALUES(:question, :possibleResponse)"; + $this->connection->executeQuery($query, array( + ':question' => array($idQuestion, PDO::PARAM_INT), + ':possibleResponse' => array($idPossibleReponse, PDO::PARAM_INT) + )); + + $this->connection->executeQuery(); + + + foreach ($question->getCategories()[$i] as $keyword){ + $gatewayForm = new GatewayForm($this->connection); + $gatewayForm->assignKeywordToQuestion($keyword, $idQuestion); + } + } + } + } +} \ No newline at end of file From 31c96ee1c854a957c01bb9872815f91e3758bf6d Mon Sep 17 00:00:00 2001 From: Ness Gaster Date: Mon, 30 Jan 2023 17:14:44 +0100 Subject: [PATCH 2/6] Gateway Question updateQuestion(Question :question) --- Source/API/script/Gateway/GatewayQuestion.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Source/API/script/Gateway/GatewayQuestion.php b/Source/API/script/Gateway/GatewayQuestion.php index 45f7bbe..ebf57fb 100644 --- a/Source/API/script/Gateway/GatewayQuestion.php +++ b/Source/API/script/Gateway/GatewayQuestion.php @@ -2,9 +2,9 @@ namespace API\Gateway; -use API\PDO; use FORM_BusinessClass\BoxQuestion; use FORM_BusinessClass\Question; +use PDO; class GatewayQuestion { @@ -58,4 +58,17 @@ class GatewayQuestion } } } + + public function updateQuestion(Question $question){ + $query = "UPDATE Question SET content = :content, type = :type, form = :form WHERE id = :id"; + $this->connection->executeQuery($query, array( + ':content' => array($question->getContent(), PDO::PARAM_STR), + ':type' => array(get_class($question), PDO::PARAM_STR), + ':form' => array($question->getForm(), PDO::PARAM_STR), + ':id' => array($question->getId(), PDO::PARAM_STR) + )); + + $this->connection->executeQuery(); + } + } \ No newline at end of file From e275d1f9a233646090d637e368779fabda2e7ff8 Mon Sep 17 00:00:00 2001 From: Ness Gaster Date: Mon, 30 Jan 2023 21:11:07 +0100 Subject: [PATCH 3/6] Gateway Question delete(Question :question) --- Source/API/script/Gateway/GatewayQuestion.php | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/Source/API/script/Gateway/GatewayQuestion.php b/Source/API/script/Gateway/GatewayQuestion.php index ebf57fb..f8ed3b8 100644 --- a/Source/API/script/Gateway/GatewayQuestion.php +++ b/Source/API/script/Gateway/GatewayQuestion.php @@ -59,7 +59,40 @@ class GatewayQuestion } } - public function updateQuestion(Question $question){ + public function deleteQuestion(Question $question) + { + if(get_class($question) == BoxQuestion::class) { + $query = "DELETE FROM Propose WHERE question = :id"; + $this->connection->executeQuery($query, array( + ':id' => array($question->getId(), PDO::PARAM_INT) + )); + $this->connection->executeQuery(); + + $listPossibleResponse = $question->getPossibleResponses(); + for ($i = 0; $i < count($listPossibleResponse); $i++){ + $query = "DELETE FROM Reference WHERE response = :id"; + $this->connection->executeQuery($query, array( + ':id' => array($listPossibleResponse[$i]->getId(), PDO::PARAM_INT) + )); + $this->connection->executeQuery(); + + $query = "DELETE FROM PossibleResponse WHERE id = :id"; + $this->connection->executeQuery($query, array( + ':id' => array($listPossibleResponse[$i]->getId(), PDO::PARAM_INT) + )); + $this->connection->executeQuery(); + } + } + + $query = "DELETE FROM Question WHERE id = :id"; + $this->connection->executeQuery($query, array( + ':id' => array($question->getId(), PDO::PARAM_INT) + )); + $this->connection->executeQuery(); + } + + public function updateQuestion(Question $question) + { $query = "UPDATE Question SET content = :content, type = :type, form = :form WHERE id = :id"; $this->connection->executeQuery($query, array( ':content' => array($question->getContent(), PDO::PARAM_STR), From e5c8d75d6f6f479368f76bce50135ab0dab9429d Mon Sep 17 00:00:00 2001 From: Ness Gaster Date: Mon, 30 Jan 2023 22:04:34 +0100 Subject: [PATCH 4/6] Gateway Question Select(idForm: string): array --- Source/API/script/Gateway/GatewayQuestion.php | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Source/API/script/Gateway/GatewayQuestion.php b/Source/API/script/Gateway/GatewayQuestion.php index f8ed3b8..d21a3a5 100644 --- a/Source/API/script/Gateway/GatewayQuestion.php +++ b/Source/API/script/Gateway/GatewayQuestion.php @@ -104,4 +104,48 @@ class GatewayQuestion $this->connection->executeQuery(); } + 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"; + $this->connection->executeQuery($query, array( + ':form' => array($idForm, PDO::PARAM_STR) + )); + + $this->connection->executeQuery(); + + $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) + )); + + $this->connection->executeQuery(); + + $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"; + $this->connection->executeQuery($query, array( + ':id' => array($row["id"], PDO::PARAM_STR) + )); + + $this->connection->executeQuery(); + + $tmpTab[] = $this->connection->getResults(); + } + $keywordResponse[] = $tmpTab; + } + else{ + $possibleResponse[] = null; + $keywordResponse[] = null; + } + } + return array($resultQuestion, $possibleResponse, $keywordResponse); + } } \ No newline at end of file From 454e643825273857f6a2be90f973252433435a51 Mon Sep 17 00:00:00 2001 From: Ness Gaster Date: Mon, 30 Jan 2023 22:28:50 +0100 Subject: [PATCH 5/6] Gateway Keyword --- Source/API/script/Gateway/GatewayKeyword.php | 41 ++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Source/API/script/Gateway/GatewayKeyword.php diff --git a/Source/API/script/Gateway/GatewayKeyword.php b/Source/API/script/Gateway/GatewayKeyword.php new file mode 100644 index 0000000..f1815f2 --- /dev/null +++ b/Source/API/script/Gateway/GatewayKeyword.php @@ -0,0 +1,41 @@ +connection = $connection; + } + + public function insertKeyword(Keyword $keyword) + { + $query = "INSERT INTO Keyword(id, word) VALUES(:id, :word)"; + $this->connection->executeQuery($query, array( + ':id' => array($keyword->getId(), PDO::PARAM_INT), + ':word' => array($keyword->getWord(), PDO::PARAM_INT) + )); + $this->connection->executeQuery(); + } + + public function deleteKeyword(Keyword $keyword) + { + $query = "DELETE FROM Keyword WHERE id = :id"; + $this->connection->executeQuery($query, array( + ':id' => array($keyword->getId(), PDO::PARAM_INT) + )); + $this->connection->executeQuery(); + } + + public function getAllKeyword(): array + { + $query = "SELECT * FROM Keyword"; + $this->connection->executeQuery($query); + $this->connection->executeQuery(); + + return $this->connection->getResults(); + } +} \ No newline at end of file From b1a080252493b99636d4697c66b2e139a5d64c91 Mon Sep 17 00:00:00 2001 From: Ness Gaster Date: Mon, 30 Jan 2023 23:20:00 +0100 Subject: [PATCH 6/6] Gateway Form assignKeywordToQuestion deleteKeywordFromQuestion --- Source/API/script/Gateway/GatewayForm.php | 55 +++++++++++++++++++ Source/API/script/Gateway/GatewayQuestion.php | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 Source/API/script/Gateway/GatewayForm.php diff --git a/Source/API/script/Gateway/GatewayForm.php b/Source/API/script/Gateway/GatewayForm.php new file mode 100644 index 0000000..208a3a7 --- /dev/null +++ b/Source/API/script/Gateway/GatewayForm.php @@ -0,0 +1,55 @@ +connection = $connection; + } + + public function assignKeywordToQuestion(Keyword $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"; + $this->connection->executeQuery($query, array( + ':id' => array($idQuestion, PDO::PARAM_STR), + ':response' => array($response, PDO::PARAM_STR) + )); + $this->connection->executeQuery(); + + $idPossibleResponse = $this->connection->getResults()[0][0]; + + + $query = "INSERT INTO Reference(id, word) VALUES(:id, :word)"; + $this->connection->executeQuery($query, array( + ':idResponse' => array($idPossibleResponse, PDO::PARAM_INT), + ':idKeword' => array($keyword->getId(), PDO::PARAM_INT) + )); + $this->connection->executeQuery(); + } + + public function deleteKeywordFromQuestion(Keyword $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( + ':id' => array($question->getId(), PDO::PARAM_STR), + ':response' => array($response, PDO::PARAM_STR) + )); + $this->connection->executeQuery(); + + $idPossibleResponse = $this->connection->getResults()[0][0]; + + $query = "DELETE FROM Reference WHERE response = :idResponse AND keyword = :idKeword"; + $this->connection->executeQuery($query, array( + ':idResponse' => array($idPossibleResponse, PDO::PARAM_INT), + ':idKeword' => array($keyword->getId(), PDO::PARAM_INT) + )); + $this->connection->executeQuery(); + } +} \ No newline at end of file diff --git a/Source/API/script/Gateway/GatewayQuestion.php b/Source/API/script/Gateway/GatewayQuestion.php index d21a3a5..dc87851 100644 --- a/Source/API/script/Gateway/GatewayQuestion.php +++ b/Source/API/script/Gateway/GatewayQuestion.php @@ -53,7 +53,7 @@ class GatewayQuestion foreach ($question->getCategories()[$i] as $keyword){ $gatewayForm = new GatewayForm($this->connection); - $gatewayForm->assignKeywordToQuestion($keyword, $idQuestion); + $gatewayForm->assignKeywordToQuestion($keyword, $listPossibleResponse[$i], $idQuestion); } } }