fix teacher pages display between school years
Tests / PHPUnit (push) Failing after 1m12s

This commit is contained in:
root
2026-07-15 20:52:19 -04:00
parent 5f27dccd0f
commit 9de2ab2a9f
9 changed files with 106 additions and 53 deletions
+50 -6
View File
@@ -68,8 +68,15 @@ class TeacherClassModel extends Model
public function getClassAssignmentsByUserId(int $userId, ?string $schoolYear = null, ?string $semester = null): array
{
$hasSectionSchoolYear = $this->db->fieldExists('school_year', 'classSection');
$sectionJoin = 'tc.class_section_id = cs.class_section_id';
if ($schoolYear && $hasSectionSchoolYear) {
$sectionJoin .= ' AND cs.school_year = ' . $this->db->escape($schoolYear);
}
$builder = $this->db->table('teacher_class tc')
->select([
'tc.class_section_id AS teacher_class_section_id',
'cs.class_section_name',
'cs.class_section_id AS class_section_pk',
'cs.class_section_id',
@@ -79,22 +86,28 @@ class TeacherClassModel extends Model
'tc.position',
'tc.school_year',
])
->join('classSection cs', 'tc.class_section_id = cs.class_section_id', 'inner')
->join('classSection cs', $sectionJoin, 'left', false)
->join('classes c', 'cs.class_id = c.id', 'left')
->where('tc.teacher_id', $userId)
->where('tc.class_section_id IS NOT NULL', null, false)
->orderBy('cs.class_section_name', 'ASC');
->orderBy('COALESCE(cs.class_section_name, tc.class_section_id)', 'ASC', false);
if ($schoolYear) $builder->where('tc.school_year', $schoolYear);
if ($schoolYear) {
$builder->where('tc.school_year', $schoolYear);
}
$rows = $builder->get()->getResultArray();
$out = [];
foreach ($rows as $r) {
$sectionId = (int) ($r['teacher_class_section_id'] ?? $r['class_section_id'] ?? 0);
$sectionName = $r['class_section_name']
?? $this->fallbackClassSectionName($sectionId, $schoolYear);
$out[] = [
'class_section_pk' => (int)$r['class_section_pk'],
'class_section_id' => (int)$r['class_section_id'],
'class_section_name' => $r['class_section_name'],
'class_section_pk' => (int)($r['class_section_pk'] ?? $sectionId),
'class_section_id' => $sectionId,
'class_section_name' => $sectionName,
'class_id' => isset($r['class_id']) ? (int)$r['class_id'] : null,
'class_name' => $r['class_name'] ?? null,
'teacher_id' => (int)$r['teacher_id'],
@@ -107,6 +120,37 @@ class TeacherClassModel extends Model
return $out;
}
private function fallbackClassSectionName(int $classSectionId, ?string $schoolYear = null): string
{
if ($classSectionId <= 0) {
return 'Class';
}
if ($schoolYear && $this->db->fieldExists('school_year', 'classSection')) {
$row = $this->db->table('classSection')
->select('class_section_name')
->where('class_section_id', $classSectionId)
->where('school_year', $schoolYear)
->get()
->getRowArray();
if (!empty($row['class_section_name'])) {
return (string) $row['class_section_name'];
}
}
$row = $this->db->table('classSection')
->select('class_section_name')
->where('class_section_id', $classSectionId)
->orderBy('id', 'DESC')
->get()
->getRowArray();
return !empty($row['class_section_name'])
? (string) $row['class_section_name']
: ('Class ' . $classSectionId);
}
public function getClassSectionsByTeacherId($teacherId)
{
$builder = $this->db->table('teacher_class')