✨ finished exam final state, restrictions, status badges & course accordion
Browse files at this commite37d07827e293f03bb5d3b9846b3e64908b4cc42Parent: 74200be- feat
- ✨ finished exam final state, restrictions, status badges & course accordion
22 changed files +1546 -59
- src/api/getExamLive.ts +6 -0
- src/api/getExamStatistics.ts +6 -0
- src/api/getStudentAnswers.ts +6 -0
- src/api/getTime.ts +5 -0
- src/api/getUserById.ts +6 -0
- src/api/patchMe.ts +6 -0
- src/api/patchUserProfile.ts +6 -0
- src/api/types.ts +64 -0
- src/components/exams/answer-sheet-view.tsx +88 -0
- src/components/exams/exam-card.tsx +48 -7
- src/components/exams/exam-questions-panel.tsx +25 -21
- src/components/exams/exam-room-ws.tsx +478 -0
- src/components/layout/user-menu.tsx +8 -1
- src/components/users/profile-form.tsx +104 -0
- src/i18n/messages.ts +130 -1
- src/pages/exam-detail-page.tsx +121 -17
- src/pages/exam-room-page.tsx +2 -2
- src/pages/exams-page.tsx +61 -9
- src/pages/live-monitor-page.tsx +281 -0
- src/pages/profile-page.tsx +78 -0
- src/routes/router.tsx +16 -0
- vite.config.ts +1 -1
src/api/getExamLive.tsadded+6 -0
| @@ -0,0 +1,6 @@ | |||
| 1 | +import { client } from "@/api/client"; | ||
| 2 | +import type { LiveMonitor } from "@/api/types"; | ||
| 3 | + | ||
| 4 | +export async function getExamLive(examId: string, signal?: AbortSignal): Promise<LiveMonitor> { | ||
| 5 | + return client<LiveMonitor>(`/exams/${encodeURIComponent(examId)}/live`, { signal }); | ||
| 6 | +} | ||
src/api/getExamStatistics.tsadded+6 -0
| @@ -0,0 +1,6 @@ | |||
| 1 | +import { client } from "@/api/client"; | ||
| 2 | +import type { ExamStatistics } from "@/api/types"; | ||
| 3 | + | ||
| 4 | +export async function getExamStatistics(examId: string, signal?: AbortSignal): Promise<ExamStatistics> { | ||
| 5 | + return client<ExamStatistics>(`/exams/${encodeURIComponent(examId)}/statistics`, { signal }); | ||
| 6 | +} | ||
src/api/getStudentAnswers.tsadded+6 -0
| @@ -0,0 +1,6 @@ | |||
| 1 | +import { client } from "@/api/client"; | ||
| 2 | +import type { StudentAnswerSheet } from "@/api/types"; | ||
| 3 | + | ||
| 4 | +export async function getStudentAnswers(examId: string, userId: string, signal?: AbortSignal): Promise<StudentAnswerSheet> { | ||
| 5 | + return client<StudentAnswerSheet>(`/exams/${encodeURIComponent(examId)}/attempts/${encodeURIComponent(userId)}/answers`, { signal }); | ||
| 6 | +} | ||
src/api/getTime.tsadded+5 -0
| @@ -0,0 +1,5 @@ | |||
| 1 | +import { client } from "@/api/client"; | ||
| 2 | + | ||
| 3 | +export async function getTime(signal?: AbortSignal): Promise<{ now: number }> { | ||
| 4 | + return client<{ now: number }>("/time", { signal }); | ||
| 5 | +} | ||
src/api/getUserById.tsadded+6 -0
| @@ -0,0 +1,6 @@ | |||
| 1 | +import { client } from "@/api/client"; | ||
| 2 | +import type { User } from "@/api/types"; | ||
| 3 | + | ||
| 4 | +export async function getUserById(id: string, signal?: AbortSignal): Promise<User> { | ||
| 5 | + return client<User>(`/users/${encodeURIComponent(id)}`, { signal }); | ||
| 6 | +} | ||
src/api/patchMe.tsadded+6 -0
| @@ -0,0 +1,6 @@ | |||
| 1 | +import { client } from "@/api/client"; | ||
| 2 | +import type { ProfileUpdate } from "@/api/types"; | ||
| 3 | + | ||
| 4 | +export async function patchMe(body: ProfileUpdate): Promise<void> { | ||
| 5 | + await client("/auth/me", { method: "PATCH", body }); | ||
| 6 | +} | ||
src/api/patchUserProfile.tsadded+6 -0
| @@ -0,0 +1,6 @@ | |||
| 1 | +import { client } from "@/api/client"; | ||
| 2 | +import type { ProfileUpdate } from "@/api/types"; | ||
| 3 | + | ||
| 4 | +export async function patchUserProfile(id: string, body: ProfileUpdate): Promise<void> { | ||
| 5 | + await client(`/users/${encodeURIComponent(id)}/profile`, { method: "PATCH", body }); | ||
| 6 | +} | ||
src/api/types.tsmodified+64 -0
| @@ -9,6 +9,19 @@ export type User = { | |||
| 9 | id: string; | 9 | id: string; |
| 10 | username: string; | 10 | username: string; |
| 11 | role: Role; | 11 | role: Role; |
| 12 | + name: string | null; | ||
| 13 | + surname: string | null; | ||
| 14 | + email: string | null; | ||
| 15 | + phone: string | null; | ||
| 16 | + birth_date: string | null; | ||
| 17 | +}; | ||
| 18 | + | ||
| 19 | +export type ProfileUpdate = { | ||
| 20 | + name?: string | null; | ||
| 21 | + surname?: string | null; | ||
| 22 | + email?: string | null; | ||
| 23 | + phone?: string | null; | ||
| 24 | + birth_date?: string | null; | ||
| 12 | }; | 25 | }; |
| 13 | 26 | ||
| 14 | export type PersonRef = { | 27 | export type PersonRef = { |
| @@ -136,6 +149,57 @@ export type MarksReport = { | |||
| 136 | overall_average: number | null; | 149 | overall_average: number | null; |
| 137 | }; | 150 | }; |
| 138 | 151 | ||
| 152 | +export type ExamStatistics = { | ||
| 153 | + graded: number; | ||
| 154 | + average: number | null; | ||
| 155 | + min: number | null; | ||
| 156 | + max: number | null; | ||
| 157 | +}; | ||
| 158 | + | ||
| 159 | +export type GradedAnswer = { | ||
| 160 | + question_id: string; | ||
| 161 | + text: string; | ||
| 162 | + kind: QuestionKind; | ||
| 163 | + points: number; | ||
| 164 | + choices: string[] | null; | ||
| 165 | + correct: number | null; | ||
| 166 | + selected: number | null; | ||
| 167 | + text_answer: string | null; | ||
| 168 | + is_correct: boolean | null; | ||
| 169 | + auto_score: { earned: number; possible: number }; | ||
| 170 | +}; | ||
| 171 | + | ||
| 172 | +export type StudentAnswerSheet = { | ||
| 173 | + user: string; | ||
| 174 | + answers: GradedAnswer[]; | ||
| 175 | + auto_score: { earned: number; possible: number }; | ||
| 176 | +}; | ||
| 177 | + | ||
| 178 | +export type LiveRosterEntry = { | ||
| 179 | + user: string; | ||
| 180 | + username: string; | ||
| 181 | + display_name: string | null; | ||
| 182 | + status: AttemptStatus | "not_started"; | ||
| 183 | + deadline: number | null; | ||
| 184 | + remaining_ms: number; | ||
| 185 | + mark: number | null; | ||
| 186 | + answered: number; | ||
| 187 | + last_activity: number | null; | ||
| 188 | +}; | ||
| 189 | + | ||
| 190 | +export type LiveMonitor = { | ||
| 191 | + exam: string; | ||
| 192 | + now: number; | ||
| 193 | + question_count: number; | ||
| 194 | + students: LiveRosterEntry[]; | ||
| 195 | + counts: { | ||
| 196 | + not_started: number; | ||
| 197 | + in_progress: number; | ||
| 198 | + submitted: number; | ||
| 199 | + expired: number; | ||
| 200 | + }; | ||
| 201 | +}; | ||
| 202 | + | ||
| 139 | export const EXAM_KINDS: ExamKind[] = [ | 203 | export const EXAM_KINDS: ExamKind[] = [ |
| 140 | "homework", | 204 | "homework", |
| 141 | "quiz", | 205 | "quiz", |
src/components/exams/answer-sheet-view.tsxadded+88 -0
| @@ -0,0 +1,88 @@ | |||
| 1 | +import { For, Show, Suspense, createResource } from "solid-js"; | ||
| 2 | +import { getStudentAnswers } from "@/api/getStudentAnswers"; | ||
| 3 | +import { Badge } from "@/components/ui/badge"; | ||
| 4 | +import { PageSpinner } from "@/components/ui/page-spinner"; | ||
| 5 | +import { useT } from "@/stores/preferences-context"; | ||
| 6 | + | ||
| 7 | +export function AnswerSheetView(props: { examId: string; userId: string }) { | ||
| 8 | + const t = useT(); | ||
| 9 | + const [sheet] = createResource( | ||
| 10 | + () => [props.examId, props.userId] as const, | ||
| 11 | + async ([examId, userId]) => getStudentAnswers(examId, userId), | ||
| 12 | + ); | ||
| 13 | + | ||
| 14 | + return ( | ||
| 15 | + <Suspense fallback={<PageSpinner />}> | ||
| 16 | + <Show when={sheet()}> | ||
| 17 | + {(s) => ( | ||
| 18 | + <div class="space-y-4"> | ||
| 19 | + <div class="flex flex-wrap items-center justify-between gap-2 rounded-md border bg-muted/30 px-4 py-3"> | ||
| 20 | + <p class="text-sm font-medium">{props.userId}</p> | ||
| 21 | + <Badge variant="secondary"> | ||
| 22 | + {t("exams.autoScore")}: {s().auto_score.earned}/{s().auto_score.possible} | ||
| 23 | + </Badge> | ||
| 24 | + </div> | ||
| 25 | + | ||
| 26 | + <For each={s().answers}> | ||
| 27 | + {(answer, idx) => ( | ||
| 28 | + <div class="rounded-md border p-4"> | ||
| 29 | + <div class="mb-2 flex flex-wrap items-center gap-2"> | ||
| 30 | + <span class="text-xs font-semibold text-muted-foreground">#{idx() + 1}</span> | ||
| 31 | + <Badge variant="outline">{answer.points} {t("questions.points")}</Badge> | ||
| 32 | + <Badge variant={answer.is_correct === true ? "default" : answer.is_correct === false ? "destructive" : "outline"}> | ||
| 33 | + {answer.is_correct === true ? t("exams.isCorrect") : | ||
| 34 | + answer.is_correct === false ? "✗" : | ||
| 35 | + answer.kind === "text" ? t("questions.kind.text") : "—"} | ||
| 36 | + </Badge> | ||
| 37 | + <Badge variant="outline">{t("exams.autoScore")}: {answer.auto_score.earned}/{answer.auto_score.possible}</Badge> | ||
| 38 | + </div> | ||
| 39 | + <p class="mb-3 whitespace-pre-wrap text-sm font-medium">{answer.text}</p> | ||
| 40 | + | ||
| 41 | + <Show | ||
| 42 | + when={answer.kind === "choice"} | ||
| 43 | + fallback={ | ||
| 44 | + <div class="rounded-sm bg-muted/40 p-3"> | ||
| 45 | + <p class="text-xs text-muted-foreground">{t("exams.textAnswer")}</p> | ||
| 46 | + <p class="mt-1 whitespace-pre-wrap text-sm">{answer.text_answer || "—"}</p> | ||
| 47 | + </div> | ||
| 48 | + } | ||
| 49 | + > | ||
| 50 | + <div class="space-y-1.5"> | ||
| 51 | + <For each={answer.choices}> | ||
| 52 | + {(choice, ci) => ( | ||
| 53 | + <div | ||
| 54 | + class={`flex items-center gap-2 rounded-sm border px-3 py-2 text-sm ${ | ||
| 55 | + ci() === answer.correct && ci() === answer.selected | ||
| 56 | + ? "border-green-500 bg-green-50 dark:bg-green-950/30" | ||
| 57 | + : ci() === answer.correct | ||
| 58 | + ? "border-green-300 bg-green-50/50 dark:bg-green-950/20" | ||
| 59 | + : ci() === answer.selected | ||
| 60 | + ? "border-destructive bg-destructive/10" | ||
| 61 | + : "border-border" | ||
| 62 | + }`} | ||
| 63 | + > | ||
| 64 | + <span class="flex h-5 w-5 shrink-0 items-center justify-center rounded-[3px] border text-[11px] font-medium"> | ||
| 65 | + {ci() === answer.correct || ci() === answer.selected | ||
| 66 | + ? ci() === answer.correct && ci() === answer.selected | ||
| 67 | + ? "✓" | ||
| 68 | + : ci() === answer.correct | ||
| 69 | + ? "✓" | ||
| 70 | + : "✗" | ||
| 71 | + : String.fromCharCode(65 + ci())} | ||
| 72 | + </span> | ||
| 73 | + <span>{choice}</span> | ||
| 74 | + {ci() === answer.correct && <Badge variant="outline" class="ml-auto text-[10px]">{t("questions.correct")}</Badge>} | ||
| 75 | + </div> | ||
| 76 | + )} | ||
| 77 | + </For> | ||
| 78 | + </div> | ||
| 79 | + </Show> | ||
| 80 | + </div> | ||
| 81 | + )} | ||
| 82 | + </For> | ||
| 83 | + </div> | ||
| 84 | + )} | ||
| 85 | + </Show> | ||
| 86 | + </Suspense> | ||
| 87 | + ); | ||
| 88 | +} | ||
src/components/exams/exam-card.tsxmodified+48 -7
| @@ -1,11 +1,13 @@ | |||
| 1 | +import { Show } from "solid-js"; | ||
| 1 | import { Link } from "@tanstack/solid-router"; | 2 | import { Link } from "@tanstack/solid-router"; |
| 2 | import type { Exam } from "@/api/types"; | 3 | import type { Exam } from "@/api/types"; |
| 3 | import { Badge } from "@/components/ui/badge"; | 4 | import { Badge } from "@/components/ui/badge"; |
| 5 | +import { cn } from "@/lib/cn"; | ||
| 4 | import { examKindLabel } from "@/lib/exam-labels"; | 6 | import { examKindLabel } from "@/lib/exam-labels"; |
| 5 | import { examDurationMs, formatDateTime, formatDurationMinutes } from "@/lib/format"; | 7 | import { examDurationMs, formatDateTime, formatDurationMinutes } from "@/lib/format"; |
| 6 | import { usePreferences, useT } from "@/stores/preferences-context"; | 8 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 7 | 9 | ||
| 8 | -export function ExamCard(props: { exam: Exam }) { | 10 | +export function ExamCard(props: { exam: Exam; courseTitle?: string }) { |
| 9 | const t = useT(); | 11 | const t = useT(); |
| 10 | const { locale } = usePreferences(); | 12 | const { locale } = usePreferences(); |
| 11 | const modeLabel = () => { | 13 | const modeLabel = () => { |
| @@ -14,16 +16,55 @@ export function ExamCard(props: { exam: Exam }) { | |||
| 14 | return t("exams.unscheduled"); | 16 | return t("exams.unscheduled"); |
| 15 | }; | 17 | }; |
| 16 | 18 | ||
| 19 | + const status = () => { | ||
| 20 | + const now = Date.now(); | ||
| 21 | + if (props.exam.ends_at != null && props.exam.ends_at < now) return "finished"; | ||
| 22 | + if (props.exam.starts_at != null && props.exam.starts_at > now) return "upcoming"; | ||
| 23 | + return "active"; | ||
| 24 | + }; | ||
| 25 | + | ||
| 26 | + const statusLabel = () => { | ||
| 27 | + const s = status(); | ||
| 28 | + if (s === "finished") return t("exams.finished"); | ||
| 29 | + if (s === "upcoming") return t("exams.upcoming"); | ||
| 30 | + return t("exams.active"); | ||
| 31 | + }; | ||
| 32 | + | ||
| 33 | + const statusClass = () => { | ||
| 34 | + const s = status(); | ||
| 35 | + if (s === "finished") return "bg-muted text-muted-foreground border-muted"; | ||
| 36 | + if (s === "upcoming") return "bg-amber-500/15 text-amber-600 border-amber-500/30"; | ||
| 37 | + return "bg-emerald-500/15 text-emerald-600 border-emerald-500/30"; | ||
| 38 | + }; | ||
| 39 | + | ||
| 40 | + const statusDot = () => { | ||
| 41 | + const s = status(); | ||
| 42 | + if (s === "finished") return "bg-muted-foreground"; | ||
| 43 | + if (s === "upcoming") return "bg-amber-600"; | ||
| 44 | + return "bg-emerald-600"; | ||
| 45 | + }; | ||
| 46 | + | ||
| 17 | return ( | 47 | return ( |
| 18 | <Link to="/exams/$id" params={{ id: props.exam.id }} class="group block h-full"> | 48 | <Link to="/exams/$id" params={{ id: props.exam.id }} class="group block h-full"> |
| 19 | <article class="surface-card relative flex h-full flex-col overflow-hidden transition-all group-hover:-translate-y-0.5 group-hover:border-rose-500/30"> | 49 | <article class="surface-card relative flex h-full flex-col overflow-hidden transition-all group-hover:-translate-y-0.5 group-hover:border-rose-500/30"> |
| 20 | <div class="flex items-start justify-between gap-3 border-b border-border/50 bg-gradient-to-br from-rose-500/10 via-transparent to-transparent p-5"> | 50 | <div class="flex items-start justify-between gap-3 border-b border-border/50 bg-gradient-to-br from-rose-500/10 via-transparent to-transparent p-5"> |
| 21 | - <h3 class="font-display text-lg font-semibold leading-snug group-hover:text-primary"> | 51 | + <div class="min-w-0 flex-1"> |
| 22 | - {props.exam.title} | 52 | + <Show when={props.courseTitle}> |
| 23 | - </h3> | 53 | + <p class="mb-0.5 truncate text-xs text-muted-foreground">{props.courseTitle}</p> |
| 24 | - <Badge variant="outline" class="rounded-sm capitalize"> | 54 | + </Show> |
| 25 | - {examKindLabel(String(props.exam.kind), t)} | 55 | + <h3 class="font-display text-lg font-semibold leading-snug group-hover:text-primary"> |
| 26 | - </Badge> | 56 | + {props.exam.title} |
| 57 | + </h3> | ||
| 58 | + </div> | ||
| 59 | + <div class="flex shrink-0 flex-col items-stretch gap-1.5"> | ||
| 60 | + <Badge variant="outline" class={cn("rounded-sm capitalize w-full text-right", statusClass())}> | ||
| 61 | + <span class={cn("mr-1.5 inline-block h-1.5 w-1.5 rounded-full", statusDot())} /> | ||
| 62 | + {statusLabel()} | ||
| 63 | + </Badge> | ||
| 64 | + <Badge variant="outline" class="rounded-sm capitalize w-full text-right"> | ||
| 65 | + {examKindLabel(String(props.exam.kind), t)} | ||
| 66 | + </Badge> | ||
| 67 | + </div> | ||
| 27 | </div> | 68 | </div> |
| 28 | <div class="p-5"> | 69 | <div class="p-5"> |
| 29 | <p class="line-clamp-3 text-sm text-muted-foreground"> | 70 | <p class="line-clamp-3 text-sm text-muted-foreground"> |
src/components/exams/exam-questions-panel.tsxmodified+25 -21
| @@ -25,7 +25,7 @@ type QuestionValues = { | |||
| 25 | correct: number | null; | 25 | correct: number | null; |
| 26 | }; | 26 | }; |
| 27 | 27 | ||
| 28 | -export function ExamQuestionsPanel(props: { examId: string }) { | 28 | +export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean }) { |
| 29 | const t = useT(); | 29 | const t = useT(); |
| 30 | const [questions, { refetch }] = createResource(() => props.examId, async (examId) => { | 30 | const [questions, { refetch }] = createResource(() => props.examId, async (examId) => { |
| 31 | try { | 31 | try { |
| @@ -63,18 +63,20 @@ export function ExamQuestionsPanel(props: { examId: string }) { | |||
| 63 | <section class="surface-card space-y-4 p-5"> | 63 | <section class="surface-card space-y-4 p-5"> |
| 64 | <div class="flex flex-wrap items-center justify-between gap-2"> | 64 | <div class="flex flex-wrap items-center justify-between gap-2"> |
| 65 | <h2 class="font-display text-lg font-semibold">{t("questions.title")}</h2> | 65 | <h2 class="font-display text-lg font-semibold">{t("questions.title")}</h2> |
| 66 | - <Button | 66 | + <Show when={!props.readOnly}> |
| 67 | - type="button" | 67 | + <Button |
| 68 | - variant="outline" | 68 | + type="button" |
| 69 | - size="sm" | 69 | + variant="outline" |
| 70 | - onClick={() => { | 70 | + size="sm" |
| 71 | - setEditing(null); | 71 | + onClick={() => { |
| 72 | - setShowForm((v) => !v); | 72 | + setEditing(null); |
| 73 | - }} | 73 | + setShowForm((v) => !v); |
| 74 | - > | 74 | + }} |
| 75 | - <IconPlus class="h-4 w-4" /> | 75 | + > |
| 76 | - {t("questions.add")} | 76 | + <IconPlus class="h-4 w-4" /> |
| 77 | - </Button> | 77 | + {t("questions.add")} |
| 78 | + </Button> | ||
| 79 | + </Show> | ||
| 78 | </div> | 80 | </div> |
| 79 | 81 | ||
| 80 | <Show when={showForm() || editing()}> | 82 | <Show when={showForm() || editing()}> |
| @@ -121,14 +123,16 @@ export function ExamQuestionsPanel(props: { examId: string }) { | |||
| 121 | </ol> | 123 | </ol> |
| 122 | </Show> | 124 | </Show> |
| 123 | </div> | 125 | </div> |
| 124 | - <div class="flex gap-2"> | 126 | + <Show when={!props.readOnly}> |
| 125 | - <Button type="button" variant="outline" size="sm" onClick={() => setEditing(q)}> | 127 | + <div class="flex gap-2"> |
| 126 | - {t("common.edit")} | 128 | + <Button type="button" variant="outline" size="sm" onClick={() => setEditing(q)}> |
| 127 | - </Button> | 129 | + {t("common.edit")} |
| 128 | - <Button type="button" variant="ghost" size="sm" class="text-destructive" onClick={() => setRemoveQuestion(q)}> | 130 | + </Button> |
| 129 | - <IconTrash class="h-4 w-4" /> | 131 | + <Button type="button" variant="ghost" size="sm" class="text-destructive" onClick={() => setRemoveQuestion(q)}> |
| 130 | - </Button> | 132 | + <IconTrash class="h-4 w-4" /> |
| 131 | - </div> | 133 | + </Button> |
| 134 | + </div> | ||
| 135 | + </Show> | ||
| 132 | </div> | 136 | </div> |
| 133 | </li> | 137 | </li> |
| 134 | )} | 138 | )} |
src/components/exams/exam-room-ws.tsxadded+478 -0
| @@ -0,0 +1,478 @@ | |||
| 1 | +import { For, Show, Suspense, createEffect, createMemo, createSignal, onCleanup } from "solid-js"; | ||
| 2 | +import { getExamAttempt } from "@/api/getExamAttempt"; | ||
| 3 | +import { getExamAttemptQuestions } from "@/api/getExamAttemptQuestions"; | ||
| 4 | +import { postExamAttempt } from "@/api/postExamAttempt"; | ||
| 5 | +import { postExamAttemptAnswer } from "@/api/postExamAttemptAnswer"; | ||
| 6 | +import { postExamAttemptFinish } from "@/api/postExamAttemptFinish"; | ||
| 7 | +import { formatApiError } from "@/api/client"; | ||
| 8 | +import type { AttemptQuestion, Exam, ExamAttempt } from "@/api/types"; | ||
| 9 | +import { Badge } from "@/components/ui/badge"; | ||
| 10 | +import { Button } from "@/components/ui/button"; | ||
| 11 | +import { ConfirmDialog } from "@/components/ui/confirm-dialog"; | ||
| 12 | +import { PageSpinner } from "@/components/ui/page-spinner"; | ||
| 13 | +import { Textarea } from "@/components/ui/textarea"; | ||
| 14 | +import { formatDateTime } from "@/lib/format"; | ||
| 15 | +import { usePreferences, useT } from "@/stores/preferences-context"; | ||
| 16 | + | ||
| 17 | +type WsState = "connecting" | "connected" | "disconnected"; | ||
| 18 | +type WsMessage = | ||
| 19 | + | { type: "state"; status: string; deadline: number | null; remaining_ms: number; now: number; answered: number; question_count: number } | ||
| 20 | + | { type: "saved"; question_id: string; updated_at: number } | ||
| 21 | + | { type: "finished"; finished_at: number } | ||
| 22 | + | { type: "expired" } | ||
| 23 | + | { type: "pong" } | ||
| 24 | + | { type: "error"; message: string }; | ||
| 25 | + | ||
| 26 | +function formatRemaining(ms: number): string { | ||
| 27 | + const safe = Math.max(0, ms); | ||
| 28 | + const totalSeconds = Math.floor(safe / 1000); | ||
| 29 | + const hours = Math.floor(totalSeconds / 3600); | ||
| 30 | + const minutes = Math.floor((totalSeconds % 3600) / 60); | ||
| 31 | + const seconds = totalSeconds % 60; | ||
| 32 | + return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`; | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +export function ExamRoomWS(props: { exam: Exam }) { | ||
| 36 | + const t = useT(); | ||
| 37 | + const [error, setError] = createSignal(""); | ||
| 38 | + const [pending, setPending] = createSignal(false); | ||
| 39 | + const [finishOpen, setFinishOpen] = createSignal(false); | ||
| 40 | + const [roomOpen, setRoomOpen] = createSignal(false); | ||
| 41 | + const [attempt, setAttempt] = createSignal<ExamAttempt | null>(null); | ||
| 42 | + const [questions, setQuestions] = createSignal<AttemptQuestion[]>([]); | ||
| 43 | + const [remainingMs, setRemainingMs] = createSignal(0); | ||
| 44 | + const [wsState, setWsState] = createSignal<WsState>("disconnected"); | ||
| 45 | + const scheduled = createMemo(() => props.exam.mode === "sync" || props.exam.mode === "async"); | ||
| 46 | + const canWrite = createMemo(() => attempt()?.status === "in_progress" && remainingMs() > 0); | ||
| 47 | + | ||
| 48 | + let ws: WebSocket | null = null; | ||
| 49 | + | ||
| 50 | + const wsUrl = () => { | ||
| 51 | + const proto = window.location.protocol === "https:" ? "wss:" : "ws:"; | ||
| 52 | + return `${proto}//${window.location.host}/exams/${encodeURIComponent(props.exam.id)}/attempt/ws`; | ||
| 53 | + }; | ||
| 54 | + | ||
| 55 | + const connectWs = () => { | ||
| 56 | + if (ws) ws.close(); | ||
| 57 | + setWsState("connecting"); | ||
| 58 | + const socket = new WebSocket(wsUrl()); | ||
| 59 | + ws = socket; | ||
| 60 | + | ||
| 61 | + socket.onopen = () => setWsState("connected"); | ||
| 62 | + socket.onclose = () => { | ||
| 63 | + setWsState("disconnected"); | ||
| 64 | + ws = null; | ||
| 65 | + }; | ||
| 66 | + socket.onerror = () => { | ||
| 67 | + setWsState("disconnected"); | ||
| 68 | + ws = null; | ||
| 69 | + }; | ||
| 70 | + | ||
| 71 | + socket.onmessage = (event) => { | ||
| 72 | + try { | ||
| 73 | + const msg: WsMessage = JSON.parse(event.data); | ||
| 74 | + handleWsMessage(msg); | ||
| 75 | + } catch { | ||
| 76 | + // ignore | ||
| 77 | + } | ||
| 78 | + }; | ||
| 79 | + }; | ||
| 80 | + | ||
| 81 | + const handleWsMessage = (msg: WsMessage) => { | ||
| 82 | + switch (msg.type) { | ||
| 83 | + case "state": { | ||
| 84 | + setAttempt((prev) => | ||
| 85 | + prev | ||
| 86 | + ? { ...prev, status: msg.status as any, deadline: msg.deadline, remaining_ms: msg.remaining_ms, answered: msg.answered, question_count: msg.question_count, now: msg.now } | ||
| 87 | + : { | ||
| 88 | + id: undefined, | ||
| 89 | + exam: props.exam.id, | ||
| 90 | + status: msg.status as any, | ||
| 91 | + deadline: msg.deadline, | ||
| 92 | + remaining_ms: msg.remaining_ms, | ||
| 93 | + mark: null, | ||
| 94 | + answered: msg.answered, | ||
| 95 | + question_count: msg.question_count, | ||
| 96 | + now: msg.now, | ||
| 97 | + }, | ||
| 98 | + ); | ||
| 99 | + setRemainingMs(msg.remaining_ms); | ||
| 100 | + break; | ||
| 101 | + } | ||
| 102 | + case "saved": { | ||
| 103 | + setQuestions((prev) => | ||
| 104 | + prev.map((q) => | ||
| 105 | + q.id === msg.question_id | ||
| 106 | + ? { ...q, answer: { ...q.answer, updated_at: msg.updated_at } } | ||
| 107 | + : q, | ||
| 108 | + ), | ||
| 109 | + ); | ||
| 110 | + break; | ||
| 111 | + } | ||
| 112 | + case "finished": { | ||
| 113 | + setAttempt((prev) => (prev ? { ...prev, status: "submitted" as any, remaining_ms: 0 } : prev)); | ||
| 114 | + setRemainingMs(0); | ||
| 115 | + break; | ||
| 116 | + } | ||
| 117 | + case "expired": { | ||
| 118 | + setAttempt((prev) => (prev ? { ...prev, status: "expired" as any, remaining_ms: 0 } : prev)); | ||
| 119 | + setRemainingMs(0); | ||
| 120 | + break; | ||
| 121 | + } | ||
| 122 | + case "pong": { | ||
| 123 | + break; | ||
| 124 | + } | ||
| 125 | + case "error": { | ||
| 126 | + setError(msg.message); | ||
| 127 | + break; | ||
| 128 | + } | ||
| 129 | + } | ||
| 130 | + }; | ||
| 131 | + | ||
| 132 | + const sendWs = (data: unknown) => { | ||
| 133 | + if (ws && ws.readyState === WebSocket.OPEN) { | ||
| 134 | + ws.send(JSON.stringify(data)); | ||
| 135 | + } | ||
| 136 | + }; | ||
| 137 | + | ||
| 138 | + const start = async () => { | ||
| 139 | + setPending(true); | ||
| 140 | + setError(""); | ||
| 141 | + try { | ||
| 142 | + const next = await postExamAttempt(props.exam.id); | ||
| 143 | + setAttempt(next); | ||
| 144 | + setRoomOpen(true); | ||
| 145 | + const qs = await getExamAttemptQuestions(props.exam.id); | ||
| 146 | + setQuestions(qs); | ||
| 147 | + connectWs(); | ||
| 148 | + } catch (err) { | ||
| 149 | + setError(formatApiError(err)); | ||
| 150 | + } finally { | ||
| 151 | + setPending(false); | ||
| 152 | + } | ||
| 153 | + }; | ||
| 154 | + | ||
| 155 | + const resume = async () => { | ||
| 156 | + setRoomOpen(true); | ||
| 157 | + try { | ||
| 158 | + const next = await getExamAttempt(props.exam.id); | ||
| 159 | + setAttempt(next); | ||
| 160 | + const qs = await getExamAttemptQuestions(props.exam.id); | ||
| 161 | + setQuestions(qs); | ||
| 162 | + connectWs(); | ||
| 163 | + } catch (err) { | ||
| 164 | + setError(formatApiError(err)); | ||
| 165 | + } | ||
| 166 | + }; | ||
| 167 | + | ||
| 168 | + const saveAnswer = async (question: AttemptQuestion, value: string) => { | ||
| 169 | + setError(""); | ||
| 170 | + if (ws && ws.readyState === WebSocket.OPEN) { | ||
| 171 | + const payload: Record<string, unknown> = { type: "answer", question_id: question.id }; | ||
| 172 | + if (question.kind === "choice") { | ||
| 173 | + payload.selected = Number(value); | ||
| 174 | + } else { | ||
| 175 | + payload.text = value; | ||
| 176 | + } | ||
| 177 | + sendWs(payload); | ||
| 178 | + setQuestions((prev) => | ||
| 179 | + prev.map((q) => (q.id === question.id ? { ...q, answer: { ...q.answer, selected: question.kind === "choice" ? Number(value) : undefined, text: question.kind === "text" ? value : undefined } } : q)), | ||
| 180 | + ); | ||
| 181 | + } else { | ||
| 182 | + setPending(true); | ||
| 183 | + try { | ||
| 184 | + if (question.kind === "choice") { | ||
| 185 | + await postExamAttemptAnswer(props.exam.id, { question_id: question.id, selected: Number(value) }); | ||
| 186 | + } else { | ||
| 187 | + await postExamAttemptAnswer(props.exam.id, { question_id: question.id, text: value }); | ||
| 188 | + } | ||
| 189 | + const next = await getExamAttempt(props.exam.id); | ||
| 190 | + setAttempt(next); | ||
| 191 | + const qs = await getExamAttemptQuestions(props.exam.id); | ||
| 192 | + setQuestions(qs); | ||
| 193 | + } catch (err) { | ||
| 194 | + setError(formatApiError(err)); | ||
| 195 | + } finally { | ||
| 196 | + setPending(false); | ||
| 197 | + } | ||
| 198 | + } | ||
| 199 | + }; | ||
| 200 | + | ||
| 201 | + const finish = async () => { | ||
| 202 | + setPending(true); | ||
| 203 | + setError(""); | ||
| 204 | + try { | ||
| 205 | + if (ws && ws.readyState === WebSocket.OPEN) { | ||
| 206 | + sendWs({ type: "finish" }); | ||
| 207 | + } else { | ||
| 208 | + await postExamAttemptFinish(props.exam.id); | ||
| 209 | + const next = await getExamAttempt(props.exam.id); | ||
| 210 | + setAttempt(next); | ||
| 211 | + } | ||
| 212 | + } catch (err) { | ||
| 213 | + setError(formatApiError(err)); | ||
| 214 | + } finally { | ||
| 215 | + setPending(false); | ||
| 216 | + } | ||
| 217 | + }; | ||
| 218 | + | ||
| 219 | + onCleanup(() => { | ||
| 220 | + if (ws) ws.close(); | ||
| 221 | + }); | ||
| 222 | + | ||
| 223 | + createEffect(() => { | ||
| 224 | + const current = attempt(); | ||
| 225 | + if (!current || current.status !== "in_progress" || current.remaining_ms <= 0) return; | ||
| 226 | + const startedAt = Date.now(); | ||
| 227 | + const initial = current.remaining_ms; | ||
| 228 | + const timer = window.setInterval(() => { | ||
| 229 | + setRemainingMs(Math.max(0, initial - (Date.now() - startedAt))); | ||
| 230 | + }, 1000); | ||
| 231 | + onCleanup(() => window.clearInterval(timer)); | ||
| 232 | + }); | ||
| 233 | + | ||
| 234 | + return ( | ||
| 235 | + <section class="surface-card space-y-4 p-5"> | ||
| 236 | + <Show when={!roomOpen()}> | ||
| 237 | + <div class="flex flex-wrap items-center justify-between gap-3"> | ||
| 238 | + <div> | ||
| 239 | + <h2 class="font-display text-lg font-semibold">{t("attempt.title")}</h2> | ||
| 240 | + <p class="mt-1 text-sm text-muted-foreground"> | ||
| 241 | + {props.exam.mode === "sync" ? t("exams.mode.sync") : props.exam.mode === "async" ? t("exams.mode.async") : t("attempt.unscheduled")} | ||
| 242 | + </p> | ||
| 243 | + </div> | ||
| 244 | + <Show when={scheduled()} fallback={<Badge variant="outline">{t("attempt.unscheduled")}</Badge>}> | ||
| 245 | + <Show | ||
| 246 | + when={attempt()} | ||
| 247 | + fallback={ | ||
| 248 | + <Button type="button" disabled={pending()} onClick={() => void start()}> | ||
| 249 | + {t("attempt.start")} | ||
| 250 | + </Button> | ||
| 251 | + } | ||
| 252 | + > | ||
| 253 | + <Button type="button" variant="outline" disabled={pending()} onClick={() => void resume()}> | ||
| 254 | + {t("attempt.resume")} | ||
| 255 | + </Button> | ||
| 256 | + </Show> | ||
| 257 | + </Show> | ||
| 258 | + </div> | ||
| 259 | + </Show> | ||
| 260 | + | ||
| 261 | + {error() && <p class="rounded-sm bg-destructive/10 px-3 py-2 text-sm text-destructive">{error()}</p>} | ||
| 262 | + | ||
| 263 | + <Suspense fallback={<PageSpinner />}> | ||
| 264 | + <Show when={scheduled()}> | ||
| 265 | + <Show when={attempt()} fallback={<p class="rounded-sm bg-muted/40 px-3 py-4 text-sm text-muted-foreground">{t("attempt.notStarted")}</p>}> | ||
| 266 | + {(a) => <AttemptSummaryWS attempt={a()} remainingMs={remainingMs()} wsState={wsState()} />} | ||
| 267 | + </Show> | ||
| 268 | + </Show> | ||
| 269 | + | ||
| 270 | + <Show when={attempt() && roomOpen()}> | ||
| 271 | + <Show when={!canWrite()}> | ||
| 272 | + <p class="rounded-sm bg-muted/40 px-3 py-3 text-sm text-muted-foreground">{t("attempt.closed")}</p> | ||
| 273 | + </Show> | ||
| 274 | + <div class="grid items-start gap-4 lg:grid-cols-[minmax(0,1fr)_12rem] xl:grid-cols-[minmax(0,1fr)_13rem] 2xl:grid-cols-[minmax(0,1fr)_14rem]"> | ||
| 275 | + <div class="grid auto-rows-fr items-stretch gap-4 2xl:grid-cols-2"> | ||
| 276 | + <For each={questions()}> | ||
| 277 | + {(question, index) => ( | ||
| 278 | + <QuestionAnswerCardWS | ||
| 279 | + index={index() + 1} | ||
| 280 | + nextQuestionId={questions()[index() + 1]?.id} | ||
| 281 | + question={question} | ||
| 282 | + disabled={!canWrite() || pending()} | ||
| 283 | + onSave={(value) => saveAnswer(question, value)} | ||
| 284 | + /> | ||
| 285 | + )} | ||
| 286 | + </For> | ||
| 287 | + </div> | ||
| 288 | + <aside class="surface-card sticky top-4 space-y-3 p-4"> | ||
| 289 | + <h3 class="font-display text-sm font-semibold">{t("questions.title")}</h3> | ||
| 290 | + <div class="grid grid-cols-4 gap-2"> | ||
| 291 | + <For each={questions()}> | ||
| 292 | + {(question, index) => ( | ||
| 293 | + <a | ||
| 294 | + href={`#question-${question.id}`} | ||
| 295 | + class="inline-flex h-9 items-center justify-center rounded-sm border bg-background text-sm font-medium hover:bg-accent" | ||
| 296 | + > | ||
| 297 | + {index() + 1} | ||
| 298 | + </a> | ||
| 299 | + )} | ||
| 300 | + </For> | ||
| 301 | + </div> | ||
| 302 | + <Show when={canWrite()}> | ||
| 303 | + <Button type="button" variant="destructive" class="w-full" disabled={pending()} onClick={() => setFinishOpen(true)}> | ||
| 304 | + {t("attempt.finish")} | ||
| 305 | + </Button> | ||
| 306 | + </Show> | ||
| 307 | + </aside> | ||
| 308 | + </div> | ||
| 309 | + </Show> | ||
| 310 | + </Suspense> | ||
| 311 | + | ||
| 312 | + <ConfirmDialog | ||
| 313 | + open={finishOpen()} | ||
| 314 | + onOpenChange={setFinishOpen} | ||
| 315 | + title={t("attempt.finish")} | ||
| 316 | + summary={props.exam.title} | ||
| 317 | + onConfirm={finish} | ||
| 318 | + /> | ||
| 319 | + </section> | ||
| 320 | + ); | ||
| 321 | +} | ||
| 322 | + | ||
| 323 | +function AttemptSummaryWS(props: { attempt: ExamAttempt; remainingMs: number; wsState: WsState }) { | ||
| 324 | + const t = useT(); | ||
| 325 | + const { locale } = usePreferences(); | ||
| 326 | + const statusLabel = () => { | ||
| 327 | + if (props.attempt.status === "in_progress") return t("attempt.inProgress"); | ||
| 328 | + if (props.attempt.status === "submitted") return t("attempt.submitted"); | ||
| 329 | + if (props.attempt.status === "expired") return t("attempt.expired"); | ||
| 330 | + return props.attempt.status; | ||
| 331 | + }; | ||
| 332 | + const wsLabel = () => { | ||
| 333 | + if (props.wsState === "connecting") return t("ws.connecting"); | ||
| 334 | + if (props.wsState === "connected") return t("ws.connected"); | ||
| 335 | + return t("ws.disconnected"); | ||
| 336 | + }; | ||
| 337 | + return ( | ||
| 338 | + <div class="grid auto-rows-fr gap-3 text-sm sm:grid-cols-2 xl:grid-cols-6"> | ||
| 339 | + <div class="h-full rounded-md border bg-background/60 p-3"> | ||
| 340 | + <p class="text-xs text-muted-foreground">{t("attempt.status")}</p> | ||
| 341 | + <p class="mt-1 font-medium">{statusLabel()}</p> | ||
| 342 | + </div> | ||
| 343 | + <div class="h-full rounded-md border bg-background/60 p-3"> | ||
| 344 | + <p class="text-xs text-muted-foreground">{t("attempt.remaining")}</p> | ||
| 345 | + <p class="mt-1 font-mono font-medium">{formatRemaining(props.remainingMs)}</p> | ||
| 346 | + </div> | ||
| 347 | + <div class="h-full rounded-md border bg-background/60 p-3"> | ||
| 348 | + <p class="text-xs text-muted-foreground">{t("attempt.progress")}</p> | ||
| 349 | + <p class="mt-1 font-medium">{props.attempt.answered} / {props.attempt.question_count}</p> | ||
| 350 | + </div> | ||
| 351 | + <div class="h-full rounded-md border bg-background/60 p-3"> | ||
| 352 | + <p class="text-xs text-muted-foreground">{t("attempt.deadline")}</p> | ||
| 353 | + <p class="mt-1 font-medium">{formatDateTime(props.attempt.deadline, locale())}</p> | ||
| 354 | + </div> | ||
| 355 | + <div class="h-full rounded-md border bg-background/60 p-3"> | ||
| 356 | + <p class="text-xs text-muted-foreground">{t("attempt.serverNow")}</p> | ||
| 357 | + <p class="mt-1 font-medium">{formatDateTime(props.attempt.now, locale())}</p> | ||
| 358 | + </div> | ||
| 359 | + <div class="h-full rounded-md border bg-background/60 p-3"> | ||
| 360 | + <p class="text-xs text-muted-foreground">{t("ws.ping")}</p> | ||
| 361 | + <p class="mt-1 font-medium">{wsLabel()}</p> | ||
| 362 | + </div> | ||
| 363 | + <Show when={props.attempt.mark != null}> | ||
| 364 | + <div class="h-full rounded-md border bg-background/60 p-3"> | ||
| 365 | + <p class="text-xs text-muted-foreground">{t("attempt.mark")}</p> | ||
| 366 | + <p class="mt-1 font-medium">{props.attempt.mark}</p> | ||
| 367 | + </div> | ||
| 368 | + </Show> | ||
| 369 | + </div> | ||
| 370 | + ); | ||
| 371 | +} | ||
| 372 | + | ||
| 373 | +function QuestionAnswerCardWS(props: { | ||
| 374 | + index: number; | ||
| 375 | + nextQuestionId?: string; | ||
| 376 | + question: AttemptQuestion; | ||
| 377 | + disabled: boolean; | ||
| 378 | + onSave: (value: string) => Promise<void>; | ||
| 379 | +}) { | ||
| 380 | + const t = useT(); | ||
| 381 | + const { locale } = usePreferences(); | ||
| 382 | + const [value, setValue] = createSignal( | ||
| 383 | + props.question.kind === "choice" | ||
| 384 | + ? props.question.answer?.selected != null | ||
| 385 | + ? String(props.question.answer.selected) | ||
| 386 | + : "" | ||
| 387 | + : props.question.answer?.text ?? "", | ||
| 388 | + ); | ||
| 389 | + const [saved, setSaved] = createSignal(false); | ||
| 390 | + | ||
| 391 | + createEffect(() => { | ||
| 392 | + setValue( | ||
| 393 | + props.question.kind === "choice" | ||
| 394 | + ? props.question.answer?.selected != null | ||
| 395 | + ? String(props.question.answer.selected) | ||
| 396 | + : "" | ||
| 397 | + : props.question.answer?.text ?? "", | ||
| 398 | + ); | ||
| 399 | + setSaved(false); | ||
| 400 | + }); | ||
| 401 | + | ||
| 402 | + const save = async () => { | ||
| 403 | + await props.onSave(value()); | ||
| 404 | + setSaved(true); | ||
| 405 | + if (props.nextQuestionId) { | ||
| 406 | + document.getElementById(`question-${props.nextQuestionId}`)?.scrollIntoView({ behavior: "smooth", block: "start" }); | ||
| 407 | + } | ||
| 408 | + }; | ||
| 409 | + | ||
| 410 | + return ( | ||
| 411 | + <article id={`question-${props.question.id}`} class="surface-card min-h-[18rem] scroll-mt-24 p-5"> | ||
| 412 | + <div class="mb-3 flex flex-wrap items-center gap-2"> | ||
| 413 | + <span class="text-xs font-semibold text-muted-foreground">#{props.index}</span> | ||
| 414 | + <Badge variant="outline">{props.question.points} {t("questions.points")}</Badge> | ||
| 415 | + <Show when={saved()}> | ||
| 416 | + <Badge variant="outline">{t("attempt.saved")}</Badge> | ||
| 417 | + </Show> | ||
| 418 | + <Show when={props.question.answer?.updated_at}> | ||
| 419 | + {(updatedAt) => <Badge variant="outline">{t("attempt.savedAt")}: {formatDateTime(updatedAt(), locale())}</Badge>} | ||
| 420 | + </Show> | ||
| 421 | + </div> | ||
| 422 | + <p class="mb-4 whitespace-pre-wrap text-sm font-medium">{props.question.text}</p> | ||
| 423 | + <Show | ||
| 424 | + when={props.question.kind === "choice"} | ||
| 425 | + fallback={ | ||
| 426 | + <Textarea | ||
| 427 | + value={value()} | ||
| 428 | + rows={4} | ||
| 429 | + disabled={props.disabled} | ||
| 430 | + maxlength={10000} | ||
| 431 | + onInput={(e) => { | ||
| 432 | + setSaved(false); | ||
| 433 | + setValue(e.currentTarget.value); | ||
| 434 | + }} | ||
| 435 | + /> | ||
| 436 | + } | ||
| 437 | + > | ||
| 438 | + <div class="space-y-2"> | ||
| 439 | + <For each={props.question.choices ?? []}> | ||
| 440 | + {(choice, choiceIndex) => ( | ||
| 441 | + <button | ||
| 442 | + type="button" | ||
| 443 | + class="flex w-full items-center gap-3 rounded-sm border px-3 py-2 text-left text-sm transition-colors hover:bg-accent disabled:cursor-not-allowed disabled:opacity-60" | ||
| 444 | + disabled={props.disabled} | ||
| 445 | + onClick={() => { | ||
| 446 | + setSaved(false); | ||
| 447 | + setValue(String(choiceIndex())); | ||
| 448 | + }} | ||
| 449 | + > | ||
| 450 | + <span | ||
| 451 | + class={ | ||
| 452 | + value() === String(choiceIndex()) | ||
| 453 | + ? "flex h-4 w-4 shrink-0 items-center justify-center rounded-[3px] border border-primary bg-primary" | ||
| 454 | + : "h-4 w-4 shrink-0 rounded-[3px] border border-input bg-background" | ||
| 455 | + } | ||
| 456 | + > | ||
| 457 | + <span | ||
| 458 | + class={value() === String(choiceIndex()) ? "h-2 w-2 rounded-[1px] bg-primary-foreground" : "hidden"} | ||
| 459 | + /> | ||
| 460 | + </span> | ||
| 461 | + <span>{choice}</span> | ||
| 462 | + </button> | ||
| 463 | + )} | ||
| 464 | + </For> | ||
| 465 | + </div> | ||
| 466 | + </Show> | ||
| 467 | + <Button | ||
| 468 | + type="button" | ||
| 469 | + size="sm" | ||
| 470 | + class="mt-3" | ||
| 471 | + disabled={props.disabled || (props.question.kind === "choice" && value() === "")} | ||
| 472 | + onClick={() => void save()} | ||
| 473 | + > | ||
| 474 | + {t("attempt.saveAnswer")} | ||
| 475 | + </Button> | ||
| 476 | + </article> | ||
| 477 | + ); | ||
| 478 | +} | ||
src/components/layout/user-menu.tsxmodified+8 -1
| @@ -12,7 +12,7 @@ import { | |||
| 12 | DropdownMenuSubTrigger, | 12 | DropdownMenuSubTrigger, |
| 13 | DropdownMenuTrigger, | 13 | DropdownMenuTrigger, |
| 14 | } from "@/components/ui/dropdown-menu"; | 14 | } from "@/components/ui/dropdown-menu"; |
| 15 | -import { IconGlobe, IconLogout, IconMoon, IconSun } from "@/components/ui/icons"; | 15 | +import { IconEdit, IconGlobe, IconLogout, IconMoon, IconSun } from "@/components/ui/icons"; |
| 16 | import { useAuth } from "@/stores/auth-context"; | 16 | import { useAuth } from "@/stores/auth-context"; |
| 17 | import { usePreferences, useT } from "@/stores/preferences-context"; | 17 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 18 | import { cn } from "@/lib/cn"; | 18 | import { cn } from "@/lib/cn"; |
| @@ -164,6 +164,13 @@ export function UserMenu() { | |||
| 164 | 164 | ||
| 165 | <DropdownMenuSeparator /> | 165 | <DropdownMenuSeparator /> |
| 166 | 166 | ||
| 167 | + <DropdownMenuItem onSelect={() => void navigate({ to: "/profile" })}> | ||
| 168 | + <IconEdit class="h-4 w-4 shrink-0" /> | ||
| 169 | + <span>{t("profile.edit")}</span> | ||
| 170 | + </DropdownMenuItem> | ||
| 171 | + | ||
| 172 | + <DropdownMenuSeparator /> | ||
| 173 | + | ||
| 167 | <DropdownMenuItem destructive onSelect={() => void onLogout()}> | 174 | <DropdownMenuItem destructive onSelect={() => void onLogout()}> |
| 168 | <IconLogout class="h-4 w-4 shrink-0" /> | 175 | <IconLogout class="h-4 w-4 shrink-0" /> |
| 169 | <span>{t("nav.logout")}</span> | 176 | <span>{t("nav.logout")}</span> |
src/components/users/profile-form.tsxadded+104 -0
| @@ -0,0 +1,104 @@ | |||
| 1 | +import { createSignal } from "solid-js"; | ||
| 2 | +import { patchMe } from "@/api/patchMe"; | ||
| 3 | +import type { ProfileUpdate, User } from "@/api/types"; | ||
| 4 | +import { formatApiError } from "@/api/client"; | ||
| 5 | +import { Button } from "@/components/ui/button"; | ||
| 6 | +import { Input } from "@/components/ui/input"; | ||
| 7 | +import { Label } from "@/components/ui/label"; | ||
| 8 | +import { useT } from "@/stores/preferences-context"; | ||
| 9 | + | ||
| 10 | +const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
| 11 | +const PHONE_RE = /^\+?[\d\s().-]{7,20}$/; | ||
| 12 | +const DATE_RE = /^\d{4}-\d{2}-\d{2}$/; | ||
| 13 | + | ||
| 14 | +function isFutureDate(iso: string) { | ||
| 15 | + const d = new Date(`${iso}T00:00:00Z`); | ||
| 16 | + if (Number.isNaN(d.getTime())) return true; | ||
| 17 | + const tomorrow = new Date(); | ||
| 18 | + tomorrow.setUTCDate(tomorrow.getUTCDate() + 1); | ||
| 19 | + tomorrow.setUTCHours(0, 0, 0, 0); | ||
| 20 | + return d.getTime() > tomorrow.getTime(); | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +export function ProfileForm(props: { user: User; onSaved: () => void }) { | ||
| 24 | + const t = useT(); | ||
| 25 | + const [name, setName] = createSignal(props.user.name ?? ""); | ||
| 26 | + const [surname, setSurname] = createSignal(props.user.surname ?? ""); | ||
| 27 | + const [email, setEmail] = createSignal(props.user.email ?? ""); | ||
| 28 | + const [phone, setPhone] = createSignal(props.user.phone ?? ""); | ||
| 29 | + const [birthDate, setBirthDate] = createSignal(props.user.birth_date ?? ""); | ||
| 30 | + const [error, setError] = createSignal(""); | ||
| 31 | + const [pending, setPending] = createSignal(false); | ||
| 32 | + | ||
| 33 | + const validate = (): string | null => { | ||
| 34 | + const e = email().trim(); | ||
| 35 | + const p = phone().trim(); | ||
| 36 | + const b = birthDate().trim(); | ||
| 37 | + if (e && !EMAIL_RE.test(e)) return t("profile.emailInvalid"); | ||
| 38 | + if (p && !PHONE_RE.test(p)) return t("profile.phoneInvalid"); | ||
| 39 | + if (b && (!DATE_RE.test(b) || isFutureDate(b))) return t("profile.dateInvalid"); | ||
| 40 | + return null; | ||
| 41 | + }; | ||
| 42 | + | ||
| 43 | + const buildBody = (): ProfileUpdate => { | ||
| 44 | + const b: ProfileUpdate = {}; | ||
| 45 | + const n = name().trim(); | ||
| 46 | + const s = surname().trim(); | ||
| 47 | + const e = email().trim(); | ||
| 48 | + const p = phone().trim(); | ||
| 49 | + const d = birthDate().trim(); | ||
| 50 | + if (n !== (props.user.name ?? "")) b.name = n || null; | ||
| 51 | + if (s !== (props.user.surname ?? "")) b.surname = s || null; | ||
| 52 | + if (e !== (props.user.email ?? "")) b.email = e || null; | ||
| 53 | + if (p !== (props.user.phone ?? "")) b.phone = p || null; | ||
| 54 | + if (d !== (props.user.birth_date ?? "")) b.birth_date = d || null; | ||
| 55 | + return b; | ||
| 56 | + }; | ||
| 57 | + | ||
| 58 | + const submit = async (e: Event) => { | ||
| 59 | + e.preventDefault(); | ||
| 60 | + setError(""); | ||
| 61 | + const msg = validate(); | ||
| 62 | + if (msg) { setError(msg); return; } | ||
| 63 | + const body = buildBody(); | ||
| 64 | + if (Object.keys(body).length === 0) { props.onSaved(); return; } | ||
| 65 | + setPending(true); | ||
| 66 | + try { | ||
| 67 | + await patchMe(body); | ||
| 68 | + props.onSaved(); | ||
| 69 | + } catch (err) { | ||
| 70 | + setError(formatApiError(err)); | ||
| 71 | + } finally { | ||
| 72 | + setPending(false); | ||
| 73 | + } | ||
| 74 | + }; | ||
| 75 | + | ||
| 76 | + return ( | ||
| 77 | + <form onSubmit={submit} class="space-y-3"> | ||
| 78 | + <div class="grid gap-3 sm:grid-cols-2"> | ||
| 79 | + <div class="space-y-1.5"> | ||
| 80 | + <Label for="pf-name">{t("profile.name")}</Label> | ||
| 81 | + <Input id="pf-name" value={name()} onInput={(e) => setName(e.currentTarget.value)} /> | ||
| 82 | + </div> | ||
| 83 | + <div class="space-y-1.5"> | ||
| 84 | + <Label for="pf-surname">{t("profile.surname")}</Label> | ||
| 85 | + <Input id="pf-surname" value={surname()} onInput={(e) => setSurname(e.currentTarget.value)} /> | ||
| 86 | + </div> | ||
| 87 | + </div> | ||
| 88 | + <div class="space-y-1.5"> | ||
| 89 | + <Label for="pf-email">{t("profile.email")}</Label> | ||
| 90 | + <Input id="pf-email" type="email" value={email()} onInput={(e) => setEmail(e.currentTarget.value)} /> | ||
| 91 | + </div> | ||
| 92 | + <div class="space-y-1.5"> | ||
| 93 | + <Label for="pf-phone">{t("profile.phone")}</Label> | ||
| 94 | + <Input id="pf-phone" type="tel" value={phone()} onInput={(e) => setPhone(e.currentTarget.value)} /> | ||
| 95 | + </div> | ||
| 96 | + <div class="space-y-1.5"> | ||
| 97 | + <Label for="pf-birth">{t("profile.birthDate")}</Label> | ||
| 98 | + <Input id="pf-birth" type="date" value={birthDate()} onInput={(e) => setBirthDate(e.currentTarget.value)} /> | ||
| 99 | + </div> | ||
| 100 | + {error() && <p class="text-sm text-destructive">{error()}</p>} | ||
| 101 | + <Button type="submit" disabled={pending()}>{t("common.save")}</Button> | ||
| 102 | + </form> | ||
| 103 | + ); | ||
| 104 | +} | ||
src/i18n/messages.tsmodified+130 -1
| @@ -291,7 +291,50 @@ export type MessageKey = | |||
| 291 | | "marks.exam" | 291 | | "marks.exam" |
| 292 | | "marks.weight" | 292 | | "marks.weight" |
| 293 | | "marks.mark" | 293 | | "marks.mark" |
| 294 | - | "exams.mustBelongCourse"; | 294 | + | "exams.mustBelongCourse" |
| 295 | + | "exams.statistics" | ||
| 296 | + | "exams.graded" | ||
| 297 | + | "exams.average" | ||
| 298 | + | "exams.min" | ||
| 299 | + | "exams.max" | ||
| 300 | + | "exams.finished" | ||
| 301 | + | "exams.active" | ||
| 302 | + | "exams.upcoming" | ||
| 303 | + | "exams.liveMonitor" | ||
| 304 | + | "exams.liveMonitorDesc" | ||
| 305 | + | "exams.finalState" | ||
| 306 | + | "exams.finalStateDesc" | ||
| 307 | + | "exams.liveRoster" | ||
| 308 | + | "exams.notStarted" | ||
| 309 | + | "exams.lastActivity" | ||
| 310 | + | "exams.answerSheet" | ||
| 311 | + | "exams.autoScore" | ||
| 312 | + | "exams.earned" | ||
| 313 | + | "exams.possible" | ||
| 314 | + | "exams.isCorrect" | ||
| 315 | + | "exams.textAnswer" | ||
| 316 | + | "exams.nameless" | ||
| 317 | + | "exams.emptyRoster" | ||
| 318 | + | "exams.selectStudent" | ||
| 319 | + | "exams.viewSheet" | ||
| 320 | + | "profile.title" | ||
| 321 | + | "profile.subtitle" | ||
| 322 | + | "profile.name" | ||
| 323 | + | "profile.surname" | ||
| 324 | + | "profile.email" | ||
| 325 | + | "profile.phone" | ||
| 326 | + | "profile.birthDate" | ||
| 327 | + | "profile.edit" | ||
| 328 | + | "profile.saved" | ||
| 329 | + | "profile.emailInvalid" | ||
| 330 | + | "profile.phoneInvalid" | ||
| 331 | + | "profile.dateInvalid" | ||
| 332 | + | "profile.clearField" | ||
| 333 | + | "ws.connecting" | ||
| 334 | + | "ws.connected" | ||
| 335 | + | "ws.disconnected" | ||
| 336 | + | "ws.error" | ||
| 337 | + | "ws.ping"; | ||
| 295 | 338 | ||
| 296 | type Dict = Record<MessageKey, string>; | 339 | type Dict = Record<MessageKey, string>; |
| 297 | 340 | ||
| @@ -601,6 +644,49 @@ const en: Dict = { | |||
| 601 | "marks.weight": "Weight", | 644 | "marks.weight": "Weight", |
| 602 | "marks.mark": "Mark", | 645 | "marks.mark": "Mark", |
| 603 | "exams.mustBelongCourse": "Exams are created under a course. Open a course to add one.", | 646 | "exams.mustBelongCourse": "Exams are created under a course. Open a course to add one.", |
| 647 | + "exams.statistics": "Statistics", | ||
| 648 | + "exams.graded": "Graded", | ||
| 649 | + "exams.average": "Average", | ||
| 650 | + "exams.min": "Min", | ||
| 651 | + "exams.max": "Max", | ||
| 652 | + "exams.finished": "Finished", | ||
| 653 | + "exams.active": "Active", | ||
| 654 | + "exams.upcoming": "Upcoming", | ||
| 655 | + "exams.liveMonitor": "Live Monitor", | ||
| 656 | + "exams.liveMonitorDesc": "Real-time exam roster, progress, and marks.", | ||
| 657 | + "exams.finalState": "Final State", | ||
| 658 | + "exams.finalStateDesc": "Final exam results, progress, and marks.", | ||
| 659 | + "exams.liveRoster": "Live Roster", | ||
| 660 | + "exams.notStarted": "Not started", | ||
| 661 | + "exams.lastActivity": "Last activity", | ||
| 662 | + "exams.answerSheet": "Answer Sheet", | ||
| 663 | + "exams.autoScore": "Auto-score", | ||
| 664 | + "exams.earned": "Earned", | ||
| 665 | + "exams.possible": "Possible", | ||
| 666 | + "exams.isCorrect": "Correct", | ||
| 667 | + "exams.textAnswer": "Text answer", | ||
| 668 | + "exams.nameless": "Unnamed", | ||
| 669 | + "exams.emptyRoster": "No enrolled students yet.", | ||
| 670 | + "exams.selectStudent": "Select a student from the roster", | ||
| 671 | + "exams.viewSheet": "View answers", | ||
| 672 | + "profile.title": "My Profile", | ||
| 673 | + "profile.subtitle": "Personal information (optional).", | ||
| 674 | + "profile.name": "Name", | ||
| 675 | + "profile.surname": "Surname", | ||
| 676 | + "profile.email": "Email", | ||
| 677 | + "profile.phone": "Phone", | ||
| 678 | + "profile.birthDate": "Birth date", | ||
| 679 | + "profile.edit": "Edit profile", | ||
| 680 | + "profile.saved": "Profile saved", | ||
| 681 | + "profile.emailInvalid": "Enter a valid email address (e.g. name@example.com)", | ||
| 682 | + "profile.phoneInvalid": "Enter a valid phone number (7-15 digits, optional +)", | ||
| 683 | + "profile.dateInvalid": "Enter a real YYYY-MM-DD date, not in the future", | ||
| 684 | + "profile.clearField": "Clear", | ||
| 685 | + "ws.connecting": "Connecting…", | ||
| 686 | + "ws.connected": "Connected", | ||
| 687 | + "ws.disconnected": "Disconnected", | ||
| 688 | + "ws.error": "Connection error", | ||
| 689 | + "ws.ping": "Ping", | ||
| 604 | }; | 690 | }; |
| 605 | 691 | ||
| 606 | const tr: Dict = { | 692 | const tr: Dict = { |
| @@ -909,6 +995,49 @@ const tr: Dict = { | |||
| 909 | "marks.weight": "Ağırlık", | 995 | "marks.weight": "Ağırlık", |
| 910 | "marks.mark": "Not", | 996 | "marks.mark": "Not", |
| 911 | "exams.mustBelongCourse": "Sınavlar ders altında oluşturulur. Eklemek için bir ders aç.", | 997 | "exams.mustBelongCourse": "Sınavlar ders altında oluşturulur. Eklemek için bir ders aç.", |
| 998 | + "exams.statistics": "İstatistikler", | ||
| 999 | + "exams.graded": "Notlanan", | ||
| 1000 | + "exams.average": "Ortalama", | ||
| 1001 | + "exams.min": "En düşük", | ||
| 1002 | + "exams.max": "En yüksek", | ||
| 1003 | + "exams.finished": "Bitti", | ||
| 1004 | + "exams.active": "Aktif", | ||
| 1005 | + "exams.upcoming": "Yakında", | ||
| 1006 | + "exams.liveMonitor": "Canlı İzleme", | ||
| 1007 | + "exams.liveMonitorDesc": "Sınav durumu, ilerleme ve notlar gerçek zamanlı.", | ||
| 1008 | + "exams.finalState": "Son Durum", | ||
| 1009 | + "exams.finalStateDesc": "Sınav sonuçları, ilerleme ve notlar.", | ||
| 1010 | + "exams.liveRoster": "Canlı Liste", | ||
| 1011 | + "exams.notStarted": "Başlamadı", | ||
| 1012 | + "exams.lastActivity": "Son hareket", | ||
| 1013 | + "exams.answerSheet": "Cevap Kâğıdı", | ||
| 1014 | + "exams.autoScore": "Otomatik puan", | ||
| 1015 | + "exams.earned": "Alınan", | ||
| 1016 | + "exams.possible": "Mümkün", | ||
| 1017 | + "exams.isCorrect": "Doğru", | ||
| 1018 | + "exams.textAnswer": "Metin cevap", | ||
| 1019 | + "exams.nameless": "İsimsiz", | ||
| 1020 | + "exams.emptyRoster": "Henüz kayıtlı öğrenci yok.", | ||
| 1021 | + "exams.selectStudent": "Listeden bir öğrenci seç", | ||
| 1022 | + "exams.viewSheet": "Cevapları gör", | ||
| 1023 | + "profile.title": "Profilim", | ||
| 1024 | + "profile.subtitle": "Kişisel bilgiler (isteğe bağlı).", | ||
| 1025 | + "profile.name": "Ad", | ||
| 1026 | + "profile.surname": "Soyad", | ||
| 1027 | + "profile.email": "E-posta", | ||
| 1028 | + "profile.phone": "Telefon", | ||
| 1029 | + "profile.birthDate": "Doğum tarihi", | ||
| 1030 | + "profile.edit": "Profili düzenle", | ||
| 1031 | + "profile.saved": "Profil kaydedildi", | ||
| 1032 | + "profile.emailInvalid": "Geçerli bir e-posta adresi girin (örn. ad@ornek.com)", | ||
| 1033 | + "profile.phoneInvalid": "Geçerli bir telefon numarası girin (7-15 hane, isteğe bağlı +)", | ||
| 1034 | + "profile.dateInvalid": "Geçerli bir YYYY-AA-GG tarihi girin, gelecekte olmasın", | ||
| 1035 | + "profile.clearField": "Temizle", | ||
| 1036 | + "ws.connecting": "Bağlanıyor…", | ||
| 1037 | + "ws.connected": "Bağlı", | ||
| 1038 | + "ws.disconnected": "Bağlantı kesildi", | ||
| 1039 | + "ws.error": "Bağlantı hatası", | ||
| 1040 | + "ws.ping": "Ping", | ||
| 912 | }; | 1041 | }; |
| 913 | 1042 | ||
| 914 | export const messages: Record<Locale, Dict> = { en, tr }; | 1043 | export const messages: Record<Locale, Dict> = { en, tr }; |
src/pages/exam-detail-page.tsxmodified+121 -17
| @@ -5,12 +5,14 @@ import { deleteExamResultByUserId } from "@/api/deleteExamResultByUserId"; | |||
| 5 | import { getExamById } from "@/api/getExamById"; | 5 | import { getExamById } from "@/api/getExamById"; |
| 6 | import { getExamResult } from "@/api/getExamResult"; | 6 | import { getExamResult } from "@/api/getExamResult"; |
| 7 | import { getExamResults } from "@/api/getExamResults"; | 7 | import { getExamResults } from "@/api/getExamResults"; |
| 8 | +import { getExamStatistics } from "@/api/getExamStatistics"; | ||
| 8 | import { getCourseEnrollments } from "@/api/getCourseEnrollments"; | 9 | import { getCourseEnrollments } from "@/api/getCourseEnrollments"; |
| 9 | import { patchExamById } from "@/api/patchExamById"; | 10 | import { patchExamById } from "@/api/patchExamById"; |
| 10 | import { postExamResult } from "@/api/postExamResult"; | 11 | import { postExamResult } from "@/api/postExamResult"; |
| 11 | import { ApiError, formatApiError } from "@/api/client"; | 12 | import { ApiError, formatApiError } from "@/api/client"; |
| 12 | import { ExamForm } from "@/components/exams/exam-form"; | 13 | import { ExamForm } from "@/components/exams/exam-form"; |
| 13 | import { ExamQuestionsPanel } from "@/components/exams/exam-questions-panel"; | 14 | import { ExamQuestionsPanel } from "@/components/exams/exam-questions-panel"; |
| 15 | +import { AnswerSheetView } from "@/components/exams/answer-sheet-view"; | ||
| 14 | import { ExamResultBadge } from "@/components/exams/exam-result-badge"; | 16 | import { ExamResultBadge } from "@/components/exams/exam-result-badge"; |
| 15 | import { GradeForm } from "@/components/exams/grade-form"; | 17 | import { GradeForm } from "@/components/exams/grade-form"; |
| 16 | import { RouteGuard } from "@/components/layout/route-guard"; | 18 | import { RouteGuard } from "@/components/layout/route-guard"; |
| @@ -19,7 +21,7 @@ import { Alert } from "@/components/ui/alert"; | |||
| 19 | import { Badge } from "@/components/ui/badge"; | 21 | import { Badge } from "@/components/ui/badge"; |
| 20 | import { Button } from "@/components/ui/button"; | 22 | import { Button } from "@/components/ui/button"; |
| 21 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; | 23 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; |
| 22 | -import { IconChevronLeft, IconEdit, IconExam, IconTrash } from "@/components/ui/icons"; | 24 | +import { IconChevronLeft, IconEdit, IconExam, IconEye, IconTrash } from "@/components/ui/icons"; |
| 23 | import { PageSpinner } from "@/components/ui/page-spinner"; | 25 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 24 | import { | 26 | import { |
| 25 | Table, | 27 | Table, |
| @@ -32,6 +34,7 @@ import { | |||
| 32 | import { hasMinRole } from "@/lib/roles"; | 34 | import { hasMinRole } from "@/lib/roles"; |
| 33 | import { examKindLabel } from "@/lib/exam-labels"; | 35 | import { examKindLabel } from "@/lib/exam-labels"; |
| 34 | import { examDurationMs, formatDateTime, formatDurationMinutes } from "@/lib/format"; | 36 | import { examDurationMs, formatDateTime, formatDurationMinutes } from "@/lib/format"; |
| 37 | +import { cn } from "@/lib/cn"; | ||
| 35 | import { useAuth } from "@/stores/auth-context"; | 38 | import { useAuth } from "@/stores/auth-context"; |
| 36 | import { usePreferences, useT } from "@/stores/preferences-context"; | 39 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 37 | 40 | ||
| @@ -74,6 +77,17 @@ function ExamDetailContent() { | |||
| 74 | return getExamResults(examId); | 77 | return getExamResults(examId); |
| 75 | }, | 78 | }, |
| 76 | ); | 79 | ); |
| 80 | + const [stats] = createResource( | ||
| 81 | + () => (isTeacherPlus() ? id() : null), | ||
| 82 | + async (examId) => { | ||
| 83 | + if (!examId) return null; | ||
| 84 | + try { | ||
| 85 | + return await getExamStatistics(examId); | ||
| 86 | + } catch { | ||
| 87 | + return null; | ||
| 88 | + } | ||
| 89 | + }, | ||
| 90 | + ); | ||
| 77 | const [roster] = createResource( | 91 | const [roster] = createResource( |
| 78 | () => (isTeacherPlus() ? exam()?.course ?? null : null), | 92 | () => (isTeacherPlus() ? exam()?.course ?? null : null), |
| 79 | async (courseId) => (courseId ? getCourseEnrollments(courseId) : []), | 93 | async (courseId) => (courseId ? getCourseEnrollments(courseId) : []), |
| @@ -84,11 +98,24 @@ function ExamDetailContent() { | |||
| 84 | const [pending, setPending] = createSignal(false); | 98 | const [pending, setPending] = createSignal(false); |
| 85 | const [deleteOpen, setDeleteOpen] = createSignal(false); | 99 | const [deleteOpen, setDeleteOpen] = createSignal(false); |
| 86 | const [removeUserId, setRemoveUserId] = createSignal<string | null>(null); | 100 | const [removeUserId, setRemoveUserId] = createSignal<string | null>(null); |
| 101 | + const [sheetUserId, setSheetUserId] = createSignal<string | null>(null); | ||
| 102 | + | ||
| 103 | + const examStatus = () => { | ||
| 104 | + const e = exam(); | ||
| 105 | + if (!e) return { finished: false, upcoming: false }; | ||
| 106 | + const now = Date.now(); | ||
| 107 | + const finished = e.ends_at != null && e.ends_at < now; | ||
| 108 | + const upcoming = e.starts_at != null && e.starts_at > now; | ||
| 109 | + return { finished, upcoming }; | ||
| 110 | + }; | ||
| 111 | + const isFinished = () => examStatus().finished; | ||
| 112 | + const isUpcoming = () => examStatus().upcoming; | ||
| 87 | 113 | ||
| 88 | const canManage = () => { | 114 | const canManage = () => { |
| 89 | const e = exam(); | 115 | const e = exam(); |
| 90 | const u = auth.user(); | 116 | const u = auth.user(); |
| 91 | if (!e || !u) return false; | 117 | if (!e || !u) return false; |
| 118 | + if (isFinished()) return false; | ||
| 92 | return e.creator === u.id || hasMinRole(u.role, "manager"); | 119 | return e.creator === u.id || hasMinRole(u.role, "manager"); |
| 93 | }; | 120 | }; |
| 94 | 121 | ||
| @@ -147,7 +174,7 @@ function ExamDetailContent() { | |||
| 147 | {t("common.back")} | 174 | {t("common.back")} |
| 148 | </Button> | 175 | </Button> |
| 149 | </Link> | 176 | </Link> |
| 150 | - <Show when={ex().mode === "sync" || ex().mode === "async"}> | 177 | + <Show when={!isFinished() && !isUpcoming() && (ex().mode === "sync" || ex().mode === "async")}> |
| 151 | <Link to="/exam-room/$id" params={{ id: id() }}> | 178 | <Link to="/exam-room/$id" params={{ id: id() }}> |
| 152 | <Button size="sm" class="rounded-sm"> | 179 | <Button size="sm" class="rounded-sm"> |
| 153 | <IconExam class="h-4 w-4" /> | 180 | <IconExam class="h-4 w-4" /> |
| @@ -155,6 +182,14 @@ function ExamDetailContent() { | |||
| 155 | </Button> | 182 | </Button> |
| 156 | </Link> | 183 | </Link> |
| 157 | </Show> | 184 | </Show> |
| 185 | + <Show when={isTeacherPlus() && !isUpcoming() && (ex().mode === "sync" || ex().mode === "async")}> | ||
| 186 | + <Link to="/exams/$id/live" params={{ id: id() }}> | ||
| 187 | + <Button variant="outline" size="sm" class="rounded-sm"> | ||
| 188 | + <IconEye class="h-4 w-4" /> | ||
| 189 | + {isFinished() ? t("exams.finalState") : t("exams.liveMonitor")} | ||
| 190 | + </Button> | ||
| 191 | + </Link> | ||
| 192 | + </Show> | ||
| 158 | <Show when={canManage()}> | 193 | <Show when={canManage()}> |
| 159 | <div class="ml-1 flex items-center gap-1 border-l border-border pl-1"> | 194 | <div class="ml-1 flex items-center gap-1 border-l border-border pl-1"> |
| 160 | <Button | 195 | <Button |
| @@ -183,6 +218,20 @@ function ExamDetailContent() { | |||
| 183 | } | 218 | } |
| 184 | > | 219 | > |
| 185 | <div class="flex flex-wrap items-center gap-2 pt-1"> | 220 | <div class="flex flex-wrap items-center gap-2 pt-1"> |
| 221 | + <Badge variant="outline" class={cn( | ||
| 222 | + "rounded-sm capitalize", | ||
| 223 | + isFinished() && "bg-muted text-muted-foreground border-muted", | ||
| 224 | + !isFinished() && !isUpcoming() && "bg-emerald-500/15 text-emerald-600 border-emerald-500/30", | ||
| 225 | + isUpcoming() && "bg-amber-500/15 text-amber-600 border-amber-500/30", | ||
| 226 | + )}> | ||
| 227 | + <span class={cn( | ||
| 228 | + "mr-1.5 inline-block h-1.5 w-1.5 rounded-full", | ||
| 229 | + isFinished() && "bg-muted-foreground", | ||
| 230 | + !isFinished() && !isUpcoming() && "bg-emerald-600", | ||
| 231 | + isUpcoming() && "bg-amber-600", | ||
| 232 | + )} /> | ||
| 233 | + {isFinished() ? t("exams.finished") : isUpcoming() ? t("exams.upcoming") : t("exams.active")} | ||
| 234 | + </Badge> | ||
| 186 | <Badge variant="outline" class="rounded-sm capitalize"> | 235 | <Badge variant="outline" class="rounded-sm capitalize"> |
| 187 | {examKindLabel(String(ex().kind), t)} | 236 | {examKindLabel(String(ex().kind), t)} |
| 188 | </Badge> | 237 | </Badge> |
| @@ -259,20 +308,59 @@ function ExamDetailContent() { | |||
| 259 | </section> | 308 | </section> |
| 260 | </Show> | 309 | </Show> |
| 261 | 310 | ||
| 262 | - <Show when={isTeacherPlus()}> | 311 | + <Show when={isTeacherPlus() && sheetUserId()}> |
| 263 | - <ExamQuestionsPanel examId={id()} /> | 312 | + <section class="surface-card p-5"> |
| 313 | + <h2 class="mb-4 font-display text-lg font-semibold">{t("exams.answerSheet")} — {sheetUserId()}</h2> | ||
| 314 | + <Suspense fallback={<PageSpinner />}> | ||
| 315 | + <AnswerSheetView examId={id()} userId={sheetUserId()!} /> | ||
| 316 | + </Suspense> | ||
| 317 | + </section> | ||
| 318 | + </Show> | ||
| 264 | 319 | ||
| 320 | + <Show when={isTeacherPlus()}> | ||
| 265 | <section class="surface-card p-5"> | 321 | <section class="surface-card p-5"> |
| 266 | - <h2 class="mb-4 font-display text-lg font-semibold">{t("exams.gradeStudent")}</h2> | 322 | + <h2 class="mb-4 font-display text-lg font-semibold">{t("exams.statistics")}</h2> |
| 267 | - <GradeForm | 323 | + <Suspense fallback={<PageSpinner />}> |
| 268 | - students={gradeStudents()} | 324 | + <Show when={stats()}> |
| 269 | - onSubmit={async (values) => { | 325 | + {(s) => ( |
| 270 | - await postExamResult(id(), values); | 326 | + <div class="grid gap-3 text-sm sm:grid-cols-4"> |
| 271 | - await refetchResults(); | 327 | + <div class="rounded-md border p-3"> |
| 272 | - }} | 328 | + <p class="text-xs text-muted-foreground">{t("exams.graded")}</p> |
| 273 | - /> | 329 | + <p class="mt-1 font-display text-2xl font-semibold tabular-nums">{s().graded}</p> |
| 330 | + </div> | ||
| 331 | + <div class="rounded-md border p-3"> | ||
| 332 | + <p class="text-xs text-muted-foreground">{t("exams.average")}</p> | ||
| 333 | + <p class="mt-1 font-display text-2xl font-semibold tabular-nums">{s().average == null ? "—" : s().average}</p> | ||
| 334 | + </div> | ||
| 335 | + <div class="rounded-md border p-3"> | ||
| 336 | + <p class="text-xs text-muted-foreground">{t("exams.min")}</p> | ||
| 337 | + <p class="mt-1 font-display text-2xl font-semibold tabular-nums">{s().min == null ? "—" : s().min}</p> | ||
| 338 | + </div> | ||
| 339 | + <div class="rounded-md border p-3"> | ||
| 340 | + <p class="text-xs text-muted-foreground">{t("exams.max")}</p> | ||
| 341 | + <p class="mt-1 font-display text-2xl font-semibold tabular-nums">{s().max == null ? "—" : s().max}</p> | ||
| 342 | + </div> | ||
| 343 | + </div> | ||
| 344 | + )} | ||
| 345 | + </Show> | ||
| 346 | + </Suspense> | ||
| 274 | </section> | 347 | </section> |
| 275 | 348 | ||
| 349 | + <ExamQuestionsPanel examId={id()} readOnly={isFinished() || isUpcoming()} /> | ||
| 350 | + | ||
| 351 | + <Show when={!isFinished()}> | ||
| 352 | + <section class="surface-card p-5"> | ||
| 353 | + <h2 class="mb-4 font-display text-lg font-semibold">{t("exams.gradeStudent")}</h2> | ||
| 354 | + <GradeForm | ||
| 355 | + students={gradeStudents()} | ||
| 356 | + onSubmit={async (values) => { | ||
| 357 | + await postExamResult(id(), values); | ||
| 358 | + await refetchResults(); | ||
| 359 | + }} | ||
| 360 | + /> | ||
| 361 | + </section> | ||
| 362 | + </Show> | ||
| 363 | + | ||
| 276 | <section class="surface-card p-5"> | 364 | <section class="surface-card p-5"> |
| 277 | <h2 class="mb-4 font-display text-lg font-semibold">{t("exams.results")}</h2> | 365 | <h2 class="mb-4 font-display text-lg font-semibold">{t("exams.results")}</h2> |
| 278 | <Suspense fallback={<PageSpinner />}> | 366 | <Suspense fallback={<PageSpinner />}> |
| @@ -290,7 +378,10 @@ function ExamDetailContent() { | |||
| 290 | <TableHead>{t("events.userId")}</TableHead> | 378 | <TableHead>{t("events.userId")}</TableHead> |
| 291 | <TableHead>{t("form.mark")}</TableHead> | 379 | <TableHead>{t("form.mark")}</TableHead> |
| 292 | <TableHead>{t("exams.gradedBy")}</TableHead> | 380 | <TableHead>{t("exams.gradedBy")}</TableHead> |
| 293 | - <TableHead class="w-24" /> | 381 | + <TableHead>{t("exams.answerSheet")}</TableHead> |
| 382 | + <Show when={!isFinished()}> | ||
| 383 | + <TableHead class="w-24" /> | ||
| 384 | + </Show> | ||
| 294 | </TableRow> | 385 | </TableRow> |
| 295 | </TableHeader> | 386 | </TableHeader> |
| 296 | <TableBody> | 387 | <TableBody> |
| @@ -307,13 +398,26 @@ function ExamDetailContent() { | |||
| 307 | type="button" | 398 | type="button" |
| 308 | variant="ghost" | 399 | variant="ghost" |
| 309 | size="sm" | 400 | size="sm" |
| 310 | - class="text-destructive hover:bg-destructive/10 hover:text-destructive" | 401 | + class="rounded-sm" |
| 311 | - onClick={() => setRemoveUserId(row.user)} | 402 | + onClick={() => setSheetUserId(sheetUserId() === row.user ? null : row.user)} |
| 312 | > | 403 | > |
| 313 | - <IconTrash class="h-4 w-4" /> | 404 | + <IconEye class="h-4 w-4" /> |
| 314 | - {t("common.remove")} | ||
| 315 | </Button> | 405 | </Button> |
| 316 | </TableCell> | 406 | </TableCell> |
| 407 | + <Show when={!isFinished()}> | ||
| 408 | + <TableCell> | ||
| 409 | + <Button | ||
| 410 | + type="button" | ||
| 411 | + variant="ghost" | ||
| 412 | + size="sm" | ||
| 413 | + class="text-destructive hover:bg-destructive/10 hover:text-destructive" | ||
| 414 | + onClick={() => setRemoveUserId(row.user)} | ||
| 415 | + > | ||
| 416 | + <IconTrash class="h-4 w-4" /> | ||
| 417 | + {t("common.remove")} | ||
| 418 | + </Button> | ||
| 419 | + </TableCell> | ||
| 420 | + </Show> | ||
| 317 | </TableRow> | 421 | </TableRow> |
| 318 | )} | 422 | )} |
| 319 | </For> | 423 | </For> |
src/pages/exam-room-page.tsxmodified+2 -2
| @@ -2,7 +2,7 @@ import { Link, useLocation } from "@tanstack/solid-router"; | |||
| 2 | import { Show, Suspense, createResource } from "solid-js"; | 2 | import { Show, Suspense, createResource } from "solid-js"; |
| 3 | import { getExamById } from "@/api/getExamById"; | 3 | import { getExamById } from "@/api/getExamById"; |
| 4 | import { ApiError } from "@/api/client"; | 4 | import { ApiError } from "@/api/client"; |
| 5 | -import { StudentExamRoom } from "@/components/exams/student-exam-room"; | 5 | +import { ExamRoomWS } from "@/components/exams/exam-room-ws"; |
| 6 | import { RouteGuard } from "@/components/layout/route-guard"; | 6 | import { RouteGuard } from "@/components/layout/route-guard"; |
| 7 | import { PageHeader } from "@/components/layout/page-header"; | 7 | import { PageHeader } from "@/components/layout/page-header"; |
| 8 | import { Alert } from "@/components/ui/alert"; | 8 | import { Alert } from "@/components/ui/alert"; |
| @@ -56,7 +56,7 @@ function ExamRoomContent() { | |||
| 56 | </div> | 56 | </div> |
| 57 | } | 57 | } |
| 58 | /> | 58 | /> |
| 59 | - <StudentExamRoom exam={ex()} compact /> | 59 | + <ExamRoomWS exam={ex()} /> |
| 60 | </div> | 60 | </div> |
| 61 | )} | 61 | )} |
| 62 | </Show> | 62 | </Show> |
src/pages/exams-page.tsxmodified+61 -9
| @@ -1,12 +1,16 @@ | |||
| 1 | -import { For, Show, Suspense, createResource } from "solid-js"; | 1 | +import { For, Show, Suspense, createMemo, createResource, createSignal } from "solid-js"; |
| 2 | import { Link } from "@tanstack/solid-router"; | 2 | import { Link } from "@tanstack/solid-router"; |
| 3 | +import { getCourses } from "@/api/getCourses"; | ||
| 3 | import { getExams } from "@/api/getExams"; | 4 | import { getExams } from "@/api/getExams"; |
| 4 | import { ExamCard } from "@/components/exams/exam-card"; | 5 | import { ExamCard } from "@/components/exams/exam-card"; |
| 5 | import { RouteGuard } from "@/components/layout/route-guard"; | 6 | import { RouteGuard } from "@/components/layout/route-guard"; |
| 6 | import { PageHeader } from "@/components/layout/page-header"; | 7 | import { PageHeader } from "@/components/layout/page-header"; |
| 7 | import { CollapsibleHelp } from "@/components/ui/collapsible-help"; | 8 | import { CollapsibleHelp } from "@/components/ui/collapsible-help"; |
| 9 | +import { Badge } from "@/components/ui/badge"; | ||
| 8 | import { Button } from "@/components/ui/button"; | 10 | import { Button } from "@/components/ui/button"; |
| 11 | +import { IconChevronRight } from "@/components/ui/icons"; | ||
| 9 | import { PageSpinner } from "@/components/ui/page-spinner"; | 12 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 13 | +import { cn } from "@/lib/cn"; | ||
| 10 | import { useAuth } from "@/stores/auth-context"; | 14 | import { useAuth } from "@/stores/auth-context"; |
| 11 | import { useT } from "@/stores/preferences-context"; | 15 | import { useT } from "@/stores/preferences-context"; |
| 12 | import { hasMinRole } from "@/lib/roles"; | 16 | import { hasMinRole } from "@/lib/roles"; |
| @@ -23,8 +27,28 @@ function ExamsContent() { | |||
| 23 | const auth = useAuth(); | 27 | const auth = useAuth(); |
| 24 | const t = useT(); | 28 | const t = useT(); |
| 25 | const [exams] = createResource(() => getExams()); | 29 | const [exams] = createResource(() => getExams()); |
| 30 | + const [courses] = createResource(() => getCourses()); | ||
| 31 | + const [openCourse, setOpenCourse] = createSignal<string | null>(null); | ||
| 26 | const canCreate = () => hasMinRole(auth.user()?.role, "teacher"); | 32 | const canCreate = () => hasMinRole(auth.user()?.role, "teacher"); |
| 27 | 33 | ||
| 34 | + const courseSections = createMemo(() => { | ||
| 35 | + const all = exams(); | ||
| 36 | + if (!all) return []; | ||
| 37 | + const grouped = new Map<string, typeof all>(); | ||
| 38 | + for (const exam of all) { | ||
| 39 | + const cid = exam.course; | ||
| 40 | + if (!grouped.has(cid)) grouped.set(cid, []); | ||
| 41 | + grouped.get(cid)!.push(exam); | ||
| 42 | + } | ||
| 43 | + return (courses() ?? []) | ||
| 44 | + .filter((c) => (grouped.get(c.id)?.length ?? 0) > 0) | ||
| 45 | + .map((c) => ({ id: c.id, title: c.title, exams: grouped.get(c.id)! })); | ||
| 46 | + }); | ||
| 47 | + | ||
| 48 | + const toggleCourse = (id: string) => { | ||
| 49 | + setOpenCourse((prev) => (prev === id ? null : id)); | ||
| 50 | + }; | ||
| 51 | + | ||
| 28 | return ( | 52 | return ( |
| 29 | <div class="space-y-6"> | 53 | <div class="space-y-6"> |
| 30 | <PageHeader | 54 | <PageHeader |
| @@ -65,15 +89,43 @@ function ExamsContent() { | |||
| 65 | </div> | 89 | </div> |
| 66 | } | 90 | } |
| 67 | > | 91 | > |
| 68 | - <ul class="grid gap-4 sm:grid-cols-2 xl:grid-cols-3"> | 92 | + <div class="space-y-4"> |
| 69 | - <For each={list()}> | 93 | + <For each={courseSections()}> |
| 70 | - {(exam) => ( | 94 | + {(section) => { |
| 71 | - <li> | 95 | + const isOpen = () => openCourse() === section.id; |
| 72 | - <ExamCard exam={exam} /> | 96 | + return ( |
| 73 | - </li> | 97 | + <section class="surface-card overflow-hidden"> |
| 74 | - )} | 98 | + <button |
| 99 | + type="button" | ||
| 100 | + onClick={() => toggleCourse(section.id)} | ||
| 101 | + class="flex w-full items-center justify-between gap-3 px-5 py-4 text-left transition-colors hover:bg-muted/30" | ||
| 102 | + > | ||
| 103 | + <div class="flex items-center gap-3"> | ||
| 104 | + <IconChevronRight | ||
| 105 | + class={cn("h-4 w-4 text-muted-foreground transition-transform", isOpen() && "rotate-90")} | ||
| 106 | + /> | ||
| 107 | + <h2 class="font-display text-base font-semibold">{section.title}</h2> | ||
| 108 | + </div> | ||
| 109 | + <Badge variant="outline" class="rounded-sm">{section.exams.length}</Badge> | ||
| 110 | + </button> | ||
| 111 | + <Show when={isOpen()}> | ||
| 112 | + <div class="border-t border-border/50 px-5 pb-5 pt-4"> | ||
| 113 | + <ul class="grid gap-4 sm:grid-cols-2 xl:grid-cols-3"> | ||
| 114 | + <For each={section.exams}> | ||
| 115 | + {(exam) => ( | ||
| 116 | + <li> | ||
| 117 | + <ExamCard exam={exam} courseTitle={section.title} /> | ||
| 118 | + </li> | ||
| 119 | + )} | ||
| 120 | + </For> | ||
| 121 | + </ul> | ||
| 122 | + </div> | ||
| 123 | + </Show> | ||
| 124 | + </section> | ||
| 125 | + ); | ||
| 126 | + }} | ||
| 75 | </For> | 127 | </For> |
| 76 | - </ul> | 128 | + </div> |
| 77 | </Show> | 129 | </Show> |
| 78 | )} | 130 | )} |
| 79 | </Show> | 131 | </Show> |
src/pages/live-monitor-page.tsxadded+281 -0
| @@ -0,0 +1,281 @@ | |||
| 1 | +import { For, Show, Suspense, createEffect, createResource, createSignal, onCleanup } from "solid-js"; | ||
| 2 | +import { Link, useLocation } from "@tanstack/solid-router"; | ||
| 3 | +import { getExamLive } from "@/api/getExamLive"; | ||
| 4 | +import { getExamById } from "@/api/getExamById"; | ||
| 5 | +import { ApiError } from "@/api/client"; | ||
| 6 | +import type { LiveMonitor, LiveRosterEntry } from "@/api/types"; | ||
| 7 | +import type { MessageKey } from "@/i18n/messages"; | ||
| 8 | +import { RouteGuard } from "@/components/layout/route-guard"; | ||
| 9 | +import { PageHeader } from "@/components/layout/page-header"; | ||
| 10 | +import { Alert } from "@/components/ui/alert"; | ||
| 11 | +import { Badge } from "@/components/ui/badge"; | ||
| 12 | +import { Button } from "@/components/ui/button"; | ||
| 13 | +import { IconChevronLeft, IconChevronRight } from "@/components/ui/icons"; | ||
| 14 | +import { PageSpinner } from "@/components/ui/page-spinner"; | ||
| 15 | +import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; | ||
| 16 | +import { usePreferences, useT } from "@/stores/preferences-context"; | ||
| 17 | +import { formatDateTime } from "@/lib/format"; | ||
| 18 | +import { cn } from "@/lib/cn"; | ||
| 19 | + | ||
| 20 | +type SortKey = "username" | "status" | "progress" | "remaining" | "activity" | "mark"; | ||
| 21 | + | ||
| 22 | +const PAGE_SIZE = 10; | ||
| 23 | + | ||
| 24 | +export default function LiveMonitorPage() { | ||
| 25 | + return ( | ||
| 26 | + <RouteGuard> | ||
| 27 | + <LiveMonitorContent /> | ||
| 28 | + </RouteGuard> | ||
| 29 | + ); | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +const STATUS_KEY: Record<string, MessageKey> = { | ||
| 33 | + not_started: "exams.notStarted", | ||
| 34 | + in_progress: "attempt.inProgress", | ||
| 35 | + submitted: "attempt.submitted", | ||
| 36 | + expired: "attempt.expired", | ||
| 37 | +}; | ||
| 38 | + | ||
| 39 | +function labelFromStatus(status: string, t: (key: MessageKey) => string): string { | ||
| 40 | + const k = STATUS_KEY[status]; | ||
| 41 | + return k ? t(k) : status; | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +function LiveMonitorContent() { | ||
| 45 | + const location = useLocation(); | ||
| 46 | + const t = useT(); | ||
| 47 | + const { locale } = usePreferences(); | ||
| 48 | + const id = () => decodeURIComponent(location().pathname.split("/")[2] ?? ""); | ||
| 49 | + | ||
| 50 | + const [exam] = createResource(id, (eid) => getExamById(eid)); | ||
| 51 | + const [snapshot, setSnapshot] = createSignal<LiveMonitor | null>(null); | ||
| 52 | + const [error, setError] = createSignal(""); | ||
| 53 | + const [sortKey, setSortKey] = createSignal<SortKey>("status"); | ||
| 54 | + const [sortDir, setSortDir] = createSignal<"asc" | "desc">("asc"); | ||
| 55 | + const [page, setPage] = createSignal(0); | ||
| 56 | + | ||
| 57 | + const isFinished = () => { | ||
| 58 | + const e = exam(); | ||
| 59 | + if (!e || e.ends_at == null) return false; | ||
| 60 | + return e.ends_at < Date.now(); | ||
| 61 | + }; | ||
| 62 | + | ||
| 63 | + const fetchSnapshot = async () => { | ||
| 64 | + try { | ||
| 65 | + const data = await getExamLive(id()); | ||
| 66 | + setSnapshot(data); | ||
| 67 | + } catch (err) { | ||
| 68 | + console.error("[live-monitor] fetch error:", err); | ||
| 69 | + setError(err instanceof ApiError ? err.message : "Failed to load"); | ||
| 70 | + } | ||
| 71 | + }; | ||
| 72 | + | ||
| 73 | + createEffect(() => { | ||
| 74 | + const eid = id(); | ||
| 75 | + if (!eid) return; | ||
| 76 | + void fetchSnapshot(); | ||
| 77 | + if (isFinished()) return; | ||
| 78 | + const interval = setInterval(fetchSnapshot, 2000); | ||
| 79 | + onCleanup(() => clearInterval(interval)); | ||
| 80 | + }); | ||
| 81 | + | ||
| 82 | + const toggleSort = (key: SortKey) => { | ||
| 83 | + setPage(0); | ||
| 84 | + if (sortKey() === key) { | ||
| 85 | + setSortDir((d) => (d === "asc" ? "desc" : "asc")); | ||
| 86 | + } else { | ||
| 87 | + setSortKey(key); | ||
| 88 | + setSortDir("asc"); | ||
| 89 | + } | ||
| 90 | + }; | ||
| 91 | + | ||
| 92 | + const sortFn = (a: LiveRosterEntry, b: LiveRosterEntry) => { | ||
| 93 | + const d = sortDir(); | ||
| 94 | + const cmp = (() => { | ||
| 95 | + const k = sortKey(); | ||
| 96 | + if (k === "username") return (a.display_name || a.username || "").localeCompare(b.display_name || b.username || ""); | ||
| 97 | + if (k === "status") return labelFromStatus(a.status, t).localeCompare(labelFromStatus(b.status, t)); | ||
| 98 | + if (k === "progress") return (a.answered ?? 0) - (b.answered ?? 0); | ||
| 99 | + if (k === "remaining") return (a.remaining_ms ?? 0) - (b.remaining_ms ?? 0); | ||
| 100 | + if (k === "activity") return (a.last_activity ?? 0) - (b.last_activity ?? 0); | ||
| 101 | + if (k === "mark") return (a.mark ?? -1) - (b.mark ?? -1); | ||
| 102 | + return 0; | ||
| 103 | + })(); | ||
| 104 | + return d === "asc" ? cmp : -cmp; | ||
| 105 | + }; | ||
| 106 | + | ||
| 107 | + return ( | ||
| 108 | + <Suspense fallback={<PageSpinner />}> | ||
| 109 | + <div class="space-y-6"> | ||
| 110 | + <Show when={exam()}> | ||
| 111 | + {(ex) => ( | ||
| 112 | + <PageHeader | ||
| 113 | + compact | ||
| 114 | + accent="rose" | ||
| 115 | + eyebrow={isFinished() ? t("exams.finalState") : t("exams.liveMonitor")} | ||
| 116 | + title={ex().title} | ||
| 117 | + description={isFinished() ? t("exams.finalStateDesc") : t("exams.liveMonitorDesc")} | ||
| 118 | + actions={ | ||
| 119 | + <div class="flex flex-wrap items-center gap-1 rounded-md border bg-background/70 p-1 shadow-sm"> | ||
| 120 | + <Link to="/exams/$id" params={{ id: id() }}> | ||
| 121 | + <Button variant="ghost" size="sm" class="rounded-sm"> | ||
| 122 | + <IconChevronLeft class="h-4 w-4" /> | ||
| 123 | + {t("common.back")} | ||
| 124 | + </Button> | ||
| 125 | + </Link> | ||
| 126 | + </div> | ||
| 127 | + } | ||
| 128 | + /> | ||
| 129 | + )} | ||
| 130 | + </Show> | ||
| 131 | + | ||
| 132 | + {error() && <Alert variant="destructive">{error()}</Alert>} | ||
| 133 | + | ||
| 134 | + <Show when={snapshot()}> | ||
| 135 | + {(s) => { | ||
| 136 | + const m = s(); | ||
| 137 | + if (!m) return null; | ||
| 138 | + const counts = m.counts ?? { not_started: 0, in_progress: 0, submitted: 0, expired: 0 }; | ||
| 139 | + const raw = Array.isArray(m.students) ? m.students : []; | ||
| 140 | + const sorted = [...raw].sort(sortFn); | ||
| 141 | + const totalPages = Math.max(1, Math.ceil(sorted.length / PAGE_SIZE)); | ||
| 142 | + const safePage = Math.min(page(), totalPages - 1); | ||
| 143 | + const pageItems = sorted.slice(safePage * PAGE_SIZE, (safePage + 1) * PAGE_SIZE); | ||
| 144 | + | ||
| 145 | + return ( | ||
| 146 | + <> | ||
| 147 | + <section class="grid gap-3 sm:grid-cols-2 lg:grid-cols-4"> | ||
| 148 | + <div class="surface-card p-4"> | ||
| 149 | + <p class="text-xs text-muted-foreground">{t("exams.notStarted")}</p> | ||
| 150 | + <p class="mt-1 font-display text-2xl font-semibold tabular-nums">{counts.not_started}</p> | ||
| 151 | + </div> | ||
| 152 | + <div class="surface-card p-4"> | ||
| 153 | + <p class="text-xs text-muted-foreground">{t("attempt.inProgress")}</p> | ||
| 154 | + <p class="mt-1 font-display text-2xl font-semibold tabular-nums">{counts.in_progress}</p> | ||
| 155 | + </div> | ||
| 156 | + <div class="surface-card p-4"> | ||
| 157 | + <p class="text-xs text-muted-foreground">{t("attempt.submitted")}</p> | ||
| 158 | + <p class="mt-1 font-display text-2xl font-semibold tabular-nums">{counts.submitted}</p> | ||
| 159 | + </div> | ||
| 160 | + <div class="surface-card p-4"> | ||
| 161 | + <p class="text-xs text-muted-foreground">{t("attempt.expired")}</p> | ||
| 162 | + <p class="mt-1 font-display text-2xl font-semibold tabular-nums">{counts.expired}</p> | ||
| 163 | + </div> | ||
| 164 | + </section> | ||
| 165 | + | ||
| 166 | + <section class="surface-card p-2 sm:p-4"> | ||
| 167 | + <h2 class="mb-4 font-display text-lg font-semibold">{t("exams.liveRoster")}</h2> | ||
| 168 | + <Show | ||
| 169 | + when={sorted.length > 0} | ||
| 170 | + fallback={<p class="rounded-sm bg-muted/40 px-3 py-4 text-sm text-muted-foreground">{t("exams.emptyRoster")}</p>} | ||
| 171 | + > | ||
| 172 | + <Table> | ||
| 173 | + <TableHeader> | ||
| 174 | + <TableRow> | ||
| 175 | + <SortHead label={t("admin.username")} sortKey="username" currentKey={sortKey()} currentDir={sortDir()} onSort={toggleSort} class="text-left" /> | ||
| 176 | + <SortHead label={t("attempt.status")} sortKey="status" currentKey={sortKey()} currentDir={sortDir()} onSort={toggleSort} class="text-center" /> | ||
| 177 | + <SortHead label={t("attempt.progress")} sortKey="progress" currentKey={sortKey()} currentDir={sortDir()} onSort={toggleSort} class="text-center" /> | ||
| 178 | + <SortHead label={t("attempt.remaining")} sortKey="remaining" currentKey={sortKey()} currentDir={sortDir()} onSort={toggleSort} class="text-center" /> | ||
| 179 | + <SortHead label={t("exams.lastActivity")} sortKey="activity" currentKey={sortKey()} currentDir={sortDir()} onSort={toggleSort} class="text-center" /> | ||
| 180 | + <SortHead label={t("marks.mark")} sortKey="mark" currentKey={sortKey()} currentDir={sortDir()} onSort={toggleSort} class="text-center" /> | ||
| 181 | + </TableRow> | ||
| 182 | + </TableHeader> | ||
| 183 | + <TableBody> | ||
| 184 | + <For each={pageItems}> | ||
| 185 | + {(entry) => ( | ||
| 186 | + <TableRow> | ||
| 187 | + <TableCell class="font-medium"> | ||
| 188 | + {entry.display_name || entry.username || t("exams.nameless")} | ||
| 189 | + </TableCell> | ||
| 190 | + <TableCell> | ||
| 191 | + <Badge variant={ | ||
| 192 | + entry.status === "in_progress" ? "default" : | ||
| 193 | + entry.status === "submitted" ? "secondary" : | ||
| 194 | + "outline" | ||
| 195 | + }> | ||
| 196 | + {labelFromStatus(entry.status, t)} | ||
| 197 | + </Badge> | ||
| 198 | + </TableCell> | ||
| 199 | + <TableCell class="tabular-nums text-center">{entry.answered}/{m.question_count}</TableCell> | ||
| 200 | + <TableCell class="tabular-nums text-center"> | ||
| 201 | + {entry.status === "in_progress" && entry.remaining_ms > 0 | ||
| 202 | + ? `${Math.ceil(entry.remaining_ms / 60000)}dk` | ||
| 203 | + : entry.status === "in_progress" && entry.remaining_ms <= 0 | ||
| 204 | + ? `<1dk` | ||
| 205 | + : "—"} | ||
| 206 | + </TableCell> | ||
| 207 | + <TableCell class="text-xs text-center"> | ||
| 208 | + {entry.last_activity ? formatDateTime(entry.last_activity, locale()) : "—"} | ||
| 209 | + </TableCell> | ||
| 210 | + <TableCell class="tabular-nums font-semibold text-center"> | ||
| 211 | + {entry.mark != null ? entry.mark : "—"} | ||
| 212 | + </TableCell> | ||
| 213 | + </TableRow> | ||
| 214 | + )} | ||
| 215 | + </For> | ||
| 216 | + </TableBody> | ||
| 217 | + </Table> | ||
| 218 | + | ||
| 219 | + <Show when={sorted.length > PAGE_SIZE}> | ||
| 220 | + <div class="mt-4 flex items-center justify-between gap-2 border-t border-border/60 pt-3"> | ||
| 221 | + <Button | ||
| 222 | + type="button" | ||
| 223 | + variant="outline" | ||
| 224 | + size="sm" | ||
| 225 | + class="h-8 gap-1" | ||
| 226 | + disabled={safePage <= 0} | ||
| 227 | + onClick={() => setPage((p) => Math.max(0, p - 1))} | ||
| 228 | + > | ||
| 229 | + <IconChevronLeft class="h-3.5 w-3.5" /> | ||
| 230 | + {t("common.prev")} | ||
| 231 | + </Button> | ||
| 232 | + <span class="text-xs tabular-nums text-muted-foreground"> | ||
| 233 | + {t("common.pageOf", { page: safePage + 1, total: totalPages })} | ||
| 234 | + </span> | ||
| 235 | + <Button | ||
| 236 | + type="button" | ||
| 237 | + variant="outline" | ||
| 238 | + size="sm" | ||
| 239 | + class="h-8 gap-1" | ||
| 240 | + disabled={safePage >= totalPages - 1} | ||
| 241 | + onClick={() => setPage((p) => Math.min(totalPages - 1, p + 1))} | ||
| 242 | + > | ||
| 243 | + {t("common.next")} | ||
| 244 | + <IconChevronRight class="h-3.5 w-3.5" /> | ||
| 245 | + </Button> | ||
| 246 | + </div> | ||
| 247 | + </Show> | ||
| 248 | + </Show> | ||
| 249 | + </section> | ||
| 250 | + </> | ||
| 251 | + ); | ||
| 252 | + }} | ||
| 253 | + </Show> | ||
| 254 | + </div> | ||
| 255 | + </Suspense> | ||
| 256 | + ); | ||
| 257 | +} | ||
| 258 | + | ||
| 259 | +function SortHead(props: { | ||
| 260 | + label: string; | ||
| 261 | + sortKey: SortKey; | ||
| 262 | + currentKey: SortKey; | ||
| 263 | + currentDir: "asc" | "desc"; | ||
| 264 | + onSort: (key: SortKey) => void; | ||
| 265 | + class?: string; | ||
| 266 | +}) { | ||
| 267 | + const active = () => props.sortKey === props.currentKey; | ||
| 268 | + return ( | ||
| 269 | + <TableHead | ||
| 270 | + class={cn("cursor-pointer select-none hover:text-foreground", props.class, active() && "text-foreground")} | ||
| 271 | + onClick={() => props.onSort(props.sortKey)} | ||
| 272 | + > | ||
| 273 | + <span class="inline-flex items-center gap-1"> | ||
| 274 | + {props.label} | ||
| 275 | + <Show when={active()}> | ||
| 276 | + <span class="text-[10px]">{props.currentDir === "asc" ? "▲" : "▼"}</span> | ||
| 277 | + </Show> | ||
| 278 | + </span> | ||
| 279 | + </TableHead> | ||
| 280 | + ); | ||
| 281 | +} | ||
src/pages/profile-page.tsxadded+78 -0
| @@ -0,0 +1,78 @@ | |||
| 1 | +import { Show, Suspense } from "solid-js"; | ||
| 2 | +import { useNavigate } from "@tanstack/solid-router"; | ||
| 3 | +import { ProfileForm } from "@/components/users/profile-form"; | ||
| 4 | +import { RouteGuard } from "@/components/layout/route-guard"; | ||
| 5 | +import { PageHeader } from "@/components/layout/page-header"; | ||
| 6 | +import { PageSpinner } from "@/components/ui/page-spinner"; | ||
| 7 | +import { useAuth } from "@/stores/auth-context"; | ||
| 8 | +import { useT } from "@/stores/preferences-context"; | ||
| 9 | + | ||
| 10 | +export default function ProfilePage() { | ||
| 11 | + return ( | ||
| 12 | + <RouteGuard> | ||
| 13 | + <ProfileContent /> | ||
| 14 | + </RouteGuard> | ||
| 15 | + ); | ||
| 16 | +} | ||
| 17 | + | ||
| 18 | +function ProfileContent() { | ||
| 19 | + const auth = useAuth(); | ||
| 20 | + const t = useT(); | ||
| 21 | + const navigate = useNavigate(); | ||
| 22 | + | ||
| 23 | + return ( | ||
| 24 | + <Suspense fallback={<PageSpinner />}> | ||
| 25 | + <Show when={auth.user()}> | ||
| 26 | + {(u) => ( | ||
| 27 | + <div class="space-y-6"> | ||
| 28 | + <PageHeader | ||
| 29 | + accent="mint" | ||
| 30 | + eyebrow={t("nav.account")} | ||
| 31 | + title={u().username} | ||
| 32 | + description={`${u().role} · ${u().id}`} | ||
| 33 | + /> | ||
| 34 | + | ||
| 35 | + <div class="grid gap-6 lg:grid-cols-2"> | ||
| 36 | + <section class="surface-card p-5"> | ||
| 37 | + <h2 class="mb-4 font-display text-lg font-semibold">{t("profile.edit")}</h2> | ||
| 38 | + <ProfileForm | ||
| 39 | + user={u()} | ||
| 40 | + onSaved={async () => { | ||
| 41 | + await auth.refresh(); | ||
| 42 | + void navigate({ to: "/" }); | ||
| 43 | + }} | ||
| 44 | + /> | ||
| 45 | + </section> | ||
| 46 | + | ||
| 47 | + <section class="surface-card space-y-4 p-5"> | ||
| 48 | + <h2 class="font-display text-lg font-semibold">{t("nav.account")}</h2> | ||
| 49 | + <div class="grid gap-3 text-sm"> | ||
| 50 | + <div class="rounded-md border p-3"> | ||
| 51 | + <p class="text-xs text-muted-foreground">{t("profile.name")}</p> | ||
| 52 | + <p class="mt-1 font-medium">{u().name || "—"}</p> | ||
| 53 | + </div> | ||
| 54 | + <div class="rounded-md border p-3"> | ||
| 55 | + <p class="text-xs text-muted-foreground">{t("profile.surname")}</p> | ||
| 56 | + <p class="mt-1 font-medium">{u().surname || "—"}</p> | ||
| 57 | + </div> | ||
| 58 | + <div class="rounded-md border p-3"> | ||
| 59 | + <p class="text-xs text-muted-foreground">{t("profile.email")}</p> | ||
| 60 | + <p class="mt-1 font-medium">{u().email || "—"}</p> | ||
| 61 | + </div> | ||
| 62 | + <div class="rounded-md border p-3"> | ||
| 63 | + <p class="text-xs text-muted-foreground">{t("profile.phone")}</p> | ||
| 64 | + <p class="mt-1 font-medium">{u().phone || "—"}</p> | ||
| 65 | + </div> | ||
| 66 | + <div class="rounded-md border p-3"> | ||
| 67 | + <p class="text-xs text-muted-foreground">{t("profile.birthDate")}</p> | ||
| 68 | + <p class="mt-1 font-medium">{u().birth_date || "—"}</p> | ||
| 69 | + </div> | ||
| 70 | + </div> | ||
| 71 | + </section> | ||
| 72 | + </div> | ||
| 73 | + </div> | ||
| 74 | + )} | ||
| 75 | + </Show> | ||
| 76 | + </Suspense> | ||
| 77 | + ); | ||
| 78 | +} | ||
src/routes/router.tsxmodified+16 -0
| @@ -31,6 +31,8 @@ const ExamRoomPage = lazyRoute(() => import("@/pages/exam-room-page")); | |||
| 31 | const CoursesPage = lazyRoute(() => import("@/pages/courses-page")); | 31 | const CoursesPage = lazyRoute(() => import("@/pages/courses-page")); |
| 32 | const CourseDetailPage = lazyRoute(() => import("@/pages/course-detail-page")); | 32 | const CourseDetailPage = lazyRoute(() => import("@/pages/course-detail-page")); |
| 33 | const MarksPage = lazyRoute(() => import("@/pages/marks-page")); | 33 | const MarksPage = lazyRoute(() => import("@/pages/marks-page")); |
| 34 | +const ProfilePage = lazyRoute(() => import("@/pages/profile-page")); | ||
| 35 | +const LiveMonitorPage = lazyRoute(() => import("@/pages/live-monitor-page")); | ||
| 34 | const AdminUsersPage = lazyRoute(() => import("@/pages/admin-users-page")); | 36 | const AdminUsersPage = lazyRoute(() => import("@/pages/admin-users-page")); |
| 35 | const GuidePage = lazyRoute(() => import("@/pages/guide-page")); | 37 | const GuidePage = lazyRoute(() => import("@/pages/guide-page")); |
| 36 | 38 | ||
| @@ -126,6 +128,18 @@ const marksRoute = createRoute({ | |||
| 126 | component: MarksPage, | 128 | component: MarksPage, |
| 127 | }); | 129 | }); |
| 128 | 130 | ||
| 131 | +const liveMonitorRoute = createRoute({ | ||
| 132 | + getParentRoute: () => rootRoute, | ||
| 133 | + path: "/exams/$id/live", | ||
| 134 | + component: LiveMonitorPage, | ||
| 135 | +}); | ||
| 136 | + | ||
| 137 | +const profileRoute = createRoute({ | ||
| 138 | + getParentRoute: () => rootRoute, | ||
| 139 | + path: "/profile", | ||
| 140 | + component: ProfilePage, | ||
| 141 | +}); | ||
| 142 | + | ||
| 129 | const adminUsersRoute = createRoute({ | 143 | const adminUsersRoute = createRoute({ |
| 130 | getParentRoute: () => rootRoute, | 144 | getParentRoute: () => rootRoute, |
| 131 | path: "/admin/users", | 145 | path: "/admin/users", |
| @@ -147,10 +161,12 @@ const routeTree = rootRoute.addChildren([ | |||
| 147 | eventDetailRoute, | 161 | eventDetailRoute, |
| 148 | examDetailRoute, | 162 | examDetailRoute, |
| 149 | examRoomRoute, | 163 | examRoomRoute, |
| 164 | + liveMonitorRoute, | ||
| 150 | examsRoute, | 165 | examsRoute, |
| 151 | coursesRoute, | 166 | coursesRoute, |
| 152 | courseDetailRoute, | 167 | courseDetailRoute, |
| 153 | marksRoute, | 168 | marksRoute, |
| 169 | + profileRoute, | ||
| 154 | adminUsersRoute, | 170 | adminUsersRoute, |
| 155 | guideRoute, | 171 | guideRoute, |
| 156 | ]); | 172 | ]); |
vite.config.tsmodified+1 -1
| @@ -13,7 +13,7 @@ export default defineConfig({ | |||
| 13 | }, | 13 | }, |
| 14 | server: { | 14 | server: { |
| 15 | proxy: Object.fromEntries( | 15 | proxy: Object.fromEntries( |
| 16 | - ["/auth", "/users", "/notes", "/events", "/exams", "/courses", "/marks", "/health"].map((p) => [ | 16 | + ["/auth", "/users", "/notes", "/events", "/exams", "/courses", "/marks", "/health", "/time"].map((p) => [ |
| 17 | p, | 17 | p, |
| 18 | { target, changeOrigin: true }, | 18 | { target, changeOrigin: true }, |
| 19 | ]), | 19 | ]), |