diff --git a/src/components/exams/exam-card.tsx b/src/components/exams/exam-card.tsx index ffd94b2..d833cdd 100644 --- a/src/components/exams/exam-card.tsx +++ b/src/components/exams/exam-card.tsx @@ -5,6 +5,7 @@ import { Badge } from "@/components/ui/badge"; import { ExamLink } from "@/components/exams/exam-link"; import { cn } from "@/lib/cn"; import { examKindLabel } from "@/lib/exam-labels"; +import { examDisplayStatus, examStatusTone } from "@/lib/exam-status"; import { examWeight } from "@/lib/exam-weight"; import { examDurationMs, formatDateTime, formatDurationMinutes } from "@/lib/format"; import { scheduleStatusClass, scheduleStatusDotClass } from "@/lib/schedule-status"; @@ -22,17 +23,16 @@ export function ExamCard(props: { exam: Exam; courseTitle?: string; now?: number }; const status = () => { - if (props.exam.mode !== "sync" && props.exam.mode !== "async" && props.exam.mode !== "open") return "unscheduled"; - if (props.exam.mode === "open") return "active"; - const current = props.now ?? Date.now(); - if (props.exam.ends_at != null && props.exam.ends_at < current) return "finished"; - if (props.exam.starts_at != null && props.exam.starts_at > current) return "upcoming"; - return "active"; + return examDisplayStatus(props.exam, props.now ?? Date.now()); }; const statusLabel = () => { const s = status(); + if (s === "draft") return t("exams.draft"); if (s === "unscheduled") return t("exams.unscheduled"); + if (s === "submitted") return t("attempt.submitted"); + if (s === "expired") return t("attempt.expired"); + if (s === "no_attempts_left") return t("attempt.noAttemptsLeft"); if (s === "finished") return t("exams.finished"); if (s === "upcoming") return t("exams.upcoming"); return t("exams.active"); @@ -51,8 +51,8 @@ export function ExamCard(props: { exam: Exam; courseTitle?: string; now?: number {props.exam.title} - - + + {statusLabel()} diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts index 5921226..2d07b7e 100644 --- a/src/i18n/messages.ts +++ b/src/i18n/messages.ts @@ -372,6 +372,7 @@ export type MessageKey = | "attempt.submittedInfo" | "attempt.expired" | "attempt.expiredInfo" + | "attempt.noAttemptsLeft" | "attempt.closed" | "attempt.inProgress" | "attempt.absent" @@ -983,6 +984,7 @@ const en: Dict = { "attempt.submittedInfo": "You have already submitted this exam. It cannot be reopened.", "attempt.expired": "Expired", "attempt.expiredInfo": "The time for this exam has expired. It cannot be reopened.", + "attempt.noAttemptsLeft": "No attempts left", "attempt.closed": "This attempt is closed. Answers are read-only.", "attempt.inProgress": "In progress", "attempt.absent": "No-show", @@ -1604,6 +1606,7 @@ const tr: Dict = { "attempt.submittedInfo": "Bu sınavı zaten teslim ettin. Yeniden açılamaz.", "attempt.expired": "Süresi doldu", "attempt.expiredInfo": "Bu sınavın süresi doldu. Yeniden açılamaz.", + "attempt.noAttemptsLeft": "Deneme hakkı bitti", "attempt.closed": "Bu oturum kapalı. Cevaplar salt okunur.", "attempt.inProgress": "Devam ediyor", "attempt.absent": "Katılmadı", diff --git a/src/lib/exam-status.ts b/src/lib/exam-status.ts new file mode 100644 index 0000000..fafebd5 --- /dev/null +++ b/src/lib/exam-status.ts @@ -0,0 +1,30 @@ +import type { AttemptStatus, Exam } from "@/api/types"; + +export type ExamAttemptSummary = { + status: AttemptStatus | "not_started"; + attempts_used: number; + max_attempts: number; +}; + +export type ExamDisplayStatus = "draft" | "unscheduled" | "upcoming" | "active" | "finished" | "submitted" | "expired" | "no_attempts_left"; + +export function isSittableExam(exam: Exam): boolean { + return exam.mode === "sync" || exam.mode === "async" || exam.mode === "open"; +} + +export function examDisplayStatus(exam: Exam, now: number, attempt?: ExamAttemptSummary | null): ExamDisplayStatus { + if (attempt?.status === "submitted" || attempt?.status === "expired") return attempt.status; + if (attempt && attempt.max_attempts > 0 && attempt.attempts_used >= attempt.max_attempts) return "no_attempts_left"; + if (exam.draft) return "draft"; + if (!isSittableExam(exam)) return "unscheduled"; + if (exam.mode === "open") return "active"; + if (exam.ends_at != null && exam.ends_at < now) return "finished"; + if (exam.starts_at != null && exam.starts_at > now) return "upcoming"; + return "active"; +} + +export function examStatusTone(status: ExamDisplayStatus): string { + if (status === "submitted") return "submitted"; + if (status === "expired" || status === "no_attempts_left") return "finished"; + return status; +} diff --git a/src/pages/dashboard-page.tsx b/src/pages/dashboard-page.tsx index 8eb41e6..81f3cf6 100644 --- a/src/pages/dashboard-page.tsx +++ b/src/pages/dashboard-page.tsx @@ -1,7 +1,8 @@ -import { For, Show, createEffect, createMemo, createResource, createSignal, type Component } from "solid-js"; +import { For, Show, createEffect, createMemo, createResource, createSignal, onCleanup, type Component } from "solid-js"; import { Link } from "@tanstack/solid-router"; import { formatApiError } from "@/api/client"; import { getCourses } from "@/api/getCourses"; +import { getExamAttempt } from "@/api/getExamAttempt"; import { getEvents } from "@/api/getEvents"; import { getExams } from "@/api/getExams"; import { getMyCourses } from "@/api/getMyCourses"; @@ -29,6 +30,7 @@ import type { MessageKey } from "@/i18n/messages"; import { cn } from "@/lib/cn"; import { createNow } from "@/lib/create-now"; import { examKindLabel } from "@/lib/exam-labels"; +import { examDisplayStatus, isSittableExam, type ExamAttemptSummary } from "@/lib/exam-status"; import { formatDateTime } from "@/lib/format"; import { hasMinRole } from "@/lib/roles"; import { scheduleStatusClass, scheduleStatusDotClass } from "@/lib/schedule-status"; @@ -68,17 +70,17 @@ const ROLE_KEY: Record = { admin: "role.admin", }; -function examWindow(exam: Exam, now: number): AttentionKind | "upcoming" | "past" | "unscheduled" { - if (exam.mode === "open") return "active"; - if (exam.mode !== "sync" && exam.mode !== "async") return "unscheduled"; - if (exam.ends_at != null && exam.ends_at < now) return "past"; +function examWindow(exam: Exam, now: number, attempt?: ExamAttemptSummary | null): AttentionKind | "upcoming" | "past" | "unscheduled" { + const status = examDisplayStatus(exam, now, attempt); + if (status === "active") return "active"; + if (status !== "upcoming") return status === "unscheduled" ? "unscheduled" : "past"; if (exam.starts_at != null && exam.starts_at > now) { const delta = exam.starts_at - now; if (delta <= 24 * 60 * 60 * 1000) return "today"; if (delta <= WEEK_MS) return "soon"; return "upcoming"; } - return "active"; + return "upcoming"; } function eventWindow(event: Event, now: number): AttentionKind | "upcoming" | "past" | "unscheduled" { @@ -114,6 +116,7 @@ function DashboardContent() { const [draggingPortal, setDraggingPortal] = createSignal(null); const [dragOverPortal, setDragOverPortal] = createSignal(null); const [editingPortalOrder, setEditingPortalOrder] = createSignal(false); + const [attemptStatuses, setAttemptStatuses] = createSignal>({}); const [courses] = createResource( () => (role() !== "student" ? true : null), @@ -173,6 +176,26 @@ function DashboardContent() { return all; }); + createEffect(() => { + if (role() !== "student") return; + const exams = visibleExams(); + const refresh = () => { + for (const exam of exams) { + if (!isSittableExam(exam)) continue; + const current = attemptStatuses()[exam.id]; + if (current?.status === "submitted" || current?.status === "expired") continue; + if (current && current.max_attempts > 0 && current.attempts_used >= current.max_attempts) continue; + if (current != null && current.status !== "in_progress") continue; + void getExamAttempt(exam.id) + .then((attempt) => setAttemptStatuses((prev) => ({ ...prev, [exam.id]: { status: attempt.status, attempts_used: attempt.attempts_used, max_attempts: attempt.max_attempts } }))) + .catch(() => setAttemptStatuses((prev) => ({ ...prev, [exam.id]: { status: "not_started", attempts_used: 0, max_attempts: exam.max_attempts } }))); + } + }; + refresh(); + const interval = window.setInterval(refresh, 5000); + onCleanup(() => window.clearInterval(interval)); + }); + const countCoursesByKind = (kind: Course["kind"]) => scopedCourses().filter((course) => course.kind === kind).length; const courseCount = createMemo(() => countCoursesByKind("course")); const studyCount = createMemo(() => countCoursesByKind("study")); @@ -297,7 +320,7 @@ function DashboardContent() { const items: AttentionItem[] = []; for (const exam of visibleExams()) { - const status = examWindow(exam, n); + const status = examWindow(exam, n, attemptStatuses()[exam.id]); if (status === "active" || status === "today" || status === "soon") { items.push({ id: exam.id, @@ -342,7 +365,7 @@ function DashboardContent() { for (const exam of visibleExams()) { if (exam.starts_at != null && exam.starts_at > n && exam.starts_at <= n + WEEK_MS) { - const w = examWindow(exam, n); + const w = examWindow(exam, n, attemptStatuses()[exam.id]); if (w === "upcoming" || w === "soon" || w === "today") { items.push({ id: exam.id, diff --git a/src/pages/exam-detail-page.tsx b/src/pages/exam-detail-page.tsx index bc1af55..3cfacd9 100644 --- a/src/pages/exam-detail-page.tsx +++ b/src/pages/exam-detail-page.tsx @@ -14,7 +14,7 @@ import { getSettings } from "@/api/getSettings"; import { patchExamById } from "@/api/patchExamById"; import { postExamResult } from "@/api/postExamResult"; import { ApiError, formatApiError } from "@/api/client"; -import type { AttemptStatus, ExamResult } from "@/api/types"; +import type { ExamResult } from "@/api/types"; import { ExamForm } from "@/components/exams/exam-form"; import { ExamQuestionsPanel } from "@/components/exams/exam-questions-panel"; import { AnswerSheetView } from "@/components/exams/answer-sheet-view"; @@ -35,6 +35,7 @@ import { TableRowActions } from "@/components/ui/table-row-actions"; import { hasMinRole } from "@/lib/roles"; import { createNow } from "@/lib/create-now"; import { examKindLabel } from "@/lib/exam-labels"; +import { examDisplayStatus, examStatusTone, isSittableExam, type ExamAttemptSummary, type ExamDisplayStatus } from "@/lib/exam-status"; import { examWeight } from "@/lib/exam-weight"; import { examDurationMs, formatDateTime, formatDurationMinutes } from "@/lib/format"; import { personId, personLabel, personLabelWithId } from "@/lib/person"; @@ -92,7 +93,10 @@ function ExamDetailContent() { questions: false, results: false, }); - const isSittable = () => exam()?.mode === "sync" || exam()?.mode === "async" || exam()?.mode === "open"; + const isSittable = () => { + const e = exam(); + return e ? isSittableExam(e) : false; + }; const isScheduled = () => isSittable(); const [ownResult] = createResource( @@ -157,16 +161,17 @@ function ExamDetailContent() { }; const isFinished = () => examStatus().finished; const isUpcoming = () => examStatus().upcoming; - const ownAttemptStatus = (): AttemptStatus | null => ownAttempt()?.status ?? null; - const detailStatus = () => { - const own = ownAttemptStatus(); - if (own === "submitted" || own === "expired") return own; - return !isScheduled() ? "unscheduled" : isFinished() ? "finished" : isUpcoming() ? "upcoming" : "active"; + const ownAttemptSummary = (): ExamAttemptSummary | null => { + const attempt = ownAttempt(); + return attempt ? { status: attempt.status, attempts_used: attempt.attempts_used, max_attempts: attempt.max_attempts } : null; }; + const noAttemptsLeft = () => detailStatus() === "no_attempts_left"; + const detailStatus = (): ExamDisplayStatus => exam() ? examDisplayStatus(exam()!, now(), ownAttemptSummary()) : "unscheduled"; const detailStatusLabel = () => { const status = detailStatus(); if (status === "submitted") return t("attempt.submitted"); if (status === "expired") return t("attempt.expired"); + if (status === "no_attempts_left") return t("attempt.noAttemptsLeft"); if (status === "unscheduled") return t("exams.unscheduled"); if (status === "finished") return t("exams.finished"); if (status === "upcoming") return t("exams.upcoming"); @@ -301,7 +306,7 @@ function ExamDetailContent() { -