fix parent and teacher pages to follow year filter
Tests / PHPUnit (push) Failing after 1m15s

This commit is contained in:
root
2026-07-15 20:03:36 -04:00
parent feb1b29a32
commit 5f27dccd0f
32 changed files with 582 additions and 364 deletions
+99 -24
View File
@@ -54,6 +54,7 @@ class ClassProgressController extends BaseController
public function create()
{
$context = $this->resolveSchoolYearContext();
$teacherId = (int) session()->get('user_id');
$assignments = $this->loadTeacherSections($teacherId);
$first = $assignments[0] ?? null;
@@ -76,12 +77,15 @@ class ClassProgressController extends BaseController
'classId' => $classId,
'sundayOptions' => $sundayOptions,
'defaultWeekStart' => $defaultWeekStart,
'isSchoolYearReadonly' => $context->isReadonly(),
];
return view('teacher/class_progress_submit', $data);
}
public function store()
{
$this->assertSchoolYearWritable($this->resolveSchoolYearContext());
[, $schoolYear] = $this->resolveCurrentTerm();
$subjectSections = self::SUBJECT_SECTIONS;
$rules = [
'class_section_id' => 'required|integer',
@@ -132,8 +136,9 @@ class ClassProgressController extends BaseController
->select('id')
->where('class_section_id', $classSectionId)
->where('week_start', $weekStart)
->where('teacher_id', $teacherId)
->findAll();
->where('teacher_id', $teacherId);
$this->applyProgressSchoolYearScope($existingReports, $schoolYear);
$existingReports = $existingReports->findAll();
if (! $confirmOverwrite && ! empty($existingReports)) {
return redirect()->back()
@@ -175,7 +180,7 @@ class ClassProgressController extends BaseController
'homework' => $homework ?: null,
'status' => $status,
'flags_json' => $this->normalizeFlags($this->request->getPost('flags')),
];
] + $this->progressReportSchoolYearData($schoolYear);
$reportId = $this->reportModel->insert($data, true);
$attachmentField = "attachment_$slug";
@@ -198,6 +203,7 @@ class ClassProgressController extends BaseController
public function history()
{
$teacherId = (int) session()->get('user_id');
[$semester, $schoolYear] = $this->resolveCurrentTerm();
$assignments = $this->loadTeacherSections($teacherId);
$selectedSectionId = (int) $this->request->getGet('class_section_id');
$validSectionIds = array_column($assignments, 'class_section_id');
@@ -207,7 +213,6 @@ class ClassProgressController extends BaseController
if ($selectedSectionId && ! in_array($selectedSectionId, $validSectionIds, true)) {
$selectedSectionId = $validSectionIds[0] ?? null;
}
[$semester, $schoolYear] = $this->resolveCurrentTerm();
$allowedTeacherIds = $this->resolveAssignedTeacherIds($selectedSectionId, $semester, $schoolYear);
if (empty($allowedTeacherIds)) {
$allowedTeacherIds = [$teacherId];
@@ -217,6 +222,7 @@ class ClassProgressController extends BaseController
->join('classSection cs', 'cs.class_section_id = class_progress_reports.class_section_id', 'left')
->join('users u', 'u.id = class_progress_reports.teacher_id', 'left')
->whereIn('teacher_id', $allowedTeacherIds);
$this->applyProgressSchoolYearScope($builder, $schoolYear, true);
if ($selectedSectionId) {
$builder->where('class_progress_reports.class_section_id', $selectedSectionId);
}
@@ -251,24 +257,27 @@ class ClassProgressController extends BaseController
'subjectSections' => self::SUBJECT_SECTIONS,
'classSectionOptions' => $sectionOptions,
'selectedSectionId' => $selectedSectionId,
'schoolYear' => $schoolYear,
'isSchoolYearReadonly' => $this->resolveSchoolYearContext()->isReadonly(),
]);
}
public function view($id)
{
$teacherId = (int) session()->get('user_id');
$row = $this->reportModel
[$semester, $schoolYear] = $this->resolveCurrentTerm();
$rowQuery = $this->reportModel
->select('class_progress_reports.*, cs.class_section_name, CONCAT(IFNULL(u.firstname, ""), " ", IFNULL(u.lastname, "")) AS teacher_name')
->join('classSection cs', 'cs.class_section_id = class_progress_reports.class_section_id', 'left')
->join('users u', 'u.id = class_progress_reports.teacher_id', 'left')
->where('class_progress_reports.id', (int) $id)
->first();
->where('class_progress_reports.id', (int) $id);
$this->applyProgressSchoolYearScope($rowQuery, $schoolYear, true);
$row = $rowQuery->first();
if (! $row) {
throw new PageNotFoundException('Progress report not found.');
}
[$semester, $schoolYear] = $this->resolveCurrentTerm();
$allowedTeacherIds = $this->resolveAssignedTeacherIds((int) $row['class_section_id'], $semester, $schoolYear);
if (empty($allowedTeacherIds)) {
if ($teacherId !== (int) $row['teacher_id']) {
@@ -286,7 +295,9 @@ class ClassProgressController extends BaseController
->join('users u', 'u.id = class_progress_reports.teacher_id', 'left')
->whereIn('teacher_id', $allowedTeacherIds)
->where('class_progress_reports.class_section_id', $row['class_section_id'])
->where('week_start', $row['week_start'])
->where('week_start', $row['week_start']);
$this->applyProgressSchoolYearScope($weeklyReportsQuery, $schoolYear);
$weeklyReports = $weeklyReportsQuery
->orderBy('subject', 'ASC')
->findAll();
@@ -313,17 +324,18 @@ class ClassProgressController extends BaseController
public function edit($id)
{
$teacherId = (int) session()->get('user_id');
$row = $this->reportModel
[$semester, $schoolYear] = $this->resolveCurrentTerm();
$rowQuery = $this->reportModel
->select('class_progress_reports.*, cs.class_section_name')
->join('classSection cs', 'cs.class_section_id = class_progress_reports.class_section_id', 'left')
->where('class_progress_reports.id', (int) $id)
->first();
->where('class_progress_reports.id', (int) $id);
$this->applyProgressSchoolYearScope($rowQuery, $schoolYear);
$row = $rowQuery->first();
if (! $row) {
throw new PageNotFoundException('Progress report not found.');
}
[$semester, $schoolYear] = $this->resolveCurrentTerm();
$allowedTeacherIds = $this->resolveAssignedTeacherIds((int) $row['class_section_id'], $semester, $schoolYear);
if (empty($allowedTeacherIds)) {
if ($teacherId !== (int) $row['teacher_id']) {
@@ -338,7 +350,9 @@ class ClassProgressController extends BaseController
->select('class_progress_reports.*')
->whereIn('teacher_id', $allowedTeacherIds)
->where('class_section_id', $row['class_section_id'])
->where('week_start', $row['week_start'])
->where('week_start', $row['week_start']);
$this->applyProgressSchoolYearScope($weeklyReportsQuery, $schoolYear);
$weeklyReports = $weeklyReportsQuery
->orderBy('subject', 'ASC')
->findAll();
@@ -405,18 +419,23 @@ class ClassProgressController extends BaseController
'isEdit' => true,
'formAction' => base_url('teacher/progress/update/' . (int) $row['id']),
'submitLabel' => 'Update Progress',
'isSchoolYearReadonly' => $this->resolveSchoolYearContext()->isReadonly(),
]);
}
public function update($id)
{
$this->assertSchoolYearWritable($this->resolveSchoolYearContext());
$teacherId = (int) session()->get('user_id');
$row = $this->reportModel->find((int) $id);
[$semester, $schoolYear] = $this->resolveCurrentTerm();
$rowQuery = $this->reportModel
->where('class_progress_reports.id', (int) $id);
$this->applyProgressSchoolYearScope($rowQuery, $schoolYear);
$row = $rowQuery->first();
if (! $row) {
throw new PageNotFoundException('Progress report not found.');
}
[$semester, $schoolYear] = $this->resolveCurrentTerm();
$allowedTeacherIds = $this->resolveAssignedTeacherIds((int) $row['class_section_id'], $semester, $schoolYear);
if (empty($allowedTeacherIds)) {
if ($teacherId !== (int) $row['teacher_id']) {
@@ -473,8 +492,9 @@ class ClassProgressController extends BaseController
->select('id')
->where('class_section_id', $classSectionId)
->where('week_start', $weekStart)
->where('teacher_id', $teacherId)
->findAll();
->where('teacher_id', $teacherId);
$this->applyProgressSchoolYearScope($conflicts, $schoolYear);
$conflicts = $conflicts->findAll();
if (! $confirmOverwrite && ! empty($conflicts)) {
return redirect()->back()
->withInput()
@@ -497,7 +517,9 @@ class ClassProgressController extends BaseController
->select('class_progress_reports.*')
->whereIn('teacher_id', $allowedTeacherIds)
->where('class_section_id', $classSectionId)
->where('week_start', $row['week_start'])
->where('week_start', $row['week_start']);
$this->applyProgressSchoolYearScope($weeklyReportsQuery, $schoolYear);
$weeklyReports = $weeklyReportsQuery
->orderBy('subject', 'ASC')
->findAll();
@@ -533,7 +555,7 @@ class ClassProgressController extends BaseController
'unit_title' => $unitTitle,
'covered' => $covered,
'homework' => $homework ?: null,
];
] + $this->progressReportSchoolYearData($schoolYear);
if ($flagsInput !== null) {
$data['flags_json'] = $this->normalizeFlags($flagsInput);
}
@@ -698,8 +720,7 @@ class ClassProgressController extends BaseController
protected function loadTeacherSections(int $teacherId): array
{
$schoolYear = (string) ($this->configModel->getConfig('school_year') ?? '');
$semester = (string) ($this->configModel->getConfig('semester') ?? '');
[$semester, $schoolYear] = $this->resolveCurrentTerm();
return $this->teacherClassModel->getClassAssignmentsByUserId($teacherId, $schoolYear, $semester);
}
@@ -878,7 +899,7 @@ class ClassProgressController extends BaseController
protected function resolveProgressDateRange(): ?array
{
$schoolYear = (string) ($this->configModel->getConfig('school_year') ?? '');
$schoolYear = $this->currentSchoolYearName((string) ($this->configModel->getConfig('school_year') ?? ''));
if ($schoolYear === '') {
return null;
}
@@ -946,11 +967,65 @@ class ClassProgressController extends BaseController
protected function resolveCurrentTerm(): array
{
$schoolYear = (string) ($this->configModel->getConfig('school_year') ?? '');
$schoolYear = $this->currentSchoolYearName((string) ($this->configModel->getConfig('school_year') ?? ''));
$semester = (string) ($this->configModel->getConfig('semester') ?? '');
return [$semester, $schoolYear];
}
protected function applyProgressSchoolYearScope($builder, string $schoolYear, bool $canScopeBySection = false): void
{
if ($schoolYear === '') {
return;
}
$db = db_connect();
$hasReportYear = $db->fieldExists('school_year', 'class_progress_reports');
$hasSectionYear = $db->fieldExists('school_year', 'classSection');
$semesterResolver = new SemesterRangeService($this->configModel);
[$rangeStart, $rangeEnd] = $semesterResolver->getSchoolYearRange($schoolYear);
if ($hasReportYear) {
if ($rangeStart !== '' && $rangeEnd !== '') {
$builder
->groupStart()
->where('class_progress_reports.school_year', $schoolYear)
->orGroupStart()
->groupStart()
->where('class_progress_reports.school_year IS NULL', null, false)
->orWhere('class_progress_reports.school_year', '')
->groupEnd()
->where('class_progress_reports.week_start >=', $rangeStart)
->where('class_progress_reports.week_start <=', $rangeEnd)
->groupEnd()
->groupEnd();
return;
}
$builder->where('class_progress_reports.school_year', $schoolYear);
return;
}
if ($canScopeBySection && $hasSectionYear) {
$builder->where('cs.school_year', $schoolYear);
return;
}
if ($rangeStart !== '' && $rangeEnd !== '') {
$builder
->where('class_progress_reports.week_start >=', $rangeStart)
->where('class_progress_reports.week_start <=', $rangeEnd);
}
}
protected function progressReportSchoolYearData(string $schoolYear): array
{
if ($schoolYear === '' || ! db_connect()->fieldExists('school_year', 'class_progress_reports')) {
return [];
}
return ['school_year' => $schoolYear];
}
protected function resolveAssignedTeacherIds(?int $classSectionId, string $semester, string $schoolYear): array
{
if (! $classSectionId || $schoolYear === '') {