From eeaef928b547ddcdaa35ea6096ecfaf6f5dc2a83 Mon Sep 17 00:00:00 2001 From: Jonathan Miller Date: Sun, 21 Jun 2026 00:24:43 -0500 Subject: [PATCH] =?UTF-8?q?fix:=20safety=20checklist=20=E2=80=94=20verify?= =?UTF-8?q?=20pending+unchecked=20before=20marking,=20auto-complete=20stat?= =?UTF-8?q?us?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Helper/SafetyChecklistHelper.php | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/source/packages/plg_system_mokosuitefield/src/Helper/SafetyChecklistHelper.php b/source/packages/plg_system_mokosuitefield/src/Helper/SafetyChecklistHelper.php index ae4a76c..c789fde 100644 --- a/source/packages/plg_system_mokosuitefield/src/Helper/SafetyChecklistHelper.php +++ b/source/packages/plg_system_mokosuitefield/src/Helper/SafetyChecklistHelper.php @@ -50,6 +50,18 @@ class SafetyChecklistHelper { $db = Factory::getContainer()->get(DatabaseInterface::class); + // Verify item belongs to a pending checklist and is not already checked + $db->setQuery($db->getQuery(true) + ->select('sci.id, sci.checked, sc.status AS checklist_status') + ->from($db->quoteName('#__mokosuitefield_safety_checklist_items', 'sci')) + ->join('INNER', $db->quoteName('#__mokosuitefield_safety_checklists', 'sc') . ' ON sc.id = sci.checklist_id') + ->where('sci.id = ' . (int) $itemId)); + $existing = $db->loadObject(); + + if (!$existing || $existing->checklist_status !== 'pending' || (int) $existing->checked === 1) { + return false; + } + $update = (object) [ 'id' => $itemId, 'checked' => 1, @@ -59,7 +71,26 @@ class SafetyChecklistHelper 'checked_by' => Factory::getApplication()->getIdentity()->id, ]; - return $db->updateObject('#__mokosuitefield_safety_checklist_items', $update, 'id'); + $db->updateObject('#__mokosuitefield_safety_checklist_items', $update, 'id'); + + // Auto-complete checklist if all items are checked + $db->setQuery($db->getQuery(true) + ->select('sc.id, COUNT(sci2.id) AS total, SUM(CASE WHEN sci2.checked = 1 THEN 1 ELSE 0 END) AS done') + ->from($db->quoteName('#__mokosuitefield_safety_checklist_items', 'sci2')) + ->join('INNER', $db->quoteName('#__mokosuitefield_safety_checklists', 'sc') . ' ON sc.id = sci2.checklist_id') + ->where('sci2.id = ' . (int) $itemId) + ->group('sc.id')); + $progress = $db->loadObject(); + + if ($progress && (int) $progress->done === (int) $progress->total) { + $db->setQuery($db->getQuery(true) + ->update('#__mokosuitefield_safety_checklists') + ->set($db->quoteName('status') . ' = ' . $db->quote('completed')) + ->where('id = ' . (int) $progress->id)); + $db->execute(); + } + + return true; } /**