From 7a90d0aba2e518b3164752100d3f3242fdaef411 Mon Sep 17 00:00:00 2001 From: Ness Gaster Date: Mon, 30 Jan 2023 17:01:45 +0100 Subject: [PATCH] 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