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

112 lines
3.3 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'];
$id = [];
$answer = [];
$category = [];
foreach ($answersAndCategories as $answerAndCategory) {
$idAndAnswer = explode("||", $answerAndCategory)[0];
$id[] = explode("#", $idAndAnswer)[0];
$answer[] = explode("#", $idAndAnswer)[1];
$categs = explode("||", $answerAndCategory)[1];
$categs = explode("_", $categs);
array_pop($categs);
$category[] = $categs;
}
$titleForm = (new GatewayForm())->getForm()[0]["title"];
(new GatewayListResponseOfCandidate())->insertListResponsesOfCandidate($id, $answer, $category, $titleForm);
}
/**
* 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='action' value='submitForm'>
\t\t</form>\n
\t</div>\n";
} else {
$html.= "\t\t</form>\n
\t</div>\n";
}
return $html;
}
function login(): void
{
global $sel,$error;
if(validate::notEmpty($_REQUEST('email')) && validate::notEmpty($_REQUEST('password'))){
$email=Clean::email($_REQUEST('email'));
$password=Clean::password($_REQUEST('password'));
if(!validate::email($email) || !validate::password($password))
{
$error="Invalid email or password";
throw new InvalidEmailOrPasswordException();
}
}
else {
$error="Undefined email or password";
throw new UndefinedEmailOrPasswordException();
}
if(!password_verify($password . $sel, (new GatewayForm())->getPassword($email))){
$error="Wrong email or password";
$_SESSION['role']='Candidate';
throw new WrongEmailOrPasswordException();
}
else{
$_SESSION['role']='Admin';
}
}
}