| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- /**
- * Snippet : AuditExpress_SelectPack
- * Vue "Choisir un pack"
- *
- * Propriétés (dans le tag snippet MODX) :
- * &quiz_resource_id = `5` ID de la ressource quiz
- * &landing_resource_id = `3` ID de la ressource landing
- *
- * POST action : ae_select_pack
- * → crée un AuditRequest en BD, stocke audit_uid en session
- * → redirige vers la ressource quiz
- */
- declare(strict_types=1);
- // ── Bootstrap ──────────────────────────────────────────────────────────────
- $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';
- $quizResourceId = (int) ($scriptProperties['quiz_resource_id'] ?? $modx->getOption('ae.resource.quiz', null, 0));
- $landingResourceId = (int) ($scriptProperties['landing_resource_id'] ?? $modx->getOption('ae.resource.landing', null, 0));
- // ── Traitement POST ────────────────────────────────────────────────────────
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['ae_action'] ?? '') === 'select_pack') {
- $packId = trim($_POST['pack_id'] ?? '');
- if (!$packId) {
- $modx->sendRedirect($modx->makeUrl($modx->resource->get('id'), '', '', 'full'));
- return '';
- }
- // Vérifier que le pack existe
- $repo = new \AuditExpress\PackRepository($corePath . 'config/');
- try {
- $packData = $repo->getPack($packId);
- } catch (\InvalidArgumentException $e) {
- $modx->sendRedirect($modx->makeUrl($modx->resource->get('id'), '', '', 'full'));
- return '';
- }
- // Créer l'objet AuditRequest en BD
- /** @var AuditRequest $auditReq */
- $auditReq = $modx->newObject('AuditRequest');
- $auditReq->set('audit_uid', sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
- mt_rand(0, 0xffff), mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
- ));
- $auditReq->set('pack_id', $packId);
- $auditReq->set('status', 'draft');
- $auditReq->set('step', 0);
- $auditReq->set('ip_hash', hash('sha256', $_SERVER['REMOTE_ADDR'] ?? ''));
- $auditReq->setAnswersArray([]);
- $auditReq->setOptionalArray([]);
- $auditReq->save();
- // Stocker en session
- $modx->user->getUserProfile(); // init session MODX
- $_SESSION['ae_audit_uid'] = $auditReq->get('audit_uid');
- $_SESSION['ae_pack_id'] = $packId;
- // Rediriger vers quiz
- $quizUrl = $quizResourceId
- ? $modx->makeUrl($quizResourceId, '', '', 'full')
- : '/quiz/';
- $modx->sendRedirect($quizUrl);
- return '';
- }
- // ── Rendu GET ──────────────────────────────────────────────────────────────
- $packCatalogue = [
- [
- 'id' => 'onboarding-v1',
- 'title' => 'Onboarding collaborateur',
- 'desc' => "Clarté du flux, traçabilité, continuité — auditez votre process d'intégration.",
- 'tag' => 'Pré-configuré',
- 'tagClass' => 'tag--pre',
- ],
- [
- 'id' => 'validation-facture',
- 'title' => 'Validation de facture',
- 'desc' => 'Contrôles, séparation des tâches, preuve — évaluez votre circuit factures.',
- 'tag' => 'Pré-configuré',
- 'tagClass' => 'tag--pre',
- ],
- [
- 'id' => 'process-generique',
- 'title' => 'Process générique',
- 'desc' => 'Applicable à tout process opérationnel : achat, production, relation client, support, projet…',
- 'tag' => 'Universel',
- 'tagClass' => 'tag--new',
- ],
- [
- 'id' => 'process-generique-v2',
- 'title' => 'Diagnostic d\'opportunité',
- 'desc' => 'Évaluez sur 8 axes si ce process est prioritaire à automatiser : friction, volume, risque, données, outils, variabilité, collaboration, déploiement.',
- 'tag' => 'Diagnostic d\'opportunité',
- 'tagClass' => 'tag--v2',
- ],
- ];
- $actionUrl = $modx->makeUrl($modx->resource->get('id'), '', '', 'full');
- $landingUrl = $landingResourceId ? $modx->makeUrl($landingResourceId, '', '', 'full') : '/';
- $cards = '';
- foreach ($packCatalogue as $p) {
- $cards .= $modx->getChunk('ae_pack_card', [
- 'pack_id' => htmlspecialchars($p['id'], ENT_QUOTES),
- 'title' => htmlspecialchars($p['title'], ENT_QUOTES),
- 'desc' => htmlspecialchars($p['desc'], ENT_QUOTES),
- 'tag' => htmlspecialchars($p['tag'], ENT_QUOTES),
- 'tag_class' => htmlspecialchars($p['tagClass'], ENT_QUOTES),
- 'action_url' => $actionUrl,
- ]);
- }
- return $modx->getChunk('ae_select_pack', [
- 'cards' => $cards,
- 'action_url' => $actionUrl,
- 'landing_url' => $landingUrl,
- ]);
|