diff --git a/src/pages/CourseDetail.tsx b/src/pages/CourseDetail.tsx new file mode 100644 index 0000000..75d8a9a --- /dev/null +++ b/src/pages/CourseDetail.tsx @@ -0,0 +1,307 @@ +// One course: view-first, with an edit toggle for its creator (or manager+), +// the course's exams with in-course creation, and — for teacher+ — the +// enrollment roster. + +import { A, useNavigate, useParams } from "@solidjs/router"; +import { For, Show, createResource, createSignal } from "solid-js"; +import { Empty, ErrorLine, Loading } from "../components/Feedback"; +import { IconEdit, IconPlus } from "../components/Icons"; +import { createAction } from "../lib/action"; +import { ApiError, courses } from "../lib/api"; +import { useAuth } from "../lib/auth"; +import { EXAM_KINDS, LIMITS, type Enrollment } from "../lib/types"; + +export default function CourseDetail() { + const params = useParams<{ id: string }>(); + const navigate = useNavigate(); + const { user, can } = useAuth(); + const [editing, setEditing] = createSignal(false); + + const [course, { mutate: setCourse }] = createResource(() => params.id, courses.get); + const canManage = () => { + const current = course(); + return current !== undefined && (current.creator === user()?.id || can("manager")); + }; + + const save = createAction(async (form: HTMLFormElement) => { + const data = new FormData(form); + setCourse( + await courses.update(params.id, { + title: String(data.get("title")), + description: String(data.get("description")), + }), + ); + setEditing(false); + }); + + const remove = createAction(async () => { + await courses.remove(params.id); + navigate("/courses", { replace: true }); + }); + + return ( + : } + > + {(current) => ( +
+
+
+

{current().title}

+
+ + + +
+ + +

{current().description}

+
+ + +
{ + e.preventDefault(); + void save.run(e.currentTarget); + }} + > + + + + + + + + + +
+ + + + + +
+ )} +
+ ); +} + +/** The course's exams; creator (or manager+) adds new ones here. */ +function CourseExams(props: { courseId: string; canManage: boolean }) { + const [list, { mutate }] = createResource(() => props.courseId, courses.exams); + const [creating, setCreating] = createSignal(false); + + const create = createAction(async (form: HTMLFormElement) => { + const data = new FormData(form); + const created = await courses.createExam(props.courseId, { + title: String(data.get("title")), + description: String(data.get("description")), + kind: String(data.get("kind")), + weight: Number(data.get("weight")), + }); + mutate((current) => [created, ...(current ?? [])]); + form.reset(); + setCreating(false); + }); + + return ( +
+
+

Exams

+ + + +
+ + +
{ + e.preventDefault(); + void create.run(e.currentTarget); + }} + > + queueMicrotask(() => el.focus())} + /> + +
+ + +
+ + + + + + + +
+ + + + + + + + )} + + ); +} + +/** The caller's own mark; the backend answers 404 while ungraded. */ +function MyResult(props: { examId: string }) { + const [result] = createResource( + () => props.examId, + async (id) => { + try { + return await exams.myResult(id); + } catch (err) { + if (err instanceof ApiError && err.status === 404) return null; + throw err; + } + }, + ); + + return ( +
+

My result

+ }> + Not graded yet.}> + {(mine) => ( +

+ {mine().mark} + / 100 +

+ )} +
+
+
+ ); +} + +/** Mark statistics over the graded results. Teacher+ only. */ +function Statistics(props: { examId: string }) { + const [stats] = createResource(() => props.examId, exams.statistics); + const round = (value: number) => (Math.round(value * 100) / 100).toString(); + + return ( +
+

Statistics

+ }> + + {(current) => ( + 0} fallback={Nothing graded yet.}> +
+
+ Graded + {current().graded} +
+
+ Average + {round(current().average ?? 0)} +
+
+ Min + {current().min} +
+
+ Max + {current().max} +
+
+
+ )} +
+
+
+ ); +} + +/** Full result table + grade form. Teacher+ only (the backend enforces course + * management rights and target enrollment on write). */ +function Results(props: { examId: string }) { + const { user } = useAuth(); + const [results, { mutate }] = createResource(() => props.examId, exams.results); + + const grade = createAction(async (form: HTMLFormElement) => { + const data = new FormData(form); + const graded = await exams.grade( + props.examId, + String(data.get("user_id")), + Number(data.get("mark")), + ); + // One result per (exam, user); grading again overwrites. + mutate((current) => [ + ...(current ?? []).filter((r) => r.user !== graded.user), + graded, + ]); + form.reset(); + }); + + const removeResult = createAction(async (result: ExamResult) => { + await exams.removeResult(props.examId, result.user); + mutate((current) => current?.filter((r) => r.user !== result.user)); + }); + + return ( +
+

Results

+
{ + e.preventDefault(); + void grade.run(e.currentTarget); + }} + > + + + +
+ + + }> + No results yet.}> +
+ + + + + + + + + + {(result) => ( + + + + + + )} + + +
StudentMark +
{result.user === user()?.id ? "you" : result.user}{result.mark} + +
+
+
+
+
+ ); +} + +function NotFoundMessage(props: { error: unknown }) { + const message = + props.error instanceof ApiError && props.error.status === 404 + ? "This exam does not exist." + : "Failed to load the exam."; + return {message}; +} diff --git a/src/pages/Exams.tsx b/src/pages/Exams.tsx new file mode 100644 index 0000000..0998301 --- /dev/null +++ b/src/pages/Exams.tsx @@ -0,0 +1,53 @@ +// All exams across courses. Creation lives on the course page; cards link to +// the detail page where editing, grading, results, and statistics live. + +import { A } from "@solidjs/router"; +import { For, Show, createResource } from "solid-js"; +import { Empty, Loading } from "../components/Feedback"; +import { exams } from "../lib/api"; +import { useAuth } from "../lib/auth"; + +export default function Exams() { + const { can } = useAuth(); + const [list] = createResource(exams.list); + + return ( +
+
+
+

