From f7005deada68bfcd901e9c50273a3b759e20d2eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 24 Jul 2026 03:02:02 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(exams):=20view=20a=20student's?= =?UTF-8?q?=20previous=20exam=20attempts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The backend now keeps every sitting's answers, drawings, and mark instead of wiping on retake. Surface that in the grader's answer sheet: fetch the student's attempt list + mark history and, when there is more than one sitting, show an attempt picker (each tab labelled with its mark, the latest tagged current). Selecting an older sitting loads its answers and drawings read-only — grading still targets the current attempt. Four new read-only API wrappers hit the history endpoints; prior-attempt views reuse the existing answer-sheet + drawing-replay components. --- src/api/__tests__/exams/exam-results.test.ts | 46 +++++++++ src/api/exams/getStudentAttemptAnswerImage.ts | 12 +++ src/api/exams/getStudentAttemptAnswers.ts | 15 +++ src/api/exams/getStudentAttempts.ts | 6 ++ src/api/exams/getStudentMarksHistory.ts | 11 +++ src/api/exams/index.ts | 4 + src/components/exams/answer-sheet-view.tsx | 98 +++++++++++++++++-- src/i18n/messages.ts | 12 +++ 8 files changed, 198 insertions(+), 6 deletions(-) create mode 100644 src/api/exams/getStudentAttemptAnswerImage.ts create mode 100644 src/api/exams/getStudentAttemptAnswers.ts create mode 100644 src/api/exams/getStudentAttempts.ts create mode 100644 src/api/exams/getStudentMarksHistory.ts diff --git a/src/api/__tests__/exams/exam-results.test.ts b/src/api/__tests__/exams/exam-results.test.ts index 1c3c044..22d0e64 100644 --- a/src/api/__tests__/exams/exam-results.test.ts +++ b/src/api/__tests__/exams/exam-results.test.ts @@ -5,6 +5,7 @@ import { postExamResult } from "../../exams"; import { deleteExamResultByUserId } from "../../exams"; import { getExamStatistics } from "../../exams"; import { getStudentAnswers, getStudentAnswerImage } from "../../exams"; +import { getStudentAttempts, getStudentAttemptAnswers, getStudentAttemptAnswerImage, getStudentMarksHistory } from "../../exams"; import { lastFetchCall, mockFetch204, mockFetchBlob, mockFetchSuccess } from "../helpers/mock-fetch"; describe("exams API - results", () => { @@ -95,4 +96,49 @@ describe("exams API - results", () => { const [url] = lastFetchCall(); expect(url).toBe("/api/exams/ex1/attempts/u1/answers/q1/image"); }); + + it("getStudentAttempts calls /exams/:id/students/:userId/attempts", async () => { + mockFetchSuccess([1, 2]); + + const result = await getStudentAttempts("ex1", "u1"); + expect(result).toEqual([1, 2]); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/exams/ex1/students/u1/attempts"); + expect(init?.method).toBe("GET"); + }); + + it("getStudentAttemptAnswers calls the per-seq answers route", async () => { + const mockAnswers = { exam: "ex1", answers: [] }; + mockFetchSuccess(mockAnswers); + + const result = await getStudentAttemptAnswers("ex1", "u1", 2); + expect(result).toEqual(mockAnswers); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/exams/ex1/students/u1/attempts/2/answers"); + expect(init?.method).toBe("GET"); + }); + + it("getStudentAttemptAnswerImage calls the per-seq answer-image route", async () => { + mockFetchBlob(new Blob([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], { type: "image/png" })); + + const result = await getStudentAttemptAnswerImage("ex1", "u1", 2, "q1"); + expect(result).toBeInstanceOf(Blob); + + const [url] = lastFetchCall(); + expect(url).toBe("/api/exams/ex1/students/u1/attempts/2/answers/q1/image"); + }); + + it("getStudentMarksHistory calls /exams/:id/students/:userId/marks", async () => { + const mockMarks = [{ id: "r1", mark: 60 }, { id: "r2", mark: 80 }]; + mockFetchSuccess(mockMarks); + + const result = await getStudentMarksHistory("ex1", "u1"); + expect(result).toEqual(mockMarks); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/exams/ex1/students/u1/marks"); + expect(init?.method).toBe("GET"); + }); }); diff --git a/src/api/exams/getStudentAttemptAnswerImage.ts b/src/api/exams/getStudentAttemptAnswerImage.ts new file mode 100644 index 0000000..de4df42 --- /dev/null +++ b/src/api/exams/getStudentAttemptAnswerImage.ts @@ -0,0 +1,12 @@ +import { blobClient } from "../client"; + +/** Grader view of a specific past sitting's answer image (mirrors getStudentAnswerImage, scoped to a seq). */ +export async function getStudentAttemptAnswerImage( + examId: string, + userId: string, + seq: number, + questionId: string, + signal?: AbortSignal, +): Promise { + return blobClient(`/exams/${examId}/students/${userId}/attempts/${seq}/answers/${questionId}/image`, signal); +} diff --git a/src/api/exams/getStudentAttemptAnswers.ts b/src/api/exams/getStudentAttemptAnswers.ts new file mode 100644 index 0000000..52c442e --- /dev/null +++ b/src/api/exams/getStudentAttemptAnswers.ts @@ -0,0 +1,15 @@ +import { client } from "@/api/client"; +import type { StudentAnswerSheet } from "@/api/client"; + +/** Grader view of a specific past sitting's answer sheet (same shape as the current attempt). */ +export async function getStudentAttemptAnswers( + examId: string, + userId: string, + seq: number, + signal?: AbortSignal, +): Promise { + return client( + `/exams/${encodeURIComponent(examId)}/students/${encodeURIComponent(userId)}/attempts/${seq}/answers`, + { signal }, + ); +} diff --git a/src/api/exams/getStudentAttempts.ts b/src/api/exams/getStudentAttempts.ts new file mode 100644 index 0000000..58293dc --- /dev/null +++ b/src/api/exams/getStudentAttempts.ts @@ -0,0 +1,6 @@ +import { client } from "@/api/client"; + +/** Sitting seqs a student has for this exam, ascending (e.g. [1, 2]). Grader-only. */ +export async function getStudentAttempts(examId: string, userId: string, signal?: AbortSignal): Promise { + return client(`/exams/${encodeURIComponent(examId)}/students/${encodeURIComponent(userId)}/attempts`, { signal }); +} diff --git a/src/api/exams/getStudentMarksHistory.ts b/src/api/exams/getStudentMarksHistory.ts new file mode 100644 index 0000000..1f04d4a --- /dev/null +++ b/src/api/exams/getStudentMarksHistory.ts @@ -0,0 +1,11 @@ +import { client } from "../client"; +import type { ExamResult } from "../client"; + +/** Full mark history for a student, oldest-first — index 0 = seq 1 (marks carry no seq). Grader-only. */ +export async function getStudentMarksHistory( + examId: string, + userId: string, + signal?: AbortSignal, +): Promise { + return client(`/exams/${encodeURIComponent(examId)}/students/${encodeURIComponent(userId)}/marks`, { signal }); +} diff --git a/src/api/exams/index.ts b/src/api/exams/index.ts index e9f193c..8ed4473 100644 --- a/src/api/exams/index.ts +++ b/src/api/exams/index.ts @@ -27,3 +27,7 @@ export * from "./postExamAttemptAnswerImage"; export * from "./deleteExamAttemptAnswerImage"; export * from "./getExamAnswerImageBlob"; export * from "./getStudentAnswerImage"; +export * from "./getStudentAttempts"; +export * from "./getStudentAttemptAnswers"; +export * from "./getStudentAttemptAnswerImage"; +export * from "./getStudentMarksHistory"; diff --git a/src/components/exams/answer-sheet-view.tsx b/src/components/exams/answer-sheet-view.tsx index 64d9e70..a5d358c 100644 --- a/src/components/exams/answer-sheet-view.tsx +++ b/src/components/exams/answer-sheet-view.tsx @@ -1,7 +1,8 @@ -import { For, Show, Suspense, createResource } from "solid-js"; +import { For, Show, Suspense, createResource, createSignal } from "solid-js"; import { ApiError } from "@/api/client"; import { getExamQuestions } from "@/api/exams"; import { getStudentAnswers, getStudentAnswerImage } from "@/api/exams"; +import { getStudentAttempts, getStudentAttemptAnswers, getStudentAttemptAnswerImage, getStudentMarksHistory } from "@/api/exams"; import type { StudentAnswerSheet } from "@/api/client"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; @@ -15,13 +16,71 @@ import { useT } from "@/stores/preferences-context"; export function AnswerSheetView(props: { examId: string; userId: string }) { const t = useT(); - const [data] = createResource( + + // Which sitting the grader is viewing. null = the latest (grade-of-record), the default. + const [selectedSeq, setSelectedSeq] = createSignal(null); + + // Attempt list + full mark history (oldest-first, index i = seq i+1). Both grader-only, + // both tolerate "student never sat" by degrading to empty. + const [attempts] = createResource( + () => [props.examId, props.userId] as const, + async ([examId, userId]) => { + try { + return await getStudentAttempts(examId, userId); + } catch (err) { + if (err instanceof ApiError && err.status === 404) return [] as number[]; + throw err; + } + }, + ); + const [marks] = createResource( () => [props.examId, props.userId] as const, async ([examId, userId]) => { + try { + return await getStudentMarksHistory(examId, userId); + } catch (err) { + if (err instanceof ApiError && err.status === 404) return []; + throw err; + } + }, + ); + + const latestSeq = () => { + const a = attempts(); + return a && a.length ? a[a.length - 1] : null; + }; + // Effective seq being shown, and whether it's the current (re-gradable) sitting. + const activeSeq = () => selectedSeq() ?? latestSeq(); + const isLatest = () => { + const s = activeSeq(); + return s == null || s === latestSeq(); + }; + const markForSeq = (seq: number) => marks()?.[seq - 1]?.mark ?? null; + + const answerImageUrl = (questionId: string) => { + const seq = activeSeq(); + return isLatest() || seq == null + ? `/api/exams/${props.examId}/attempts/${props.userId}/answers/${questionId}/image` + : `/api/exams/${props.examId}/students/${props.userId}/attempts/${seq}/answers/${questionId}/image`; + }; + const fetchAnswerImage = (questionId: string) => { + const seq = activeSeq(); + return isLatest() || seq == null + ? getStudentAnswerImage(props.examId, props.userId, questionId) + : getStudentAttemptAnswerImage(props.examId, props.userId, seq, questionId); + }; + + const [data] = createResource( + // Re-fetch when the grader switches attempt. Latest uses the current-attempt route + // (unchanged from before); an older seq uses the per-seq history route. + () => [props.examId, props.userId, activeSeq(), isLatest()] as const, + async ([examId, userId, seq, latest]) => { const questions = await getExamQuestions(examId); let sheet: StudentAnswerSheet; try { - sheet = await getStudentAnswers(examId, userId); + sheet = latest || seq == null + ? await getStudentAnswers(examId, userId) + : await getStudentAttemptAnswers(examId, userId, seq); } catch (err) { if (err instanceof ApiError && err.status === 404) { sheet = { @@ -40,9 +99,36 @@ export function AnswerSheetView(props: { examId: string; userId: string }) { return ( }> + 1}> +
+ {t("exams.previousAttempts")} + + {(seq) => ( + + )} + +
+
{(d) => (
+ +

{t("exams.pastAttemptReadOnly")}

+

{personLabelWithId(d().sheet.user)}

0} fallback={{t("exams.notStarted")}}> @@ -86,12 +172,12 @@ export function AnswerSheetView(props: { examId: string; userId: string }) {
getStudentAnswerImage(d().sheet.exam, d().sheet.user.id, row.question.id)} - src={`/api/exams/${d().sheet.exam}/attempts/${d().sheet.user.id}/answers/${row.question.id}/image`} + fetchBlob={() => fetchAnswerImage(row.question.id)} + src={answerImageUrl(row.question.id)} alt={t("exams.drawAnswer")} imgClass="h-64 w-full max-w-2xl rounded-md border bg-background object-contain" /> - +