diff --git a/app/Controllers/View/StudentController.php b/app/Controllers/View/StudentController.php index 365703e..94d5c91 100644 --- a/app/Controllers/View/StudentController.php +++ b/app/Controllers/View/StudentController.php @@ -935,15 +935,18 @@ class StudentController extends BaseController $previousSchoolYear = (string)($queueRow['school_year_from'] ?? ($this->previousSchoolYearName($year) ?? '')); $previousScore = $this->previousAverageScore($studentId, $previousSchoolYear); if ($previousScore === null && $this->db->tableExists('student_decisions')) { - $decisionRow = $this->db->table('student_decisions') - ->select('year_score') - ->where('student_id', $studentId) - ->where('school_year', $previousSchoolYear) - ->orderBy('updated_at', 'DESC') - ->orderBy('id', 'DESC') - ->get() - ->getRowArray(); - $previousScore = is_numeric($decisionRow['year_score'] ?? null) ? (float)$decisionRow['year_score'] : null; + $scoreField = $this->studentDecisionScoreField(); + if ($scoreField !== null) { + $decisionRow = $this->db->table('student_decisions') + ->select($scoreField . ' AS year_score') + ->where('student_id', $studentId) + ->where('school_year', $previousSchoolYear) + ->orderBy('updated_at', 'DESC') + ->orderBy('id', 'DESC') + ->get() + ->getRowArray(); + $previousScore = is_numeric($decisionRow['year_score'] ?? null) ? (float)$decisionRow['year_score'] : null; + } } $previousScore = $previousScore === null ? 0.0 : max(0.0, min(100.0, (float)$previousScore)); @@ -993,6 +996,14 @@ class StudentController extends BaseController private function distributionCandidates(int $classId, string $year): array { + if (! $this->db->tableExists('promotion_queue')) { + return $this->mergeDistributionCandidates( + $this->decisionDistributionCandidates($classId, $year), + $this->currentYearDistributionCandidates($classId, $year), + $this->registeredKgDistributionCandidates($classId, $year) + ); + } + if ($this->isDistributionKgClass($classId, $year)) { return $this->mergeDistributionCandidates( $this->kgDistributionCandidates($classId, $year), @@ -1000,13 +1011,22 @@ class StudentController extends BaseController ); } + $scoreField = $this->studentDecisionScoreField(); + $sourceYearForDecision = $this->previousSchoolYearName($year); + $select = 'pq.id AS promotion_queue_id, pq.student_id, pq.school_year_from, pq.to_class_id, source_cs.class_section_name AS source_class_name, students.firstname, students.lastname, students.gender, students.age, students.dob'; + if ($scoreField !== null && $sourceYearForDecision !== null) { + $select .= ', sd.' . $scoreField . ' AS decision_score'; + } + $builder = $this->db->table('promotion_queue pq') - ->select('pq.id AS promotion_queue_id, pq.student_id, pq.school_year_from, pq.to_class_id, students.firstname, students.lastname, students.gender, students.age, students.dob, sd.year_score AS decision_score') + ->select($select) ->join('students', 'students.id = pq.student_id', 'left') - ->join('student_decisions sd', 'sd.student_id = pq.student_id AND sd.school_year = pq.school_year_from', 'left') + ->join('classSection source_cs', 'source_cs.class_section_id = pq.from_class_section_id', 'left') ->where('pq.school_year_to', $year) - ->whereIn('pq.status', ['queued', 'assigned']) - ->groupBy('pq.id'); + ->whereIn('pq.status', ['queued', 'assigned']); + if ($scoreField !== null && $sourceYearForDecision !== null) { + $builder->join('student_decisions sd', 'sd.student_id = pq.student_id AND sd.school_year = ' . $this->db->escape($sourceYearForDecision), 'left', false); + } $this->applyDistributionAgeFilter($builder, $year); $rows = $builder ->get() @@ -1067,7 +1087,7 @@ class StudentController extends BaseController $builder = $this->db->table('student_class sc') ->select('0 AS promotion_queue_id, sc.student_id, sc.school_year AS school_year_from, cs.class_id AS source_class_id, cs.class_section_name AS source_class_name, students.firstname, students.lastname, students.gender, students.age, students.dob, students.registration_grade', false) ->join('students', 'students.id = sc.student_id', 'inner') - ->join('classSection cs', 'cs.class_section_id = sc.class_section_id AND cs.school_year = sc.school_year', 'left') + ->join('classSection cs', 'cs.class_section_id = sc.class_section_id AND cs.school_year = ' . $this->db->escape($year), 'left', false) ->where('sc.school_year', $year) ->where('sc.class_section_id IS NOT NULL', null, false); @@ -1090,7 +1110,7 @@ class StudentController extends BaseController $builder = $this->db->table('enrollments e') ->select('0 AS promotion_queue_id, e.student_id, e.school_year AS school_year_from, cs.class_id AS source_class_id, cs.class_section_name AS source_class_name, students.firstname, students.lastname, students.gender, students.age, students.dob, students.registration_grade', false) ->join('students', 'students.id = e.student_id', 'inner') - ->join('classSection cs', 'cs.class_section_id = e.class_section_id AND cs.school_year = e.school_year', 'left') + ->join('classSection cs', 'cs.class_section_id = e.class_section_id AND cs.school_year = ' . $this->db->escape($year), 'left', false) ->where('e.school_year', $year) ->whereIn('e.enrollment_status', ['admission under review', 'payment pending', 'enrolled']) ->groupStart() @@ -1221,7 +1241,7 @@ class StudentController extends BaseController $builder = $this->db->table('enrollments e') ->select('0 AS promotion_queue_id, e.student_id, e.school_year AS school_year_from, cs.class_id AS source_class_id, cs.class_section_name AS source_class_name, students.firstname, students.lastname, students.gender, students.age, students.dob, students.registration_grade', false) ->join('students', 'students.id = e.student_id', 'inner') - ->join('classSection cs', 'cs.class_section_id = e.class_section_id AND cs.school_year = e.school_year', 'left') + ->join('classSection cs', 'cs.class_section_id = e.class_section_id AND cs.school_year = ' . $this->db->escape($year), 'left', false) ->where('e.school_year', $year) ->whereIn('e.enrollment_status', ['admission under review', 'payment pending', 'enrolled']) ->groupStart() @@ -1384,7 +1404,7 @@ class StudentController extends BaseController if ($this->db->tableExists('student_class')) { $builder = $this->db->table('student_class sc') ->select('cs.class_section_name') - ->join('classSection cs', 'cs.class_section_id = sc.class_section_id AND cs.school_year = sc.school_year', 'left') + ->join('classSection cs', 'cs.class_section_id = sc.class_section_id AND cs.school_year = ' . $this->db->escape($previousYear), 'left', false) ->where('sc.student_id', $studentId) ->where('sc.school_year', $previousYear) ->where('sc.class_section_id IS NOT NULL', null, false); @@ -1407,7 +1427,7 @@ class StudentController extends BaseController if ($this->db->tableExists('enrollments')) { $rows = $this->db->table('enrollments e') ->select('cs.class_section_name') - ->join('classSection cs', 'cs.class_section_id = e.class_section_id AND cs.school_year = e.school_year', 'left') + ->join('classSection cs', 'cs.class_section_id = e.class_section_id AND cs.school_year = ' . $this->db->escape($previousYear), 'left', false) ->where('e.student_id', $studentId) ->where('e.school_year', $previousYear) ->where('e.class_section_id IS NOT NULL', null, false) @@ -1570,8 +1590,14 @@ class StudentController extends BaseController return []; } + $scoreField = $this->studentDecisionScoreField(); + $select = 'sd.id AS decision_id, sd.student_id, sd.class_section_name, sd.decision, students.firstname, students.lastname, students.gender, students.age, students.dob'; + if ($scoreField !== null) { + $select .= ', sd.' . $scoreField . ' AS year_score'; + } + $builder = $this->db->table('student_decisions sd') - ->select('sd.id AS decision_id, sd.student_id, sd.class_section_name, sd.year_score, sd.decision, students.firstname, students.lastname, students.gender, students.age, students.dob') + ->select($select) ->join('students', 'students.id = sd.student_id', 'left') ->where('sd.school_year', $previousSchoolYear) ->where('students.is_active', 1) @@ -1629,6 +1655,23 @@ class StudentController extends BaseController return $out; } + private function studentDecisionScoreField(): ?string + { + if (! $this->db->tableExists('student_decisions')) { + return null; + } + + if ($this->db->fieldExists('year_score', 'student_decisions')) { + return 'year_score'; + } + + if ($this->db->fieldExists('semester_score', 'student_decisions')) { + return 'semester_score'; + } + + return null; + } + private function targetClassIdFromDecision(string $classSectionName, string $decision, string $targetSchoolYear = ''): ?int { $baseName = strtoupper(trim(preg_replace('/-.+$/', '', $classSectionName) ?? '')); @@ -1772,7 +1815,7 @@ class StudentController extends BaseController if ($this->db->tableExists('student_class')) { $builder = $this->db->table('student_class sc') ->select('cs.class_section_name') - ->join('classSection cs', 'cs.class_section_id = sc.class_section_id AND cs.school_year = sc.school_year', 'left') + ->join('classSection cs', 'cs.class_section_id = sc.class_section_id AND cs.school_year = ' . $this->db->escape($previousYear), 'left', false) ->where('sc.student_id', $studentId) ->where('sc.school_year', $previousYear) ->where('sc.class_section_id IS NOT NULL', null, false); @@ -1799,7 +1842,7 @@ class StudentController extends BaseController if (empty($names) && $this->db->tableExists('enrollments')) { $rows = $this->db->table('enrollments e') ->select('cs.class_section_name') - ->join('classSection cs', 'cs.class_section_id = e.class_section_id AND cs.school_year = e.school_year', 'left') + ->join('classSection cs', 'cs.class_section_id = e.class_section_id AND cs.school_year = ' . $this->db->escape($previousYear), 'left', false) ->where('e.student_id', $studentId) ->where('e.school_year', $previousYear) ->where('e.class_section_id IS NOT NULL', null, false) @@ -2387,7 +2430,7 @@ class StudentController extends BaseController $builder = $this->db->table('student_section_distribution_drafts d') ->select('d.id AS draft_id, d.class_id, d.class_section_id, d.previous_school_year, d.previous_final_score, cs.class_section_name, students.firstname, students.lastname, students.gender, students.dob, d.student_id') - ->join('classSection cs', 'cs.class_section_id = d.class_section_id AND cs.school_year = d.school_year', 'left') + ->join('classSection cs', 'cs.class_section_id = d.class_section_id AND cs.school_year = ' . $this->db->escape($year), 'left', false) ->join('students', 'students.id = d.student_id', 'left') ->where('d.class_id', $classId) ->where('d.school_year', $year)