AuditExpress_Quiz.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Snippet : AuditExpress_Quiz
  4. * Vue "Question N/20" — 1 question par chargement de page, PRG pattern
  5. *
  6. * Propriétés :
  7. * &select_resource_id = `4` Ressource choix du pack
  8. * &optional_resource_id = `6` Ressource questions optionnelles
  9. *
  10. * Session attendue : ae_audit_uid, ae_pack_id
  11. *
  12. * POST action : ae_answer
  13. * body : question_id, option_id
  14. * → sauvegarde la réponse dans AuditRequest.answers
  15. * → incrémente AuditRequest.step
  16. * → redirige sur la même ressource (question suivante) ou vers optional
  17. */
  18. declare(strict_types=1);
  19. $corePath = $modx->getOption('ae.core_path', null,
  20. $modx->getOption('core_path') . 'components/audit_express/');
  21. $modx->addPackage('audit_express', $corePath . 'model/');
  22. require_once $corePath . 'src/PackRepository.php';
  23. $selectResourceId = (int) ($scriptProperties['select_resource_id'] ?? $modx->getOption('ae.resource.select', null, 0));
  24. $optionalResourceId = (int) ($scriptProperties['optional_resource_id'] ?? $modx->getOption('ae.resource.optional', null, 0));
  25. // ── Récupérer l'audit en cours ─────────────────────────────────────────────
  26. $auditUid = $_SESSION['ae_audit_uid'] ?? '';
  27. if (!$auditUid) {
  28. $url = $selectResourceId ? $modx->makeUrl($selectResourceId, '', '', 'full') : '/';
  29. $modx->sendRedirect($url);
  30. return '';
  31. }
  32. /** @var AuditRequest|null $auditReq */
  33. $auditReq = $modx->getObject('AuditRequest', ['audit_uid' => $auditUid]);
  34. if (!$auditReq) {
  35. unset($_SESSION['ae_audit_uid'], $_SESSION['ae_pack_id']);
  36. $url = $selectResourceId ? $modx->makeUrl($selectResourceId, '', '', 'full') : '/';
  37. $modx->sendRedirect($url);
  38. return '';
  39. }
  40. $packId = $auditReq->get('pack_id');
  41. $repo = new \AuditExpress\PackRepository($corePath . 'config/');
  42. $pack = $repo->getPack($packId);
  43. $questions = $pack['questions'] ?? [];
  44. $total = count($questions);
  45. // ── Traitement POST ────────────────────────────────────────────────────────
  46. if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['ae_action'] ?? '') === 'ae_answer') {
  47. $questionId = trim($_POST['question_id'] ?? '');
  48. $optionId = trim($_POST['option_id'] ?? '');
  49. // Valider que l'option appartient bien à la question
  50. $validOption = false;
  51. foreach ($questions as $q) {
  52. if ($q['id'] === $questionId) {
  53. foreach ($q['options'] as $opt) {
  54. if ($opt['id'] === $optionId) { $validOption = true; break 2; }
  55. }
  56. }
  57. }
  58. if ($validOption) {
  59. $answers = $auditReq->getAnswersArray();
  60. $answers[$questionId] = $optionId;
  61. $auditReq->setAnswersArray($answers);
  62. // Calculer le nouvel index : prochaine question sans réponse
  63. $nextStep = 0;
  64. foreach ($questions as $idx => $q) {
  65. if (empty($answers[$q['id']])) { $nextStep = $idx; break; }
  66. $nextStep = $idx + 1;
  67. }
  68. $auditReq->set('step', min($nextStep, $total));
  69. $auditReq->save();
  70. // Toutes les questions répondues → aller vers optional
  71. if ($nextStep >= $total) {
  72. $optUrl = $optionalResourceId
  73. ? $modx->makeUrl($optionalResourceId, '', '', 'full')
  74. : '/optional/';
  75. $modx->sendRedirect($optUrl);
  76. return '';
  77. }
  78. }
  79. // Rediriger vers la même page (PRG) pour afficher la prochaine question
  80. $modx->sendRedirect($modx->makeUrl($modx->resource->get('id'), '', '', 'full'));
  81. return '';
  82. }
  83. // ── Action "retour" ────────────────────────────────────────────────────────
  84. if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['ae_action'] ?? '') === 'ae_prev') {
  85. $currentStep = max(0, $auditReq->get('step') - 1);
  86. $auditReq->set('step', $currentStep);
  87. $auditReq->save();
  88. $modx->sendRedirect($modx->makeUrl($modx->resource->get('id'), '', '', 'full'));
  89. return '';
  90. }
  91. // ── Rendu GET ──────────────────────────────────────────────────────────────
  92. $step = (int) $auditReq->get('step');
  93. $step = max(0, min($step, $total - 1));
  94. $q = $questions[$step];
  95. $pos = $step + 1;
  96. $pct = (int) round(($step / $total) * 100);
  97. $isLast = ($step === $total - 1);
  98. $answers = $auditReq->getAnswersArray();
  99. $selected = $answers[$q['id']] ?? '';
  100. // Construire les options radio
  101. $optionsHtml = '';
  102. foreach ($q['options'] as $opt) {
  103. $isSelected = ($selected === $opt['id']);
  104. $isNa = ($opt['id'] === 'NA' || str_ends_with(strtolower($opt['id']), '_na'));
  105. $optionsHtml .= $modx->getChunk('ae_question_option', [
  106. 'question_id' => htmlspecialchars($q['id'], ENT_QUOTES),
  107. 'option_id' => htmlspecialchars($opt['id'], ENT_QUOTES),
  108. 'label' => htmlspecialchars($opt['label'], ENT_QUOTES),
  109. 'checked' => $isSelected ? 'checked' : '',
  110. 'cls_selected'=> $isSelected ? 'opt--selected' : '',
  111. 'cls_na' => $isNa ? 'opt--na' : '',
  112. ]);
  113. }
  114. $actionUrl = $modx->makeUrl($modx->resource->get('id'), '', '', 'full');
  115. $selectUrl = $selectResourceId ? $modx->makeUrl($selectResourceId, '', '', 'full') : '/';
  116. $remainLabel = $isLast
  117. ? 'Dernière question'
  118. : 'encore ' . ($total - $pos) . ' question' . ($total - $pos > 1 ? 's' : '');
  119. $progressHtml = $modx->getChunk('ae_quiz_progress', [
  120. 'pos' => $pos,
  121. 'total' => $total,
  122. 'pct' => $pct,
  123. 'remain_label' => htmlspecialchars($remainLabel, ENT_QUOTES),
  124. 'pack_title' => htmlspecialchars($pack['pack_title'] ?? $packId, ENT_QUOTES),
  125. ]);
  126. return $modx->getChunk('ae_quiz', [
  127. 'progress' => $progressHtml,
  128. 'question_id' => htmlspecialchars($q['id'], ENT_QUOTES),
  129. 'question' => htmlspecialchars($q['label'], ENT_QUOTES),
  130. 'help' => isset($q['help']) ? '<p class="qHelp">' . htmlspecialchars($q['help'], ENT_QUOTES) . '</p>' : '',
  131. 'options' => $optionsHtml,
  132. 'action_url' => $actionUrl,
  133. 'select_url' => $selectUrl,
  134. 'audit_uid' => htmlspecialchars($auditUid, ENT_QUOTES),
  135. 'can_prev' => $step > 0 ? '' : 'disabled',
  136. 'btn_next' => $isLast ? 'Voir mon résultat →' : 'Suivant →',
  137. 'btn_class' => $selected ? 'btn--primary' : 'btn--outline',
  138. 'can_next' => $selected ? '' : 'disabled',
  139. ]);