fix db tables to have school year

This commit is contained in:
root
2026-07-12 01:02:04 -04:00
parent ed11cccecc
commit ec9fca8c45
42 changed files with 988 additions and 195 deletions
+30 -6
View File
@@ -28,7 +28,6 @@ class UserModel extends Model
'failed_attempts',
'last_failed_at',
'semester',
'school_year',
'status',
'is_suspended',
'is_verified',
@@ -237,13 +236,23 @@ class UserModel extends Model
public function getUsersByRoleAndSchoolYear(string $roleName, string $schoolYear): array
{
return $this->select('users.*')
$builder = $this->select('users.*')
->join('user_roles', 'user_roles.user_id = users.id')
->join('roles', 'roles.id = user_roles.role_id')
->where('roles.name', $roleName)
->where('users.school_year', $schoolYear)
->where('user_roles.deleted_at', null)
->findAll();
->where('user_roles.deleted_at', null);
if ($this->userRolesHaveSchoolYear()) {
$builder->where('user_roles.school_year', $schoolYear);
} elseif ($this->roleUsesTeacherAssignments($roleName)) {
$builder->join(
'teacher_class tc_year',
'tc_year.teacher_id = users.id AND tc_year.school_year = ' . $this->db->escape($schoolYear),
'inner'
);
}
return $builder->groupBy('users.id')->findAll();
}
public function countAdminsBySchoolYear(string $schoolYear): int
@@ -260,8 +269,8 @@ class UserModel extends Model
)
->join('user_roles ur', 'ur.user_id = u.id', 'inner')
->join('roles r', 'r.id = ur.role_id', 'inner')
->where('u.school_year', $schoolYear)
->where('r.is_active', 1)
->where('LOWER(r.name) NOT IN ("guest","teacher","teacher_assistant","parent")', null, false)
->groupBy('u.id')
->having('is_admin', 1);
@@ -407,4 +416,19 @@ class UserModel extends Model
->orderBy($sort === 'roles' ? 'roles' : $sort, $order)
->findAll();
}
private function userRolesHaveSchoolYear(): bool
{
try {
return in_array('school_year', $this->db->getFieldNames('user_roles'), true);
} catch (\Throwable $e) {
return false;
}
}
private function roleUsesTeacherAssignments(string $roleName): bool
{
$role = strtolower(trim($roleName));
return $role === 'ta' || str_contains($role, 'teacher');
}
}