fix parent, teacher and admin pages

This commit is contained in:
root
2026-04-25 00:00:23 -04:00
parent 4cd98f1d30
commit eafe4eb134
30 changed files with 1566 additions and 105 deletions
+30 -12
View File
@@ -11,17 +11,14 @@ class RoleStaffService
public function syncStaffRecord(User $user, array $newRoleNames): void
{
$excludedRoles = ['parent', 'student', 'guest'];
$loweredNewRoles = array_map('strtolower', $newRoleNames);
$existingStaff = Staff::query()->where('user_id', $user->id)->first();
$existingRoles = [];
if ($existingStaff && !empty($existingStaff->role_name)) {
$existingRoles = array_map('strtolower', array_map('trim', explode(',', $existingStaff->role_name)));
}
$allRoles = array_unique(array_merge($existingRoles, $loweredNewRoles));
$staffRoles = array_filter($newRoleNames, fn ($role) => !in_array(strtolower($role), $excludedRoles, true));
$activeRole = !empty($staffRoles) ? $staffRoles[0] : 'inactive';
$staffRoles = array_values(array_unique(array_filter(
array_map(
static fn ($role) => trim((string) $role),
$newRoleNames
),
static fn ($role) => $role !== '' && !in_array(strtolower($role), $excludedRoles, true)
)));
$activeRole = !empty($staffRoles) ? strtolower((string) $staffRoles[0]) : 'inactive';
$email = $this->generateUniqueStaffEmail($user->firstname ?? '', $user->lastname ?? '', (int) $user->id);
@@ -31,7 +28,7 @@ class RoleStaffService
'lastname' => $user->lastname ?? '',
'email' => $email,
'phone' => $user->cellphone ?? '',
'role_name' => implode(', ', $allRoles),
'role_name' => $this->compactRoleList($staffRoles),
'active_role' => $activeRole,
'school_year' => $user->school_year ?? '',
];
@@ -42,6 +39,27 @@ class RoleStaffService
}
}
/**
* Fit the current staff role list into the legacy `staff.role_name` field.
*/
private function compactRoleList(array $roles, int $maxLength = 100): string
{
if ($roles === []) {
return 'inactive';
}
$out = '';
foreach ($roles as $role) {
$next = $out === '' ? $role : $out . ', ' . $role;
if (strlen($next) > $maxLength) {
break;
}
$out = $next;
}
return $out !== '' ? $out : substr((string) $roles[0], 0, $maxLength);
}
private function generateUniqueStaffEmail(string $firstname, string $lastname, int $userId): string
{
$domain = '@alrahmaisgl.org';