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.
143 lines
5.5 KiB
143 lines
5.5 KiB
<?php
|
|
|
|
namespace Gateway;
|
|
|
|
use Config\Connection;
|
|
use Config\ConnectClass;
|
|
use PDO;
|
|
use PDOException;
|
|
|
|
class GatewayQuestion
|
|
{
|
|
private Connection $connection;
|
|
|
|
public function __construct()
|
|
{
|
|
try{
|
|
$this->connection = (new ConnectClass)->connect();
|
|
}catch(PDOException $e){
|
|
throw new PDOException($e->getMessage(), $e->getCode(), $e);
|
|
}
|
|
}
|
|
|
|
public function insertQuestion(string $contentQuestion, string $classQuestion, int $idForm, array $listPossibleResponse, array $listOfCategories): void
|
|
{
|
|
$gatewayPossibleResponse = new GatewayPossibleResponse();
|
|
|
|
$query = "INSERT INTO Question(content, type, form) VALUES(:content, :type, :form)";
|
|
$this->connection->executeQuery($query, array(
|
|
':content' => array($contentQuestion, PDO::PARAM_STR),
|
|
':type' => array($classQuestion, PDO::PARAM_STR),
|
|
':form' => array($idForm, PDO::PARAM_INT)
|
|
));
|
|
|
|
$idQuestion = $this->connection->lastInsertId();
|
|
|
|
if($classQuestion != "TextQuestionAPI"){
|
|
|
|
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 ($listOfCategories[$i] as $keyword){
|
|
$gatewayForm = new GatewayForm();
|
|
$gatewayForm->assignKeywordToQuestion($keyword, $listPossibleResponse[$i], $idQuestion);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function deleteQuestion(string $questionClass, int $idQuestion, array $questionGetPossibleResponse): void
|
|
{
|
|
if($questionClass == "BoxQuestionAPI") {
|
|
$query = "DELETE FROM Propose WHERE question = :id";
|
|
$this->connection->executeQuery($query, array(
|
|
':id' => array($idQuestion, PDO::PARAM_INT)
|
|
));
|
|
|
|
$listPossibleResponse = $questionGetPossibleResponse;
|
|
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($idQuestion, PDO::PARAM_INT)
|
|
));
|
|
}
|
|
|
|
public function updateQuestion(string $questionContent, string $questionClass, string $questionGetForm, string $idQuestion): void
|
|
{
|
|
$query = "UPDATE Question SET content = :content, type = :type, form = :form WHERE id = :id";
|
|
$this->connection->executeQuery($query, array(
|
|
':content' => array($questionContent, PDO::PARAM_STR),
|
|
':type' => array($questionClass, PDO::PARAM_STR),
|
|
':form' => array($questionGetForm, PDO::PARAM_STR),
|
|
':id' => array($idQuestion, PDO::PARAM_STR)
|
|
));
|
|
}
|
|
|
|
public function getAllQuestions(array $idForm): array
|
|
{
|
|
$query = "SELECT * FROM Question WHERE form = :form";
|
|
$this->connection->executeQuery($query, array(
|
|
':form' => array($idForm[0], 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(int $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];
|
|
}
|
|
} |