Fix MySQL scope ENUM to unit_exam in AiQuestionBankGeneratorService and ExamController

This commit is contained in:
Hamza-Ayed
2026-09-03 16:44:37 +03:00
parent 8d7ffcf2eb
commit be4fe3dabf
2 changed files with 11 additions and 6 deletions
+8 -3
View File
@@ -414,6 +414,11 @@ class ExamController
if (!$colCheck) { if (!$colCheck) {
Database::query("ALTER TABLE exam_attempts ADD COLUMN completed_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP AFTER ai_diagnostic_report"); 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) { } catch (\Throwable $e) {
error_log("ExamController ensureSchema notice: " . $e->getMessage()); error_log("ExamController ensureSchema notice: " . $e->getMessage());
} }
@@ -424,18 +429,18 @@ class ExamController
self::ensureSchema(); self::ensureSchema();
$exam = Database::selectOne( $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; $examId = $exam ? (int)$exam['id'] : 0;
if (!$examId) { if (!$examId) {
$examId = (int)Database::insert( $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) "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 { } else {
Database::query( 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] [$examId]
); );
} }
@@ -63,7 +63,7 @@ class AiQuestionBankGeneratorService
// Ensure Unit Exam exists in database // Ensure Unit Exam exists in database
$exam = Database::selectOne( $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}%"] [$courseId, "%{$unitTitle}%"]
); );
@@ -71,12 +71,12 @@ class AiQuestionBankGeneratorService
if (!$examId) { if (!$examId) {
$examId = (int)Database::insert( $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) "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}"] [$courseId, "اختبار الفهم الشامل: {$unitTitle}"]
); );
} else { } else {
Database::query( 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] ["اختبار الفهم الشامل: {$unitTitle}", $examId]
); );
} }