From 065466853073c0eb3cbc0e41d5563f7da3d9a52f Mon Sep 17 00:00:00 2001 From: root Date: Thu, 28 May 2026 01:55:34 -0400 Subject: [PATCH] fix report ranking --- .../View/ReportCardsController.php | 64 +++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/app/Controllers/View/ReportCardsController.php b/app/Controllers/View/ReportCardsController.php index 4dfc0a2..734e09a 100644 --- a/app/Controllers/View/ReportCardsController.php +++ b/app/Controllers/View/ReportCardsController.php @@ -1584,13 +1584,18 @@ $scoresEndY = $pdf->GetY(); $firstSemesterScore = $secondSemesterScore; } + $rankingScore = $secondSemesterScore; + if ($normSemester === 'spring' && is_numeric($firstSemesterScore) && is_numeric($secondSemesterScore)) { + $rankingScore = round(((float)$firstSemesterScore + (float)$secondSemesterScore) / 2, 1); + } + $termRanking = $this->calculateTermRanking( $studentId, $sectionCode, $sectionId, $refYear, $refSemester !== '' ? $refSemester : (string)($score['semester'] ?? ''), - $secondSemesterScore + $rankingScore ); // Total semester days from attendance records (max total_attendance within same term) @@ -1748,8 +1753,11 @@ $scoresEndY = $pdf->GetY(); return null; } + $semesterForRank = trim((string)$semester); + $rankByFinalScore = $this->normalizeSemester($semesterForRank) === 'spring'; + $builder = $this->db->table('semester_scores ss') - ->select('ss.student_id, ss.semester_score, ss.updated_at, ss.id, s.firstname, s.lastname') + ->select('ss.student_id, ss.semester, ss.semester_score, ss.updated_at, ss.id, s.firstname, s.lastname') ->join('students s', 's.id = ss.student_id', 'inner') ->where('s.is_active', 1) ->where('ss.school_year', $schoolYear) @@ -1757,8 +1765,8 @@ $scoresEndY = $pdf->GetY(); ->orderBy('ss.updated_at', 'DESC') ->orderBy('ss.id', 'DESC'); - if (trim((string)$semester) !== '') { - $this->applySemesterFilter($builder, (string)$semester, 'ss.semester'); + if ($semesterForRank !== '') { + $this->applySemesterFilter($builder, $semesterForRank, 'ss.semester'); } $rows = $builder->get()->getResultArray(); @@ -1767,6 +1775,7 @@ $scoresEndY = $pdf->GetY(); } $scoresByStudent = []; + $studentIds = []; foreach ($rows as $row) { $sid = (int)($row['student_id'] ?? 0); if ($sid <= 0 || isset($scoresByStudent[$sid])) { @@ -1784,12 +1793,59 @@ $scoresEndY = $pdf->GetY(); 'firstname' => trim((string)($row['firstname'] ?? '')), 'lastname' => trim((string)($row['lastname'] ?? '')), ]; + $studentIds[] = $sid; } if (empty($scoresByStudent) || !isset($scoresByStudent[$studentId])) { return null; } + if ($rankByFinalScore) { + $firstRowsBuilder = $this->db->table('semester_scores ss') + ->select('ss.student_id, ss.semester_score, ss.updated_at, ss.id') + ->where('ss.school_year', $schoolYear) + ->whereIn('ss.class_section_id', $sectionIds) + ->whereIn('ss.student_id', $studentIds) + ->orderBy('ss.updated_at', 'DESC') + ->orderBy('ss.id', 'DESC'); + + if ($semesterForRank !== '') { + $this->applySemesterExclusion($firstRowsBuilder, $semesterForRank, 'ss.semester'); + } + + $firstRows = $firstRowsBuilder->get()->getResultArray(); + $firstScoresByStudent = []; + foreach ($firstRows as $row) { + $sid = (int)($row['student_id'] ?? 0); + if ($sid <= 0 || isset($firstScoresByStudent[$sid])) { + continue; + } + + $scoreVal = $row['semester_score'] ?? null; + if (!is_numeric($scoreVal)) { + continue; + } + + $firstScoresByStudent[$sid] = (float)$scoreVal; + } + + foreach ($scoresByStudent as $sid => &$rankRow) { + if (!isset($firstScoresByStudent[$sid])) { + unset($scoresByStudent[$sid]); + continue; + } + + $rankRow['score'] = round(((float)$firstScoresByStudent[$sid] + (float)$rankRow['score']) / 2, 4); + } + unset($rankRow); + + if (empty($scoresByStudent) || !isset($scoresByStudent[$studentId])) { + return null; + } + + $scoresByStudent[$studentId]['score'] = round((float)$studentScore, 4); + } + $rankable = array_values($scoresByStudent); usort($rankable, static function (array $a, array $b): int { $scoreCmp = $b['score'] <=> $a['score'];