AuditExpress_SelectPack.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Snippet : AuditExpress_SelectPack
  4. * Vue "Choisir un pack"
  5. *
  6. * Propriétés (dans le tag snippet MODX) :
  7. * &quiz_resource_id = `5` ID de la ressource quiz
  8. * &landing_resource_id = `3` ID de la ressource landing
  9. *
  10. * POST action : ae_select_pack
  11. * → crée un AuditRequest en BD, stocke audit_uid en session
  12. * → redirige vers la ressource quiz
  13. */
  14. declare(strict_types=1);
  15. // ── Bootstrap ──────────────────────────────────────────────────────────────
  16. $corePath = $modx->getOption('ae.core_path', null,
  17. $modx->getOption('core_path') . 'components/audit_express/');
  18. $modx->addPackage('audit_express', $corePath . 'model/');
  19. require_once $corePath . 'src/PackRepository.php';
  20. $quizResourceId = (int) ($scriptProperties['quiz_resource_id'] ?? $modx->getOption('ae.resource.quiz', null, 0));
  21. $landingResourceId = (int) ($scriptProperties['landing_resource_id'] ?? $modx->getOption('ae.resource.landing', null, 0));
  22. // ── Traitement POST ────────────────────────────────────────────────────────
  23. if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['ae_action'] ?? '') === 'select_pack') {
  24. $packId = trim($_POST['pack_id'] ?? '');
  25. if (!$packId) {
  26. $modx->sendRedirect($modx->makeUrl($modx->resource->get('id'), '', '', 'full'));
  27. return '';
  28. }
  29. // Vérifier que le pack existe
  30. $repo = new \AuditExpress\PackRepository($corePath . 'config/');
  31. try {
  32. $packData = $repo->getPack($packId);
  33. } catch (\InvalidArgumentException $e) {
  34. $modx->sendRedirect($modx->makeUrl($modx->resource->get('id'), '', '', 'full'));
  35. return '';
  36. }
  37. // Créer l'objet AuditRequest en BD
  38. /** @var AuditRequest $auditReq */
  39. $auditReq = $modx->newObject('AuditRequest');
  40. $auditReq->set('audit_uid', sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  41. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  42. mt_rand(0, 0xffff),
  43. mt_rand(0, 0x0fff) | 0x4000,
  44. mt_rand(0, 0x3fff) | 0x8000,
  45. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  46. ));
  47. $auditReq->set('pack_id', $packId);
  48. $auditReq->set('status', 'draft');
  49. $auditReq->set('step', 0);
  50. $auditReq->set('ip_hash', hash('sha256', $_SERVER['REMOTE_ADDR'] ?? ''));
  51. $auditReq->setAnswersArray([]);
  52. $auditReq->setOptionalArray([]);
  53. $auditReq->save();
  54. // Stocker en session
  55. $modx->user->getUserProfile(); // init session MODX
  56. $_SESSION['ae_audit_uid'] = $auditReq->get('audit_uid');
  57. $_SESSION['ae_pack_id'] = $packId;
  58. // Rediriger vers quiz
  59. $quizUrl = $quizResourceId
  60. ? $modx->makeUrl($quizResourceId, '', '', 'full')
  61. : '/quiz/';
  62. $modx->sendRedirect($quizUrl);
  63. return '';
  64. }
  65. // ── Rendu GET ──────────────────────────────────────────────────────────────
  66. $packCatalogue = [
  67. [
  68. 'id' => 'onboarding-v1',
  69. 'title' => 'Onboarding collaborateur',
  70. 'desc' => "Fiabilisez l'intégration de vos nouveaux collaborateurs : clarté du flux, traçabilité, continuité.",
  71. 'tag' => 'Pré-configuré',
  72. 'tagClass' => 'tag--pre',
  73. ],
  74. [
  75. 'id' => 'validation-facture',
  76. 'title' => 'Validation de facture',
  77. 'desc' => 'Contrôles, séparation des tâches, preuve : évaluez la robustesse de votre circuit factures.',
  78. 'tag' => 'Pré-configuré',
  79. 'tagClass' => 'tag--pre',
  80. ],
  81. [
  82. 'id' => 'process-generique',
  83. 'title' => 'Process générique',
  84. 'desc' => 'Applicable à tout process opérationnel : achat, production, relation client, support, projet…',
  85. 'tag' => 'Universel',
  86. 'tagClass' => 'tag--new',
  87. ],
  88. [
  89. 'id' => 'process-generique-v2',
  90. 'title' => 'Diagnostic d\'opportunité',
  91. 'desc' => 'Évaluez sur 8 axes si ce process est prioritaire à automatiser : friction, volume, risque, données, outils, variabilité, collaboration, déploiement.',
  92. 'tag' => 'Diagnostic d\'opportunité',
  93. 'tagClass' => 'tag--v2',
  94. ],
  95. ];
  96. $actionUrl = $modx->makeUrl($modx->resource->get('id'), '', '', 'full');
  97. $landingUrl = $landingResourceId ? $modx->makeUrl($landingResourceId, '', '', 'full') : '/';
  98. $cards = '';
  99. foreach ($packCatalogue as $p) {
  100. $cards .= $modx->getChunk('ae_pack_card', [
  101. 'pack_id' => htmlspecialchars($p['id'], ENT_QUOTES),
  102. 'title' => htmlspecialchars($p['title'], ENT_QUOTES),
  103. 'desc' => htmlspecialchars($p['desc'], ENT_QUOTES),
  104. 'tag' => htmlspecialchars($p['tag'], ENT_QUOTES),
  105. 'tag_class' => htmlspecialchars($p['tagClass'], ENT_QUOTES),
  106. 'action_url' => $actionUrl,
  107. ]);
  108. }
  109. return $modx->getChunk('ae_select_pack', [
  110. 'cards' => $cards,
  111. 'action_url' => $actionUrl,
  112. 'landing_url' => $landingUrl,
  113. ]);