diff --git a/src/components/Exercises/FillBlanks/index.tsx b/src/components/Exercises/FillBlanks/index.tsx index 96316496..9fec8f82 100644 --- a/src/components/Exercises/FillBlanks/index.tsx +++ b/src/components/Exercises/FillBlanks/index.tsx @@ -20,9 +20,10 @@ const FillBlanks: React.FC = ({ onNext, onBack, }) => { - const { shuffleMaps, exam, partIndex, questionIndex, exerciseIndex } = useExamStore((state) => state); + const { shuffles, exam, partIndex, questionIndex, exerciseIndex } = useExamStore((state) => state); const [answers, setAnswers] = useState<{ id: string; solution: string }[]>(userSolutions); const hasExamEnded = useExamStore((state) => state.hasExamEnded); + const shuffleMaps = shuffles.find((x) => x.exerciseID == id)?.shuffles; const [currentMCSelection, setCurrentMCSelection] = useState<{ id: string, selection: FillBlanksMCOption }>(); @@ -77,7 +78,7 @@ const FillBlanks: React.FC = ({ }; const renderLines = (line: string) => { return ( -
+
{reactStringReplace(line, /({{\d+}})/g, (match) => { const id = match.replaceAll(/[\{\}]/g, ""); const userSolution = answers.find((x) => x.id === id); @@ -127,10 +128,10 @@ const FillBlanks: React.FC = ({ const getShuffles = () => { let shuffle = {}; - if (shuffleMaps.length !== 0) { + if (shuffleMaps) { shuffle = { shuffleMaps: shuffleMaps.filter((map) => - answers.some(answer => answer.id === map.id) + answers.some(answer => answer.id === map.questionID) ) } } diff --git a/src/components/Exercises/MultipleChoice.tsx b/src/components/Exercises/MultipleChoice.tsx index e72896a0..e4e95d76 100644 --- a/src/components/Exercises/MultipleChoice.tsx +++ b/src/components/Exercises/MultipleChoice.tsx @@ -1,5 +1,5 @@ /* eslint-disable @next/next/no-img-element */ -import { MultipleChoiceExercise, MultipleChoiceQuestion } from "@/interfaces/exam"; +import { MultipleChoiceExercise, MultipleChoiceQuestion, ShuffleMap } from "@/interfaces/exam"; import useExamStore from "@/stores/examStore"; import clsx from "clsx"; import { useEffect, useState } from "react"; @@ -77,13 +77,15 @@ export default function MultipleChoice({ id, prompt, type, questions, userSoluti const { questionIndex, exam, - shuffleMaps, + shuffles, hasExamEnded, userSolutions: storeUserSolutions, setQuestionIndex, setUserSolutions } = useExamStore((state) => state); + const shuffleMaps = shuffles.find((x) => x.exerciseID == id)?.shuffles; + const scrollToTop = () => Array.from(document.getElementsByTagName("body")).forEach((body) => body.scrollTo(0, 0)); useEffect(() => { @@ -104,6 +106,16 @@ export default function MultipleChoice({ id, prompt, type, questions, userSoluti setAnswers((prev) => [...prev.filter((x) => x.question !== question.id), { option, question: question.id }]); }; + const getShuffledSolution = (originalSolution: string, questionShuffleMap: ShuffleMap) => { + for (const [newPosition, originalPosition] of Object.entries(questionShuffleMap.map)) { + if (originalPosition === originalSolution) { + return newPosition; + } + } + return originalSolution; + } + + const calculateScore = () => { const total = questions.length; const correct = answers.filter((x) => { @@ -112,34 +124,25 @@ export default function MultipleChoice({ id, prompt, type, questions, userSoluti }); let isSolutionCorrect; - if (shuffleMaps.length == 0) { + if (!shuffleMaps) { isSolutionCorrect = matchingQuestion?.solution === x.option; } else { - const shuffleMap = shuffleMaps.find((map) => map.id == x.question) - isSolutionCorrect = shuffleMap?.map[x.option] == matchingQuestion?.solution; + const shuffleMap = shuffleMaps.find((map) => map.questionID == x.question); + if (shuffleMap) { + isSolutionCorrect = getShuffledSolution(x.option, shuffleMap) == matchingQuestion?.solution; + }else { + isSolutionCorrect = matchingQuestion?.solution === x.option; + } } return isSolutionCorrect || false; }).length; const missing = total - correct; - return { total, correct, missing }; }; - const getShuffles = () => { - let shuffle = {}; - if (shuffleMaps.length !== 0) { - shuffle = { - shuffleMaps: shuffleMaps.filter((map) => - answers.some(answer => answer.question === map.id) - ) - } - } - return shuffle; - } - const next = () => { if (questionIndex === questions.length - 1) { - onNext({ exercise: id, solutions: answers, score: calculateScore(), type, ...getShuffles() }); + onNext({ exercise: id, solutions: answers, score: calculateScore(), type, shuffleMaps: shuffleMaps }); } else { setQuestionIndex(questionIndex + 1); } @@ -148,7 +151,7 @@ export default function MultipleChoice({ id, prompt, type, questions, userSoluti const back = () => { if (questionIndex === 0) { - onBack({ exercise: id, solutions: answers, score: calculateScore(), type, ...getShuffles() }); + onBack({ exercise: id, solutions: answers, score: calculateScore(), type, shuffleMaps: shuffleMaps }); } else { setQuestionIndex(questionIndex - 1); } diff --git a/src/components/Solutions/FillBlanks.tsx b/src/components/Solutions/FillBlanks.tsx index 4f2aea4b..32e9c15d 100644 --- a/src/components/Solutions/FillBlanks.tsx +++ b/src/components/Solutions/FillBlanks.tsx @@ -28,7 +28,6 @@ export default function FillBlanksSolutions({ const total = text.match(/({{\d+}})/g)?.length || 0; const correct = correctUserSolutions!.filter((x) => { const solution = solutions.find((y) => x.id.toString() === y.id.toString())?.solution; - console.log(solution); if (!solution) return false; const option = words.find((w) => { diff --git a/src/components/Solutions/MultipleChoice.tsx b/src/components/Solutions/MultipleChoice.tsx index 320163e4..6f29e9ab 100644 --- a/src/components/Solutions/MultipleChoice.tsx +++ b/src/components/Solutions/MultipleChoice.tsx @@ -6,6 +6,7 @@ import { useEffect, useState } from "react"; import reactStringReplace from "react-string-replace"; import { CommonProps } from "."; import Button from "../Low/Button"; +import { v4 } from "uuid"; function Question({ id, @@ -17,34 +18,12 @@ function Question({ }: MultipleChoiceQuestion & { userSolution: string | undefined; onSelectOption?: (option: string) => void; showSolution?: boolean }) { const { userSolutions } = useExamStore((state) => state); - const getShuffledOptions = (options: { id: string, text: string }[], questionShuffleMap: ShuffleMap) => { - const shuffledOptions = ['A', 'B', 'C', 'D'].map(newId => { - const originalId = questionShuffleMap.map[newId]; - const originalOption = options.find(option => option.id === originalId); - return { - id: newId, - text: originalOption!.text - }; - }); - return shuffledOptions; - } - - const getShuffledSolution = (originalSolution: string, questionShuffleMap: ShuffleMap) => { - for (const [newPosition, originalPosition] of Object.entries(questionShuffleMap.map)) { - if (originalPosition === originalSolution) { - return newPosition; - } - } - return originalSolution; - } - const questionShuffleMap = userSolutions.reduce((foundMap, userSolution) => { if (foundMap) return foundMap; - return userSolution.shuffleMaps?.find(map => map.id === id) || null; + return userSolution.shuffleMaps?.find(map => map.questionID === id) || null; }, null as ShuffleMap | null); - const questionOptions = questionShuffleMap ? getShuffledOptions(options as { id: string, text: string }[], questionShuffleMap) : options; - const newSolution = questionShuffleMap ? getShuffledSolution(solution, questionShuffleMap) : solution; + const newSolution = questionShuffleMap ? questionShuffleMap?.map[solution] : solution; const renderPrompt = (prompt: string) => { return reactStringReplace(prompt, /(.*?<\/u>)/g, (match) => { @@ -70,15 +49,15 @@ function Question({ {isNaN(Number(id)) ? ( {renderPrompt(prompt).filter((x) => x?.toString() !== "")} ) : ( - + <> - {id} - {renderPrompt(prompt).filter((x) => x?.toString() !== "")} + {id} - {renderPrompt(prompt).filter((x) => x?.toString() !== "")} )}
{variant === "image" && - questionOptions.map((option) => ( + options.map((option) => (
))} {variant === "text" && - questionOptions.map((option) => ( + options.map((option) => (
@@ -106,14 +85,23 @@ function Question({ export default function MultipleChoice({ id, type, prompt, questions, userSolutions, onNext, onBack }: MultipleChoiceExercise & CommonProps) { const { questionIndex, setQuestionIndex, partIndex, exam } = useExamStore((state) => state); + const stats = useExamStore((state) => state.userSolutions); const calculateScore = () => { const total = questions.length; + const questionShuffleMap = stats.find((x) => x.exercise == id)?.shuffleMaps; const correct = userSolutions.filter( - (x) => questions.find((y) => y.id.toString() === x.question.toString())?.solution === x.option || false, + (x) => { + if (questionShuffleMap) { + const shuffleMap = questionShuffleMap.find((y) => y.questionID === x.question) + const originalSol = questions.find((y) => y.id.toString() === x.question.toString())?.solution!; + return x.option == shuffleMap?.map[originalSol] + } else { + return questions.find((y) => y.id.toString() === x.question.toString())?.solution === x.option || false + } + }, ).length; const missing = total - userSolutions.filter((x) => questions.find((y) => y.id.toString() === x.question.toString())).length; - return { total, correct, missing }; }; @@ -159,12 +147,12 @@ export default function MultipleChoice({ id, type, prompt, questions, userSoluti Wrong
-
+