ADD SCHOOL YEAR MANAGEMENT
Tests / PHPUnit (push) Failing after 40s

This commit is contained in:
root
2026-07-12 02:21:39 -04:00
parent c7f67da9bf
commit e06ccc9cc0
36 changed files with 6722 additions and 327 deletions
+26 -54
View File
@@ -242,9 +242,7 @@ class UserModel extends Model
->where('roles.name', $roleName)
->where('user_roles.deleted_at', null);
if ($this->userRolesHaveSchoolYear()) {
$builder->where('user_roles.school_year', $schoolYear);
} elseif ($this->roleUsesTeacherAssignments($roleName)) {
if ($this->roleUsesTeacherAssignments($roleName)) {
$builder->join(
'teacher_class tc_year',
'tc_year.teacher_id = users.id AND tc_year.school_year = ' . $this->db->escape($schoolYear),
@@ -366,49 +364,32 @@ class UserModel extends Model
}
if (!empty($schoolYear)) {
// Prefer user_roles.school_year if present (true year-scoped roles)
$userRolesFields = [];
try {
$userRolesFields = $this->db->getFieldNames('user_roles');
} catch (\Throwable $e) {
// ignore
}
$escYear = $this->db->escape($schoolYear);
if (in_array('school_year', $userRolesFields, true)) {
$builder->where('ur.school_year', $schoolYear);
} else {
// Fallback: role-aware filter
$escYear = $this->db->escape($schoolYear);
// Include user if:
// (A) They are assigned as teacher/TA in teacher_class for that year
// OR
// (B) They have at least one non-teacher-ish (and non-parent) role (admin/staff/etc.)
//
// Teacher-ish detection: name contains 'teacher' OR equals 'ta'
$builder->groupStart()
// A) has teacher assignment that year
->where("
EXISTS (
SELECT 1
FROM teacher_class tc
WHERE tc.teacher_id = users.id
AND tc.school_year = {$escYear}
)
", null, false)
// B) OR has any non-teacher-ish, non-parent role
->orWhere("
EXISTS (
SELECT 1
FROM user_roles ur2
JOIN roles r2 ON r2.id = ur2.role_id
WHERE ur2.user_id = users.id
AND LOWER(r2.name) NOT IN ('parent','ta')
AND LOWER(r2.name) NOT LIKE '%teacher%'
)
", null, false)
->groupEnd();
}
// Include user if:
// (A) They are assigned as teacher/TA in teacher_class for that year
// OR
// (B) They have at least one non-teacher-ish (and non-parent) global role.
$builder->groupStart()
->where("
EXISTS (
SELECT 1
FROM teacher_class tc
WHERE tc.teacher_id = users.id
AND tc.school_year = {$escYear}
)
", null, false)
->orWhere("
EXISTS (
SELECT 1
FROM user_roles ur2
JOIN roles r2 ON r2.id = ur2.role_id
WHERE ur2.user_id = users.id
AND LOWER(r2.name) NOT IN ('parent','ta')
AND LOWER(r2.name) NOT LIKE '%teacher%'
)
", null, false)
->groupEnd();
}
return $builder
@@ -417,15 +398,6 @@ class UserModel extends Model
->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));