From 9de2ab2a9f5f2ceb548a7db797a01e5b390b5778 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 15 Jul 2026 20:52:19 -0400 Subject: [PATCH] fix teacher pages display between school years --- .../SendExamDraftDeadlineReminders.php | 5 +- .../View/LandingPageController.php | 12 +++- app/Controllers/View/StudentController.php | 12 ++-- app/Controllers/View/TeacherController.php | 4 +- ...5-000000_RemoveClassAssignmentSemester.php | 23 ++++---- app/Models/StudentModel.php | 19 +++++++ app/Models/TeacherClassModel.php | 56 +++++++++++++++++-- .../teacher_class_assignment.php | 11 ---- app/Views/partials/navbar.php | 17 +++--- 9 files changed, 106 insertions(+), 53 deletions(-) diff --git a/app/Commands/SendExamDraftDeadlineReminders.php b/app/Commands/SendExamDraftDeadlineReminders.php index 7adfe80..0f86744 100644 --- a/app/Commands/SendExamDraftDeadlineReminders.php +++ b/app/Commands/SendExamDraftDeadlineReminders.php @@ -54,13 +54,10 @@ class SendExamDraftDeadlineReminders extends BaseCommand $db = Database::connect(); $teacherClassRows = $db->table('teacher_class') - ->select('teacher_id, class_section_id, school_year, semester') + ->select('teacher_id, class_section_id, school_year') ->when($schoolYear !== '', static function ($builder) use ($schoolYear) { return $builder->where('school_year', $schoolYear); }) - ->when($semester !== '', static function ($builder) use ($semester) { - return $builder->where('semester', $semester); - }) ->get() ->getResultArray(); diff --git a/app/Controllers/View/LandingPageController.php b/app/Controllers/View/LandingPageController.php index 531167e..121ba56 100644 --- a/app/Controllers/View/LandingPageController.php +++ b/app/Controllers/View/LandingPageController.php @@ -57,6 +57,7 @@ class LandingPageController extends BaseController // Fetch Enrollment and Refund Deadlines from Configuration $this->schoolYear = $this->configModel->getConfig('school_year'); $this->semester = $this->configModel->getConfig('semester'); + $this->schoolYear = $this->selectedSchoolYearName((string) ($this->schoolYear ?? '')); $this->lastDayOfRegistration = $this->configModel->getConfig('enrollment_deadline') ?? 'Not set'; $this->refundDeadline = $this->configModel->getConfig('refund_deadline') ?? 'Not set'; @@ -103,7 +104,7 @@ class LandingPageController extends BaseController // Get all class assignments for this teacher in the current term $assignments = $this->teacherClassModel->getClassAssignmentsByUserId( (int)$user_id, - (string)$this->schoolYear, + $this->selectedSchoolYearName((string) ($this->schoolYear ?? '')), (string)$this->semester ); @@ -126,6 +127,15 @@ class LandingPageController extends BaseController return $chosen; } + private function selectedSchoolYearName(string $fallback): string + { + try { + return service('schoolYearContext')->resolve(service('request'))->yearName(); + } catch (\Throwable) { + return $fallback; + } + } + public function administrator() { diff --git a/app/Controllers/View/StudentController.php b/app/Controllers/View/StudentController.php index 05386a9..7691181 100644 --- a/app/Controllers/View/StudentController.php +++ b/app/Controllers/View/StudentController.php @@ -1673,14 +1673,10 @@ class StudentController extends BaseController $students = []; if (in_array($role, ['parent', 'parent_dashboard'], true)) { + $schoolYear = $this->currentSchoolYearName((string) ($this->schoolYear ?? '')); $parentId = (int)(session()->get('user_id') ?? 0); - if ($parentId > 0) { - $students = $this->studentModel - ->select('id, school_id, firstname, lastname') - ->where('parent_id', $parentId) - ->orderBy('lastname', 'ASC') - ->orderBy('firstname', 'ASC') - ->findAll(); + if ($parentId > 0 && $schoolYear !== '') { + $students = $this->studentModel->getByParentAndYear($parentId, $schoolYear); } } elseif (in_array($role, ['teacher', 'teacher_assistant', 'teacher_dashboard'], true)) { $schoolYear = $this->currentSchoolYearName((string) ($this->schoolYear ?? '')); @@ -1706,7 +1702,7 @@ class StudentController extends BaseController )), static fn($v) => $v > 0)); if (!empty($sectionIds)) { $studentClassModel = new \App\Models\StudentClassModel(); - $rows = $studentClassModel->getStudentsByClassSectionIds($sectionIds); + $rows = $studentClassModel->getStudentsByClassSectionIds($sectionIds, $schoolYear); $unique = []; foreach ($rows as $r) { $sid = (int)($r['student_id'] ?? 0); diff --git a/app/Controllers/View/TeacherController.php b/app/Controllers/View/TeacherController.php index 4a85a28..1182f34 100644 --- a/app/Controllers/View/TeacherController.php +++ b/app/Controllers/View/TeacherController.php @@ -57,6 +57,7 @@ class TeacherController extends BaseController // Retrieve the configuration values $this->semester = $this->configModel->getConfig('semester'); $this->schoolYear = $this->configModel->getConfig('school_year'); + $this->schoolYear = $this->currentSchoolYearName((string) ($this->schoolYear ?? '')); } @@ -123,7 +124,7 @@ class TeacherController extends BaseController if (!empty($classSectionIds)) { // 1) Fetch all students for the selected class sections - $students = $this->studentClassModel->getStudentsByClassSectionIds($classSectionIds); + $students = $this->studentClassModel->getStudentsByClassSectionIds($classSectionIds, (string) $this->schoolYear); if (!empty($students)) { // 2) Collect unique student IDs @@ -344,7 +345,6 @@ class TeacherController extends BaseController 'section_id' => $sectionId, 'class_section_name' => $classNames[$sectionId] ?? 'N/A', 'role' => $role, - 'semester' => (string)($assign['semester'] ?? $this->semester ?? ''), 'school_year' => (string)($assign['school_year'] ?? $schoolYear), ]; } diff --git a/app/Database/Migrations/2026-01-25-000000_RemoveClassAssignmentSemester.php b/app/Database/Migrations/2026-01-25-000000_RemoveClassAssignmentSemester.php index 89f894a..7edda36 100644 --- a/app/Database/Migrations/2026-01-25-000000_RemoveClassAssignmentSemester.php +++ b/app/Database/Migrations/2026-01-25-000000_RemoveClassAssignmentSemester.php @@ -8,13 +8,20 @@ class RemoveClassAssignmentSemester extends Migration { public function up() { - // Keep the legacy semester columns in place. They may be ignored by newer - // code, but dropping them during a post-restore migrate destroys data. + if ( + $this->db->tableExists('teacher_class') + && $this->db->fieldExists('semester', 'teacher_class') + ) { + $this->forge->dropColumn('teacher_class', 'semester'); + } } public function down() { - if ($this->db->tableExists('teacher_class')) { + if ( + $this->db->tableExists('teacher_class') + && ! $this->db->fieldExists('semester', 'teacher_class') + ) { $this->forge->addColumn('teacher_class', [ 'semester' => [ 'type' => 'VARCHAR', @@ -24,16 +31,6 @@ class RemoveClassAssignmentSemester extends Migration ], ]); } - if ($this->db->tableExists('student_class')) { - $this->forge->addColumn('student_class', [ - 'semester' => [ - 'type' => 'VARCHAR', - 'constraint' => 255, - 'null' => false, - 'default' => '', - ], - ]); - } } private function indexExists(string $table, string $index): bool diff --git a/app/Models/StudentModel.php b/app/Models/StudentModel.php index e5b9a65..ed15de7 100644 --- a/app/Models/StudentModel.php +++ b/app/Models/StudentModel.php @@ -314,6 +314,25 @@ class StudentModel extends Model ->findAll(); } + public function getByParentAndYear(int $parentId, string $schoolYear): array + { + $schoolYear = trim($schoolYear); + + if ($parentId <= 0 || $schoolYear === '') { + return []; + } + + return $this->select('students.id, students.school_id, students.firstname, students.lastname') + ->join('student_class', 'student_class.student_id = students.id', 'inner') + ->where('students.parent_id', $parentId) + ->where('students.is_active', 1) + ->where('student_class.school_year', $schoolYear) + ->groupBy('students.id, students.school_id, students.firstname, students.lastname') + ->orderBy('students.lastname', 'ASC') + ->orderBy('students.firstname', 'ASC') + ->findAll(); + } + public function getSchoolIdByStudentId($studentId) { return $this->select('school_id') diff --git a/app/Models/TeacherClassModel.php b/app/Models/TeacherClassModel.php index d80db34..ddab36d 100644 --- a/app/Models/TeacherClassModel.php +++ b/app/Models/TeacherClassModel.php @@ -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') diff --git a/app/Views/administrator/teacher_class_assignment.php b/app/Views/administrator/teacher_class_assignment.php index 4425f75..63beaec 100644 --- a/app/Views/administrator/teacher_class_assignment.php +++ b/app/Views/administrator/teacher_class_assignment.php @@ -30,13 +30,6 @@ - - - @@ -183,9 +176,7 @@ document.addEventListener('DOMContentLoaded', function () { position = label.toLowerCase() === 'ta' ? 'ta' : 'main'; } btn.setAttribute('data-position', position); - var assignmentSemester = assignment.semester || ''; var assignmentSchoolYear = assignment.school_year || selectedYear || ''; - btn.setAttribute('data-semester', assignmentSemester); btn.setAttribute('data-school-year', assignmentSchoolYear); li.appendChild(btn); @@ -488,7 +479,6 @@ document.addEventListener('DOMContentLoaded', function () { var teacherId = removeBtn.getAttribute('data-teacher-id'); var classSectionId = removeBtn.getAttribute('data-class-section-id'); var position = removeBtn.getAttribute('data-position'); - var semester = removeBtn.getAttribute('data-semester') || ''; var schoolYear = removeBtn.getAttribute('data-school-year') || ''; var className = removeBtn.getAttribute('data-class-name') || 'this class'; var teacherName = removeBtn.getAttribute('data-teacher-name') || 'this teacher'; @@ -498,7 +488,6 @@ document.addEventListener('DOMContentLoaded', function () { teacher_id: teacherId, class_section_id: classSectionId, position: position, - semester: semester, school_year: schoolYear }); } diff --git a/app/Views/partials/navbar.php b/app/Views/partials/navbar.php index d4e8617..d6c738f 100644 --- a/app/Views/partials/navbar.php +++ b/app/Views/partials/navbar.php @@ -49,14 +49,15 @@ switch ($role) { try { $studentModel = new \App\Models\StudentModel(); if (in_array($role, ['parent', 'parent_dashboard'], true)) { + $configModel = new \App\Models\ConfigurationModel(); + try { + $schoolYear = service('schoolYearContext')->resolve(service('request'))->yearName(); + } catch (\Throwable $e) { + $schoolYear = session()->get('school_year') ?? $configModel->getConfig('school_year'); + } $parentId = (int)(session()->get('user_id') ?? 0); - if ($parentId > 0) { - $scoreCardStudents = $studentModel - ->select('id, school_id, firstname, lastname') - ->where('parent_id', $parentId) - ->orderBy('lastname', 'ASC') - ->orderBy('firstname', 'ASC') - ->findAll(); + if ($parentId > 0 && !empty($schoolYear)) { + $scoreCardStudents = $studentModel->getByParentAndYear($parentId, (string) $schoolYear); } } else { $configModel = new \App\Models\ConfigurationModel(); @@ -85,7 +86,7 @@ switch ($role) { )), static fn($v) => $v > 0)); if (!empty($sectionIds)) { $studentClassModel = new \App\Models\StudentClassModel(); - $rows = $studentClassModel->getStudentsByClassSectionIds($sectionIds); + $rows = $studentClassModel->getStudentsByClassSectionIds($sectionIds, (string) $schoolYear); $unique = []; foreach ($rows as $r) { $sid = (int)($r['student_id'] ?? 0);