diff --git a/backend/app/Controllers/ExamController.php b/backend/app/Controllers/ExamController.php index cd0d0dc..01ef77c 100644 --- a/backend/app/Controllers/ExamController.php +++ b/backend/app/Controllers/ExamController.php @@ -436,11 +436,11 @@ class ExamController 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_exam', 'اختبار الفهم الشامل: الوحدة الأولى — أنظمة المعادلات', 70.00, 100, 45, 1, 1)" + VALUES (UUID(), 1, NULL, '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, scope = 'unit_exam', is_published = 1 WHERE id = ?", + "UPDATE exams SET title = 'اختبار الفهم الشامل: الوحدة الأولى — أنظمة المعادلات', duration_minutes = 45, total_points = 100, passing_percentage = 70.00, scope = 'unit_exam', lesson_id = NULL, is_published = 1 WHERE id = ?", [$examId] ); } diff --git a/backend/app/Services/AiQuestionBankGeneratorService.php b/backend/app/Services/AiQuestionBankGeneratorService.php index 70a1afa..434ded8 100644 --- a/backend/app/Services/AiQuestionBankGeneratorService.php +++ b/backend/app/Services/AiQuestionBankGeneratorService.php @@ -61,6 +61,15 @@ class AiQuestionBankGeneratorService $aiQuestions = self::synthesizeQuestionsFromMarkdown($mdFiles, $targetCount); } + // Ensure valid course_id exists in database + $courseCheck = Database::selectOne("SELECT id FROM courses WHERE id = ? LIMIT 1", [$courseId]); + if (!$courseCheck) { + $firstCourse = Database::selectOne("SELECT id FROM courses LIMIT 1"); + if ($firstCourse) { + $courseId = (int)$firstCourse['id']; + } + } + // Ensure Unit Exam exists in database $exam = Database::selectOne( "SELECT id FROM exams WHERE course_id = ? AND (scope = 'unit_exam' OR scope = 'unit_comprehensive') AND (title LIKE ? OR title LIKE '%الوحدة الأولى%') LIMIT 1", @@ -71,12 +80,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_exam', ?, 45, 70.00, 100, 1, 1)", + VALUES (UUID(), ?, NULL, 'ai_adaptive', 'unit_exam', ?, 45, 70.00, 100, 1, 1)", [$courseId, "اختبار الفهم الشامل: {$unitTitle}"] ); } else { Database::query( - "UPDATE exams SET title = ?, duration_minutes = 45, scope = 'unit_exam', is_published = 1 WHERE id = ?", + "UPDATE exams SET title = ?, duration_minutes = 45, scope = 'unit_exam', lesson_id = NULL, is_published = 1 WHERE id = ?", ["اختبار الفهم الشامل: {$unitTitle}", $examId] ); }