You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
5.9 KiB
152 lines
5.9 KiB
<?php
|
|
|
|
namespace API\Gateway;
|
|
|
|
use Connection;
|
|
use BusinessClass\BoxQuestion;
|
|
use 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);
|
|
}
|
|
} |