@ -36,7 +36,9 @@ class ControllerAdminQuestions
echo $twig->render($vues["adminQuestions"], [
'questions' => $questions,
'chapters' => $chapters,
'error' => $_SESSION["error"],
]);
$_SESSION["error"] = null;
} else {
header("Location:/loginAdmin");
}
@ -55,6 +57,22 @@ class ControllerAdminQuestions
function add($param)
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
$_SESSION["error"] = "Méthode non autorisée.";
header("Location:/admin/questions");
} else {
$trimmedContent = trim($_POST['content']);
$trimmedAnswer1 = trim($_POST['answer1']);
$trimmedAnswer2 = trim($_POST['answer2']);
$trimmedAnswer3 = trim($_POST['answer3']);
$trimmedAnswer4 = trim($_POST['answer4']);
if (
isset($_POST['content']) & & !empty($_POST['content']) & & !empty($trimmedContent)
& & isset($_POST['answer1']) & & !empty($_POST['answer1']) & & !empty($trimmedAnswer1)
& & isset($_POST['answer2']) & & !empty($_POST['answer2']) & & !empty($trimmedAnswer2)
& & isset($_POST['answer3']) & & !empty($_POST['answer3']) & & !empty($trimmedAnswer3)
& & isset($_POST['answer4']) & & !empty($_POST['answer4']) & & !empty($trimmedAnswer4)
) {
$content = $_POST['content'];
$idChapter = intval($_POST['idChapter']);
$AnswersPost = array();
@ -81,7 +99,6 @@ class ControllerAdminQuestions
];
}
$answersId = array();
for ($i = 0; $i < = 3; $i++) {
$answersId[$i] = $this->mdAnswer->addAnswer($Answers[$i]);
@ -97,16 +114,27 @@ class ControllerAdminQuestions
$this->mdQuestion->updateQuestion($idquestion, $Question);
header("Location:/admin/questions");
} else {
$_SESSION["error"] = "Veuillez remplir tous les champs";
header("Location:/admin/questions");
}
}
}
function updatemodal($param)
{
$question = $this->mdQuestion->getQuestionByID($param["id"]);
$answers = $this->mdAnswer->getAnswersByIDQuestions($param["id"]) ;
$question = $this->mdQuestion->getQuestionByID($param["id"]) ?? null ;
$chapters = $this->mdChapter->getChapters();
$answers = $this->mdAnswer->getAnswersByIDQuestions($param["id"]) ?? null;
$chapters = $this->mdChapter->getChapters() ?? null;
if ($question == null || $answers == null || $chapters == null) {
$_SESSION["error"] = "Erreur lors de la récupération des données";
header("Location:/admin/questions");
} else {
echo $this->twig->render($this->vues["adminQuestionsModal"], [
'question' => $question,
@ -114,10 +142,20 @@ class ControllerAdminQuestions
'answers' => $answers,
]);
}
}
function update($param)
{
if (
isset($_POST['content'], $_POST['answer1'], $_POST['answer2'], $_POST['answer3'], $_POST['answer4'], $_POST['idChapter'], $_POST['correctAnswer']) & &
!empty($_POST['content']) & & !empty(trim($_POST['content'])) & &
!empty($_POST['answer1']) & & !empty(trim($_POST['answer1'])) & &
!empty($_POST['answer2']) & & !empty(trim($_POST['answer2'])) & &
!empty($_POST['answer3']) & & !empty(trim($_POST['answer3'])) & &
!empty($_POST['answer4']) & & !empty(trim($_POST['answer4'])) & &
!empty($_POST['idChapter']) & &
is_numeric($_POST['correctAnswer']) & & $_POST['correctAnswer'] >= 0 & & $_POST['correctAnswer'] < = 3
) {
$id = intval($_POST['id']);
$content = $_POST['content'];
$idChapter = intval($_POST['idChapter']);
@ -156,5 +194,10 @@ class ControllerAdminQuestions
}
header("Location:/admin/questions");
} else {
$_SESSION["error"] = "Veuillez remplir tous les champs";
header("Location:/admin/questions");
}
}
}