getOption('ae.core_path', null, $modx->getOption('core_path') . 'components/audit_express/'); $modx->addPackage('audit_express', $corePath . 'model/'); require_once $corePath . 'src/PackRepository.php'; require_once $corePath . 'src/Engine.php'; require_once $corePath . 'src/RulesRepository.php'; $quizResourceId = (int) ($scriptProperties['quiz_resource_id'] ?? $modx->getOption('ae.resource.quiz', null, 0)); $resultsResourceId = (int) ($scriptProperties['results_resource_id'] ?? $modx->getOption('ae.resource.results', null, 0)); // ── Récupérer l'audit ────────────────────────────────────────────────────── $auditUid = $_SESSION['ae_audit_uid'] ?? ''; if (!$auditUid) { $modx->sendRedirect($quizResourceId ? $modx->makeUrl($quizResourceId, '', '', 'full') : '/'); return ''; } /** @var AuditRequest|null $auditReq */ $auditReq = $modx->getObject('AuditRequest', ['audit_uid' => $auditUid]); if (!$auditReq) { $modx->sendRedirect($quizResourceId ? $modx->makeUrl($quizResourceId, '', '', 'full') : '/'); return ''; } $packId = $auditReq->get('pack_id'); $repo = new \AuditExpress\PackRepository($corePath . 'config/'); $pack = $repo->getPack($packId); // ── POST : sauvegarder + calculer ───────────────────────────────────────── if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['ae_action'] ?? '') === 'ae_optional') { // Collecter les données optionnelles postées (valeurs numériques) $optional = []; foreach ($pack['optional_questions'] ?? [] as $key => $oq) { if (isset($_POST['opt_' . $key])) { $optional[$key] = (int) $_POST['opt_' . $key]; } } $auditReq->setOptionalArray($optional); // Calcul du score $rulesRepo = new \AuditExpress\RulesRepository($corePath); $rulesBundle = $rulesRepo->getRules(); $answers = $auditReq->getAnswersArray(); try { $result = \AuditExpress\Engine::compute($pack, $answers, $optional, $rulesBundle); $auditReq->setResultArray($result); $auditReq->set('status', 'completed'); $auditReq->save(); } catch (\InvalidArgumentException $e) { // Réponse(s) manquante(s) → retour quiz $modx->sendRedirect($quizResourceId ? $modx->makeUrl($quizResourceId, '', '', 'full') : '/'); return ''; } $resultsUrl = $resultsResourceId ? $modx->makeUrl($resultsResourceId, '', 'audit=' . $auditUid, 'full') : '/resultats/?audit=' . $auditUid; $modx->sendRedirect($resultsUrl); return ''; } // ── Rendu GET ────────────────────────────────────────────────────────────── $optQs = $pack['optional_questions'] ?? []; $optional = $auditReq->getOptionalArray(); $quizUrl = $quizResourceId ? $modx->makeUrl($quizResourceId, '', '', 'full') : '/'; $actionUrl = $modx->makeUrl($modx->resource->get('id'), '', '', 'full'); $fieldsHtml = ''; foreach ($optQs as $key => $oq) { $cur = $optional[$key] ?? null; $radiosHtml = ''; foreach ($oq['options'] as $opt) { $val = (string) ($opt['value'] ?? $opt['id']); $radiosHtml .= $modx->getChunk('ae_optional_radio', [ 'key' => htmlspecialchars($key, ENT_QUOTES), 'value' => htmlspecialchars($val, ENT_QUOTES), 'label' => htmlspecialchars($opt['label'], ENT_QUOTES), 'checked' => ((string) $cur === $val) ? 'checked' : '', ]); } $fieldsHtml .= $modx->getChunk('ae_optional_field', [ 'label' => htmlspecialchars($oq['label'], ENT_QUOTES), 'radios' => $radiosHtml, ]); } return $modx->getChunk('ae_optional', [ 'fields' => $fieldsHtml, 'action_url' => $actionUrl, 'quiz_url' => $quizUrl, 'audit_uid' => htmlspecialchars($auditUid, ENT_QUOTES), ]);