fix distribution page
Tests / PHPUnit (push) Successful in 1m17s

This commit is contained in:
root
2026-07-31 19:49:49 -04:00
parent 10dc8a33b4
commit b6f3b14e7b
3 changed files with 368 additions and 50 deletions
+65 -4
View File
@@ -919,7 +919,7 @@ class StudentController extends BaseController
}
// Fetch lettered sections for this class
$letters = $this->classSectionModel->getLetterSectionsByClassId($classId);
$letters = $this->letterSectionsForDistribution($classId, $year);
if (empty($letters)) {
$msg = 'No lettered sections found for the selected class.';
return $isAjax ? $json(['ok' => false, 'message' => $msg], 400) : redirect()->back()->with('error', $msg);
@@ -1061,6 +1061,24 @@ class StudentController extends BaseController
return $out;
}
private function letterSectionsForDistribution(int $classId, string $year): array
{
$query = $this->classSectionModel
->where('class_id', $classId)
->like('class_section_name', '-', 'both')
->orderBy('class_section_name', 'ASC');
if ($year !== '' && $this->db->fieldExists('school_year', 'classSection')) {
$query->where('school_year', $year);
}
$sections = $query->findAll();
if (!empty($sections) || $year === '' || ! $this->db->fieldExists('school_year', 'classSection')) {
return $sections;
}
return $this->classSectionModel->getLetterSectionsByClassId($classId);
}
private function decisionDistributionCandidates(int $classId, string $targetSchoolYear): array
{
$previousSchoolYear = $this->previousSchoolYearName($targetSchoolYear);
@@ -1315,12 +1333,22 @@ class StudentController extends BaseController
{
try {
$year = trim((string)($this->request->getGet('school_year') ?? $this->schoolYear));
$includeClassIds = $this->parseIncludedClassIds($this->request->getGet('include_class_ids'));
// Fetch base sections (no dash) and filter to KG, 1..9, youth
$bases = $this->classSectionModel
$baseQuery = $this->classSectionModel
->where("class_section_name NOT LIKE '%-%'", null, false)
->orderBy('class_id', 'ASC')
->findAll();
->orderBy('class_id', 'ASC');
if ($year !== '' && $this->db->fieldExists('school_year', 'classSection')) {
$baseQuery->where('school_year', $year);
}
$bases = $baseQuery->findAll();
if (empty($bases) && $year !== '' && $this->db->fieldExists('school_year', 'classSection')) {
$bases = $this->classSectionModel
->where("class_section_name NOT LIKE '%-%'", null, false)
->orderBy('class_id', 'ASC')
->findAll();
}
$wanted = [];
foreach ($bases as $r) {
@@ -1337,10 +1365,27 @@ class StudentController extends BaseController
$num = (int)$name;
if ($num >= 1 && $num <= 9) {
$wanted[] = $r;
continue;
}
}
if (in_array((int)($r['class_id'] ?? 0), $includeClassIds, true)) {
$wanted[] = $r;
}
}
$deduped = [];
$seenClassIds = [];
foreach ($wanted as $r) {
$classId = (int)($r['class_id'] ?? 0);
if ($classId <= 0 || isset($seenClassIds[$classId])) {
continue;
}
$seenClassIds[$classId] = true;
$deduped[] = $r;
}
$wanted = $deduped;
$out = [];
foreach ($wanted as $r) {
$classId = (int)$r['class_id'];
@@ -1371,6 +1416,22 @@ class StudentController extends BaseController
}
}
private function parseIncludedClassIds($raw): array
{
if ($raw === null || $raw === '') {
return [];
}
if (!is_array($raw)) {
$raw = explode(',', (string)$raw);
}
return array_values(array_unique(array_filter(
array_map('intval', $raw),
static fn(int $id): bool => $id > 0
)));
}
private function savedDistributionSections(int $classId, string $year): array
{
if (! $this->db->tableExists('student_section_distribution_drafts')) {