Shuffles fixed
This commit is contained in:
@@ -20,9 +20,10 @@ const FillBlanks: React.FC<FillBlanksExercise & CommonProps> = ({
|
||||
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<FillBlanksExercise & CommonProps> = ({
|
||||
};
|
||||
const renderLines = (line: string) => {
|
||||
return (
|
||||
<div className="text-base leading-5">
|
||||
<div className="text-base leading-5" key={v4()}>
|
||||
{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<FillBlanksExercise & CommonProps> = ({
|
||||
|
||||
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)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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>.*?<\/u>)/g, (match) => {
|
||||
@@ -70,15 +49,15 @@ function Question({
|
||||
{isNaN(Number(id)) ? (
|
||||
<span>{renderPrompt(prompt).filter((x) => x?.toString() !== "<u>")} </span>
|
||||
) : (
|
||||
<span className="text-lg">
|
||||
<span className="text-lg" key={v4()}>
|
||||
<>
|
||||
{id} - <span className="text-lg">{renderPrompt(prompt).filter((x) => x?.toString() !== "<u>")} </span>
|
||||
{id} - <span className="text-lg" key={v4()}>{renderPrompt(prompt).filter((x) => x?.toString() !== "<u>")} </span>
|
||||
</>
|
||||
</span>
|
||||
)}
|
||||
<div className="flex flex-wrap gap-4 justify-between">
|
||||
{variant === "image" &&
|
||||
questionOptions.map((option) => (
|
||||
options.map((option) => (
|
||||
<div
|
||||
key={option?.id}
|
||||
className={clsx(
|
||||
@@ -90,7 +69,7 @@ function Question({
|
||||
</div>
|
||||
))}
|
||||
{variant === "text" &&
|
||||
questionOptions.map((option) => (
|
||||
options.map((option) => (
|
||||
<div
|
||||
key={option?.id}
|
||||
className={clsx("flex border p-4 rounded-xl gap-2 cursor-pointer bg-white text-base select-none", optionColor(option!.id))}>
|
||||
@@ -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
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="self-end flex justify-between w-full gap-8 absolute bottom-8 left-0 px-8">
|
||||
<Button color="purple" variant="outline" onClick={back} className="max-w-[200px] w-full"
|
||||
disabled={
|
||||
exam && typeof partIndex !== "undefined" && exam.module === "level" &&
|
||||
exam && typeof partIndex !== "undefined" && exam.module === "level" &&
|
||||
typeof exam.parts[0].intro === "string" && questionIndex === 0 && partIndex === 0}
|
||||
>
|
||||
Back
|
||||
|
||||
Reference in New Issue
Block a user