test Model methods
continuous-integration/drone/push Build is passing Details

AdminInterface
Alexis 2 years ago
parent 0913b3bf82
commit b730acbf4d

@ -9,7 +9,7 @@
<component name="PHPCodeSnifferOptionsConfiguration">
<option name="transferred" value="true" />
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="8.2" />
<component name="PhpProjectSharedConfiguration" php_language_level="8.0" />
<component name="PhpStanOptionsConfiguration">
<option name="transferred" value="true" />
</component>

@ -13,10 +13,10 @@ class APIController
} else {
$action = $_REQUEST['action'];
}
$listGateway = array(""); //TODO : nom des différentes Gateway à mettre
$listGateway = array("\\Gateway\\GatewayForm", "\\Gateway\\GatewayKeyword", "\\Gateway\\GatewayQuestion");
foreach ($listGateway as $gateway) // Pour chaque Gateway
{
/* On regarde s'il implémente une fonction du même nom que l'action reçue */
/* On regarde si elle implémente une fonction du même nom que l'action reçue */
if(method_exists($gateway, $action))
{
(new $gateway)->$action(); // Si oui, on appelle cette fonction

@ -1,5 +1,10 @@
<?php
namespace API\script\Config;
use PDO;
use PDOStatement;
class Connection extends PDO
{
private PDOStatement $stmt;

@ -1,10 +1,12 @@
<?php
require_once "./Connection.php";
use API\script\Config\Connection;
require_once __DIR__ ."/Connection.php";
function connect(): int|Connection
{
function connect()
{
$dsn = "mysql:host=".$_ENV["HOST"].";dbname=".$_ENV["DATABASE"].";";
$login = $_ENV["USER"];
$password = $_ENV["PASSWORD"];
@ -13,8 +15,7 @@ function connect(): int|Connection
$connection = new Connection($dsn,$login,$password);
}catch (PDOException){
} catch (PDOException $e){
http_response_code(404);
return http_response_code();
}

@ -1,9 +1,12 @@
<?php
namespace API\Gateway;
namespace API\script\Gateway;
include(__DIR__ . "/../Config/config.php");
use API\script\Config\Connection;
use BusinessClass\Form;
use BusinessClass\Keyword;
use Connection;
use BusinessClass\Question;
use PDO;
@ -11,9 +14,29 @@ class GatewayForm
{
private Connection $connection;
public function __construct(Connection $connection)
public function __construct()
{
$this->connection = connect();
}
public function insertForm(Form $form): void
{
if(empty($this->getForm()))
{
$query = "INSERT INTO Form(title, description) VALUES(:title, :description)";
$this->connection->executeQuery($query, array(
':title' => array($form->getTitle(), PDO::PARAM_STR),
':description' => array($form->getDescription(), PDO::PARAM_STR)
));
}
}
public function getForm(): array
{
$this->connection = $connection;
$query = "SELECT * FROM Form";
$this->connection->executeQuery($query);
return $this->connection->getResults();
}
public function assignKeywordToQuestion(Keyword $keyword, string $response, int $idQuestion)
@ -23,17 +46,15 @@ class GatewayForm
':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)";
$query = "INSERT INTO Reference(possibleResponse, keyword) VALUES(:possibleResponse, :keyword)";
$this->connection->executeQuery($query, array(
':idResponse' => array($idPossibleResponse, PDO::PARAM_INT),
':idKeword' => array($keyword->getId(), PDO::PARAM_INT)
':possibleResponse' => array($idPossibleResponse, PDO::PARAM_INT),
':keyword' => array($keyword->getId(), PDO::PARAM_INT)
));
$this->connection->executeQuery();
}
public function deleteKeywordFromQuestion(Keyword $keyword, string $response, Question $question)
@ -43,7 +64,6 @@ class GatewayForm
':id' => array($question->getId(), PDO::PARAM_STR),
':response' => array($response, PDO::PARAM_STR)
));
$this->connection->executeQuery();
$idPossibleResponse = $this->connection->getResults()[0][0];
@ -52,6 +72,5 @@ class GatewayForm
':idResponse' => array($idPossibleResponse, PDO::PARAM_INT),
':idKeword' => array($keyword->getId(), PDO::PARAM_INT)
));
$this->connection->executeQuery();
}
}

@ -1,17 +1,19 @@
<?php
namespace API\Gateway;
namespace API\script\Gateway;
include(__DIR__ . "/../Config/config.php");
use API\script\Config\Connection;
use BusinessClass\Keyword;
use Connection;
class GatewayKeyword
{
private Connection $connection;
public function __construct(Connection $connection)
public function __construct()
{
$this->connection = $connection;
$this->connection = connect();
}
public function insertKeyword(Keyword $keyword)
@ -21,7 +23,6 @@ class GatewayKeyword
':id' => array($keyword->getId(), PDO::PARAM_INT),
':word' => array($keyword->getWord(), PDO::PARAM_INT)
));
$this->connection->executeQuery();
}
public function deleteKeyword(Keyword $keyword)
@ -30,14 +31,12 @@ class GatewayKeyword
$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();
}

