Add grammar exercise creation in course generation
- Integrated functionality to create example grammar exercises for grammar lessons during course creation. - Added a new helper function to generate gap fill and multiple choice exercises based on lesson data. - Enhanced logging to confirm the number of grammar exercises created, improving feedback during course setup.
This commit is contained in:
141
backend/scripts/add-grammar-exercises-to-existing-courses.js
Normal file
141
backend/scripts/add-grammar-exercises-to-existing-courses.js
Normal file
@@ -0,0 +1,141 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Script zum Hinzufügen von Grammatik-Übungen zu bestehenden Kursen
|
||||
*
|
||||
* Verwendung:
|
||||
* node backend/scripts/add-grammar-exercises-to-existing-courses.js
|
||||
*
|
||||
* Fügt Beispiel-Grammatik-Übungen zu allen Grammar-Lektionen hinzu, die noch keine Übungen haben.
|
||||
*/
|
||||
|
||||
import { sequelize } from '../utils/sequelize.js';
|
||||
import VocabCourseLesson from '../models/community/vocab_course_lesson.js';
|
||||
import VocabGrammarExercise from '../models/community/vocab_grammar_exercise.js';
|
||||
import User from '../models/community/user.js';
|
||||
|
||||
async function findOrCreateSystemUser() {
|
||||
let systemUser = await User.findOne({
|
||||
where: {
|
||||
username: 'system'
|
||||
}
|
||||
});
|
||||
|
||||
if (!systemUser) {
|
||||
systemUser = await User.findOne({
|
||||
where: {
|
||||
username: 'admin'
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (!systemUser) {
|
||||
console.error('❌ System-Benutzer nicht gefunden. Bitte erstelle einen System-Benutzer.');
|
||||
throw new Error('System user not found');
|
||||
}
|
||||
|
||||
return systemUser;
|
||||
}
|
||||
|
||||
// Erstelle Beispiel-Grammatik-Übungen für eine Grammar-Lektion
|
||||
function createExampleGrammarExercises(lessonId, lessonTitle, ownerUserId) {
|
||||
const exercises = [];
|
||||
|
||||
// Beispiel-Übung 1: Gap Fill (Lückentext)
|
||||
exercises.push({
|
||||
lessonId: lessonId,
|
||||
exerciseTypeId: 1, // gap_fill
|
||||
exerciseNumber: 1,
|
||||
title: `${lessonTitle} - Übung 1`,
|
||||
instruction: 'Fülle die Lücken mit den richtigen Wörtern.',
|
||||
questionData: JSON.stringify({
|
||||
type: 'gap_fill',
|
||||
text: 'Hallo! Wie geht es {gap}? Mir geht es {gap}, danke!',
|
||||
gaps: 2
|
||||
}),
|
||||
answerData: JSON.stringify({
|
||||
type: 'gap_fill',
|
||||
answers: ['dir', 'gut']
|
||||
}),
|
||||
explanation: 'Die richtigen Antworten sind "dir" und "gut".',
|
||||
createdByUserId: ownerUserId
|
||||
});
|
||||
|
||||
// Beispiel-Übung 2: Multiple Choice
|
||||
exercises.push({
|
||||
lessonId: lessonId,
|
||||
exerciseTypeId: 2, // multiple_choice
|
||||
exerciseNumber: 2,
|
||||
title: `${lessonTitle} - Übung 2`,
|
||||
instruction: 'Wähle die richtige Antwort aus.',
|
||||
questionData: JSON.stringify({
|
||||
type: 'multiple_choice',
|
||||
question: 'Wie sagt man "Guten Tag"?',
|
||||
options: ['Guten Tag', 'Gute Nacht', 'Auf Wiedersehen', 'Tschüss']
|
||||
}),
|
||||
answerData: JSON.stringify({
|
||||
type: 'multiple_choice',
|
||||
correctAnswer: 0
|
||||
}),
|
||||
explanation: 'Die richtige Antwort ist "Guten Tag".',
|
||||
createdByUserId: ownerUserId
|
||||
});
|
||||
|
||||
return exercises;
|
||||
}
|
||||
|
||||
async function addGrammarExercisesToExistingCourses() {
|
||||
await sequelize.authenticate();
|
||||
console.log('Datenbankverbindung erfolgreich hergestellt.\n');
|
||||
|
||||
const systemUser = await findOrCreateSystemUser();
|
||||
console.log(`Verwende System-Benutzer: ${systemUser.username} (ID: ${systemUser.id})\n`);
|
||||
|
||||
// Finde alle Grammar-Lektionen ohne Übungen
|
||||
const grammarLessons = await sequelize.query(
|
||||
`SELECT l.id, l.title, l.course_id, c.owner_user_id
|
||||
FROM community.vocab_course_lesson l
|
||||
JOIN community.vocab_course c ON c.id = l.course_id
|
||||
WHERE l.lesson_type = 'grammar'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM community.vocab_grammar_exercise e
|
||||
WHERE e.lesson_id = l.id
|
||||
)
|
||||
ORDER BY l.course_id, l.lesson_number`,
|
||||
{
|
||||
type: sequelize.QueryTypes.SELECT
|
||||
}
|
||||
);
|
||||
|
||||
console.log(`Gefunden: ${grammarLessons.length} Grammar-Lektionen ohne Übungen\n`);
|
||||
|
||||
if (grammarLessons.length === 0) {
|
||||
console.log('✅ Alle Grammar-Lektionen haben bereits Übungen.');
|
||||
return;
|
||||
}
|
||||
|
||||
let addedCount = 0;
|
||||
for (const lesson of grammarLessons) {
|
||||
const exercises = createExampleGrammarExercises(lesson.id, lesson.title, lesson.owner_user_id);
|
||||
|
||||
for (const exercise of exercises) {
|
||||
await VocabGrammarExercise.create(exercise);
|
||||
addedCount++;
|
||||
}
|
||||
|
||||
console.log(`✅ ${exercises.length} Übungen zu "${lesson.title}" hinzugefügt`);
|
||||
}
|
||||
|
||||
console.log(`\n🎉 Zusammenfassung:`);
|
||||
console.log(` ${addedCount} Grammatik-Übungen zu ${grammarLessons.length} Lektionen hinzugefügt`);
|
||||
}
|
||||
|
||||
addGrammarExercisesToExistingCourses()
|
||||
.then(() => {
|
||||
sequelize.close();
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('❌ Fehler:', error);
|
||||
sequelize.close();
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user