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
+67 -16
View File
@@ -6,21 +6,26 @@ class BadgeFormDataService
{
public function __construct(
protected BadgeUserLookupService $lookupService,
protected BadgeStudentLookupService $studentLookupService,
protected BadgeTextFormatter $formatter
) {
}
public function build(?string $schoolYear, array $selectedUserIds = [], ?string $activeRole = null): array
{
public function build(
?string $schoolYear,
array $selectedUserIds = [],
array $selectedStudentIds = [],
?string $activeRole = null
): array {
$selectedUserIds = $this->formatter->normalizeIds($selectedUserIds);
$selectedStudentIds = $this->formatter->normalizeIds($selectedStudentIds);
$users = $this->lookupService->fetchStaffList($schoolYear, $selectedUserIds);
$students = $this->studentLookupService->fetchStudentList($schoolYear, $selectedStudentIds);
foreach ($users as &$u) {
if (!isset($u['user_id'])) {
if (isset($u['id'])) {
$u['user_id'] = (int) $u['id'];
}
if (!isset($u['user_id']) && isset($u['id'])) {
$u['user_id'] = (int) $u['id'];
}
$rolesStr = $u['roles'] ?? '';
@@ -38,14 +43,18 @@ class BadgeFormDataService
$u['role_name_raw'] = $u['role_name'];
$u['role_name'] = $this->formatter->formatRole((string) $u['role_name']);
$u['class_section_id'] = $u['class_section_id'] ?? null;
$u['class_section_name'] = $u['class_section_name'] ?? null;
$u['entity_type'] = 'user';
$u['row_id'] = 'user:' . (int) ($u['user_id'] ?? 0);
$u['role_group'] = $this->detectRoleGroup(
strtolower((string) ($u['roles_raw'] ?? ($u['role_name_raw'] ?? '')))
);
}
unset($u);
foreach ($users as &$u) {
$rolesDetect = strtolower($u['roles_raw'] ?? ($u['role_name_raw'] ?? ''));
$rolesDetect = strtolower((string) ($u['roles_raw'] ?? ($u['role_name_raw'] ?? '')));
$isTeacherish = str_contains($rolesDetect, 'teacher') || preg_match('/\bta\b/', $rolesDetect);
if ($isTeacherish && !empty($u['user_id'])) {
@@ -58,24 +67,66 @@ class BadgeFormDataService
}
unset($u);
foreach ($students as &$student) {
$student['entity_type'] = 'student';
$student['row_id'] = 'student:' . (int) ($student['student_id'] ?? 0);
$student['role_group'] = 'student';
$student['class_section_id'] = null;
if (empty($student['class_section_name'])) {
$student['class_section_name'] = $this->formatter->formatClass(
(string) ($student['registration_grade'] ?? '')
);
}
}
unset($student);
$rows = array_merge($users, $students);
usort($rows, static function (array $a, array $b): int {
return strcasecmp(
trim((string) ($a['lastname'] ?? '')) . ' ' . trim((string) ($a['firstname'] ?? '')),
trim((string) ($b['lastname'] ?? '')) . ' ' . trim((string) ($b['firstname'] ?? ''))
);
});
$rolesTabs = [
'all' => 'All',
'teacher' => 'Teachers',
'ta' => 'Teacher Assistants',
'admin' => 'Admins',
'staff' => 'Staff',
'student' => 'Students',
];
$activeRole = array_key_exists((string) $activeRole, $rolesTabs)
? (string) $activeRole
: 'teacher';
: 'all';
return [
'users' => $users,
'schoolYears' => $this->lookupService->getAvailableSchoolYears(),
'selectedYear' => $schoolYear,
'selectedUserIds' => $selectedUserIds,
'rolesTabs' => $rolesTabs,
'active_role' => $activeRole,
'users' => $rows,
'schoolYears' => $this->lookupService->getAvailableSchoolYears(),
'selectedYear' => $schoolYear,
'selectedUserIds' => $selectedUserIds,
'selectedStudentIds' => $selectedStudentIds,
'rolesTabs' => $rolesTabs,
'active_role' => $activeRole,
];
}
}
protected function detectRoleGroup(string $rolesDetect): string
{
if (str_contains($rolesDetect, 'assistant') || preg_match('/\bta\b/', $rolesDetect)) {
return 'ta';
}
if (str_contains($rolesDetect, 'teacher')) {
return 'teacher';
}
if (str_contains($rolesDetect, 'admin')) {
return 'admin';
}
return 'staff';
}
}