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']) ? '
' . htmlspecialchars($q['help'], ENT_QUOTES) . '
' : '', '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', ]);