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
+115 -6
View File
@@ -12,6 +12,7 @@ use App\Models\TeacherClassModel;
use App\Models\AdminNotificationSubjectModel;
use App\Models\NotificationModel;
use App\Models\UserNotificationModel;
use App\Services\SemesterRangeService;
use CodeIgniter\Exceptions\PageNotFoundException;
class PrintRequests extends BaseController
@@ -46,18 +47,26 @@ class PrintRequests extends BaseController
public function teacher_index()
{
$teacher_id = session()->get('user_id');
$context = $this->resolveSchoolYearContext();
$schoolYear = $context->yearName();
$data['print_requests'] = $this->printRequestModel
$printRequestsQuery = $this->printRequestModel
->select('print_requests.*, admins.firstname as admin_firstname, admins.lastname as admin_lastname')
->join('users as admins', 'admins.id = print_requests.admin_id', 'left')
->where('print_requests.teacher_id', $teacher_id)
->join('classSection cs', 'cs.class_section_id = print_requests.class_id', 'left')
->where('print_requests.teacher_id', $teacher_id);
$this->applyPrintRequestSchoolYearScope($printRequestsQuery, $schoolYear);
$data['print_requests'] = $printRequestsQuery
->orderBy('print_requests.required_by', 'DESC')
->orderBy('print_requests.id', 'DESC')
->findAll();
$teacher_classes = $this->teacherClassModel->getClassByTeacherId($teacher_id);
$teacher_classes = $this->teacherClassModel->getClassAssignmentsByUserId((int) $teacher_id, $schoolYear);
$data['class_id'] = !empty($teacher_classes) ? $teacher_classes[0]['class_section_id'] : null;
$dateOptions = $this->buildRequiredByOptions();
$data['sundays'] = $dateOptions['sundays'];
$data['times'] = $dateOptions['times'];
$data['isSchoolYearReadonly'] = $context->isReadonly();
return view('print_requests/teacher_index', $data);
}
@@ -94,6 +103,9 @@ class PrintRequests extends BaseController
public function create()
{
$context = $this->resolveSchoolYearContext();
$this->assertSchoolYearWritable($context);
$schoolYear = $context->yearName();
$validationRules = [
'file' => 'uploaded[file]|max_size[file,5120]|ext_in[file,pdf,jpg,png,jpeg,doc,docx,txt]',
'page_selection' => 'permit_empty|regex_match[/^\\s*\\d+(?:\\s*-\\s*\\d+)?(?:\\s*,\\s*\\d+(?:\\s*-\\s*\\d+)?)*\\s*$/]',
@@ -125,7 +137,7 @@ class PrintRequests extends BaseController
'required_by' => $this->request->getPost('required_by'),
'pickup_method' => $this->request->getPost('pickup_method'),
'status' => 'not_assigned',
];
] + $this->printRequestSchoolYearData($schoolYear);
$printRequestId = (int) $this->printRequestModel->insert($data, true);
if ($printRequestId > 0) {
@@ -175,6 +187,7 @@ class PrintRequests extends BaseController
// Case 2: Teacher edit
if ($this->request->getPost('num_copies')) {
$this->assertSchoolYearWritable($this->resolveSchoolYearContext());
$user_id = session()->get('user_id');
if ($request['teacher_id'] != $user_id) {
return redirect()->to('teacher/print-requests')->with('error', 'You are not authorized to edit this request.');
@@ -230,6 +243,7 @@ class PrintRequests extends BaseController
public function delete($id)
{
$this->assertSchoolYearWritable($this->resolveSchoolYearContext());
$teacher_id = session()->get('user_id');
$request = $this->printRequestModel->find($id);
@@ -260,6 +274,9 @@ class PrintRequests extends BaseController
public function copy($id)
{
$teacher_id = session()->get('user_id');
$context = $this->resolveSchoolYearContext();
$this->assertSchoolYearWritable($context);
$schoolYear = $context->yearName();
$request = $this->printRequestModel->find($id);
if (!$request) {
@@ -296,7 +313,7 @@ class PrintRequests extends BaseController
'required_by' => $request['required_by'],
'pickup_method' => $request['pickup_method'],
'status' => 'not_assigned',
];
] + $this->printRequestSchoolYearData($schoolYear);
$copiedId = (int) $this->printRequestModel->insert($data, true);
if ($copiedId > 0) {
@@ -308,6 +325,9 @@ class PrintRequests extends BaseController
public function createCopy()
{
$context = $this->resolveSchoolYearContext();
$this->assertSchoolYearWritable($context);
$schoolYear = $context->yearName();
$validationRules = [
'num_copies' => 'required|integer|greater_than[0]',
'required_by' => 'required|valid_date',
@@ -329,7 +349,7 @@ class PrintRequests extends BaseController
'required_by' => $this->request->getPost('required_by'),
'pickup_method' => $this->request->getPost('pickup_method'),
'status' => 'not_assigned',
];
] + $this->printRequestSchoolYearData($schoolYear);
$printRequestId = (int) $this->printRequestModel->insert($data, true);
if ($printRequestId > 0) {
@@ -374,6 +394,95 @@ class PrintRequests extends BaseController
];
}
private function applyPrintRequestSchoolYearScope($query, string $schoolYear): void
{
if ($schoolYear === '') {
return;
}
$hasPrintRequestYear = $this->printRequestsHaveSchoolYearColumn();
$hasClassSectionYear = db_connect()->fieldExists('school_year', 'classSection');
$range = (new SemesterRangeService($this->configModel))->getSchoolYearRange($schoolYear);
[$rangeStart, $rangeEnd] = $range;
if ($hasPrintRequestYear && $hasClassSectionYear) {
$query->groupStart()
->where('print_requests.school_year', $schoolYear)
->orGroupStart()
->groupStart()
->where('print_requests.school_year IS NULL', null, false)
->orWhere('print_requests.school_year', '')
->groupEnd()
->where('cs.school_year', $schoolYear)
->groupEnd();
if ($rangeStart !== '' && $rangeEnd !== '') {
$query
->orGroupStart()
->groupStart()
->where('print_requests.school_year IS NULL', null, false)
->orWhere('print_requests.school_year', '')
->groupEnd()
->groupStart()
->where('cs.school_year IS NULL', null, false)
->orWhere('cs.school_year', '')
->groupEnd()
->where('print_requests.required_by >=', $rangeStart . ' 00:00:00')
->where('print_requests.required_by <=', $rangeEnd . ' 23:59:59')
->groupEnd();
}
$query->groupEnd();
return;
}
if (! $hasPrintRequestYear && $hasClassSectionYear) {
$query->where('cs.school_year', $schoolYear);
return;
}
if ($rangeStart !== '' && $rangeEnd !== '') {
if ($hasPrintRequestYear) {
$query
->groupStart()
->where('print_requests.school_year', $schoolYear)
->orGroupStart()
->groupStart()
->where('print_requests.school_year IS NULL', null, false)
->orWhere('print_requests.school_year', '')
->groupEnd()
->where('print_requests.required_by >=', $rangeStart . ' 00:00:00')
->where('print_requests.required_by <=', $rangeEnd . ' 23:59:59')
->groupEnd()
->groupEnd();
return;
}
$query
->where('print_requests.required_by >=', $rangeStart . ' 00:00:00')
->where('print_requests.required_by <=', $rangeEnd . ' 23:59:59');
return;
}
if ($hasPrintRequestYear) {
$query->where('print_requests.school_year', $schoolYear);
}
}
private function printRequestSchoolYearData(string $schoolYear): array
{
if ($schoolYear === '' || ! $this->printRequestsHaveSchoolYearColumn()) {
return [];
}
return ['school_year' => $schoolYear];
}
private function printRequestsHaveSchoolYearColumn(): bool
{
return db_connect()->fieldExists('school_year', 'print_requests');
}
public function serveFile(string $filename, string $mode = 'inline')
{
$safeName = basename(trim($filename));