diff --git a/src/components/Exercises/FillBlanks/index.tsx b/src/components/Exercises/FillBlanks/index.tsx index 96316496..909860c9 100644 --- a/src/components/Exercises/FillBlanks/index.tsx +++ b/src/components/Exercises/FillBlanks/index.tsx @@ -20,9 +20,11 @@ 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 setCurrentSolution = useExamStore((state) => state.setCurrentSolution); + const shuffleMaps = shuffles.find((x) => x.exerciseID == id)?.shuffles; const [currentMCSelection, setCurrentMCSelection] = useState<{ id: string, selection: FillBlanksMCOption }>(); @@ -42,42 +44,42 @@ const FillBlanks: React.FC = ({ }, [hasExamEnded]); - let correctWords: any; + let correctWords: any; if (exam && exam.module === "level" && exam.parts[partIndex].exercises[exerciseIndex].type === "fillBlanks") { correctWords = (exam.parts[partIndex].exercises[exerciseIndex] as FillBlanksExercise).words; - } + } - const calculateScore = () => { - const total = text.match(/({{\d+}})/g)?.length || 0; - const correct = answers!.filter((x) => { - const solution = solutions.find((y) => x.id.toString() === y.id.toString())?.solution; - if (!solution) return false; - const option = correctWords!.find((w: any) => { - if (typeof w === "string") { - return w.toLowerCase() === x.solution.toLowerCase(); - } else if ('letter' in w) { - return w.word.toLowerCase() === x.solution.toLowerCase(); - } else { - return w.id.toString() === x.id.toString(); - } - }); - if (!option) return false; + const calculateScore = () => { + const total = text.match(/({{\d+}})/g)?.length || 0; + const correct = answers!.filter((x) => { + const solution = solutions.find((y) => x.id.toString() === y.id.toString())?.solution; + if (!solution) return false; + const option = correctWords!.find((w: any) => { + if (typeof w === "string") { + return w.toLowerCase() === x.solution.toLowerCase(); + } else if ('letter' in w) { + return w.word.toLowerCase() === x.solution.toLowerCase(); + } else { + return w.id.toString() === x.id.toString(); + } + }); + if (!option) return false; - if (typeof option === "string") { - return solution.toLowerCase() === option.toLowerCase(); - } else if ('letter' in option) { - return solution.toLowerCase() === option.word.toLowerCase(); - } else if ('options' in option) { - return option.options[solution as keyof typeof option.options] == x.solution; - } - return false; - }).length; - const missing = total - answers!.filter((x) => solutions.find((y) => x.id.toString() === y.id.toString())).length; + if (typeof option === "string") { + return solution.toLowerCase() === option.toLowerCase(); + } else if ('letter' in option) { + return solution.toLowerCase() === option.word.toLowerCase(); + } else if ('options' in option) { + return option.options[solution as keyof typeof option.options] == x.solution; + } + return false; + }).length; + const missing = total - answers!.filter((x) => solutions.find((y) => x.id.toString() === y.id.toString())).length; return { total, correct, missing }; - }; + }; const renderLines = (line: string) => { return ( -
+
{reactStringReplace(line, /({{\d+}})/g, (match) => { const id = match.replaceAll(/[\{\}]/g, ""); const userSolution = answers.find((x) => x.id === id); @@ -121,21 +123,14 @@ const FillBlanks: React.FC = ({ ); }; - const onSelection = (id: string, value: string) => { - setAnswers((prev) => [...prev.filter((x) => x.id !== id), { id: id, solution: value }]); + const onSelection = (questionID: string, value: string) => { + setAnswers((prev) => [...prev.filter((x) => x.id !== questionID), { id: questionID, solution: value }]); } - const getShuffles = () => { - let shuffle = {}; - if (shuffleMaps.length !== 0) { - shuffle = { - shuffleMaps: shuffleMaps.filter((map) => - answers.some(answer => answer.id === map.id) - ) - } - } - return shuffle; - } + useEffect(() => { + setCurrentSolution({ exercise: id, solutions: answers, score: calculateScore(), type, shuffleMaps: shuffleMaps }); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [answers]) return ( <> @@ -220,18 +215,21 @@ const FillBlanks: React.FC = ({ diff --git a/src/components/Exercises/MultipleChoice.tsx b/src/components/Exercises/MultipleChoice.tsx index e72896a0..e158643b 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"; @@ -24,7 +24,7 @@ function Question({ const renderPrompt = (prompt: string) => { return reactStringReplace(prompt, /(.*?<\/u>)/g, (match) => { const word = match.replaceAll("", "").replaceAll("", ""); - return word.length > 0 ? {word} : null; + return word.length > 0 ? {word} : null; }); }; @@ -49,7 +49,7 @@ function Question({ "flex flex-col items-center border border-mti-gray-platinum p-4 px-8 rounded-xl gap-4 cursor-pointer bg-white relative select-none", userSolution === option.id.toString() && "border-mti-purple-light", )}> - {option.id.toString()} + {option.id.toString()} {`Option
))} @@ -77,13 +77,17 @@ export default function MultipleChoice({ id, prompt, type, questions, userSoluti const { questionIndex, exam, - shuffleMaps, + shuffles, hasExamEnded, + partIndex, userSolutions: storeUserSolutions, setQuestionIndex, - setUserSolutions + setUserSolutions, + setCurrentSolution } = 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 +108,21 @@ export default function MultipleChoice({ id, prompt, type, questions, userSoluti setAnswers((prev) => [...prev.filter((x) => x.question !== question.id), { option, question: question.id }]); }; + useEffect(() => { + setCurrentSolution({ exercise: id, solutions: answers, score: calculateScore(), type, shuffleMaps: shuffleMaps }); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [answers]) + + 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 +131,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,8 +158,9 @@ 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 { + if (exam?.module === "level" && typeof exam.parts[0].intro !== "undefined" && questionIndex === 0) return; setQuestionIndex(questionIndex - 1); } @@ -172,7 +183,11 @@ export default function MultipleChoice({ id, prompt, type, questions, userSoluti
diff --git a/src/components/Low/Button.tsx b/src/components/Low/Button.tsx index 99de3d4b..32b1d708 100644 --- a/src/components/Low/Button.tsx +++ b/src/components/Low/Button.tsx @@ -9,6 +9,7 @@ interface Props { className?: string; disabled?: boolean; isLoading?: boolean; + padding?: string; onClick?: () => void; type?: "button" | "reset" | "submit"; } @@ -21,6 +22,7 @@ export default function Button({ className, children, type, + padding = "py-4 px-6", onClick, }: Props) { const colorClassNames: {[key in typeof color]: {[key in typeof variant]: string}} = { @@ -61,7 +63,8 @@ export default function Button({ type={type} onClick={onClick} className={clsx( - "py-4 px-6 rounded-full transition ease-in-out duration-300 disabled:cursor-not-allowed cursor-pointer", + "rounded-full transition ease-in-out duration-300 disabled:cursor-not-allowed cursor-pointer select-none", + padding, colorClassNames[color][variant], className, )} diff --git a/src/components/Medium/ModuleTitle.tsx b/src/components/Medium/ModuleTitle.tsx index c529a227..04dc84e9 100644 --- a/src/components/Medium/ModuleTitle.tsx +++ b/src/components/Medium/ModuleTitle.tsx @@ -1,13 +1,17 @@ -import {Module} from "@/interfaces"; -import useExamStore from "@/stores/examStore"; -import {moduleLabels} from "@/utils/moduleUtils"; +import { Module } from "@/interfaces"; +import { moduleLabels } from "@/utils/moduleUtils"; import clsx from "clsx"; -import {motion} from "framer-motion"; -import {ReactNode, useEffect, useState} from "react"; -import {BsBook, BsClipboard, BsHeadphones, BsMegaphone, BsPen, BsStopwatch} from "react-icons/bs"; +import { Fragment, ReactNode, useCallback, useState } from "react"; +import { BsBook, BsClipboard, BsHeadphones, BsMegaphone, BsPen, BsStopwatch } from "react-icons/bs"; import ProgressBar from "../Low/ProgressBar"; -import TimerEndedModal from "../TimerEndedModal"; import Timer from "./Timer"; +import { Exam, LevelExam, MultipleChoiceExercise, ShuffleMap, UserSolution } from "@/interfaces/exam"; +import { BsFillGrid3X3GapFill } from "react-icons/bs"; +import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; +import Button from "../Low/Button"; +import { Dialog, Transition } from "@headlessui/react"; +import useExamStore from "@/stores/examStore"; +import Modal from "../Modal"; interface Props { minTimer: number; @@ -18,13 +22,32 @@ interface Props { disableTimer?: boolean; partLabel?: string; showTimer?: boolean; + showSolutions?: boolean; + runOnClick?: ((questionIndex: number) => void) | undefined; } -export default function ModuleTitle({ - minTimer, module, label, exerciseIndex, totalExercises, disableTimer = false, partLabel, showTimer = true +export default function ModuleTitle({ + minTimer, + module, + label, + exerciseIndex, + totalExercises, + disableTimer = false, + partLabel, + showTimer = true, + showSolutions = false, + runOnClick = undefined }: Props) { + const { + userSolutions, + partIndex, + exam + } = useExamStore((state) => state); + const examExerciseIndex = useExamStore((state) => state.exerciseIndex) - const moduleIcon: {[key in Module]: ReactNode} = { + const [isOpen, setIsOpen] = useState(false); + + const moduleIcon: { [key in Module]: ReactNode } = { reading: , listening: , writing: , @@ -32,6 +55,78 @@ export default function ModuleTitle({ level: , }; + const isMultipleChoiceLevelExercise = () => { + if (exam?.module === 'level' && typeof partIndex === "number" && partIndex > -1) { + const currentExercise = (exam as LevelExam).parts[partIndex].exercises[examExerciseIndex]; + return currentExercise && currentExercise.type === 'multipleChoice'; + } + return false; + }; + + const renderMCQuestionGrid = () => { + if (!isMultipleChoiceLevelExercise() && !userSolutions) return null; + + const currentExercise = (exam as LevelExam).parts[partIndex!].exercises[examExerciseIndex] as MultipleChoiceExercise; + const userSolution = userSolutions!.find((x) => x.exercise == currentExercise.id)!; + const answeredQuestions = new Set(userSolution.solutions.map(sol => sol.question)); + const exerciseOffset = currentExercise.questions[0].id; + const lastExercise = exerciseOffset + (currentExercise.questions.length - 1); + + const getQuestionColor = (questionId: string, solution: string, userQuestionSolution: string | undefined) => { + const questionShuffleMap = userSolutions.reduce((foundMap, userSolution) => { + if (foundMap) return foundMap; + return userSolution.shuffleMaps?.find(map => map.questionID.toString() === questionId.toString()) || null; + }, null as ShuffleMap | null); + const newSolution = questionShuffleMap ? questionShuffleMap?.map[solution] : solution; + + if (!userSolutions) return ""; + + if (!userQuestionSolution) { + return "!bg-mti-gray-davy !border--mti-gray-davy !text-mti-gray-davy !text-white hover:!bg-gray-700"; + } + + return userQuestionSolution === newSolution ? + "!bg-mti-purple-light !text-mti-purple-light !text-white hover:!bg-mti-purple-dark" : + "!bg-mti-rose-light !border-mti-rose-light !text-mti-rose-light !text-white hover:!bg-mti-rose-dark"; + } + return ( + <> +

{`Part ${partIndex + 1} (Questions ${exerciseOffset} - ${lastExercise})`}

+
+ {currentExercise.questions.map((_, index) => { + const questionNumber = exerciseOffset + index; + const isAnswered = answeredQuestions.has(questionNumber); + const solution = currentExercise.questions.find((x) => x.id == questionNumber)!.solution; + + const userQuestionSolution = currentExercise.userSolutions?.find((x) => x.question == questionNumber)?.option; + return ( + + ); + })} +
+

+ Click a question number to jump to that question +

+ + ); + }; + return ( <> {showTimer && } @@ -67,8 +162,24 @@ export default function ModuleTitle({
+ {isMultipleChoiceLevelExercise() && ( + <> + + setIsOpen(false)} + className="w-full max-w-md transform overflow-hidden rounded-2xl bg-white shadow-xl transition-all" + > + <> + {renderMCQuestionGrid()} + + + + )} ); -} +} \ No newline at end of file diff --git a/src/components/Medium/Timer.tsx b/src/components/Medium/Timer.tsx index 86f1b68b..58ce4751 100644 --- a/src/components/Medium/Timer.tsx +++ b/src/components/Medium/Timer.tsx @@ -51,7 +51,7 @@ const Timer: React.FC = ({minTimer, disableTimer, standalone = false}) => void; } -export default function QuestionsModal({ isOpen, onClose, blankQuestions = true, finishingWhat = "module" }: Props) { +export default function QuestionsModal({ isOpen, onClose, type = "module", unanswered = false }: Props) { + const [isClosing, setIsClosing] = useState(false); + + const blockMultipleClicksClose = (x: boolean) => { + if (!isClosing) { + setIsClosing(true); + onClose(x); + } + + setTimeout(() => { + setIsClosing(false); + }, 400); + } + return ( onClose(false)} className="relative z-50"> @@ -34,39 +47,67 @@ export default function QuestionsModal({ isOpen, onClose, blankQuestions = true, leaveTo="opacity-0 scale-95">
- {blankQuestions ? ( + {type === "module" && ( <> Questions Unanswered - Please note that you are finishing the current {finishingWhat} and once you proceed to the next {finishingWhat}, you will no longer be + Please note that you are finishing the current module and once you proceed to the next module, you will no longer be able to change the answers of the current one, including your unanswered questions.

Are you sure you want to continue without completing those questions?
- -
- ): ( + )} + {type === "blankQuestions" && ( + <> + Questions Unanswered +
+

You have left some questions unanswered in the current part.

+

If you wish to continue, you can still access this part later using the navigation bar at the top or the "Back" button.

+

Do you want to proceed to the next part, or would you like to go back and complete the unanswered questions in the current part?

+
+
+ + +
+ + )} + {type === "submit" && ( <> Confirm Submission - Please note that you are finishing the current {finishingWhat} and once you proceed to the next {finishingWhat}, you will no longer be - able to review the answers of the current one.
-
- Are you sure you want to continue? + {unanswered ? ( + <> + By clicking "Submit," you are finalizing your exam with some questions left unanswered. Once you submit, you will not be able to review or change any of your answers, including the unanswered ones.
+
+ Are you sure you want to submit and complete the exam with unanswered questions? + + ) : ( + <> + By clicking "Submit," you are finalizing your exam. Once you submit, you will not be able to review or change any of your answers.
+
+ Are you sure you want to submit and complete the exam? + + )}
- -
diff --git a/src/components/Solutions/FillBlanks.tsx b/src/components/Solutions/FillBlanks.tsx index 4f2aea4b..92dee6b5 100644 --- a/src/components/Solutions/FillBlanks.tsx +++ b/src/components/Solutions/FillBlanks.tsx @@ -1,4 +1,4 @@ -import { FillBlanksExercise, FillBlanksMCOption } from "@/interfaces/exam"; +import { FillBlanksExercise, FillBlanksMCOption, ShuffleMap } from "@/interfaces/exam"; import clsx from "clsx"; import reactStringReplace from "react-string-replace"; import { CommonProps } from "."; @@ -16,19 +16,18 @@ export default function FillBlanksSolutions({ onNext, onBack, }: FillBlanksExercise & CommonProps) { - - // next and back was all messed up and still don't know why, anyways const storeUserSolutions = useExamStore((state) => state.userSolutions); const correctUserSolutions = storeUserSolutions.find( (solution) => solution.exercise === id )?.solutions; + + const shuffles = useExamStore((state) => state.shuffles); const calculateScore = () => { 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) => { @@ -66,16 +65,18 @@ export default function FillBlanksSolutions({ return ( {reactStringReplace(line, /({{\d+}})/g, (match) => { - const id = match.replaceAll(/[\{\}]/g, ""); - const userSolution = correctUserSolutions!.find((x) => x.id.toString() === id.toString()); - const answerSolution = solutions.find(sol => sol.id.toString() === id.toString())!.solution; + const questionId = match.replaceAll(/[\{\}]/g, ""); + const userSolution = correctUserSolutions!.find((x) => x.id.toString() === questionId.toString()); + const answerSolution = solutions.find(sol => sol.id.toString() === questionId.toString())!.solution; + const questionShuffleMap = shuffles.find((x) => x.exerciseID == id)?.shuffles.find((y) => y.questionID == questionId); + const newAnswerSolution = questionShuffleMap ? questionShuffleMap.map[answerSolution].toLowerCase() : answerSolution.toLowerCase(); if (!userSolution) { let answerText; if (typeCheckWordsMC(words)) { - const options = words.find((x) => x.id.toString() === id.toString()); + const options = words.find((x) => x.id.toString() === questionId.toString()); const correctKey = Object.keys(options!.options).find(key => - key.toLowerCase() === answerSolution.toLowerCase() + key.toLowerCase() === newAnswerSolution ); answerText = options!.options[correctKey as keyof typeof options]; } else { @@ -98,7 +99,7 @@ export default function FillBlanksSolutions({ : 'letter' in w ? w.letter.toLowerCase() === userSolution.solution.toLowerCase() : 'options' in w - ? w.id === userSolution.id + ? w.id === userSolution.questionId : false ); @@ -114,10 +115,10 @@ export default function FillBlanksSolutions({ let correct; let solutionText; if (typeCheckWordsMC(words)) { - const options = words.find((x) => x.id.toString() === id.toString()); + const options = words.find((x) => x.id.toString() === questionId.toString()); if (options) { const correctKey = Object.keys(options.options).find(key => - key.toLowerCase() === answerSolution.toLowerCase() + key.toLowerCase() === newAnswerSolution ); correct = userSolution.solution == options.options[correctKey as keyof typeof options.options]; solutionText = options.options[correctKey as keyof typeof options.options] || answerSolution; 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
-
+
+ +
- - - + )} + ); const partLabel = () => { @@ -344,8 +328,8 @@ export default function Level({ exam, showSolutions = false, onFinish, editing = } } - const modalKwargs = () => { - const allSolutionsCorrectLength = exam.parts[partIndex].exercises.every((exercise) => { + const answeredEveryQuestion = (partIndex: number) => { + return exam.parts[partIndex].exercises.every((exercise) => { const userSolution = userSolutions.find(x => x.exercise === exercise.id); if (exercise.type === "multipleChoice") { return userSolution?.solutions.length === exercise.questions.length; @@ -355,27 +339,81 @@ export default function Level({ exam, showSolutions = false, onFinish, editing = } return false; }); - - return { - blankQuestions: !allSolutionsCorrectLength, - 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) } } - } - } + useEffect(() => { + if (continueAnyways) { + setContinueAnyways(false); + nextExercise(); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [continueAnyways]); + + const modalKwargs = () => { + const kwargs: { type: "module" | "blankQuestions" | "submit", unanswered: boolean, onClose: (next?: boolean) => void; } = { + type: "blankQuestions", + unanswered: false, + onClose: function (x: boolean | undefined) { if (x) { setContinueAnyways(true); setShowQuestionsModal(false); } else { setShowQuestionsModal(false) } } + }; + + if (partIndex === exam.parts.length - 1) { + kwargs.type = "submit" + kwargs.unanswered = !exam.parts.every((_, partIndex) => answeredEveryQuestion(partIndex)); + kwargs.onClose = function (x: boolean | undefined) { if (x) { setContinueAnyways(true); setShowQuestionsModal(false); } else { setShowQuestionsModal(false) } }; + } + setQuestionModalKwargs(kwargs); + } + + const mcNavKwargs = { + userSolutions: userSolutions, + exam: exam, + partIndex: partIndex, + showSolutions: showSolutions, + "setExerciseIndex": setExerciseIndex, + "setPartIndex": setPartIndex, + "runOnClick": setQuestionIndex + } + + return ( <>
- + { - !(partIndex === 0 && storeQuestionIndex === 0 && showPartDivider) && + !(partIndex === 0 && questionIndex === 0 && showPartDivider) && } {exam.parts[0].intro && showPartDivider ? { setShowPartDivider(false); setBgColor("bg-white") }} /> : ( <> + {exam.parts[0].intro && ( +
+ + + {exam.parts.map((_, index) => + { + if (!seenParts.includes(index)) { + e.preventDefault(); + } else { + setExerciseIndex(0); + setQuestionIndex(0); + } + }} + className={({ selected }) => + clsx( + "w-full rounded-lg py-2.5 text-sm font-medium leading-5 text-ielts-level/80", + "ring-white ring-opacity-60 focus:outline-none", + "transition duration-300 ease-in-out", + selected && "bg-white shadow", + seenParts.includes(index) ? "hover:bg-white/70" : "cursor-not-allowed" + ) + } + >{`Part ${index + 1}`} + ) + } + + +
+ )} x.exercises))} disableTimer={showSolutions || editing} - showTimer={typeof exam.parts[0].intro === "undefined"} + showTimer={false} + {...mcNavKwargs} />
-1 && exerciseIndex > -1 && !!exam.parts[partIndex].context && "grid grid-cols-2 gap-4", + !!exam.parts[partIndex].context && !textRender && "grid grid-cols-2 gap-4", )}> - {partIndex > -1 && !!exam.parts[partIndex].context && renderText()} - {exerciseIndex > -1 && - partIndex > -1 && - exerciseIndex < exam.parts[partIndex].exercises.length && - !showSolutions && - !editing && - currentExercise && - renderExercise(currentExercise, exam.id, nextExercise, previousExercise)} - - {exerciseIndex > -1 && - partIndex > -1 && - exerciseIndex < exam.parts[partIndex].exercises.length && - (showSolutions || editing) && - currentExercise && - renderSolution(currentExercise, nextExercise, previousExercise)} + {textRender ? + renderText() : + <> + {exam.parts[partIndex].context && renderText()} + {(showSolutions || editing) ? + renderSolution(currentExercise, nextExercise, previousExercise) + : + renderExercise(currentExercise, exam.id, nextExercise, previousExercise) + } + + }
{/*exerciseIndex === -1 && partIndex > 0 && (
@@ -418,8 +453,8 @@ export default function Level({ exam, showSolutions = false, onFinish, editing = }} className="max-w-[200px] w-full" disabled={ - exam && typeof partIndex !== "undefined" && exam.module === "level" && - typeof exam.parts[0].intro === "string" && storeQuestionIndex === 0} + exam && typeof partIndex !== "undefined" && exam.module === "level" && + typeof exam.parts[0].intro === "string" && questionIndex === 0} > Back diff --git a/src/interfaces/exam.ts b/src/interfaces/exam.ts index 5c2cf7e2..fe919b0b 100644 --- a/src/interfaces/exam.ts +++ b/src/interfaces/exam.ts @@ -303,8 +303,13 @@ export interface MultipleChoiceQuestion { } export interface ShuffleMap { - id: string; + questionID: string; map: { [key: string]: string; } } + +export interface Shuffles { + exerciseID: string; + shuffles: ShuffleMap[] +} diff --git a/src/pages/(exam)/ExamPage.tsx b/src/pages/(exam)/ExamPage.tsx index 36df63e7..b834f98f 100644 --- a/src/pages/(exam)/ExamPage.tsx +++ b/src/pages/(exam)/ExamPage.tsx @@ -1,6 +1,6 @@ /* eslint-disable @next/next/no-img-element */ -import {Module} from "@/interfaces"; -import {useEffect, useState} from "react"; +import { Module } from "@/interfaces"; +import { useEffect, useState } from "react"; import AbandonPopup from "@/components/AbandonPopup"; import Layout from "@/components/High/Layout"; @@ -12,15 +12,15 @@ import Selection from "@/exams/Selection"; import Speaking from "@/exams/Speaking"; import Writing from "@/exams/Writing"; import useUser from "@/hooks/useUser"; -import {Exam, LevelExam, UserSolution, Variant} from "@/interfaces/exam"; -import {Stat} from "@/interfaces/user"; +import { Exam, LevelExam, UserSolution, Variant } from "@/interfaces/exam"; +import { Stat } from "@/interfaces/user"; import useExamStore from "@/stores/examStore"; -import {evaluateSpeakingAnswer, evaluateWritingAnswer} from "@/utils/evaluation"; -import {defaultExamUserSolutions, getExam} from "@/utils/exams"; +import { evaluateSpeakingAnswer, evaluateWritingAnswer } from "@/utils/evaluation"; +import { defaultExamUserSolutions, getExam } from "@/utils/exams"; import axios from "axios"; -import {useRouter} from "next/router"; -import {toast, ToastContainer} from "react-toastify"; -import {v4 as uuidv4} from "uuid"; +import { useRouter } from "next/router"; +import { toast, ToastContainer } from "react-toastify"; +import { v4 as uuidv4 } from "uuid"; import useSessions from "@/hooks/useSessions"; import ShortUniqueId from "short-unique-id"; import clsx from "clsx"; @@ -29,7 +29,7 @@ interface Props { page: "exams" | "exercises"; } -export default function ExamPage({page}: Props) { +export default function ExamPage({ page }: Props) { const [variant, setVariant] = useState("full"); const [avoidRepeated, setAvoidRepeated] = useState(false); const [hasBeenUploaded, setHasBeenUploaded] = useState(false); @@ -44,20 +44,21 @@ export default function ExamPage({page}: Props) { const assignment = useExamStore((state) => state.assignment); const initialTimeSpent = useExamStore((state) => state.timeSpent); - const {exam, setExam} = useExamStore((state) => state); - const {exams, setExams} = useExamStore((state) => state); - const {sessionId, setSessionId} = useExamStore((state) => state); - const {partIndex, setPartIndex} = useExamStore((state) => state); - const {moduleIndex, setModuleIndex} = useExamStore((state) => state); - const {questionIndex, setQuestionIndex} = useExamStore((state) => state); - const {exerciseIndex, setExerciseIndex} = useExamStore((state) => state); - const {userSolutions, setUserSolutions} = useExamStore((state) => state); - const {showSolutions, setShowSolutions} = useExamStore((state) => state); - const {selectedModules, setSelectedModules} = useExamStore((state) => state); - const {inactivity, setInactivity} = useExamStore((state) => state); - const {bgColor, setBgColor} = useExamStore((state) => state); + const { exam, setExam } = useExamStore((state) => state); + const { exams, setExams } = useExamStore((state) => state); + const { sessionId, setSessionId } = useExamStore((state) => state); + const { partIndex, setPartIndex } = useExamStore((state) => state); + const { moduleIndex, setModuleIndex } = useExamStore((state) => state); + const { questionIndex, setQuestionIndex } = useExamStore((state) => state); + const { exerciseIndex, setExerciseIndex } = useExamStore((state) => state); + const { userSolutions, setUserSolutions } = useExamStore((state) => state); + const { showSolutions, setShowSolutions } = useExamStore((state) => state); + const { selectedModules, setSelectedModules } = useExamStore((state) => state); + const { inactivity, setInactivity } = useExamStore((state) => state); + const { bgColor, setBgColor } = useExamStore((state) => state); + const setShuffleMaps = useExamStore((state) => state.setShuffles) - const {user} = useUser({redirectTo: "/login"}); + const { user } = useUser({ redirectTo: "/login" }); const router = useRouter(); // eslint-disable-next-line react-hooks/exhaustive-deps @@ -260,11 +261,11 @@ export default function ExamPage({page}: Props) { date: new Date().getTime(), isDisabled: solution.isDisabled, shuffleMaps: solution.shuffleMaps, - ...(assignment ? {assignment: assignment.id} : {}), + ...(assignment ? { assignment: assignment.id } : {}), })); axios - .post<{ok: boolean}>("/api/stats", newStats) + .post<{ ok: boolean }>("/api/stats", newStats) .then((response) => setHasBeenUploaded(response.data.ok)) .catch(() => setHasBeenUploaded(false)); } @@ -282,9 +283,9 @@ export default function ExamPage({page}: Props) { // eslint-disable-next-line react-hooks/exhaustive-deps }, [statsAwaitingEvaluation]); - useEffect(()=> { + useEffect(() => { - if(exam && exam.module === "level" && exam.parts[0].intro && !showSolutions) { + if (exam && exam.module === "level" && exam.parts[0].intro && !showSolutions) { setBgColor("bg-ielts-level-light"); } }, [exam, showSolutions, setBgColor]) @@ -332,7 +333,7 @@ export default function ExamPage({page}: Props) { ), }), ); - return Object.assign(exam, {parts}); + return Object.assign(exam, { parts }); } const exercises = exam.exercises.map((x) => @@ -340,7 +341,7 @@ export default function ExamPage({page}: Props) { userSolutions: userSolutions.find((y) => x.id === y.exercise)?.solutions, }), ); - return Object.assign(exam, {exercises}); + return Object.assign(exam, { exercises }); }; const onFinish = async (solutions: UserSolution[]) => { @@ -395,7 +396,7 @@ export default function ExamPage({page}: Props) { correct: number; }[] => { const scores: { - [key in Module]: {total: number; missing: number; correct: number}; + [key in Module]: { total: number; missing: number; correct: number }; } = { reading: { total: 0, @@ -437,7 +438,7 @@ export default function ExamPage({page}: Props) { return Object.keys(scores) .filter((x) => scores[x as Module].total > 0) - .map((x) => ({module: x as Module, ...scores[x as Module]})); + .map((x) => ({ module: x as Module, ...scores[x as Module] })); }; const renderScreen = () => { @@ -479,9 +480,11 @@ export default function ExamPage({page}: Props) { return indexA - indexB; }); setUserSolutions(orderedSolutions); + } else { setUserSolutions(userSolutions); } + setShuffleMaps([]); setShowSolutions(true); setModuleIndex(index || 0); setExerciseIndex(["reading", "listening"].includes(exams[0].module) ? -1 : 0); diff --git a/src/stores/examStore.ts b/src/stores/examStore.ts index 13e69f50..fef1b5af 100644 --- a/src/stores/examStore.ts +++ b/src/stores/examStore.ts @@ -1,5 +1,5 @@ import {Module} from "@/interfaces"; -import {Exam, ShuffleMap, UserSolution} from "@/interfaces/exam"; +import {Exam, ShuffleMap, Shuffles, UserSolution} from "@/interfaces/exam"; import {Assignment} from "@/interfaces/results"; import {create} from "zustand"; @@ -18,8 +18,9 @@ export interface ExamState { exerciseIndex: number; questionIndex: number; inactivity: number; - shuffleMaps: ShuffleMap[]; + shuffles: Shuffles[]; bgColor: string; + currentSolution?: UserSolution | undefined; } export interface ExamFunctions { @@ -37,8 +38,9 @@ export interface ExamFunctions { setExerciseIndex: (exerciseIndex: number) => void; setQuestionIndex: (questionIndex: number) => void; setInactivity: (inactivity: number) => void; - setShuffleMaps: (shuffleMaps: ShuffleMap[]) => void; + setShuffles: (shuffles: Shuffles[]) => void; setBgColor: (bgColor: string) => void; + setCurrentSolution: (currentSolution: UserSolution | undefined) => void; reset: () => void; } @@ -57,8 +59,9 @@ export const initialState: ExamState = { exerciseIndex: -1, questionIndex: 0, inactivity: 0, - shuffleMaps: [], - bgColor: "bg-white" + shuffles: [], + bgColor: "bg-white", + currentSolution: undefined }; const useExamStore = create((set) => ({ @@ -78,8 +81,9 @@ const useExamStore = create((set) => ({ setExerciseIndex: (exerciseIndex: number) => set(() => ({exerciseIndex})), setQuestionIndex: (questionIndex: number) => set(() => ({questionIndex})), setInactivity: (inactivity: number) => set(() => ({inactivity})), - setShuffleMaps: (shuffleMaps) => set(() => ({shuffleMaps})), - setBgColor: (bgColor) => set(()=> ({bgColor})), + setShuffles: (shuffles: Shuffles[]) => set(() => ({shuffles})), + setBgColor: (bgColor) => set(() => ({bgColor})), + setCurrentSolution: (currentSolution: UserSolution | undefined) => set(() => ({currentSolution})), reset: () => set(() => initialState), }));