@ -1,8 +1,10 @@
<?php
namespace API\Gateway;
namespace API\script\Gateway;
use Connection;
include(__DIR__ . "/../Config/config.php");
use API\script\Config\Connection;
use BusinessClass\BoxQuestion;
use BusinessClass\Question;
use PDO;
@ -11,22 +13,20 @@ class GatewayQuestion
{
private Connection $connection;
public function __construct(Connection $connection)
public function __construct()
{
$this->connection = $connection;
$this->connection = connect();
}
public function insertQuestion(Question $question, string $idForm)
public function insertQuestion(Question $question, int $idForm): void
{
$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)
':form' => array($idForm, PDO::PARAM_INT)
));
$this->connection->executeQuery();
$idQuestion = $this->connection->lastInsertId();
if(get_class($question) == BoxQuestion::class){
@ -39,18 +39,14 @@ class GatewayQuestion
':content' => array($listPossibleResponse[$i], PDO::PARAM_STR)
));
$this->connection->executeQuery();
$idPossibleReponse = $this->connection->lastInsertId();
$idPossibleResponse = $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)
':possibleResponse' => array($idPossibleResponse, PDO::PARAM_INT)
));
$this->connection->executeQuery();
foreach ($question->getCategories()[$i] as $keyword){
$gatewayForm = new GatewayForm($this->connection);
@ -60,14 +56,13 @@ class GatewayQuestion
}
}
public function deleteQuestion(Question $question)
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)
));
$this->connection->executeQuery();
$listPossibleResponse = $question->getPossibleResponses();
for ($i = 0; $i < count($listPossibleResponse); $i++){
@ -75,13 +70,11 @@ class GatewayQuestion
$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();
}
}
@ -89,10 +82,9 @@ class GatewayQuestion
$this->connection->executeQuery($query, array(
':id' => array($question->getId(), PDO::PARAM_INT)
));
$this->connection->executeQuery();
}
public function updateQuestion(Question $question)
public function updateQuestion(Question $question): void
{
$query = "UPDATE Question SET content = :content, type = :type, form = :form WHERE id = :id";
$this->connection->executeQuery($query, array(
@ -101,8 +93,6 @@ class GatewayQuestion
':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
@ -112,8 +102,6 @@ class GatewayQuestion
':form' => array($idForm, PDO::PARAM_STR)
));
$this->connection->executeQuery();
$resultQuestion = $this->connection->getResults();
$possibleResponse = [];
$keywordResponse = [];
@ -125,8 +113,6 @@ class GatewayQuestion
':id' => array($resultQuestion[$i]["id"], PDO::PARAM_STR)
));
$this->connection->executeQuery();
$possibleResponse[] = $this->connection->getResults();
$tmpTab = [];
foreach ($possibleResponse[$i] as $row){
@ -136,8 +122,6 @@ class GatewayQuestion
':id' => array($row["id"], PDO::PARAM_STR)
));
$this->connection->executeQuery();
$tmpTab[] = $this->connection->getResults();
}
$keywordResponse[] = $tmpTab;

@ -12,10 +12,11 @@ abstract class BoxQuestion extends Question
* @param array $possibleResponses
* @param string $content
* @param array $categories
* @param int $id
*/
public function __construct(array $possibleResponses, string $content, array $categories)
public function __construct(array $possibleResponses, string $content, array $categories, int $id)
{
parent::__construct($content);
parent::__construct($id, $content);
$this->categories = $categories;
$this->possibleResponses = $possibleResponses;
}

@ -8,10 +8,11 @@ class CheckBoxQuestion extends BoxQuestion
* @param array $possibleResponses
* @param string $content
* @param array $categories
* @param int $id
*/
public function __construct(array $possibleResponses, string $content, array $categories)
public function __construct(array $possibleResponses, string $content, array $categories, int $id)
{
parent::__construct($possibleResponses, $content, $categories);
parent::__construct($possibleResponses, $content, $categories, $id);
}
public function responseStrategy()

