From c252556528db566148687c3363b92cf9530ac9e1 Mon Sep 17 00:00:00 2001 From: Benjamin Admin Date: Mon, 27 Apr 2026 07:43:16 +0200 Subject: [PATCH] Fix: Quiz fallback to QA data when MC not generated MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Vocab units created via /vocabulary/units only have QA items, no pre-generated MC questions. Quiz now falls back to generating MC questions client-side from QA items (EN word → 4 DE options). Co-Authored-By: Claude Opus 4.6 (1M context) --- studio-v2/app/learn/[unitId]/quiz/page.tsx | 35 +++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/studio-v2/app/learn/[unitId]/quiz/page.tsx b/studio-v2/app/learn/[unitId]/quiz/page.tsx index 57579b4..e6979d2 100644 --- a/studio-v2/app/learn/[unitId]/quiz/page.tsx +++ b/studio-v2/app/learn/[unitId]/quiz/page.tsx @@ -42,10 +42,37 @@ export default function QuizPage() { const loadMC = async () => { setIsLoading(true) try { - const resp = await fetch(`${getApiBase()}/api/learning-units/${unitId}/mc`) - if (!resp.ok) throw new Error(`HTTP ${resp.status}`) - const data = await resp.json() - setQuestions(data.questions || []) + // Try MC endpoint first + let resp = await fetch(`${getApiBase()}/api/learning-units/${unitId}/mc`) + if (resp.ok) { + const data = await resp.json() + setQuestions(data.questions || []) + return + } + + // Fallback: Generate MC from QA items (vocab units don't have pre-generated MC) + resp = await fetch(`${getApiBase()}/api/learning-units/${unitId}/qa`) + if (!resp.ok) throw new Error('Keine Daten verfuegbar') + const qaData = await resp.json() + const qaItems = qaData.qa_items || [] + if (qaItems.length < 4) throw new Error('Zu wenige Woerter fuer Quiz (min. 4)') + + // Build MC questions from QA: question=EN word, options=4 DE answers (1 correct + 3 random) + const generated: MCQuestion[] = qaItems.map((item: any, idx: number) => { + const others = qaItems.filter((_: any, i: number) => i !== idx) + const distractors = [...others].sort(() => Math.random() - 0.5).slice(0, 3) + const options = [ + { id: item.id, text: item.answer }, + ...distractors.map((d: any) => ({ id: d.id, text: d.answer })), + ].sort(() => Math.random() - 0.5) + return { + id: item.id, + question: item.question, + options, + correct_answer: item.id, + } + }) + setQuestions(generated) } catch (err: any) { setError(err.message) } finally {