From 61facee902ecda783e5b7a3fa95cf84496dc2c31 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 2 Apr 2026 00:03:59 -0400 Subject: [PATCH] fix the exam draft --- app/Controllers/ClassProgressController.php | 19 +++++ app/Controllers/View/ExamDraftController.php | 73 +++++++++++--------- app/Views/admin/class_progress_list.php | 31 +++++++-- app/Views/administrator/exam_drafts.php | 6 +- app/Views/teacher/class_progress_submit.php | 12 ++++ app/Views/teacher/exam_drafts.php | 9 +++ 6 files changed, 111 insertions(+), 39 deletions(-) diff --git a/app/Controllers/ClassProgressController.php b/app/Controllers/ClassProgressController.php index f8a7c47..c40ba30 100644 --- a/app/Controllers/ClassProgressController.php +++ b/app/Controllers/ClassProgressController.php @@ -98,6 +98,10 @@ class ClassProgressController extends BaseController return redirect()->back()->withInput()->with('errors', $this->validator->getErrors()); } + if (! $this->hasIslamicUnitSelection()) { + return redirect()->back()->withInput()->with('error', 'Please select at least one Islamic Studies unit.'); + } + $attachmentErrors = $this->validateAttachmentFiles($subjectSections); if (! empty($attachmentErrors)) { return redirect()->back()->withInput()->with('errors', $attachmentErrors); @@ -431,6 +435,10 @@ class ClassProgressController extends BaseController return redirect()->back()->withInput()->with('errors', $this->validator->getErrors()); } + if (! $this->hasIslamicUnitSelection()) { + return redirect()->back()->withInput()->with('error', 'Please select at least one Islamic Studies unit.'); + } + $attachmentErrors = $this->validateAttachmentFiles($subjectSections); if (! empty($attachmentErrors)) { return redirect()->back()->withInput()->with('errors', $attachmentErrors); @@ -694,6 +702,17 @@ class ClassProgressController extends BaseController return mb_strlen($summary) > 120 ? mb_substr($summary, 0, 120) : $summary; } + protected function hasIslamicUnitSelection(): bool + { + $unitValues = array_map('trim', (array) $this->request->getPost('unit_islamic')); + foreach ($unitValues as $value) { + if ($value !== '') { + return true; + } + } + return false; + } + protected function parseUnitChapterSummary(string $summary): array { $summary = trim($summary); diff --git a/app/Controllers/View/ExamDraftController.php b/app/Controllers/View/ExamDraftController.php index f95e7a7..43f167a 100644 --- a/app/Controllers/View/ExamDraftController.php +++ b/app/Controllers/View/ExamDraftController.php @@ -240,23 +240,23 @@ class ExamDraftController extends BaseController // Group legacy uploads (admin-uploaded finalized exams) by class_section for separate tab $legacyByClass = []; if ($this->hasIsLegacyColumn) { - // Keep all submissions visible; additionally surface legacy items in a separate tab - $drafts = $allDrafts; - + // Keep legacy items out of the main submissions list; show them in the legacy tab only. + $drafts = []; foreach ($allDrafts as $d) { $isLegacy = !empty($d['is_legacy']); - if (!$isLegacy) { + if ($isLegacy) { + $cid = (int)($d['class_section_id'] ?? 0); + if (!isset($legacyByClass[$cid])) { + $legacyByClass[$cid] = [ + 'class_section_id' => $cid, + 'class_section_name' => $d['class_section_name'] ?? 'Class ' . $cid, + 'items' => [], + ]; + } + $legacyByClass[$cid]['items'][] = $d; continue; } - $cid = (int)($d['class_section_id'] ?? 0); - if (!isset($legacyByClass[$cid])) { - $legacyByClass[$cid] = [ - 'class_section_id' => $cid, - 'class_section_name' => $d['class_section_name'] ?? 'Class ' . $cid, - 'items' => [], - ]; - } - $legacyByClass[$cid]['items'][] = $d; + $drafts[] = $d; } } else { // Column missing: keep behavior simple and avoid legacy tab @@ -284,13 +284,17 @@ class ExamDraftController extends BaseController return redirect()->to('/login'); } - $classSectionId = (int) ($this->request->getPost('class_section_id') ?? 0); + $classSectionIds = $this->request->getPost('class_section_ids'); + if (!is_array($classSectionIds)) { + $classSectionIds = [$classSectionIds]; + } + $classSectionIds = array_values(array_filter(array_map('intval', $classSectionIds))); $schoolYear = trim((string) ($this->request->getPost('school_year') ?? $this->schoolYear)); $semester = trim((string) ($this->request->getPost('semester') ?? $this->semester)); $examType = trim((string) $this->request->getPost('exam_type')); - if ($classSectionId <= 0) { - return redirect()->back()->withInput()->with('error', 'Select a class section.'); + if (empty($classSectionIds)) { + return redirect()->back()->withInput()->with('error', 'Select at least one class section.'); } if ($schoolYear === '') { return redirect()->back()->withInput()->with('error', 'School year is required.'); @@ -309,9 +313,17 @@ class ExamDraftController extends BaseController return redirect()->back()->withInput()->with('error', 'File type not allowed or upload failed.'); } - $payload = [ + $pdfName = null; + if (strtolower($file->getClientExtension()) === 'pdf') { + $pdfName = $stored; + } else { + $pdfName = $this->convertDocToPdf( + $this->fullUploadPath(self::FINAL_UPLOAD_DIR, $stored), + self::FINAL_UPLOAD_DIR + ); + } + $basePayload = [ 'teacher_id' => $adminId, // store under admin user since legacy uploads are admin-only - 'class_section_id' => $classSectionId, 'semester' => ucfirst(strtolower($semester)), 'school_year' => $schoolYear, 'exam_type' => $examType === '' ? null : $examType, @@ -325,23 +337,22 @@ class ExamDraftController extends BaseController 'version' => 1, ]; if ($this->hasIsLegacyColumn) { - $payload['is_legacy'] = 1; - } - - $pdfName = null; - if (strtolower($file->getClientExtension()) === 'pdf') { - $pdfName = $stored; - } else { - $pdfName = $this->convertDocToPdf( - $this->fullUploadPath(self::FINAL_UPLOAD_DIR, $stored), - self::FINAL_UPLOAD_DIR - ); + $basePayload['is_legacy'] = 1; } if ($pdfName !== null && $this->hasFinalPdfColumn) { - $payload['final_pdf_file'] = $pdfName; + $basePayload['final_pdf_file'] = $pdfName; } - if ($this->examDraftModel->insert($payload)) { + $saved = 0; + foreach ($classSectionIds as $classSectionId) { + $payload = $basePayload; + $payload['class_section_id'] = $classSectionId; + if ($this->examDraftModel->insert($payload)) { + $saved++; + } + } + + if ($saved > 0) { return redirect()->to('/administrator/exam-drafts')->with('success', 'Old exam uploaded successfully.'); } diff --git a/app/Views/admin/class_progress_list.php b/app/Views/admin/class_progress_list.php index 443ab8e..2fa881a 100644 --- a/app/Views/admin/class_progress_list.php +++ b/app/Views/admin/class_progress_list.php @@ -168,12 +168,33 @@ $weekLabel .= ' – ' . date('M d, Y', strtotime($group['week_end'])); } $reports = $group['reports'] ?? []; - $teacherName = ''; + $teacherCounts = []; + $teacherLatest = []; foreach ($reports as $report) { - if (! empty($report['teacher_name'])) { - $teacherName = $report['teacher_name']; - break; + $name = trim((string) ($report['teacher_name'] ?? '')); + if ($name === '') { + continue; } + $teacherCounts[$name] = ($teacherCounts[$name] ?? 0) + 1; + $stamp = (string) ($report['updated_at'] ?? $report['created_at'] ?? ''); + if ($stamp !== '' && (!isset($teacherLatest[$name]) || $stamp > $teacherLatest[$name])) { + $teacherLatest[$name] = $stamp; + } + } + $teacherLabel = '-'; + if (!empty($teacherCounts)) { + $bestName = ''; + $bestCount = -1; + $bestStamp = ''; + foreach ($teacherCounts as $name => $count) { + $stamp = $teacherLatest[$name] ?? ''; + if ($count > $bestCount || ($count === $bestCount && $stamp > $bestStamp)) { + $bestName = $name; + $bestCount = $count; + $bestStamp = $stamp; + } + } + $teacherLabel = $bestName ?: '-'; } $exampleId = $reports ? reset($reports)['id'] : null; ?> @@ -198,7 +219,7 @@ - + View diff --git a/app/Views/administrator/exam_drafts.php b/app/Views/administrator/exam_drafts.php index 7450dec..fc03e51 100644 --- a/app/Views/administrator/exam_drafts.php +++ b/app/Views/administrator/exam_drafts.php @@ -182,13 +182,13 @@
- - +
Hold Ctrl (Windows) or Command (Mac) to select multiple sections.
diff --git a/app/Views/teacher/class_progress_submit.php b/app/Views/teacher/class_progress_submit.php index 5cc46b4..510dab9 100644 --- a/app/Views/teacher/class_progress_submit.php +++ b/app/Views/teacher/class_progress_submit.php @@ -306,6 +306,14 @@ const forms = document.querySelectorAll('.needs-validation'); Array.from(forms).forEach(form => { form.addEventListener('submit', event => { + const islamicUnits = form.querySelectorAll('input[name="unit_islamic[]"]'); + const hasIslamicUnit = Array.from(islamicUnits).some(input => input.value.trim() !== ''); + if (!hasIslamicUnit) { + event.preventDefault(); + event.stopPropagation(); + alert('Please select at least one Islamic Studies unit.'); + return; + } if (!form.checkValidity()) { event.preventDefault(); event.stopPropagation(); @@ -461,6 +469,10 @@ confirmButton.addEventListener('click', () => { confirmInput.value = '1'; const form = confirmButton.closest('form') || document.querySelector('form.needs-validation'); + if (form && typeof form.requestSubmit === 'function') { + form.requestSubmit(); + return; + } if (form) { form.submit(); } diff --git a/app/Views/teacher/exam_drafts.php b/app/Views/teacher/exam_drafts.php index 95b5b13..cddb642 100644 --- a/app/Views/teacher/exam_drafts.php +++ b/app/Views/teacher/exam_drafts.php @@ -137,6 +137,9 @@ + + View +
No file uploaded
@@ -152,6 +155,9 @@ + + View + @@ -199,6 +205,9 @@ Download + + View + File missing