forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

exam-status.ts74 lines, 3.3 KiBHistoryRawDownload
1import type { AttemptStatus, Exam, LiveMonitor, LiveRosterEntry } from "@/api/client";
2import type { MessageKey } from "@/i18n/messages";
3
4export type ExamAttemptSummary = {
5 status: AttemptStatus | "not_started";
6 attempts_used: number;
7 max_attempts: number;
8};
9
10export type ExamDisplayStatus = "draft" | "unscheduled" | "upcoming" | "active" | "finished" | "submitted" | "expired" | "no_attempts_left";
11
12export function isSittableExam(exam: Exam): boolean {
13 return exam.mode === "sync" || exam.mode === "async" || exam.mode === "open";
14}
15
16export function examDisplayStatus(exam: Exam, now: number, attempt?: ExamAttemptSummary | null): ExamDisplayStatus {
17 if (attempt?.status === "submitted" || attempt?.status === "expired") return attempt.status;
18
19 const noAttemptsLeft = attempt && attempt.status !== "in_progress" && attempt.max_attempts > 0 && attempt.attempts_used >= attempt.max_attempts;
20
21 if (noAttemptsLeft) {
22 return "no_attempts_left";
23 }
24
25 if (exam.draft) return "draft";
26 if (!isSittableExam(exam)) return "unscheduled";
27 if (exam.mode === "open") return "active";
28 if (exam.ends_at != null && exam.ends_at < now) return "finished";
29 if (exam.starts_at != null && exam.starts_at > now) return "upcoming";
30 return "active";
31}
32
33/**
34 * A roster entry's status as the teacher's live monitor renders it. `left` is a
35 * student who walked out of the room mid-attempt (still `in_progress` in the DB,
36 * but with `left_at` stamped).
37 */
38export type LiveDisplayStatus = LiveRosterEntry["status"] | ExamDisplayStatus | "left";
39
40/**
41 * The live-monitor display status for one roster entry. A student actively
42 * sitting the exam (`in_progress`, still in the room) is always `in_progress`,
43 * even on their final allowed attempt — `attempts_used` counts the running
44 * sitting, so a `max_attempts >= attempts_used` test would mislabel every
45 * live single-attempt student as `no_attempts_left`. Attempt exhaustion only
46 * shows once the sitting is no longer running (submitted, expired, or walked
47 * out), which `examDisplayStatus` and the `left` branch below handle.
48 */
49export function liveDisplayStatus(entry: LiveRosterEntry, exam: LiveMonitor["exam"], now: number): LiveDisplayStatus {
50 if (entry.status === "absent") return "absent";
51 if (entry.status === "not_started") return "not_started";
52 if (entry.status === "in_progress" && entry.left_at != null) {
53 return exam.max_attempts > 0 && entry.attempts_used >= exam.max_attempts ? "no_attempts_left" : "left";
54 }
55 const status = examDisplayStatus(exam, now, { status: entry.status, attempts_used: entry.attempts_used, max_attempts: exam.max_attempts });
56 return status === "active" ? "in_progress" : status;
57}
58
59export function examStatusTone(status: ExamDisplayStatus): string {
60 if (status === "submitted") return "submitted";
61 if (status === "expired" || status === "no_attempts_left") return "finished";
62 return status;
63}
64
65export function examStatusMessageKey(status: ExamDisplayStatus): MessageKey {
66 if (status === "submitted") return "attempt.submitted";
67 if (status === "expired") return "attempt.expired";
68 if (status === "no_attempts_left") return "attempt.noAttemptsLeft";
69 if (status === "draft") return "exams.draft";
70 if (status === "unscheduled") return "exams.unscheduled";
71 if (status === "finished") return "exams.finished";
72 if (status === "upcoming") return "exams.upcoming";
73 return "exams.active";
74}