From a6160c3cf089eeb026b91e47d20e6927cdf15de5 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Fri, 26 Jul 2024 23:43:23 +0100 Subject: [PATCH] Updated and fixed the level generation --- src/components/Exercises/MultipleChoice.tsx | 2 +- src/components/Solutions/MultipleChoice.tsx | 2 +- src/exams/Level.tsx | 19 ++++++++++++------- src/pages/(generation)/LevelGeneration.tsx | 8 +++++--- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/components/Exercises/MultipleChoice.tsx b/src/components/Exercises/MultipleChoice.tsx index fd52cf92..aed907e2 100644 --- a/src/components/Exercises/MultipleChoice.tsx +++ b/src/components/Exercises/MultipleChoice.tsx @@ -16,7 +16,7 @@ function Question({ onSelectOption, }: MultipleChoiceQuestion & {userSolution: string | undefined; onSelectOption?: (option: string) => void; showSolution?: boolean}) { const renderPrompt = (prompt: string) => { - return reactStringReplace(prompt, /(()\w+(<\/u>))/g, (match) => { + return reactStringReplace(prompt, /(()[\w\s']+(<\/u>))/g, (match) => { const word = match.replaceAll("", "").replaceAll("", ""); return word.length > 0 ? {word} : null; }); diff --git a/src/components/Solutions/MultipleChoice.tsx b/src/components/Solutions/MultipleChoice.tsx index a1808a67..7f1ba0fd 100644 --- a/src/components/Solutions/MultipleChoice.tsx +++ b/src/components/Solutions/MultipleChoice.tsx @@ -16,7 +16,7 @@ function Question({ userSolution, }: MultipleChoiceQuestion & {userSolution: string | undefined; onSelectOption?: (option: string) => void; showSolution?: boolean}) { const renderPrompt = (prompt: string) => { - return reactStringReplace(prompt, /(()\w+(<\/u>))/g, (match) => { + return reactStringReplace(prompt, /(()[\w\s']+(<\/u>))/g, (match) => { const word = match.replaceAll("", "").replaceAll("", ""); return word.length > 0 ? {word} : null; }); diff --git a/src/exams/Level.tsx b/src/exams/Level.tsx index 2366fada..8f1043e2 100644 --- a/src/exams/Level.tsx +++ b/src/exams/Level.tsx @@ -71,6 +71,12 @@ export default function Level({exam, showSolutions = false, onFinish}: Props) { setUserSolutions([...userSolutions.filter((x) => x.exercise !== solution.exercise), {...solution, module: "level", exam: exam.id}]); } + if (storeQuestionIndex > 0) { + const exercise = getExercise(); + setMultipleChoicesDone((prev) => [...prev.filter((x) => x.id !== exercise.id), {id: exercise.id, amount: storeQuestionIndex}]); + } + setStoreQuestionIndex(0); + if (exerciseIndex + 1 < exam.parts[partIndex].exercises.length && !hasExamEnded) { setExerciseIndex(exerciseIndex + 1); return; @@ -78,7 +84,7 @@ export default function Level({exam, showSolutions = false, onFinish}: Props) { if (partIndex + 1 < exam.parts.length && !hasExamEnded) { setPartIndex(partIndex + 1); - setExerciseIndex(showSolutions ? 0 : -1); + setExerciseIndex(!!exam.parts[partIndex + 1].context ? -1 : 0); return; } @@ -94,12 +100,6 @@ export default function Level({exam, showSolutions = false, onFinish}: Props) { return; } - if (storeQuestionIndex > 0) { - const exercise = getExercise(); - setMultipleChoicesDone((prev) => [...prev.filter((x) => x.id !== exercise.id), {id: exercise.id, amount: storeQuestionIndex}]); - } - setStoreQuestionIndex(0); - setHasExamEnded(false); if (solution) { @@ -114,6 +114,11 @@ export default function Level({exam, showSolutions = false, onFinish}: Props) { if (solution) { setUserSolutions([...userSolutions.filter((x) => x.exercise !== solution.exercise), {...solution, module: "level", exam: exam.id}]); } + + if (storeQuestionIndex > 0) { + const exercise = getExercise(); + setMultipleChoicesDone((prev) => [...prev.filter((x) => x.id !== exercise.id), {id: exercise.id, amount: storeQuestionIndex}]); + } setStoreQuestionIndex(0); setExerciseIndex(exerciseIndex - 1); diff --git a/src/pages/(generation)/LevelGeneration.tsx b/src/pages/(generation)/LevelGeneration.tsx index 2728eb98..9b6051bc 100644 --- a/src/pages/(generation)/LevelGeneration.tsx +++ b/src/pages/(generation)/LevelGeneration.tsx @@ -43,7 +43,7 @@ const QuestionDisplay = ({question, onUpdate}: {question: MultipleChoiceQuestion const [answer, setAnswer] = useState(question.solution); const renderPrompt = (prompt: string) => { - return reactStringReplace(prompt, /(()\w+(<\/u>))/g, (match) => { + return reactStringReplace(prompt, /(()[\w\s']+(<\/u>))/g, (match) => { const word = match.replaceAll("", "").replaceAll("", ""); return word.length > 0 ? {word} : null; }); @@ -280,6 +280,8 @@ const LevelGeneration = () => { axios .post<{exercises: {[key: string]: any}}>("/api/exam/level/generate/level", {nr_exercises: numberOfParts, ...body}) .then((result) => { + console.log(result.data); + playSound(typeof result.data === "string" ? "error" : "check"); if (typeof result.data === "string") return toast.error("Something went wrong, please try to generate again."); @@ -305,7 +307,7 @@ const LevelGeneration = () => { part.type === "multiple_choice_underlined" ? "Select the wrong part of the sentence." : "Select the appropriate option.", - questions: currentExercise.questions, + questions: currentExercise.questions.map((x: any) => ({...x, variant: "text"})), type: "multipleChoice", userSolutions: [], }; @@ -357,7 +359,7 @@ const LevelGeneration = () => { const mcExercise: MultipleChoiceExercise = { id: v4(), prompt: "Select the appropriate option.", - questions: currentExercise.exercises.multipleChoice, + questions: currentExercise.exercises.multipleChoice.questions.map((x: any) => ({...x, variant: "text"})), type: "multipleChoice", userSolutions: [], };