Fix exams foreign key constraint on lesson_id by using NULL instead of 0 for unit exams
This commit is contained in:
@@ -436,11 +436,11 @@ class ExamController
|
|||||||
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_exam', 'اختبار الفهم الشامل: الوحدة الأولى — أنظمة المعادلات', 70.00, 100, 45, 1, 1)"
|
VALUES (UUID(), 1, NULL, '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, 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]
|
[$examId]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,6 +61,15 @@ class AiQuestionBankGeneratorService
|
|||||||
$aiQuestions = self::synthesizeQuestionsFromMarkdown($mdFiles, $targetCount);
|
$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
|
// Ensure Unit Exam exists in database
|
||||||
$exam = Database::selectOne(
|
$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",
|
"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) {
|
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_exam', ?, 45, 70.00, 100, 1, 1)",
|
VALUES (UUID(), ?, NULL, '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, 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]
|
["اختبار الفهم الشامل: {$unitTitle}", $examId]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user