fix issues related to school year
Tests / PHPUnit (push) Failing after 34s

This commit is contained in:
root
2026-07-31 18:52:25 -04:00
parent 8d7ee3b9fc
commit 09ea144bb2
23 changed files with 537 additions and 218 deletions
@@ -72,13 +72,18 @@ class HomeworkTrackingController extends BaseController
}
$limitToSemester = $this->hasTeacherAssignments($this->schoolYear, $this->semester);
$semesterVariants = $this->getSemesterVariants($this->semester);
// Aggregate homework presence + first entered date per class_section_id + homework_index
// Aggregate submitted homework per class_section_id + homework_index.
// Adding a homework column creates blank rows for every student, so only
// indexes with at least one non-null score count as submitted.
$hwQ = $this->db->table('homework')
->select('class_section_id, homework_index, MIN(created_at) AS first_created, COUNT(*) AS cnt')
->select('class_section_id, homework_index')
->select('MIN(CASE WHEN score IS NOT NULL THEN updated_at ELSE NULL END) AS first_submitted', false)
->select('COUNT(score) AS scored_count', false)
->where('school_year', $this->schoolYear);
if ($limitToSemester && $this->semester !== '') {
$hwQ->where('semester', $this->semester);
if ($limitToSemester && $semesterVariants !== []) {
$hwQ->whereIn('semester', $semesterVariants);
}
$rows = $hwQ->groupBy('class_section_id, homework_index')
->get()
@@ -90,60 +95,32 @@ class HomeworkTrackingController extends BaseController
foreach ($rows as $r) {
$csid = (int)($r['class_section_id'] ?? 0);
$hi = (int)($r['homework_index'] ?? 0);
$cnt = (int)($r['cnt'] ?? 0);
$cnt = (int)($r['scored_count'] ?? 0);
if ($csid > 0 && $hi > 0 && $cnt > 0) {
$hasHomework[$csid][$hi] = true;
$dateStr = substr((string)($r['first_created'] ?? ''), 0, 10);
$dateStr = substr((string)($r['first_submitted'] ?? ''), 0, 10);
$hwEnteredAt[$csid][$hi] = $dateStr ?: null;
$homeworkSubmissionCounts[$csid] = ($homeworkSubmissionCounts[$csid] ?? 0) + 1;
}
}
// Build date-based presence mapped to the nearest prior non-NoSchool Sunday.
$hb = $this->db->table('homework')
->select("class_section_id, DATE(created_at) AS hw_date, MIN(created_at) AS first_created, COUNT(*) AS cnt", false)
->where('school_year', $this->schoolYear);
if ($limitToSemester && $this->semester !== '') {
$hb->where('semester', $this->semester);
$indexToDate = [];
foreach ($dateToIndex as $ymd => $homeworkIndex) {
if ($homeworkIndex !== null) {
$indexToDate[(int) $homeworkIndex] = $ymd;
}
}
$rowsByDate = $hb->groupBy('class_section_id, DATE(created_at)', '', false)
->orderBy('hw_date', 'ASC', false)
->get()
->getResultArray();
$hasHomeworkByDate = [];
$hwEnteredAtByDate = [];
foreach ($rowsByDate as $r) {
$csid = (int)($r['class_section_id'] ?? 0);
$d = substr((string)($r['hw_date'] ?? ''), 0, 10);
$cnt = (int)($r['cnt'] ?? 0);
$firstCreated = substr((string)($r['first_created'] ?? ''), 0, 10);
if ($csid <= 0 || !$d || $cnt <= 0) { continue; }
// Find the index of the last Sunday on or before $d
$baseIndex = -1;
for ($i = count($sundays) - 1; $i >= 0; $i--) {
if ($sundays[$i] <= $d) { $baseIndex = $i; break; }
}
if ($baseIndex < 0) { continue; }
// Map this homework day to only the nearest prior nonNo School Sunday
$j = $baseIndex;
while ($j >= 0 && !empty($eventDays[$sundays[$j]])) { $j--; }
if ($j < 0) { continue; }
$sd = $sundays[$j];
// Mark homework on that Sunday for this class_section (single mapping)
$hasHomeworkByDate[$csid][$sd] = true;
if (empty($hwEnteredAtByDate[$csid][$sd])) {
$hwEnteredAtByDate[$csid][$sd] = $firstCreated ?: $d;
} else {
// Keep the earliest date for display
$existing = (string)$hwEnteredAtByDate[$csid][$sd];
$candidate = $firstCreated ?: $d;
if ($candidate && (!$existing || $candidate < $existing)) {
$hwEnteredAtByDate[$csid][$sd] = $candidate;
foreach ($hasHomework as $csid => $indexes) {
foreach ($indexes as $homeworkIndex => $_submitted) {
$ymd = $indexToDate[(int) $homeworkIndex] ?? null;
if ($ymd === null) {
continue;
}
$hasHomeworkByDate[(int) $csid][$ymd] = true;
$hwEnteredAtByDate[(int) $csid][$ymd] = $hwEnteredAt[(int) $csid][(int) $homeworkIndex] ?? null;
}
}
@@ -252,6 +229,21 @@ class HomeworkTrackingController extends BaseController
return $this->teacherAssignmentCache[$year];
}
private function getSemesterVariants(string $semester): array
{
$trimmed = trim($semester);
if ($trimmed === '') {
return [];
}
return array_values(array_unique([
$trimmed,
ucfirst(strtolower($trimmed)),
strtolower($trimmed),
strtoupper($trimmed),
]));
}
/**
* Compute start/end dates for the given semester within the school year.
* Fall: 09/21/{startYear} to 01/18/{startYear+1}