/* eslint-disable @next/next/no-img-element */ import {MultipleChoiceExercise, MultipleChoiceQuestion} from "@/interfaces/exam"; import clsx from "clsx"; import {useState} from "react"; import {CommonProps} from "."; import Button from "../Low/Button"; function Question({ variant, prompt, solution, options, userSolution, }: MultipleChoiceQuestion & {userSolution: string | undefined; onSelectOption?: (option: string) => void; showSolution?: boolean}) { const optionColor = (option: string) => { if (option === solution && !userSolution) { return "!border-mti-red-light !text-mti-red-light"; } if (option === solution) { return "!border-mti-purple-light !text-mti-purple-light"; } return userSolution === option ? "!border-mti-rose-light !text-mti-rose-light" : ""; }; return (
{prompt}
{variant === "image" && options.map((option) => (
{option.id} {`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] = useState(0); const calculateScore = () => { const total = questions.length; const correct = userSolutions.filter((x) => questions.find((y) => y.id === x.question)?.solution === x.option || false).length; const missing = total - userSolutions.filter((x) => questions.find((y) => y.id === x.question)).length; return {total, correct, missing}; }; const next = () => { if (questionIndex === questions.length - 1) { onNext({exercise: id, solutions: userSolutions, score: calculateScore(), type}); } else { setQuestionIndex((prev) => prev + 1); } }; const back = () => { if (questionIndex === 0) { onBack({exercise: id, solutions: userSolutions, score: calculateScore(), type}); } else { setQuestionIndex((prev) => prev - 1); } }; return ( <>
{prompt} {questionIndex < questions.length && ( questions[questionIndex].id === x.question)?.option} /> )}
Correct
Unanswered
Wrong
); }