/* 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 "!bg-mti-gray-davy !text-white"; } if (option === newSolution) { return "!bg-mti-purple-light !text-white"; } return userSolution === option ? "!bg-mti-rose-light !text-white" : ""; }; 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) => y.id.toString() === x.question.toString())).length; return {total, correct, missing}; }; const next = () => { if (questionIndex + 1 >= questions.length - 1) { onNext({exercise: id, solutions: userSolutions, score: calculateScore(), type}); } else { setQuestionIndex(questionIndex + 2); } }; const back = () => { if (questionIndex === 0) { onBack({exercise: id, solutions: userSolutions, score: calculateScore(), type}); } else { setQuestionIndex(questionIndex - 2); } }; return ( <>
{/*{prompt}*/} {userSolutions && questionIndex < questions.length && ( questions[questionIndex].id === x.question)?.option} /> )}
{userSolutions && questionIndex + 1 < questions.length && (
questions[questionIndex + 1].id === x.question)?.option} />
)}
Correct
Unanswered
Wrong
); }