fix the exam draft

This commit is contained in:
root
2026-04-02 00:03:59 -04:00
parent ed67836701
commit 61facee902
6 changed files with 111 additions and 39 deletions
+42 -31
View File
@@ -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.');
}