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.

146 lines
5.6 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();
} }
public function insertQuestion(QuestionAPI $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) != TextQuestionAPI::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(array($keyword, $listPossibleResponse[$i], $idQuestion));
}
}
}
}
public function deleteQuestion(QuestionAPI $question): void
{
if(get_class($question) == BoxQuestionAPI::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(QuestionAPI $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(array $idForm): array //print en json 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[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(array($listQuestions[$i]["id"])); //$this->connection->getResults();
$tmpTabKeyword = [];
$tmpTabPossibleResponse = [];
foreach ($possibleResponses as $row){
$tmpTabKeyword[] = $gatewayKeyword->getKeywordsContentByReference(array($row["id"]));
$tmpTabPossibleResponse[] = $row["content"];
}
$possibleResponsesContent[] = $tmpTabPossibleResponse;
$keywordsResponses[] = $tmpTabKeyword;
}
else{
$possibleResponsesContent[] = null;
$keywordsResponses[] = null;
}
}
return array($listQuestions, $possibleResponsesContent, $keywordsResponses);
}
return array();
}
public function getQuestionContentById(array $id): string
{
$query = "SELECT content FROM Question WHERE id = :id";
$this->connection->executeQuery($query, array(
':id' => array($id[0], PDO::PARAM_INT)
));
return $this->connection->getResults()[0][0];
}
}