format('Y-m-d H:i:s') === $timeoutAt ? $date->getTimestamp() * 1000 : 0; } function PA_WEB_DASH_COUNTDOWN_DUE_STEPS($dashLists) { $ids = array(); foreach ($dashLists as $dashes) { foreach ($dashes as $dash) { if (($dash['dashStatus'] ?? '') === 'Active' && (int)($dash['dashID'] ?? 0) > 0) { $ids[(int)$dash['dashID']] = true; } } } if (!$ids) return array(); $rows = array(); $count = 0; $error = ''; $status = ''; $sql = "SELECT s.DashID,s.DashStepID AS dashStepID,s.DashStepTemplateID AS templateID, s.Insert_DateTime AS createdDateTime,s.Status AS status,s.TimeoutAtDateTime AS timeoutAt, COALESCE(NULLIF(c.Title,''),t.Title) AS title FROM PART_DashSteps s JOIN PART_DashStepTemplates t ON t.DashStepTemplateID=s.DashStepTemplateID AND t.RecordStatus=0 LEFT JOIN PART_DashStepTagCache c ON c.DashStepID=s.DashStepID AND c.RecordStatus=0 WHERE s.DashID IN (" . implode(',', array_keys($ids)) . ") AND s.DashStepUserType='PART Admin' AND s.Status='Not Completed' AND s.RecordStatus=0 AND s.TimeoutAtDateTime IS NOT NULL AND s.TimeoutAtDateTime>'0000-00-00 00:00:00' ORDER BY s.DashID,s.DashStepID"; PACommonDBSelectMultipleRecords($sql, $rows, $count, $error, $status); if ($status !== 0 && $status !== '0') { error_log('DASH countdown query failed: ' . $error); return array(); } if (!is_array($rows)) return array(); $dueByDash = array(); $distanceByDash = array(); $deadlineByDash = array(); $nowMs = (int)round(microtime(true) * 1000); foreach ($rows as $row) { $id = (int)$row['DashID']; $deadlineMs = PA_WEB_DASH_COUNTDOWN_DEADLINE_MS($row['timeoutAt']); if (!$deadlineMs) continue; $distance = abs($deadlineMs - $nowMs); if (!isset($dueByDash[$id]) || $distance < $distanceByDash[$id] || ($distance === $distanceByDash[$id] && $deadlineMs > $deadlineByDash[$id])) { $dueByDash[$id] = $row; $distanceByDash[$id] = $distance; $deadlineByDash[$id] = $deadlineMs; } } return $dueByDash; } function PA_WEB_DASH_COUNTDOWN_NEXT_EVENT_MS($step) { global $PASYSCONF, $PAAPP_CONFIG_DB_LINK; $settings = $PASYSCONF['DASH_TIMERS'] ?? array(); $dashID = (int)($step['dashID'] ?? $step['DashID'] ?? 0); $stepID = (int)($step['dashStepID'] ?? 0); $templateID = (int)($step['templateID'] ?? 0); $familySource = null; if ($templateID===25 && ($step['status'] ?? '')==='Not Completed' && $PAAPP_CONFIG_DB_LINK instanceof PDO) { if (!function_exists('PART_InformationContext')) require_once '/opt/SA/DEV/WEB/RESTAPI/DASH/PA_RESTAPI_DASH_Information.php'; $source=PART_InformationContext($stepID); if (!$source || $source['Status']!=='Not Completed') return 0; $familySource = $source; $step['timeoutAt'] = $source['TimeoutAtDateTime']; } if (empty($settings['enabled']) || $dashID < max(1, (int)($settings['min_dash_id'] ?? PHP_INT_MAX)) || $stepID < 1 || ($step['status'] ?? '') !== 'Not Completed' || !in_array($templateID, array(1, 2, 3, 5, 7, 9, 11, 14, 17, 19, 21, 23, 24, 25, 26, 27, 28), true) || !($PAAPP_CONFIG_DB_LINK instanceof PDO)) return 0; if ($templateID === 9) { $query = $PAAPP_CONFIG_DB_LINK->prepare("SELECT DashStepID FROM PART_DashSteps WHERE DashID=? AND ParentDashStepID=? AND DashStepTemplateID=25 AND Status='Not Completed' AND RecordStatus=0 ORDER BY DashStepID LIMIT 1"); $query->execute([$dashID,$stepID]); $followupID = (int)$query->fetchColumn(); if ($followupID) return PA_WEB_DASH_COUNTDOWN_NEXT_EVENT_MS(['dashID'=>$dashID,'dashStepID'=>$followupID, 'templateID'=>25,'status'=>'Not Completed']); } if (in_array($templateID,[26,27,28],true)) { $kind=[26=>'CASE_INFORMATION_DRAFT_FOLLOWUP',27=>'CASE_INFORMATION_ACK_FOLLOWUP',28=>'CASE_UPDATE_CHECKPOINT'][$templateID]; $q=$PAAPP_CONFIG_DB_LINK->prepare("SELECT MAX(e.MilestoneMinutes) FROM PART_DashTimerEvents e JOIN PART_Dash d ON d.DashID=e.DashID AND d.Status='Active' AND d.RecordStatus=0 WHERE e.SourceDashStepID=? AND e.EventType=?");$q->execute([$stepID,$kind]);$last=$q->fetchColumn(); $anchor=PA_WEB_DASH_COUNTDOWN_DEADLINE_MS($step['timeoutAt'] ?? ''); return $anchor ? $anchor+($last===null ? 0 : (int)$last+max(1,(int)($settings['case_update_repeat_minutes'] ?? 30)))*60000 : 0; } $kind = $templateID === 1 ? 'ADMIN_INITIAL' : ($templateID === 7 ? 'ADMIN_REVIEW_REMINDER' : ($templateID === 5 ? 'PI_ASSIGN_ADMIN_FOLLOWUP' : (in_array($templateID,array(17,21),true) ? 'PI_PROGRESS_ADMIN_FOLLOWUP' : ($templateID === 11 ? 'CASE_CLOSED_FOLLOWUP' : ($templateID === 19 ? 'REPEAT_ALERT_REVIEW' : ($templateID === 25 ? 'FAMILY_INFORMATION_FOLLOWUP' : 'CONTACT_FOLLOWUP')))))); try { $query = $PAAPP_CONFIG_DB_LINK->prepare("SELECT d.PIID, (SELECT MAX(MilestoneMinutes) FROM PART_DashTimerEvents WHERE SourceDashStepID=? AND EventType=?) AS LastMilestone, EXISTS(SELECT 1 FROM PART_DashTimerEvents WHERE GeneratedDashStepID=? AND EventType='PI_PROGRESS_OVERDUE') AS ProgressOrigin FROM PART_Dash d WHERE d.DashID=? AND d.Status='Active' AND d.RecordStatus=0 LIMIT 1"); $query->execute(array($stepID, $kind, $stepID, $dashID)); $state = $query->fetch(PDO::FETCH_ASSOC); } catch (Throwable $error) { return 0; } if (!$state || ($templateID === 5 && (int)$state['PIID'] > 0 && !PART_PIHandoffContext($stepID)) || (in_array($templateID,array(17,21),true) && (int)$state['ProgressOrigin'] !== 1)) return 0; $last = $state['LastMilestone'] === null ? null : (int)$state['LastMilestone']; if ($familySource) { $query = $PAAPP_CONFIG_DB_LINK->prepare("SELECT MAX(MilestoneMinutes) FROM PART_DashTimerEvents WHERE SourceDashStepID=? AND EventType='CONTACT_FOLLOWUP'"); $query->execute([(int)$familySource['DashStepID']]); $legacy = $query->fetchColumn(); if ($legacy !== null) $last = $last === null ? (int)$legacy : max($last,(int)$legacy); } $anchor = in_array($templateID, array(2, 3, 5, 9, 14, 17, 21, 23, 24, 25), true) ? ($step['timeoutAt'] ?? '') : ($step['createdDateTime'] ?? ''); $anchorMs = PA_WEB_DASH_COUNTDOWN_DEADLINE_MS($anchor); if (!$anchorMs) return 0; if ($templateID === 1) { $configured = $settings['admin_initial_minutes'] ?? array(5, 15, 30, 60); $schedule = array_values(array_unique(array_filter(array_map('intval', is_array($configured) ? $configured : array()), function ($m) { return $m > 0; }))); if (!$schedule) return 0; sort($schedule, SORT_NUMERIC); foreach ($schedule as $minute) { if ($last === null || $minute > $last) return $anchorMs + $minute * 60000; } $repeat = max(1, (int)($settings['admin_initial_repeat_minutes'] ?? 60)); $lastScheduled = end($schedule); $next = $lastScheduled + (int)(floor(max(0, $last - $lastScheduled) / $repeat) + 1) * $repeat; return $anchorMs + $next * 60000; } if (in_array($templateID, array(7, 11, 19), true)) { $configured = $templateID === 19 ? ($settings['repeat_alert_review_minutes'] ?? array(5, 15, 30, 60)) : ($templateID === 7 ? ($settings['admin_coordination_reminder_minutes'] ?? $settings['case_close_reminder_minutes'] ?? array(5, 15, 30, 60)) : ($settings['case_close_reminder_minutes'] ?? array(5, 15, 30, 60))); $schedule = array_values(array_unique(array_filter(array_map('intval', is_array($configured) ? $configured : array()), function ($m) { return $m > 0; }))); if (!$schedule) return 0; sort($schedule, SORT_NUMERIC); foreach ($schedule as $minute) { if ($last === null || $minute > $last) return $anchorMs + $minute * 60000; } $repeat = max(1, (int)($templateID === 19 ? ($settings['repeat_alert_repeat_minutes'] ?? 60) : ($templateID === 7 ? ($settings['admin_coordination_repeat_minutes'] ?? $settings['case_close_repeat_minutes'] ?? 60) : ($settings['case_close_repeat_minutes'] ?? 60)))); $lastScheduled = end($schedule); $next = $lastScheduled + (int)(floor(max(0, $last - $lastScheduled) / $repeat) + 1) * $repeat; return $anchorMs + $next * 60000; } if ($templateID === 5 || in_array($templateID,array(17,21),true)) { $repeat = max(1, (int)(in_array($templateID,array(17,21),true) ? ($settings['pi_progress_admin_repeat_minutes'] ?? 30) : ($settings['pi_assignment_admin_repeat_minutes'] ?? 30))); return $anchorMs + ($last === null ? 0 : $last + $repeat) * 60000; } $configured = $templateID === 25 ? ($settings['family_information_followup_minutes'] ?? array(0,15,30,60)) : ($settings['contact_followup_minutes'] ?? array(0,15,30,60,120,180,240)); $schedule = array_values(array_unique(array_filter(array_map('intval', is_array($configured) ? $configured : array()), function ($m) { return $m >= 0; }))); if (!$schedule) return 0; sort($schedule, SORT_NUMERIC); foreach ($schedule as $minute) { if ($last === null || $minute > $last) return $anchorMs + $minute * 60000; } $repeat = max(1, (int)($templateID === 25 ? ($settings['family_information_followup_repeat_minutes'] ?? 60) : ($settings['contact_followup_repeat_minutes'] ?? 240))); return $anchorMs + (max($last, end($schedule)) + $repeat) * 60000; } function PA_WEB_DASH_COUNTDOWN_ASSETS() { static $loaded = false; if ($loaded) return; $loaded = true; ?> ' . $titleHtml . '' : ''; if (!$deadlineMs) { echo '' . $stepName . 'No deadline set'; return; } $nowMs = (int)round(microtime(true) * 1000); $overdue = $nowMs > $deadlineMs; $nextMs = PA_WEB_DASH_COUNTDOWN_NEXT_EVENT_MS($step); $seconds = (int)floor(($startedMs ? max(0, $nowMs - $startedMs) : abs($deadlineMs - $nowMs)) / 1000); $value = $seconds >= 3600 ? intdiv($seconds, 3600) . ' hr ' . intdiv($seconds % 3600, 60) . ' min' : intdiv($seconds, 60) . ' min ' . str_pad((string)($seconds % 60), 2, '0', STR_PAD_LEFT) . ' sec'; $tag = $nextMs ? 'button' : 'span'; echo '<' . $tag . ($nextMs ? ' type="button"' : '') . ' class="pa-dash-countdown' . ($overdue ? ' pa-dash-countdown--overdue' : '') . ($nextMs ? ' pa-dash-countdown--toggle' : '') . '"' . ' data-pa-deadline-ms="' . $deadlineMs . '" data-pa-start-ms="' . $startedMs . '" data-pa-server-ms="' . $nowMs . '"' . ($nextMs ? ' data-pa-next-ms="' . $nextMs . '" aria-pressed="false"' : '') . ' title="Deadline: ' . htmlspecialchars((string)$step['timeoutAt'], ENT_QUOTES, 'UTF-8') . ' UTC' . ($nextMs ? '. Click to show the next scheduled reminder.' : '') . '">' . $stepName . '' . $value . '' . '' . ($startedMs ? ($overdue ? 'TIME OPEN - OVERDUE' : 'time open') : ($overdue ? 'OVERDUE' : 'to complete')) . '' . ($nextMs ? '' : '') . ''; }