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.
SAE4.01_FORMULAIRE/Source/Model/ModelAdmin.php

289 lines
9.6 KiB

<?php
namespace Model;
use BusinessClass\Form;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use PDOException;
/**
* Permet de développer les fonctions appelées par le controllerAdmin pour gérer
* les actions de l'administrateur
*/
class ModelAdmin
{
private Client $client;
public function __construct(){
$this->client = new Client();
}
public function goToAdmin(): void
{
global $rep, $views;
try{
require_once($rep . $views['admin']);
} catch (PDOException $e) {
$error = $e->getMessage();
require_once($rep . $views['form']);
}
}
/**
* Permet de créer et d'ajouter une question et de retourner son ID afin de la
* reconnaitre facilement dans la suite du code.
*
* @return int
* @throws Exception
*/
public function addQuestion(): int
{
$questionContent = $_POST['question'];
$type = $_POST['type'];
try {
$question = new $type(0, $questionContent);
$res = $this->client->request('GET', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/getForm');
$form = json_decode($res->getBody());
if (!empty($form)) {
$res = $this->client->request('POST', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/addQuestion?
content='.$questionContent.'&
classQuestion='.get_class($question).'&
idForm='.$form[0]['id']
);
return json_decode($res->getBody());
}
}catch (GuzzleException $g){
throw new Exception($g->getMessage(),$g->getCode(),$g);
}
return -1;
}
/**
* Permet de supprimer une question du formulaire
*
* @return void
* @throws Exception
*/
public function deleteQuestion():void
{
$idQuestion = $_POST["idQuestion"];
$type = $_POST["type"];
try {
$res = $this->client->request('DELETE', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/deleteQuestion?
classQuestion='.$type.'&
id='.$idQuestion
);
if ($res->getStatusCode()!=200){
throw new Exception('DeleteQuestion failed');
}
}catch (GuzzleException $g){
throw new Exception($g->getMessage(),$g->getCode(),$g);
}
}
/**
* Permet d'ajouter une possibilité de réponse à une question en l'assignant à des catégories.
*
* @return void
* @throws Exception
*/
public function addResponse(): void
{
$idQuestion = $_POST['idQuestion'];
$response = $_POST['response'];
$categories = $_POST['categories'];
if ($categories == null) {
$categories = [];
}
try {
$this->client->request('POST', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/insertResponseInQuestion?
response='.$response.'&
categories='.$categories.'&
$idQuestion='.$idQuestion
);
}catch (GuzzleException $g){
throw new Exception($g->getMessage(),$g->getCode(),$g);
}
}
/**
* Permet de supprimer une possible réponse à une question
*
* @return void
* @throws Exception
*/
public function deleteResponse(): void
{
try {
$res = $this->client->request('DELETE', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/deletePossibleResponse?
id='.$_POST["possibleResponse"]
);
if ($res->getStatusCode()!=200){
throw new Exception('DeletePossibleResponse failed');
}
}catch (GuzzleException $g){
throw new Exception($g->getMessage(),$g->getCode(),$g);
}
}
/**
* Permet de créer un nouveau formulaire en précisant son titre et sa description.
*
* @return void
* @throws Exception
*/
public function createForm(): void
{
try {
$res = $this->client->request('GET', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/getForm');
$formulaire = json_decode($res->getBody());
if (empty($formulaire)) {
$form = new Form(0, "Votre avis nous intéresse !!!", "Description de notre formulaire", array());
$this->client->request('POST', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/insertForm?
title='.$form->getTitle().'&
description='.$form->getDescription()
);
}
}catch (GuzzleException $g){
throw new Exception($g->getMessage(),$g->getCode(),$g);
}
}
/**
* Permet d'ajouter une nouvelle catégorie (mot-clef)
*
* @return void
* @throws Exception
*/
public function addKeyword(): void
{
$keyword = $_POST['keyword'];
try {
$this->client->request('POST', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/insertKeyword?
keyword='.$keyword
);
}catch (GuzzleException $g){
throw new Exception($g->getMessage(),$g->getCode(),$g);
}
}
/**
* Permet de supprimer une catégorie (mot-clef)
*
* @return void
* @throws Exception
*/
public function deleteKeyword(): void
{
try {
$res = $this->client->request('DELETE', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/deleteKeyword?
keyword='.$_POST["idCateg"]
);
if ($res->getStatusCode()!=200){
throw new Exception('DeleteKeyword failed');
}
}catch (GuzzleException $g){
throw new Exception($g->getMessage(),$g->getCode(),$g);
}
}
/**
* Permet de récupérer toutes les catégories existantes.
*
* @return array
* @throws Exception
*/
public function getCategories(): array
{
$categories = [];
try {
$res = $this->client->request('GET', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/getAllKeyword');
$res = json_decode($res->getBody());
foreach ($res as $category) {
$categories[] = $category["word"];
}
}catch (GuzzleException $g){
throw new Exception($g->getMessage(),$g->getCode(),$g);
}
return $categories;
}
/**
* Permet de récupérer toutes les questions existantes.
*
* @return array
* @throws Exception
*/
public function getQuestions(): array
{
try {
$res = $this->client->request('GET', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/existsForm');
if (json_decode($res->getBody())){
$res = $this->client->request('GET', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/getForm');
$idForm = json_decode($res->getBody())[0]["id"];
$res = $this->client->request('GET', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/getAllQuestions?
idForm='.$idForm
);
$questionsArray = json_decode($res->getBody());
return Factory::getBuiltObjects($questionsArray, "Question");
}else{
return array();
}
}catch (GuzzleException $g){
throw new Exception($g->getMessage(),$g->getCode(),$g);
}
}
/**
* Permet de récupérer toutes les réponses existantes.
*
* @return array
* @throws Exception
*/
public function getResponsesCandidate(): array
{
try {
$res = $this->client->request('GET', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/getAllListResponsesOfCandidate');
$responsesCandidate = json_decode($res->getBody());
$results = [];
foreach ($responsesCandidate as $response) {
$res = $this->client->request('GET', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/getDetailsListResponsesOfCandidate?
id='.$response["id"]
);
$results[] = json_decode($res->getBody());
}
return $results;
}catch (GuzzleException $g){
throw new Exception($g->getMessage(),$g->getCode(),$g);
}
}
/**
* Permet de supprimer les réponses d'une personne d'un formulaire
*
* @return void
* @throws Exception
*/
public function deleteResponsesCandidate(): void
{
try {
$res = $this->client->request('DELETE', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/deleteListResponseOfCandidate?
id='.$_POST["idResponseCandidate"]
);
if ($res->getStatusCode()!=200){
throw new Exception('DeleteListResponseOfCandidate failed');
}
}catch (GuzzleException $g){
throw new Exception($g->getMessage(),$g->getCode(),$g);
}
}
}