Enhance VocabService and VocabCourseView for improved multiple choice handling and table layout

- Updated VocabService to support multiple correct answers in multiple choice exercises, allowing for better answer validation and user feedback.
- Enhanced the extraction of correct answers and alternatives to accommodate both single and multiple correct indices.
- Improved CSS styles in VocabCourseView for better table layout, including adjustments for overflow handling and vertical alignment, enhancing overall user experience.
This commit is contained in:
Torsten Schulz (local)
2026-01-20 14:46:07 +01:00
parent 4d97f24531
commit 175a61c81c
2 changed files with 61 additions and 18 deletions

View File

@@ -1405,15 +1405,30 @@ export default class VocabService {
let correctAnswer = null;
let alternatives = [];
// Für Multiple Choice: Extrahiere die richtige Antwort aus dem Index
// Für Multiple Choice: Extrahiere die richtige(n) Antwort(en) aus dem Index/den Indizes
if (exercise.exerciseTypeId === 2 && answerData.correctAnswer !== undefined) {
const options = questionData?.options || [];
const correctIndex = answerData.correctAnswer;
if (options[correctIndex] !== undefined) {
correctAnswer = options[correctIndex];
// Unterstütze sowohl einzelne korrekte Antwort als auch Array von korrekten Antworten
let correctIndices = [];
if (Array.isArray(answerData.correctAnswer)) {
correctIndices = answerData.correctAnswer.map(idx => Number(idx));
} else {
correctIndices = [Number(answerData.correctAnswer)];
}
// Alternativen sind alle anderen Optionen
alternatives = options.filter((opt, idx) => idx !== correctIndex);
// Extrahiere alle korrekten Antworten
const correctAnswersList = correctIndices
.map(idx => options[idx])
.filter(opt => opt !== undefined);
if (correctAnswersList.length > 0) {
// Wenn mehrere richtige Antworten: Zeige alle an, getrennt durch " / "
correctAnswer = correctAnswersList.join(' / ');
}
// Alternativen sind alle anderen Optionen (nicht korrekte)
alternatives = options.filter((opt, idx) => !correctIndices.includes(idx));
}
// Für Gap Fill: Extrahiere aus answers Array
else if (exercise.exerciseTypeId === 1 && answerData.answers) {
@@ -1460,14 +1475,32 @@ export default class VocabService {
const parsedAnswerData = typeof answerData === 'string' ? JSON.parse(answerData) : answerData;
const parsedQuestionData = typeof questionData === 'string' ? JSON.parse(questionData) : questionData;
// Für Multiple Choice: Prüfe ob userAnswer (Index) mit correctAnswer (Index) übereinstimmt
// Für Multiple Choice: Prüfe ob userAnswer (Index) mit correctAnswer (Index oder Array von Indizes) übereinstimmt
if (exerciseTypeId === 2) { // multiple_choice
const correctIndex = parsedAnswerData.correctAnswer !== undefined
? parsedAnswerData.correctAnswer
: (parsedAnswerData.correct !== undefined ? parsedAnswerData.correct : null);
if (correctIndex === null) return false;
// Unterstütze sowohl einzelne korrekte Antwort als auch Array von korrekten Antworten
let correctIndices = [];
if (parsedAnswerData.correctAnswer !== undefined) {
// Kann ein einzelner Index oder ein Array von Indizes sein
if (Array.isArray(parsedAnswerData.correctAnswer)) {
correctIndices = parsedAnswerData.correctAnswer.map(idx => Number(idx));
} else {
correctIndices = [Number(parsedAnswerData.correctAnswer)];
}
} else if (parsedAnswerData.correct !== undefined) {
// Fallback: Prüfe auch 'correct' Feld
if (Array.isArray(parsedAnswerData.correct)) {
correctIndices = parsedAnswerData.correct.map(idx => Number(idx));
} else {
correctIndices = [Number(parsedAnswerData.correct)];
}
}
if (correctIndices.length === 0) return false;
// userAnswer ist der Index (0, 1, 2, ...)
return Number(userAnswer) === Number(correctIndex);
const userIndex = Number(userAnswer);
return correctIndices.includes(userIndex);
}
// Für Lückentext: Normalisiere und vergleiche