| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- /**
- * Snippet : AuditExpress_Quiz
- * Vue "Question N/20" — 1 question par chargement de page, PRG pattern
- *
- * Propriétés :
- * &select_resource_id = `4` Ressource choix du pack
- * &optional_resource_id = `6` Ressource questions optionnelles
- *
- * Session attendue : ae_audit_uid, ae_pack_id
- *
- * POST action : ae_answer
- * body : question_id, option_id
- * → sauvegarde la réponse dans AuditRequest.answers
- * → incrémente AuditRequest.step
- * → redirige sur la même ressource (question suivante) ou vers optional
- */
- declare(strict_types=1);
- $corePath = $modx->getOption('ae.core_path', null,
- $modx->getOption('core_path') . 'components/audit_express/');
- $modx->addPackage('audit_express', $corePath . 'model/');
- require_once $corePath . 'src/PackRepository.php';
- $selectResourceId = (int) ($scriptProperties['select_resource_id'] ?? $modx->getOption('ae.resource.select', null, 0));
- $optionalResourceId = (int) ($scriptProperties['optional_resource_id'] ?? $modx->getOption('ae.resource.optional', null, 0));
- // ── Récupérer l'audit en cours ─────────────────────────────────────────────
- $auditUid = $_SESSION['ae_audit_uid'] ?? '';
- if (!$auditUid) {
- $url = $selectResourceId ? $modx->makeUrl($selectResourceId, '', '', 'full') : '/';
- $modx->sendRedirect($url);
- return '';
- }
- /** @var AuditRequest|null $auditReq */
- $auditReq = $modx->getObject('AuditRequest', ['audit_uid' => $auditUid]);
- if (!$auditReq) {
- unset($_SESSION['ae_audit_uid'], $_SESSION['ae_pack_id']);
- $url = $selectResourceId ? $modx->makeUrl($selectResourceId, '', '', 'full') : '/';
- $modx->sendRedirect($url);
- return '';
- }
- $packId = $auditReq->get('pack_id');
- $repo = new \AuditExpress\PackRepository($corePath . 'config/');
- $pack = $repo->getPack($packId);
- $questions = $pack['questions'] ?? [];
- $total = count($questions);
- // ── Traitement POST ────────────────────────────────────────────────────────
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['ae_action'] ?? '') === 'ae_answer') {
- $questionId = trim($_POST['question_id'] ?? '');
- $optionId = trim($_POST['option_id'] ?? '');
- // Valider que l'option appartient bien à la question
- $validOption = false;
- foreach ($questions as $q) {
- if ($q['id'] === $questionId) {
- foreach ($q['options'] as $opt) {
- if ($opt['id'] === $optionId) { $validOption = true; break 2; }
- }
- }
- }
- if ($validOption) {
- $answers = $auditReq->getAnswersArray();
- $answers[$questionId] = $optionId;
- $auditReq->setAnswersArray($answers);
- // Calculer le nouvel index : prochaine question sans réponse
- $nextStep = 0;
- foreach ($questions as $idx => $q) {
- if (empty($answers[$q['id']])) { $nextStep = $idx; break; }
- $nextStep = $idx + 1;
- }
- $auditReq->set('step', min($nextStep, $total));
- $auditReq->save();
- // Toutes les questions répondues → aller vers optional
- if ($nextStep >= $total) {
- $optUrl = $optionalResourceId
- ? $modx->makeUrl($optionalResourceId, '', '', 'full')
- : '/optional/';
- $modx->sendRedirect($optUrl);
- return '';
- }
- }
- // Rediriger vers la même page (PRG) pour afficher la prochaine question
- $modx->sendRedirect($modx->makeUrl($modx->resource->get('id'), '', '', 'full'));
- return '';
- }
- // ── Action "retour" ────────────────────────────────────────────────────────
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['ae_action'] ?? '') === 'ae_prev') {
- $currentStep = max(0, $auditReq->get('step') - 1);
- $auditReq->set('step', $currentStep);
- $auditReq->save();
- $modx->sendRedirect($modx->makeUrl($modx->resource->get('id'), '', '', 'full'));
- return '';
- }
- // ── Rendu GET ──────────────────────────────────────────────────────────────
- $step = (int) $auditReq->get('step');
- $step = max(0, min($step, $total - 1));
- $q = $questions[$step];
- $pos = $step + 1;
- $pct = (int) round(($step / $total) * 100);
- $isLast = ($step === $total - 1);
- $answers = $auditReq->getAnswersArray();
- $selected = $answers[$q['id']] ?? '';
- // Construire les options radio
- $optionsHtml = '';
- foreach ($q['options'] as $opt) {
- $isSelected = ($selected === $opt['id']);
- $isNa = ($opt['id'] === 'NA' || str_ends_with(strtolower($opt['id']), '_na'));
- $optionsHtml .= $modx->getChunk('ae_question_option', [
- 'question_id' => htmlspecialchars($q['id'], ENT_QUOTES),
- 'option_id' => htmlspecialchars($opt['id'], ENT_QUOTES),
- 'label' => htmlspecialchars($opt['label'], ENT_QUOTES),
- 'checked' => $isSelected ? 'checked' : '',
- 'cls_selected'=> $isSelected ? 'opt--selected' : '',
- 'cls_na' => $isNa ? 'opt--na' : '',
- ]);
- }
- $actionUrl = $modx->makeUrl($modx->resource->get('id'), '', '', 'full');
- $selectUrl = $selectResourceId ? $modx->makeUrl($selectResourceId, '', '', 'full') : '/';
- $remainLabel = $isLast
- ? 'Dernière question'
- : 'encore ' . ($total - $pos) . ' question' . ($total - $pos > 1 ? 's' : '');
- $progressHtml = $modx->getChunk('ae_quiz_progress', [
- 'pos' => $pos,
- 'total' => $total,
- 'pct' => $pct,
- 'remain_label' => htmlspecialchars($remainLabel, ENT_QUOTES),
- 'pack_title' => htmlspecialchars($pack['pack_title'] ?? $packId, ENT_QUOTES),
- ]);
- return $modx->getChunk('ae_quiz', [
- 'progress' => $progressHtml,
- 'question_id' => htmlspecialchars($q['id'], ENT_QUOTES),
- 'question' => htmlspecialchars($q['label'], ENT_QUOTES),
- 'help' => isset($q['help']) ? '<p class="qHelp">' . htmlspecialchars($q['help'], ENT_QUOTES) . '</p>' : '',
- 'options' => $optionsHtml,
- 'action_url' => $actionUrl,
- 'select_url' => $selectUrl,
- 'audit_uid' => htmlspecialchars($auditUid, ENT_QUOTES),
- 'can_prev' => $step > 0 ? '' : 'disabled',
- 'btn_next' => $isLast ? 'Voir mon résultat →' : 'Suivant →',
- 'btn_class' => $selected ? 'btn--primary' : 'btn--outline',
- 'can_next' => $selected ? '' : 'disabled',
- ]);
|