diff --git a/backend/app/Controllers/ExamController.php b/backend/app/Controllers/ExamController.php index 33041d3..cd0d0dc 100644 --- a/backend/app/Controllers/ExamController.php +++ b/backend/app/Controllers/ExamController.php @@ -414,6 +414,11 @@ class ExamController if (!$colCheck) { Database::query("ALTER TABLE exam_attempts ADD COLUMN completed_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP AFTER ai_diagnostic_report"); } + + // Safely ensure scope ENUM covers unit_comprehensive if ever passed + try { + Database::query("ALTER TABLE exams MODIFY COLUMN scope ENUM('in_video_checkpoint', 'lesson_exam', 'unit_exam', 'unit_comprehensive', 'semester_final') NOT NULL DEFAULT 'in_video_checkpoint'"); + } catch (\Throwable $ign) {} } catch (\Throwable $e) { error_log("ExamController ensureSchema notice: " . $e->getMessage()); } @@ -424,18 +429,18 @@ class ExamController self::ensureSchema(); $exam = Database::selectOne( - "SELECT id FROM exams WHERE (scope = 'unit_comprehensive' OR scope = 'unit_exam' OR id = 1) AND (title LIKE '%الوحدة الأولى%' OR id = 1) LIMIT 1" + "SELECT id FROM exams WHERE (scope = 'unit_exam' OR scope = 'unit_comprehensive' OR id = 1) AND (title LIKE '%الوحدة الأولى%' OR id = 1) LIMIT 1" ); $examId = $exam ? (int)$exam['id'] : 0; if (!$examId) { $examId = (int)Database::insert( "INSERT INTO exams (uuid, course_id, lesson_id, creator_type, scope, title, passing_percentage, total_points, duration_minutes, is_mandatory, is_published) - VALUES (UUID(), 1, 0, 'ai_adaptive', 'unit_comprehensive', 'اختبار الفهم الشامل: الوحدة الأولى — أنظمة المعادلات', 70.00, 100, 45, 1, 1)" + VALUES (UUID(), 1, 0, 'ai_adaptive', 'unit_exam', 'اختبار الفهم الشامل: الوحدة الأولى — أنظمة المعادلات', 70.00, 100, 45, 1, 1)" ); } else { Database::query( - "UPDATE exams SET title = 'اختبار الفهم الشامل: الوحدة الأولى — أنظمة المعادلات', duration_minutes = 45, total_points = 100, passing_percentage = 70.00, is_published = 1 WHERE id = ?", + "UPDATE exams SET title = 'اختبار الفهم الشامل: الوحدة الأولى — أنظمة المعادلات', duration_minutes = 45, total_points = 100, passing_percentage = 70.00, scope = 'unit_exam', is_published = 1 WHERE id = ?", [$examId] ); } diff --git a/backend/app/Services/AiQuestionBankGeneratorService.php b/backend/app/Services/AiQuestionBankGeneratorService.php index cac219e..70a1afa 100644 --- a/backend/app/Services/AiQuestionBankGeneratorService.php +++ b/backend/app/Services/AiQuestionBankGeneratorService.php @@ -63,7 +63,7 @@ class AiQuestionBankGeneratorService // Ensure Unit Exam exists in database $exam = Database::selectOne( - "SELECT id FROM exams WHERE course_id = ? AND scope = 'unit_comprehensive' AND (title LIKE ? OR title LIKE '%الوحدة الأولى%') LIMIT 1", + "SELECT id FROM exams WHERE course_id = ? AND (scope = 'unit_exam' OR scope = 'unit_comprehensive') AND (title LIKE ? OR title LIKE '%الوحدة الأولى%') LIMIT 1", [$courseId, "%{$unitTitle}%"] ); @@ -71,12 +71,12 @@ class AiQuestionBankGeneratorService if (!$examId) { $examId = (int)Database::insert( "INSERT INTO exams (uuid, course_id, lesson_id, creator_type, scope, title, duration_minutes, passing_percentage, total_points, is_mandatory, is_published) - VALUES (UUID(), ?, 0, 'ai_adaptive', 'unit_comprehensive', ?, 45, 70.00, 100, 1, 1)", + VALUES (UUID(), ?, 0, 'ai_adaptive', 'unit_exam', ?, 45, 70.00, 100, 1, 1)", [$courseId, "اختبار الفهم الشامل: {$unitTitle}"] ); } else { Database::query( - "UPDATE exams SET title = ?, duration_minutes = 45, is_published = 1 WHERE id = ?", + "UPDATE exams SET title = ?, duration_minutes = 45, scope = 'unit_exam', is_published = 1 WHERE id = ?", ["اختبار الفهم الشامل: {$unitTitle}", $examId] ); }