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.
144 lines
5.5 KiB
144 lines
5.5 KiB
<?php
|
|
|
|
namespace API\script\Gateway;
|
|
|
|
use API\script\Config\Connection;
|
|
use BusinessClass\BoxQuestion;
|
|
use BusinessClass\Question;
|
|
use BusinessClass\TextQuestion;
|
|
use PDO;
|
|
|
|
class GatewayQuestion
|
|
{
|
|
private Connection $connection;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->connection = connect();
|
|
}
|
|
|
|
public function insertQuestion(Question $question, int $idForm): void
|
|
{
|
|
$gatewayPossibleResponse = new GatewayPossibleResponse();
|
|
|
|
$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_INT)
|
|
));
|
|
|
|
$idQuestion = $this->connection->lastInsertId();
|
|
|
|
if(get_class($question) != TextQuestion::class){
|
|
$listPossibleResponse = $question->getPossibleResponses();
|
|
|
|
for($i = 0; $i < count($listPossibleResponse); $i++){
|
|
|
|
$idPossibleResponse = $gatewayPossibleResponse->insertPossibleResponse($listPossibleResponse[$i]);
|
|
|
|
$query = "INSERT INTO Propose(question, possibleResponse) VALUES(:question, :possibleResponse)";
|
|
$this->connection->executeQuery($query, array(
|
|
':question' => array($idQuestion, PDO::PARAM_INT),
|
|
':possibleResponse' => array($idPossibleResponse, PDO::PARAM_INT)
|
|
));
|
|
|
|
|
|
foreach ($question->getCategories()[$i] as $keyword){
|
|
$gatewayForm = new GatewayForm();
|
|
$gatewayForm->assignKeywordToQuestion($keyword, $listPossibleResponse[$i], $idQuestion);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public function deleteQuestion(Question $question): void
|
|
{
|
|
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)
|
|
));
|
|
|
|
$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)
|
|
));
|
|
|
|
$query = "DELETE FROM PossibleResponse WHERE id = :id";
|
|
$this->connection->executeQuery($query, array(
|
|
':id' => array($listPossibleResponse[$i]->getId(), PDO::PARAM_INT)
|
|
));
|
|
}
|
|
}
|
|
|
|
$query = "DELETE FROM Question WHERE id = :id";
|
|
$this->connection->executeQuery($query, array(
|
|
':id' => array($question->getId(), PDO::PARAM_INT)
|
|
));
|
|
}
|
|
|
|
public function updateQuestion(Question $question): void
|
|
{
|
|
$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)
|
|
));
|
|
}
|
|
|
|
public function getAllQuestions(string $idForm): array //renvoie 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 form = :form";
|
|
$this->connection->executeQuery($query, array(
|
|
':form' => array($idForm, PDO::PARAM_INT)
|
|
));
|
|
|
|
$listQuestions = $this->connection->getResults();
|
|
$possibleResponsesContent = [];
|
|
$keywordsResponses = [];
|
|
$gatewayKeyword = new GatewayKeyword();
|
|
$gatewayPossibleResponse = new GatewayPossibleResponse();
|
|
|
|
if(!empty($listQuestions)) {
|
|
|
|
for ($i = 0; $i < count($listQuestions); $i++) {
|
|
|
|
if ($listQuestions[$i]["type"] != "BusinessClass/TextQuestion") {
|
|
$possibleResponses = $gatewayPossibleResponse->getPossibleResponseByQuestion($listQuestions[$i]["id"]); //$this->connection->getResults();
|
|
|
|
$tmpTabKeyword = [];
|
|
$tmpTabPossibleResponse = [];
|
|
foreach ($possibleResponses as $row){
|
|
$tmpTabKeyword[] = $gatewayKeyword->getKeywordsContentByReference($row["id"]);
|
|
|
|
$tmpTabPossibleResponse[] = $row["content"];
|
|
}
|
|
$possibleResponsesContent[] = $tmpTabPossibleResponse;
|
|
$keywordsResponses[] = $tmpTabKeyword;
|
|
}
|
|
else{
|
|
$possibleResponsesContent[] = null;
|
|
$keywordsResponses[] = null;
|
|
}
|
|
}
|
|
return array($listQuestions, $possibleResponsesContent, $keywordsResponses);
|
|
}
|
|
return array();
|
|
}
|
|
|
|
public function getQuestionContentById(string $id): string
|
|
{
|
|
$query = "SELECT content FROM Question WHERE id = :id";
|
|
$this->connection->executeQuery($query, array(
|
|
':id' => array($id, PDO::PARAM_INT)
|
|
));
|
|
|
|
return $this->connection->getResults()[0][0];
|
|
}
|
|
} |