fix exam and grading

This commit is contained in:
root
2026-06-04 13:25:31 -04:00
parent b4e6ac03c5
commit feb6be0610
22 changed files with 1288 additions and 120 deletions
+80 -21
View File
@@ -17,19 +17,45 @@ class ExamDraftAdminService
public function list(): array
{
$context = $this->configService->context();
$teacherUserColumn = (string) ($context['teacher_user_column'] ?? '');
$adminUserColumn = (string) ($context['admin_user_column'] ?? '');
$drafts = DB::table('exam_drafts as ed')
$draftsQuery = DB::table('exam_drafts as ed')
->leftJoin('classSection as cs', 'cs.class_section_id', '=', 'ed.class_section_id')
->leftJoin('users as u', 'u.id', '=', 'ed.teacher_id')
->leftJoin('users as a', 'a.id', '=', 'ed.admin_id')
->select(
'ed.*',
'cs.class_section_name',
'u.firstname as teacher_first',
'u.lastname as teacher_last',
'a.firstname as admin_first',
'a.lastname as admin_last'
)
);
if ($teacherUserColumn !== '') {
$draftsQuery
->leftJoin('users as u', "u.id", '=', "ed.{$teacherUserColumn}")
->addSelect(
'u.firstname as teacher_first',
'u.lastname as teacher_last'
);
} else {
$draftsQuery->addSelect(
DB::raw('NULL as teacher_first'),
DB::raw('NULL as teacher_last')
);
}
if ($adminUserColumn !== '') {
$draftsQuery
->leftJoin('users as a', 'a.id', '=', "ed.{$adminUserColumn}")
->addSelect(
'a.firstname as admin_first',
'a.lastname as admin_last'
);
} else {
$draftsQuery->addSelect(
DB::raw('NULL as admin_first'),
DB::raw('NULL as admin_last')
);
}
$drafts = $draftsQuery
->orderByDesc('ed.created_at')
->get()
->map(fn ($r) => (array) $r)
@@ -88,6 +114,7 @@ class ExamDraftAdminService
public function uploadLegacy(int $adminId, array $payload, \Illuminate\Http\UploadedFile $file): array
{
$context = $this->configService->context();
$adminUserColumn = (string) ($context['admin_user_column'] ?? '');
$classSectionId = (int) ($payload['class_section_id'] ?? 0);
$schoolYear = trim((string) ($payload['school_year'] ?? $context['school_year'] ?? ''));
$semester = trim((string) ($payload['semester'] ?? $context['semester'] ?? ''));
@@ -124,20 +151,35 @@ class ExamDraftAdminService
: (pathinfo($stored['original'], PATHINFO_FILENAME) . '.pdf');
$payload = [
'teacher_id' => $adminId,
'class_section_id' => $classSectionId,
'semester' => ucfirst(strtolower($semester)),
'school_year' => $schoolYear,
'exam_type' => $examType !== '' ? $examType : null,
'draft_title' => $examType !== '' ? $examType : 'Legacy Exam',
'final_file' => $pdfName,
'final_filename' => $finalFilename,
'status' => 'finalized',
'admin_id' => $adminId,
'reviewed_at' => now(),
'version' => 1,
];
if ($context['has_teacher_id'] ?? false) {
$payload['teacher_id'] = $adminId;
}
if ($context['has_author_id'] ?? false) {
$payload['author_id'] = $adminId;
}
if ($context['has_final_filename'] ?? false) {
$payload['final_filename'] = $finalFilename;
}
if ($context['has_admin_id'] ?? false) {
$payload['admin_id'] = $adminId;
} elseif ($adminUserColumn === 'reviewer_id') {
$payload['reviewer_id'] = $adminId;
}
if ($context['has_reviewed_at'] ?? false) {
$payload['reviewed_at'] = now();
}
if ($context['has_legacy'] ?? false) {
$payload['is_legacy'] = 1;
}
@@ -171,13 +213,26 @@ class ExamDraftAdminService
$status = 'reviewed';
}
$context = $this->configService->context();
$update = [
'status' => $status,
'admin_comments' => $comments !== '' ? $comments : null,
'admin_id' => $adminId,
'reviewed_at' => now(),
];
if ($context['has_admin_comments'] ?? false) {
$update['admin_comments'] = $comments !== '' ? $comments : null;
} elseif ($context['has_reviewer_comments'] ?? false) {
$update['reviewer_comments'] = $comments !== '' ? $comments : null;
}
if ($context['has_admin_id'] ?? false) {
$update['admin_id'] = $adminId;
} elseif (($context['admin_user_column'] ?? null) === 'reviewer_id') {
$update['reviewer_id'] = $adminId;
}
if ($context['has_reviewed_at'] ?? false) {
$update['reviewed_at'] = now();
}
if ($file) {
$stored = $this->fileService->storeUploadedFile(
$file,
@@ -189,7 +244,9 @@ class ExamDraftAdminService
return ['ok' => false, 'message' => 'Unable to store the final draft.'];
}
$update['final_file'] = $stored['stored'];
$update['final_filename'] = $stored['original'];
if ($context['has_final_filename'] ?? false) {
$update['final_filename'] = $stored['original'];
}
$pdfName = $this->fileService->ensurePdfExists(
$stored['stored'],
$stored['extension'],
@@ -198,27 +255,29 @@ class ExamDraftAdminService
if ($pdfName !== null && ($this->configService->context()['has_final_pdf'] ?? false)) {
$update['final_pdf_file'] = $pdfName;
}
} elseif ($status === 'finalized' && !empty($draft->teacher_file)) {
} elseif ($status === 'finalized' && !empty($draft->teacher_file ?: $draft->author_file)) {
$copied = $this->fileService->copyDraftToFinal(
(string) $draft->teacher_file,
(string) ($draft->teacher_file ?? $draft->author_file),
ExamDraftConfigService::TEACHER_UPLOAD_DIR,
ExamDraftConfigService::FINAL_UPLOAD_DIR
);
if ($copied !== null) {
$update['final_file'] = $copied;
$update['final_filename'] = $draft->teacher_filename ?? $draft->teacher_file;
if ($context['has_final_filename'] ?? false) {
$update['final_filename'] = $draft->teacher_filename ?? $draft->author_filename ?? $draft->teacher_file ?? $draft->author_file;
}
$pdfName = $this->fileService->ensurePdfExists(
$copied,
pathinfo($copied, PATHINFO_EXTENSION),
ExamDraftConfigService::FINAL_UPLOAD_DIR
);
if ($pdfName !== null && ($this->configService->context()['has_final_pdf'] ?? false)) {
if ($pdfName !== null && ($context['has_final_pdf'] ?? false)) {
$update['final_pdf_file'] = $pdfName;
}
}
}
if ($status === 'finalized' && ($this->configService->context()['has_legacy'] ?? false)) {
if ($status === 'finalized' && ($context['has_legacy'] ?? false)) {
$update['is_legacy'] = 1;
}
@@ -26,10 +26,49 @@ class ExamDraftConfigService
public function context(): array
{
$teacherUserColumn = $this->hasColumn('exam_drafts', 'teacher_id')
? 'teacher_id'
: ($this->hasColumn('exam_drafts', 'author_id') ? 'author_id' : null);
$teacherFileColumn = $this->hasColumn('exam_drafts', 'teacher_file')
? 'teacher_file'
: ($this->hasColumn('exam_drafts', 'author_file') ? 'author_file' : null);
$teacherFilenameColumn = $this->hasColumn('exam_drafts', 'teacher_filename')
? 'teacher_filename'
: ($this->hasColumn('exam_drafts', 'author_filename') ? 'author_filename' : null);
$teacherCommentColumn = $this->hasColumn('exam_drafts', 'description')
? 'description'
: ($this->hasColumn('exam_drafts', 'author_comment') ? 'author_comment' : null);
$adminUserColumn = $this->hasColumn('exam_drafts', 'admin_id')
? 'admin_id'
: ($this->hasColumn('exam_drafts', 'reviewer_id') ? 'reviewer_id' : null);
$adminCommentsColumn = $this->hasColumn('exam_drafts', 'admin_comments')
? 'admin_comments'
: ($this->hasColumn('exam_drafts', 'reviewer_comments') ? 'reviewer_comments' : null);
return [
'school_year' => (string) (Configuration::getConfig('school_year') ?? ''),
'semester' => (string) (Configuration::getConfig('semester') ?? ''),
'teacher_user_column' => $teacherUserColumn,
'teacher_file_column' => $teacherFileColumn,
'teacher_filename_column' => $teacherFilenameColumn,
'teacher_comment_column' => $teacherCommentColumn,
'admin_user_column' => $adminUserColumn,
'admin_comments_column' => $adminCommentsColumn,
'has_teacher_id' => $this->hasColumn('exam_drafts', 'teacher_id'),
'has_admin_id' => $this->hasColumn('exam_drafts', 'admin_id'),
'has_author_id' => $this->hasColumn('exam_drafts', 'author_id'),
'has_reviewer_id' => $this->hasColumn('exam_drafts', 'reviewer_id'),
'has_admin_comments' => $this->hasColumn('exam_drafts', 'admin_comments'),
'has_reviewer_comments' => $this->hasColumn('exam_drafts', 'reviewer_comments'),
'has_reviewed_at' => $this->hasColumn('exam_drafts', 'reviewed_at'),
'has_final_pdf' => $this->hasColumn('exam_drafts', 'final_pdf_file'),
'has_final_filename' => $this->hasColumn('exam_drafts', 'final_filename'),
'has_previous_draft_id' => $this->hasColumn('exam_drafts', 'previous_draft_id'),
'has_legacy' => $this->hasColumn('exam_drafts', 'is_legacy'),
];
}
+45 -13
View File
@@ -18,15 +18,19 @@ class ExamDraftTeacherService
{
$context = $this->configService->context();
$schoolYear = (string) ($context['school_year'] ?? '');
$teacherUserColumn = (string) ($context['teacher_user_column'] ?? '');
$assignments = TeacherClass::getClassAssignmentsByUserId($teacherId, $schoolYear);
$classSectionIds = array_map(static fn ($a) => (int) $a['class_section_id'], $assignments);
$drafts = ExamDraft::query()
->where('teacher_id', $teacherId)
->orderByDesc('created_at')
->get()
->toArray();
$drafts = [];
if ($teacherUserColumn !== '') {
$drafts = ExamDraft::query()
->where($teacherUserColumn, $teacherId)
->orderByDesc('created_at')
->get()
->toArray();
}
$legacyExams = [];
if (($context['has_legacy'] ?? false) && !empty($classSectionIds)) {
@@ -94,11 +98,14 @@ class ExamDraftTeacherService
}
$existingDraft = ExamDraft::query()
->where('teacher_id', $teacherId)
->where('class_section_id', $classSectionId)
->where('semester', $semester)
->where('school_year', $schoolYear)
->orderByDesc('version')
->when(
(string) ($context['teacher_user_column'] ?? '') !== '',
fn ($query) => $query->where((string) $context['teacher_user_column'], $teacherId)
)
->first();
$nextVersion = 1;
@@ -110,20 +117,45 @@ class ExamDraftTeacherService
$title = $examType !== '' ? $examType : 'Exam Draft';
$draft = ExamDraft::query()->create([
'teacher_id' => $teacherId,
$createPayload = [
'class_section_id' => $classSectionId,
'semester' => $semester,
'school_year' => $schoolYear,
'exam_type' => $examType !== '' ? $examType : null,
'draft_title' => $title,
'description' => $description !== '' ? $description : null,
'teacher_file' => $teacherFile,
'teacher_filename' => $teacherFilename,
'status' => 'submitted',
'version' => $nextVersion,
'previous_draft_id' => $previousId,
]);
];
if (($context['teacher_user_column'] ?? null) === 'teacher_id') {
$createPayload['teacher_id'] = $teacherId;
}
if (($context['teacher_user_column'] ?? null) === 'author_id') {
$createPayload['author_id'] = $teacherId;
}
if (($context['teacher_comment_column'] ?? null) === 'description') {
$createPayload['description'] = $description !== '' ? $description : null;
}
if (($context['teacher_comment_column'] ?? null) === 'author_comment') {
$createPayload['author_comment'] = $description !== '' ? $description : null;
}
if (($context['teacher_file_column'] ?? null) === 'teacher_file') {
$createPayload['teacher_file'] = $teacherFile;
}
if (($context['teacher_file_column'] ?? null) === 'author_file') {
$createPayload['author_file'] = $teacherFile;
}
if (($context['teacher_filename_column'] ?? null) === 'teacher_filename') {
$createPayload['teacher_filename'] = $teacherFilename;
}
if (($context['teacher_filename_column'] ?? null) === 'author_filename') {
$createPayload['author_filename'] = $teacherFilename;
}
if ($context['has_previous_draft_id'] ?? false) {
$createPayload['previous_draft_id'] = $previousId;
}
$draft = ExamDraft::query()->create($createPayload);
return [
'ok' => true,