@ -4,17 +4,20 @@ namespace BusinessClass;
class Form
{
private int $id;
private string $title;
private string $description;
private array $questions; // La liste des questions dans un formulaire
/**
* @param int $id
* @param string $title
* @param string $description
* @param array $questions
*/
public function __construct(string $title, string $description, array $questions)
public function __construct(int $id, string $title, string $description, array $questions)
{
$this->id = $id;
$this->title = $title;
$this->description = $description;
$this->questions = $questions;
@ -67,4 +70,12 @@ class Form
{
$this->questions = $questions;
}
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
}

@ -4,13 +4,16 @@ namespace BusinessClass;
class Keyword
{
private int $id;
private string $word;
/**
* @param int $id
* @param string $word
*/
public function __construct(string $word)
public function __construct(int $id, string $word)
{
$this->id = $id;
$this->word = $word;
}
@ -29,4 +32,12 @@ class Keyword
{
$this->word = $word;
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
}

@ -8,10 +8,11 @@ class ListBoxQuestion extends BoxQuestion
* @param array $possibleResponses
* @param string $content
* @param array $categories
* @param int $id
*/
public function __construct(array $possibleResponses, string $content, array $categories)
public function __construct(array $possibleResponses, string $content, array $categories, int $id)
{
parent::__construct($possibleResponses, $content, $categories);
parent::__construct($possibleResponses, $content, $categories, $id);
}
public function responseStrategy()

@ -4,12 +4,16 @@ namespace BusinessClass;
abstract class Question implements IResponseProcessingStrategy, IPrintQuestionStrategy
{
private int $id;
private string $content;
/**
* @param int $id
* @param string $content
*/
public function __construct(string $content)
public function __construct(int $id, string $content)
{
$this->id = $id;
$this->content = $content;
}
@ -32,4 +36,12 @@ abstract class Question implements IResponseProcessingStrategy, IPrintQuestionSt
{
$this->content = $content;
}
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
}

@ -6,10 +6,11 @@ class TextQuestion extends Question
{
/**
* @param string $content
* @param int $id
*/
public function __construct(string $content)
public function __construct(string $content, int $id)
{
parent::__construct($content);
parent::__construct($id, $content);
}
public function responseStrategy()

@ -2,7 +2,19 @@
namespace Controller;
use API\Gateway\GatewayQuestion;
use BusinessClass\ListBoxQuestion;
use Model\ModelAdmin;
class ControllerAdmin
{
public function addQuestion(): void
{
(new ModelAdmin())->addQuestion();
}
public function createForm(): void
{
(new ModelAdmin())->createForm();
}
}

@ -0,0 +1,36 @@
<?php
namespace Model;
use API\script\Gateway\GatewayForm;
use API\script\Gateway\GatewayQuestion;
use BusinessClass\Form;
use BusinessClass\ListBoxQuestion;
class ModelAdmin
{
public function addQuestion(): void
{
$question = new ListBoxQuestion(array("Réponse1", "Réponse2", "Réponse3"),
"Quelle est votre réponse préférée ?",
array(array("Catégorie1.1", "Catégorie 1.2", "Catégorie1.3"), array("Catégorie2.1", "Catégorie 2.2"), array("Catégorie3")), 0);
$form = (new GatewayForm())->getForm();
if(!empty($form))
{
(new GatewayQuestion())->insertQuestion($question, $form[0]['id']);
}
}
public function createForm(): void
{
if(empty((new GatewayForm())->getForm()))
{
$form = new Form(0, "Votre avis nous intéresse !!!", "Description de notrer formulaire", array());
(new GatewayForm())->insertForm($form);
}
}
}

@ -2,11 +2,9 @@
namespace Model;
use BusinessClass\CheckBoxQuestion;
use BusinessClass\Form;
use BusinessClass\ListBoxQuestion;
use API\script\Gateway\GatewayForm;
use API\script\Gateway\GatewayQuestion;
use BusinessClass\TextQuestion;
use BusinessClass\YesNoQuestion;
class ModelCandidate
{
@ -17,7 +15,7 @@ class ModelCandidate
public function getForm(): string
{
/* TEMPORAIRE */
/*
$title = "Candidature à un témoignage";
$description = "Ce formulaire vous permet de candidater à un potentiel témoignage vidéo.";
$questions = array(
@ -27,7 +25,37 @@ class ModelCandidate
new CheckBoxQuestion(array("Mathématiques", "Web", "Mobile", "Gestion"), "Quels matières appréciez-vous ?", array(array("Maths", "Maths2", "Maths3"), array("Web"), array(""), array("Gestion", "Gestion2"))),
);
$form = new Form($title, $description, $questions);
/* ---------- */
*/
$form = (new GatewayForm())->getForm();
if(empty($form))
{
return "PAS DE FORMULAIRE\n";
}
$title = $form[0]['title'];
$description = $form[0]['description'];
$questionsTab = (new GatewayQuestion())->getAllQuestions($form[0]['id']);
$questions = [];
for($i = 0; $i < count($questionsTab[0]); $i++)
{
if(strcmp($questionsTab[0][$i]['type'], "TextQuestion") == 0)
{
$questions[] = new TextQuestion($questionsTab[0][$i]['content'], $questionsTab[0][$i]['id']);
}
else
{
$possiblesResponses = $questionsTab[1][$i];
$content = $questionsTab[0][$i]['content'];
$categories = $questionsTab[2][$i];
$id = $questionsTab[0][$i]['id'];
$questions[] = new $questionsTab[0][$i]['type']($possiblesResponses, $content, $categories, $id);
}
}
$html = "
<h1>$title</h1>\n

Loading…
Cancel
Save