From c37a1becbf7d167895b3f85854f3e56f0f591ef3 Mon Sep 17 00:00:00 2001 From: Carlos Mesquita Date: Wed, 21 Aug 2024 13:02:30 +0100 Subject: [PATCH] Forgot to check if exam had shuffled enabled --- src/exams/Level/index.tsx | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/exams/Level/index.tsx b/src/exams/Level/index.tsx index 8eb61d4d..7353a044 100644 --- a/src/exams/Level/index.tsx +++ b/src/exams/Level/index.tsx @@ -50,10 +50,10 @@ export default function Level({ exam, showSolutions = false, onFinish, editing = const [contextWordLine, setContextWordLine] = useState(undefined); useEffect(() => { - if (showSolutions && exerciseIndex && userSolutions[exerciseIndex].shuffleMaps) { + if (showSolutions && exerciseIndex && exam.shuffle && userSolutions[exerciseIndex].shuffleMaps) { setShuffleMaps(userSolutions[exerciseIndex].shuffleMaps as ShuffleMap[]) } - }, [showSolutions, exerciseIndex, setShuffleMaps, userSolutions]) + }, [showSolutions, exerciseIndex, setShuffleMaps, userSolutions, exam.shuffle]) useEffect(() => { if (hasExamEnded && exerciseIndex === -1) { @@ -71,7 +71,7 @@ export default function Level({ exam, showSolutions = false, onFinish, editing = }; if (exam.shuffle && exercise.type === "multipleChoice" && !showSolutions) { - console.log("Shuffling"); + console.log("Shuffling MC "); const exerciseShuffles = userSolutions[exerciseIndex].shuffleMaps; if (exerciseShuffles && exerciseShuffles.length == 0) { const newShuffleMaps: ShuffleMap[] = []; @@ -100,7 +100,7 @@ export default function Level({ exam, showSolutions = false, onFinish, editing = setShuffleMaps(newShuffleMaps); } else { - console.log("retrieving shuffles"); + console.log("retrieving MC shuffles"); exercise.questions = exercise.questions.map(question => { const questionShuffleMap = shuffleMaps.find(map => map.id === question.id); if (questionShuffleMap) { @@ -116,7 +116,7 @@ export default function Level({ exam, showSolutions = false, onFinish, editing = } else if (exam.shuffle && exercise.type === "fillBlanks" && typeCheckWordsMC(exercise.words) && !showSolutions) { if (shuffleMaps.length === 0 && !showSolutions) { const newShuffleMaps: ShuffleMap[] = []; - + console.log("Shuffling Words"); exercise.words = exercise.words.map(word => { if ('options' in word) { const options = { ...word.options }; @@ -141,8 +141,28 @@ export default function Level({ exam, showSolutions = false, onFinish, editing = }); setShuffleMaps(newShuffleMaps); + } else { + console.log("Retrieving Words shuffle"); + exercise.words = exercise.words.map(word => { + if ('options' in word) { + const shuffleMap = shuffleMaps.find(map => map.id === word.id); + if (shuffleMap) { + const options = { ...word.options }; + const shuffledOptions = Object.keys(options).reduce((acc, key) => { + const shuffledKey = shuffleMap.map[key as keyof typeof options]; + acc[shuffledKey as keyof typeof options] = options[key as keyof typeof options]; + return acc; + }, {} as { [key in keyof typeof options]: string }); + + return { ...word, options: shuffledOptions }; + } + } + return word; + }); + } } + console.log(exercise); return exercise; }; @@ -338,12 +358,12 @@ export default function Level({ exam, showSolutions = false, onFinish, editing = return { blankQuestions: !allSolutionsCorrectLength, - finishingWhat: "part", - onClose: partIndex !== exam.parts.length - 1 ? ( + finishingWhat: "part", + onClose: partIndex !== exam.parts.length - 1 ? ( function (x: boolean | undefined) { if (x) { setShowQuestionsModal(false); nextExercise(); } else { setShowQuestionsModal(false) } } ) : function (x: boolean | undefined) { if (x) { setShowQuestionsModal(false); onFinish(userSolutions); } else { setShowQuestionsModal(false) } } } - + } return (