Exams

+

+ All exams across courses. + + {" "} + New ones are created inside a course. + +

+
+
+ + }> + No exams yet.}> + + + +
+ ); +} diff --git a/src/pages/Marks.tsx b/src/pages/Marks.tsx new file mode 100644 index 0000000..eb9d643 --- /dev/null +++ b/src/pages/Marks.tsx @@ -0,0 +1,151 @@ +// Weighted mark report: every enrolled course with its graded exams, the +// course averages, and the overall average. Teacher+ can look up any student. + +import { A } from "@solidjs/router"; +import { For, Show, createResource, createSignal } from "solid-js"; +import { Empty, ErrorLine, Loading } from "../components/Feedback"; +import { createAction } from "../lib/action"; +import { marks } from "../lib/api"; +import { useAuth } from "../lib/auth"; +import type { MarksReport } from "../lib/types"; + +const round = (value: number) => (Math.round(value * 100) / 100).toString(); + +export default function Marks() { + const { can } = useAuth(); + const [mine] = createResource(marks.mine); + + return ( +
+
+
+

My marks

+

Graded exams per course, weighted averages included.

+
+
+ + }> + Failed to load your marks.}> + {(report) => } + + + + + + +
+ ); +} + +/** Teacher+ tool: fetch any student's report by id. */ +function Lookup() { + const [report, setReport] = createSignal(null); + + const fetch = createAction(async (form: HTMLFormElement) => { + const data = new FormData(form); + setReport(await marks.forUser(String(data.get("user_id")))); + }); + + return ( +
+

Student lookup

+
{ + e.preventDefault(); + void fetch.run(e.currentTarget); + }} + > + + +
+ + + + {(current) => ( + <> +

+ Report for {current().user} +

+ + + )} +
+
+ ); +} + +function Report(props: { report: MarksReport }) { + return ( + Not enrolled in any course.} + > +
+
+ Overall average +

+ {props.report.overall_average === null ? ( + "—" + ) : ( + <> + {round(props.report.overall_average)} + / 100 + + )} +

+
+ + + {(block) => ( +
+
+

+ {block.course.title} +

+ + {block.average === null ? "—" : round(block.average)} + +
+ Nothing graded yet.} + > +
+ + + + + + + + + + + + {(entry) => ( + + + + + + + )} + + +
ExamKindWeightMark
+ {entry.title} + + {entry.kind} + ×{entry.weight}{entry.mark}
+
+
+
+ )} +
+
+
+ ); +}