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/ModelCandidate.php

90 lines
2.6 KiB

<?php
namespace Model;
use API\script\Gateway\GatewayForm;
use API\script\Gateway\GatewayListResponseOfCandidate;
use API\script\Gateway\GatewayQuestion;
/**
* Permet de développer les fonctions appelées par le controllerCandidate pour gérer
* les actions de l'utilisateur
*/
class ModelCandidate
{
/**
* Permet de soumettre et d'envoyer la réponse à un formulaire.
*
* @return void
*/
public function submitForm(): void
{
$answersAndCategories = $_POST['answers'];
$dataIds = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'submitForm') {
$dataIdsJson = $_POST['data_ids'];
$dataIds = json_decode($dataIdsJson);
}
$answer = [];
$category = [];
foreach ($answersAndCategories as $answerAndCategory) {
$answer[] = explode("||", $answerAndCategory)[0];
$categs = explode("||", $answerAndCategory)[1];
$categs = explode("_", $categs);
array_pop($categs);
$category[] = $categs;
}
$title = (new GatewayForm())->getForm()[0]["title"];
(new GatewayListResponseOfCandidate())->insertListResponsesOfCandidate($dataIds, $answer, $category, $title);
}
/**
* Permet de récupérer le code html à afficher dans la page du formulaire,
* on récupère donc le formulaire et toutes les questions qu'il contient.
* On les traduits en code HTML puis on le retourne. On utilise une Factory
* pour récupèrer les questions.
*
* @return string
*/
public function getForm(): string
{
$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 = Factory::getBuiltObjects($questionsTab, "Question");
$html = "
<h1>$title</h1>\n
<h3>$description</h3>\n
<div id='container_form'>\n
<form method='post'>\n";
foreach ($questions as $question) {
$html.= $question->printStrategy()."\n";
}
if (count($questions) > 0) {
$html.= "\t\t\t<input type='submit'>\n
\t\t\t<input type='hidden' name='data_ids' value=''>
\t\t\t<input type='hidden' name='action' value='submitForm'>
\t\t</form>\n
\t</div>\n";
} else {
$html.= "\t\t</form>\n
\t</div>\n";
}
return $html;
}
}