From b01a4496e6f026094a7fe224f2453358d2a50826 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 6 May 2026 18:45:13 -0400 Subject: [PATCH] fixexam files --- app/Config/Routes.php | 4 +- app/Controllers/View/FilesController.php | 69 +++++++++++++++++++++--- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index c359189..83dfadb 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -393,8 +393,8 @@ $routes->get('print-requests/file/(:segment)', 'PrintRequests::serveFile/$1', [' $routes->get('print-requests/file/(:segment)/(:alpha)', 'PrintRequests::serveFile/$1/$2', ['filter' => 'auth:teacher,teacher_assistant,admin']); $routes->get('uploads/print_requests/(:segment)', 'PrintRequests::serveFile/$1', ['filter' => 'auth:teacher,teacher_assistant,admin']); -$routes->get('exam-drafts/files/teacher/(:segment)', 'View\FilesController::examDraftTeacher/$1', ['filter' => 'auth:teacher,teacher_assistant,admin']); -$routes->get('exam-drafts/files/final/(:segment)', 'View\FilesController::examDraftFinal/$1', ['filter' => 'auth:teacher,teacher_assistant,admin']); +$routes->get('exam-drafts/files/teacher/(:segment)', 'View\FilesController::examDraftTeacher/$1', ['filter' => 'auth:teacher,teacher_assistant,admin,administrator,principal']); +$routes->get('exam-drafts/files/final/(:segment)', 'View\FilesController::examDraftFinal/$1', ['filter' => 'auth:teacher,teacher_assistant,admin,administrator,principal']); diff --git a/app/Controllers/View/FilesController.php b/app/Controllers/View/FilesController.php index 17b2b14..386d2cb 100644 --- a/app/Controllers/View/FilesController.php +++ b/app/Controllers/View/FilesController.php @@ -322,6 +322,23 @@ class FilesController extends Controller return 'teacher_file'; } + private function resolveExamDraftAuthorIdColumn($db): string + { + try { + $fields = $db->getFieldNames('exam_drafts'); + if (in_array('teacher_id', $fields, true)) { + return 'teacher_id'; + } + if (in_array('author_id', $fields, true)) { + return 'author_id'; + } + } catch (\Throwable $e) { + log_message('error', 'FilesController::resolveExamDraftAuthorIdColumn error: ' . $e->getMessage()); + } + + return 'teacher_id'; + } + private function canAccessExamDraftFile(string $filename, string $subdir): bool { $userId = (int) (session()->get('user_id') ?? 0); @@ -330,15 +347,16 @@ class FilesController extends Controller } $role = strtolower((string) (session()->get('role') ?? '')); - if ($role === 'admin') { + if (in_array($role, ['admin', 'administrator', 'principal'], true)) { return true; } $db = Database::connect(); $fileColumn = $subdir === 'finals' ? 'final_file' : $this->resolveExamDraftFileColumn($db); + $authorIdColumn = $this->resolveExamDraftAuthorIdColumn($db); $draft = $db->table('exam_drafts ed') - ->select('ed.class_section_id, ed.school_year, ed.teacher_id, ed.author_id') + ->select('ed.class_section_id, ed.school_year, ed.semester, ed.status, ed.' . $authorIdColumn . ' AS draft_author_id') ->where('ed.' . $fileColumn, $filename) ->limit(1) ->get() @@ -348,7 +366,7 @@ class FilesController extends Controller return false; } - $authorId = (int) ($draft['teacher_id'] ?? $draft['author_id'] ?? 0); + $authorId = (int) ($draft['draft_author_id'] ?? 0); if ($authorId > 0 && $authorId === $userId) { return true; } @@ -358,16 +376,53 @@ class FilesController extends Controller return false; } + $status = strtolower(trim((string) ($draft['status'] ?? ''))); + if ($subdir === 'drafts' && $status === 'draft') { + return false; + } + + $currentSchoolYear = trim((string) (session()->get('school_year') ?? '')); + $currentSemester = trim((string) (session()->get('semester') ?? '')); + $draftSchoolYear = trim((string) ($draft['school_year'] ?? '')); + $draftSemester = trim((string) ($draft['semester'] ?? '')); + + $hasCurrentAssignment = $db->table('teacher_class') + ->select('id') + ->where('teacher_id', $userId) + ->where('class_section_id', $classSectionId); + + if ($currentSchoolYear !== '') { + $hasCurrentAssignment->where('school_year', $currentSchoolYear); + } + + $hasCurrentAssignment = $hasCurrentAssignment->limit(1)->countAllResults() > 0; + + if ($hasCurrentAssignment) { + if ($subdir === 'finals') { + return true; + } + + return $draftSchoolYear === '' || $currentSchoolYear === '' || $draftSchoolYear === $currentSchoolYear; + } + $assignmentQuery = $db->table('teacher_class') ->select('id') ->where('teacher_id', $userId) ->where('class_section_id', $classSectionId); - $schoolYear = trim((string) ($draft['school_year'] ?? '')); - if ($schoolYear !== '') { - $assignmentQuery->where('school_year', $schoolYear); + if ($draftSchoolYear !== '') { + $assignmentQuery->where('school_year', $draftSchoolYear); } - return $assignmentQuery->limit(1)->countAllResults() > 0; + $hasDraftYearAssignment = $assignmentQuery->limit(1)->countAllResults() > 0; + if (!$hasDraftYearAssignment) { + return false; + } + + if ($subdir === 'finals') { + return true; + } + + return $draftSemester === '' || $currentSemester === '' || $draftSemester === $currentSemester; } }