Refactor goals_progress_alert_renderer to separate period department lines from totals

- Introduce `periodLines` array for improved clarity and logic separation.
- Update total expression to include period label for better formatting.
- Move department lines and blanks to `periodLines` and merge into output at the end of each period.
This commit is contained in:
Jeppe Bundgaard
2026-02-02 11:25:45 +01:00
parent 4816075041
commit c591439240
@@ -189,8 +189,11 @@ class goals_progress_alert_renderer
[$ps, $pe] = $period['range'];
$range = self::clampToCriteriaWindow($criteria, $ps, $pe);
// Period lines (Used to show total before the department breakdown)
$periodLines = [];
// Period header
$outLines[] = $label . ':';
//$outLines[] = $label . ':';
$total_period_count = 0;
@@ -226,7 +229,7 @@ class goals_progress_alert_renderer
? "> *" .sprintf('%s: %d', $deptData['label'], $count) . "*"
: " - " .sprintf('%s: %d', $deptData['label'], $count);
}
$outLines[] = $line;
$periodLines[] = $line;
}
// Total line for the period
if ($includeTargets && isset($criteria->target)) {
@@ -236,18 +239,23 @@ class goals_progress_alert_renderer
$re
) * count($departments);
$total_percent = $total_target > 0 ? round(($total_period_count / max(1, $total_target)) * 100, 2) : 0.0;
$outLines[] = sprintf('- Total: %d ud af %d (%.2f%%)',
$outLines[] = sprintf($label . ': %d ud af %d (%.2f%%)',
$total_period_count,
$total_target,
$total_percent
);
} else {
$outLines[] = sprintf('- Total: %d', $total_period_count);
$outLines[] = sprintf('%s: %d',
$label,
$total_period_count
);
}
// Add a blank line between periods (but not after the last one)
if ($idx < count($periods) - 1) {
$outLines[] = '';
$periodLines[] = '';
}
// Add the period's department lines
$outLines = array_merge($outLines, $periodLines);
}
return implode("\n", $outLines);