commit
878984db2a
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace API\Gateway;
|
||||
|
||||
use PDO;
|
||||
|
||||
class Connection extends PDO
|
||||
{
|
||||
private $stmt;
|
||||
|
||||
public function __construct(string $dsn, string $username, string $password)
|
||||
{
|
||||
parent::__construct($dsn, $username, $password);
|
||||
$this->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();
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace API\Gateway;
|
||||
|
||||
use FORM_BusinessClass\Question;
|
||||
use PDO;
|
||||
|
||||
class GatewayForm
|
||||
{
|
||||
private Connection $connection;
|
||||
|
||||
public function __construct(Connection $connection)
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace API\Gateway;
|
||||
|
||||
class GatewayKeyword
|
||||
{
|
||||
private Connection $connection;
|
||||
|
||||
public function __construct(Connection $connection)
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
}
|
@ -0,0 +1,151 @@
|
||||
<?php
|
||||
|
||||
namespace API\Gateway;
|
||||
|
||||
use FORM_BusinessClass\BoxQuestion;
|
||||
use FORM_BusinessClass\Question;
|
||||
use PDO;
|
||||
|
||||
class GatewayQuestion
|
||||
{
|
||||
private Connection $connection;
|
||||
|
||||
public function __construct(Connection $connection)
|
||||
{
|
||||
$this->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, $listPossibleResponse[$i], $idQuestion);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
':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();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue