/* eslint-disable @next/next/no-img-element */ import { MultipleChoiceExercise, MultipleChoiceQuestion, ShuffleMap } from "@/interfaces/exam"; import useExamStore from "@/stores/examStore"; import clsx from "clsx"; import reactStringReplace from "react-string-replace"; import { CommonProps } from "."; import Button from "../Low/Button"; import { v4 } from "uuid"; function Question({ id, variant, prompt, solution, options, userSolution, }: MultipleChoiceQuestion & { userSolution: string | undefined; onSelectOption?: (option: string) => void; showSolution?: boolean }) { const { userSolutions } = useExamStore((state) => state); const questionShuffleMap = userSolutions.reduce((foundMap, userSolution) => { if (foundMap) return foundMap; return userSolution.shuffleMaps?.find(map => map.questionID === id) || null; }, null as ShuffleMap | null); const newSolution = questionShuffleMap ? questionShuffleMap?.map[solution] : solution; const renderPrompt = (prompt: string) => { return reactStringReplace(prompt, /(.*?<\/u>)/g, (match) => { const word = match.replaceAll("", "").replaceAll("", ""); return word.length > 0 ? {word} : null; }); }; const optionColor = (option: string) => { if (option === newSolution && !userSolution) { return "!border-mti-gray-davy !text-mti-gray-davy"; } if (option === newSolution) { return "!border-mti-purple-light !text-mti-purple-light"; } return userSolution === option ? "!border-mti-rose-light !text-mti-rose-light" : ""; }; return (
{isNaN(Number(id)) ? ( {renderPrompt(prompt).filter((x) => x?.toString() !== "")} ) : ( <> {id} - {renderPrompt(prompt).filter((x) => x?.toString() !== "")} )}
{variant === "image" && options.map((option) => (
{option?.id} {"src" in option && {`Option}
))} {variant === "text" && options.map((option) => (
{option?.id}. {option?.text}
))}
); } 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) => { 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) => x.question.toString() === y.id.toString())).length; return { total, correct, missing }; }; const next = () => { if (questionIndex === questions.length - 1) { onNext({ exercise: id, solutions: userSolutions, score: calculateScore(), type }); } else { setQuestionIndex(questionIndex + 1); } }; const back = () => { if (questionIndex === 0) { onBack({ exercise: id, solutions: userSolutions, score: calculateScore(), type }); } else { setQuestionIndex(questionIndex - 1); } }; return ( <>
{/*{prompt}*/} {userSolutions && questionIndex < questions.length && ( questions[questionIndex].id === x.question)?.option} /> )}
Correct
Unanswered
Wrong
); }