Files
awo-hamburg-intranet/module/tasks/actions/user/overdue_widget_action.php

96 lines
3.0 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
require_once dirname(dirname(__DIR__)) . '/bootstrap.php';
function task_cert_user_overdue_widget_action(): array
{
$auth = TaskCertAuthContext::fromGlobals();
if (!$auth->isLoggedIn()) {
return [
'error' => 'Nicht eingeloggt.',
'assignments' => [],
'has_more' => false,
'total_count' => 0,
'limit' => 0,
];
}
$limit = max(1, min(50, TaskCertRequest::queryInt('limit', 8)));
$services = task_cert_services();
$assignments = $services['assignmentRepo']->listForUser($auth->contactId());
$now = new DateTimeImmutable();
$attentionAssignments = [];
foreach ($assignments as $assignment) {
$status = (string) ($assignment['status'] ?? '');
if (in_array($status, ['completed', 'expired'], true)) {
continue;
}
$isEscalated = (int) ($assignment['escalation_level'] ?? 0) > 0;
$isOverdueStatus = $status === 'overdue';
$isOverdueByDate = false;
$dueAtRaw = trim((string) ($assignment['due_at'] ?? ''));
if ($dueAtRaw !== '') {
try {
$isOverdueByDate = (new DateTimeImmutable($dueAtRaw)) < $now;
} catch (Throwable $throwable) {
$isOverdueByDate = false;
}
}
if (!$isEscalated && !$isOverdueStatus && !$isOverdueByDate) {
continue;
}
$assignment['is_escalated'] = $isEscalated ? 1 : 0;
$assignment['is_overdue_by_date'] = $isOverdueByDate ? 1 : 0;
$attentionAssignments[] = $assignment;
}
usort($attentionAssignments, static function (array $left, array $right): int {
$leftEscalation = (int) ($left['escalation_level'] ?? 0);
$rightEscalation = (int) ($right['escalation_level'] ?? 0);
if ($leftEscalation !== $rightEscalation) {
return $rightEscalation <=> $leftEscalation;
}
$leftOverdue = (int) ($left['is_overdue_by_date'] ?? 0);
$rightOverdue = (int) ($right['is_overdue_by_date'] ?? 0);
if ($leftOverdue !== $rightOverdue) {
return $rightOverdue <=> $leftOverdue;
}
$leftDue = trim((string) ($left['due_at'] ?? ''));
$rightDue = trim((string) ($right['due_at'] ?? ''));
if ($leftDue === '' && $rightDue !== '') {
return 1;
}
if ($leftDue !== '' && $rightDue === '') {
return -1;
}
if ($leftDue !== '' && $rightDue !== '' && $leftDue !== $rightDue) {
return strcmp($leftDue, $rightDue);
}
return ((int) ($right['id'] ?? 0)) <=> ((int) ($left['id'] ?? 0));
});
$totalCount = count($attentionAssignments);
$hasMore = $totalCount > $limit;
if ($hasMore) {
$attentionAssignments = array_slice($attentionAssignments, 0, $limit);
}
return [
'error' => null,
'assignments' => $attentionAssignments,
'has_more' => $hasMore,
'total_count' => $totalCount,
'limit' => $limit,
];
}