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

141 lines
5.0 KiB

<?php
namespace Model;
use API\script\Gateway\GatewayForm;
use API\script\Gateway\GatewayListResponseOfCandidate;
use API\script\Gateway\GatewayQuestion;
use API\script\Gateway\GatewayAdmin;
use Config\Clean;
use Config\Validate;
use Exceptions\InvalidUsernameOrPasswordException;
use Exceptions\InvalidLoginOrPasswordException;
use Exceptions\InexistantLoginException;
/**
* 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 = [];
$questionsId = [];
foreach ($answersAndCategories as $answerAndCategory) {
$exploded = explode("||",$answerAndCategory);
if( count($exploded) == 3 ){
$questionsId[] = $exploded[0];
$answer[] = $exploded[1];
$categs = $exploded[2];
$categs = explode("_", $categs);
array_pop($categs);
$category[] = $categs;
}
else {
$questionsId[] = array_shift($dataIds);
$answer[] = $answerAndCategory;
$category[] = [];
}
}
$title = (new GatewayForm())->getForm()[0]["title"];
(new GatewayListResponseOfCandidate())->insertListResponsesOfCandidate($questionsId, $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");
$nbQuestions = count($questions);
$time = round(($nbQuestions * 20)/60);
$html = "<div class='container mt-5'>
<div class='row d-flex justify-content-center align-items-center'>
<div class='col-md-8'>
<form id='regForm' method='post' action='submitForm'>
<h1 id='register'>$title</h1>
<div class='all-steps' id='all-steps'>";
for ($i = 0; $i < sizeof($questions); $i++) {
$html.= "<span class='step'><i class='fa'></i></span>";
}
$html.= "</div>";
foreach ($questions as $question) {
$html.= $question->printStrategy()."\n";
}
if (count($questions) > 0) {
$html.= "<div class='thanks-message text-center' id='text-message'> <img src='https://i.imgur.com/O18mJ1K.png' width='100' class='mb-4'>
<h3>Souhaitez-vous envoyer vos réponses ?</span>
<input type='hidden' name='data_ids' value=''>
<input type='submit' value='Envoyer' id='button'>\n
<input type='hidden' name='action' value='submitForm'>
</div>
<div style='overflow:auto;' id='nextprevious'>
<div style='float:right;'>
<button type='button' id='prevBtn' onclick='nextPrev(-1)'><i class='fa fa-angle-double-left'></i></button>
<button type='button' id='nextBtn' onclick='nextPrev(1)'><i class='fa fa-angle-double-right'></i></button> </div>
</div>
</form>
</div>
</div>
</div>";
} else {
$html.= "\t\t</form>\n
\t</div>\n";
}
return $html;
}
public function login() :void {
global $rep,$views,$sel;
$password = Clean::simpleString($_REQUEST['password']);
$identifiant = Clean::simpleString($_REQUEST['login']);
$gatewayAdmin = new GatewayAdmin();
if (Validate::login($identifiant) && Validate::password($password)){
$passwordbdd=$gatewayAdmin->getPasswordWithLogin($identifiant);
if($passwordbdd==null) throw new InexistantLoginException();
if(password_verify($sel . $password,$passwordbdd)) {
$_SESSION['role'] = 'Admin';
} else {
$_SESSION['role'] = 'Visitor';
}
}
else throw new InvalidLoginOrPasswordException();
}
}