From b2cc706a5edc97e4fa2cac0b23e105961b51eb1a Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Fri, 23 Jun 2023 10:28:33 +0100 Subject: [PATCH] Updated the Writing exercise to have the evaluation in the user solutions instead of the exercise itself --- src/components/Exercises/Writing.tsx | 2 +- src/components/Solutions/Writing.tsx | 26 ++++++++------ src/interfaces/exam.ts | 1 + src/pages/api/stats/index.ts | 2 ++ src/pages/exam.tsx | 51 ++++++++++++++-------------- 5 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src/components/Exercises/Writing.tsx b/src/components/Exercises/Writing.tsx index f293ec49..43b82d64 100644 --- a/src/components/Exercises/Writing.tsx +++ b/src/components/Exercises/Writing.tsx @@ -107,7 +107,7 @@ export default function Writing({id, prompt, info, type, wordCounter, attachment diff --git a/src/components/Solutions/Writing.tsx b/src/components/Solutions/Writing.tsx index cc4db9ad..dff0b78a 100644 --- a/src/components/Solutions/Writing.tsx +++ b/src/components/Solutions/Writing.tsx @@ -10,7 +10,7 @@ import {toast} from "react-toastify"; import Button from "../Low/Button"; import {Dialog, Transition} from "@headlessui/react"; -export default function Writing({id, prompt, info, evaluation, attachment, userSolutions, onNext, onBack}: WritingExercise & CommonProps) { +export default function Writing({id, prompt, info, attachment, userSolutions, onNext, onBack}: WritingExercise & CommonProps) { const [isModalOpen, setIsModalOpen] = useState(false); return ( @@ -74,20 +74,24 @@ export default function Writing({id, prompt, info, evaluation, attachment, userS className="w-full h-full min-h-[320px] cursor-text px-7 py-8 input border-2 border-mti-gray-platinum bg-white rounded-3xl" contentEditable={false} readOnly - value={userSolutions[0] as any} + value={userSolutions[0]!.solution} /> )} -
-
- {Object.keys(evaluation!.task_response).map((key) => ( -
- {key}: Level {evaluation!.task_response[key]} -
- ))} + {userSolutions && userSolutions.length > 0 && ( +
+
+ {Object.keys(userSolutions[0].evaluation!.task_response).map((key) => ( +
+ {key}: Level {userSolutions[0].evaluation!.task_response[key]} +
+ ))} +
+
+ {userSolutions[0].evaluation!.comment} +
-
{evaluation!.comment}
-
+ )}
diff --git a/src/interfaces/exam.ts b/src/interfaces/exam.ts index 3f7fcaf3..0bbb8f52 100644 --- a/src/interfaces/exam.ts +++ b/src/interfaces/exam.ts @@ -89,6 +89,7 @@ export interface WritingExercise { userSolutions: { id: string; solution: string; + evaluation?: WritingEvaluation; }[]; } diff --git a/src/pages/api/stats/index.ts b/src/pages/api/stats/index.ts index 555ff86a..f4413d2e 100644 --- a/src/pages/api/stats/index.ts +++ b/src/pages/api/stats/index.ts @@ -40,6 +40,8 @@ async function post(req: NextApiRequest, res: NextApiResponse) { return; } + console.log("HERE"); + const stats = req.body as Stat[]; await stats.forEach(async (stat) => await addDoc(collection(db, "stats"), stat)); diff --git a/src/pages/exam.tsx b/src/pages/exam.tsx index 1808b600..1d0fffa8 100644 --- a/src/pages/exam.tsx +++ b/src/pages/exam.tsx @@ -82,23 +82,23 @@ export default function Page() { }, [selectedModules, setExams, exams]); useEffect(() => { - (async () => { - if (selectedModules.length > 0 && exams.length === 0 && moduleIndex >= selectedModules.length && !hasBeenUploaded) { - const newStats: Stat[] = userSolutions.map((solution) => ({ - ...solution, - session: sessionId, - exam: solution.exam!, - module: solution.module!, - user: user?.id || "", - date: new Date().getTime(), - })); + if (selectedModules.length > 0 && exams.length !== 0 && moduleIndex >= selectedModules.length && !hasBeenUploaded) { + const newStats: Stat[] = userSolutions.map((solution) => ({ + ...solution, + session: sessionId, + exam: solution.exam!, + module: solution.module!, + user: user?.id || "", + date: new Date().getTime(), + })); - axios - .post<{ok: boolean}>("/api/stats", newStats) - .then((response) => setHasBeenUploaded(response.data.ok)) - .catch(() => setHasBeenUploaded(false)); - } - })(); + console.log("GOING TO SAVE"); + + axios + .post<{ok: boolean}>("/api/stats", newStats) + .then((response) => setHasBeenUploaded(response.data.ok)) + .catch(() => setHasBeenUploaded(false)); + } // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectedModules, moduleIndex, hasBeenUploaded]); @@ -123,21 +123,20 @@ export default function Page() { } }; - const evaluateWritingAnswer = async (examId: string, exerciseId: string, answer: string) => { + const evaluateWritingAnswer = async (examId: string, exerciseId: string, solution: UserSolution) => { const writingExam = exams.find((x) => x.id === examId)!; const exercise = writingExam.exercises.find((x) => x.id === exerciseId)! as WritingExercise; const response = await axios.post("/api/exam/writing/evaluate", { question: `${exercise.prompt} ${exercise.attachment ? exercise.attachment.description : ""}`.replaceAll("\n", ""), - answer: answer.trim().replaceAll("\n", " "), + answer: solution.solutions[0].solution.trim().replaceAll("\n", " "), }); if (response.status === 200) { - writingExam.exercises = [ - ...writingExam.exercises.filter((x) => x.id !== exerciseId), - Object.assign(exercise, {evaluation: response.data}), - ]; - setExams([...exams.filter((x) => x.id !== examId), writingExam].sort(sortByModule)); + setUserSolutions([ + ...userSolutions.filter((x) => x.exercise !== exerciseId), + {...solution, solutions: [{id: exerciseId, solution: solution.solutions[0].solution, evaluation: response.data}]}, + ]); } }; @@ -153,13 +152,13 @@ export default function Page() { const solutionIds = solutions.map((x) => x.exercise); if (exam && exam.module === "writing" && solutions.length > 0 && !showSolutions) { + setHasBeenUploaded(true); setIsEvaluationLoading(true); Promise.all( - exam.exercises.map((exercise) => - evaluateWritingAnswer(exam.id, exercise.id, solutions.find((x) => x.exercise === exercise.id)!.solutions[0]), - ), + exam.exercises.map((exercise) => evaluateWritingAnswer(exam.id, exercise.id, solutions.find((x) => x.exercise === exercise.id)!)), ).finally(() => { setIsEvaluationLoading(false); + setHasBeenUploaded(false); }); }