add badge logic

This commit is contained in:
root
2026-04-25 01:23:21 -04:00
parent eafe4eb134
commit 5c544f93b9
13 changed files with 1468 additions and 169 deletions
@@ -303,6 +303,108 @@ class StaffAttendanceService
];
}
$adminStatusByUser = [];
$adminIds = [];
if (!empty($days)) {
$query = DB::table('staff_attendance')
->select('user_id', 'date', 'status', 'reason')
->where('school_year', $schoolYear)
->whereIn('date', $days);
if ($semesterNorm !== '') {
$query->where('semester', $semesterNorm);
}
$rows = $query->get();
foreach ($rows as $row) {
$uid = (int) ($row->user_id ?? 0);
if ($uid <= 0) {
continue;
}
$adminIds[$uid] = true;
$adminStatusByUser[$uid][(string) $row->date] = [
'status' => strtolower((string) $row->status),
'reason' => $row->reason,
];
}
}
$adminIds = array_keys($adminIds);
$admins = [];
if (!empty($adminIds)) {
$rolesRows = DB::table('user_roles as ur')
->select('ur.user_id', 'r.name as role_name', 'r.slug as role_slug', 'r.priority')
->join('roles as r', 'r.id', '=', 'ur.role_id')
->whereIn('ur.user_id', $adminIds)
->whereNull('ur.deleted_at')
->where('r.is_active', 1)
->get();
$rolesByUser = [];
foreach ($rolesRows as $row) {
$rolesByUser[(int) $row->user_id][] = (array) $row;
}
foreach ($rolesByUser as &$list) {
usort($list, fn ($a, $b) => ((int) ($a['priority'] ?? 100)) <=> ((int) ($b['priority'] ?? 100)));
}
unset($list);
$excludeSlugs = ['parent', 'guest', 'teacher', 'teacher_assistant', 'ta', 'assistant_teacher'];
$userRows = DB::table('users')
->select('id', 'firstname', 'lastname')
->whereIn('id', $adminIds)
->get();
$namesByUser = [];
foreach ($userRows as $row) {
$namesByUser[(int) $row->id] = trim(((string) ($row->firstname ?? '')) . ' ' . ((string) ($row->lastname ?? '')));
}
foreach ($adminIds as $uid) {
$picked = null;
foreach (($rolesByUser[$uid] ?? []) as $role) {
$slug = strtolower((string) ($role['role_slug'] ?? $role['role_name'] ?? ''));
if (!in_array($slug, $excludeSlugs, true)) {
$picked = $role;
break;
}
}
if (!$picked) {
continue;
}
$totals = ['p' => 0, 'a' => 0, 'l' => 0];
$cells = [];
foreach ($days as $date) {
$cell = $adminStatusByUser[$uid][$date] ?? null;
if (!$cell || empty($cell['status'])) {
continue;
}
$cells[$date] = $cell;
if ($cell['status'] === 'present') {
$totals['p']++;
} elseif ($cell['status'] === 'absent') {
$totals['a']++;
} elseif ($cell['status'] === 'late') {
$totals['l']++;
}
}
$admins[] = [
'user_id' => $uid,
'name' => $namesByUser[$uid] !== '' ? $namesByUser[$uid] : ('User#' . $uid),
'role' => (string) ($picked['role_name'] ?? 'Admin'),
'cells' => $cells,
'totals' => $totals,
];
}
usort($admins, fn ($a, $b) => strcasecmp((string) ($a['name'] ?? ''), (string) ($b['name'] ?? '')));
}
return [
'filters' => [
'semester' => $semester,
@@ -313,7 +415,7 @@ class StaffAttendanceService
'sundays' => $days,
'noSchoolDays' => $noSchoolDays,
'sections' => $sections,
'admins' => [],
'admins' => $admins,
];
}
@@ -570,4 +672,4 @@ class StaffAttendanceService
fclose($out);
}, $filename);
}
}
}