✨ add courses, exams, and marks pages
Browse files at this commitdda2da62d8e5394857080d7c6bab42d17131fc34Parent: a35272f- feat
- ✨ add courses, exams, and marks pages
5 changed files +929 -0
- src/pages/CourseDetail.tsx +307 -0
- src/pages/Courses.tsx +101 -0
- src/pages/ExamDetail.tsx +317 -0
- src/pages/Exams.tsx +53 -0
- src/pages/Marks.tsx +151 -0
src/pages/CourseDetail.tsxadded+307 -0
| @@ -0,0 +1,307 @@ | |||
| 1 | +// One course: view-first, with an edit toggle for its creator (or manager+), | ||
| 2 | +// the course's exams with in-course creation, and — for teacher+ — the | ||
| 3 | +// enrollment roster. | ||
| 4 | + | ||
| 5 | +import { A, useNavigate, useParams } from "@solidjs/router"; | ||
| 6 | +import { For, Show, createResource, createSignal } from "solid-js"; | ||
| 7 | +import { Empty, ErrorLine, Loading } from "../components/Feedback"; | ||
| 8 | +import { IconEdit, IconPlus } from "../components/Icons"; | ||
| 9 | +import { createAction } from "../lib/action"; | ||
| 10 | +import { ApiError, courses } from "../lib/api"; | ||
| 11 | +import { useAuth } from "../lib/auth"; | ||
| 12 | +import { EXAM_KINDS, LIMITS, type Enrollment } from "../lib/types"; | ||
| 13 | + | ||
| 14 | +export default function CourseDetail() { | ||
| 15 | + const params = useParams<{ id: string }>(); | ||
| 16 | + const navigate = useNavigate(); | ||
| 17 | + const { user, can } = useAuth(); | ||
| 18 | + const [editing, setEditing] = createSignal(false); | ||
| 19 | + | ||
| 20 | + const [course, { mutate: setCourse }] = createResource(() => params.id, courses.get); | ||
| 21 | + const canManage = () => { | ||
| 22 | + const current = course(); | ||
| 23 | + return current !== undefined && (current.creator === user()?.id || can("manager")); | ||
| 24 | + }; | ||
| 25 | + | ||
| 26 | + const save = createAction(async (form: HTMLFormElement) => { | ||
| 27 | + const data = new FormData(form); | ||
| 28 | + setCourse( | ||
| 29 | + await courses.update(params.id, { | ||
| 30 | + title: String(data.get("title")), | ||
| 31 | + description: String(data.get("description")), | ||
| 32 | + }), | ||
| 33 | + ); | ||
| 34 | + setEditing(false); | ||
| 35 | + }); | ||
| 36 | + | ||
| 37 | + const remove = createAction(async () => { | ||
| 38 | + await courses.remove(params.id); | ||
| 39 | + navigate("/courses", { replace: true }); | ||
| 40 | + }); | ||
| 41 | + | ||
| 42 | + return ( | ||
| 43 | + <Show | ||
| 44 | + when={course()} | ||
| 45 | + fallback={course.error ? <NotFoundMessage error={course.error} /> : <Loading />} | ||
| 46 | + > | ||
| 47 | + {(current) => ( | ||
| 48 | + <section class="page"> | ||
| 49 | + <header class="page-head"> | ||
| 50 | + <div> | ||
| 51 | + <h1>{current().title}</h1> | ||
| 52 | + </div> | ||
| 53 | + <Show when={canManage()}> | ||
| 54 | + <button class="ghost" onClick={() => setEditing((open) => !open)}> | ||
| 55 | + <IconEdit /> {editing() ? "Close" : "Edit"} | ||
| 56 | + </button> | ||
| 57 | + </Show> | ||
| 58 | + </header> | ||
| 59 | + | ||
| 60 | + <Show when={!editing() && current().description}> | ||
| 61 | + <p class="prewrap">{current().description}</p> | ||
| 62 | + </Show> | ||
| 63 | + | ||
| 64 | + <Show when={editing()}> | ||
| 65 | + <form | ||
| 66 | + class="card stack" | ||
| 67 | + onSubmit={(e) => { | ||
| 68 | + e.preventDefault(); | ||
| 69 | + void save.run(e.currentTarget); | ||
| 70 | + }} | ||
| 71 | + > | ||
| 72 | + <input | ||
| 73 | + name="title" | ||
| 74 | + value={current().title} | ||
| 75 | + required | ||
| 76 | + maxLength={LIMITS.courseTitle} | ||
| 77 | + /> | ||
| 78 | + <textarea name="description" rows={3} maxLength={LIMITS.courseDescription}> | ||
| 79 | + {current().description} | ||
| 80 | + </textarea> | ||
| 81 | + <ErrorLine error={save.error() ?? remove.error()} /> | ||
| 82 | + <span class="row-actions"> | ||
| 83 | + <button type="submit" disabled={save.pending()}> | ||
| 84 | + Save | ||
| 85 | + </button> | ||
| 86 | + <button type="button" class="ghost" onClick={() => setEditing(false)}> | ||
| 87 | + Cancel | ||
| 88 | + </button> | ||
| 89 | + <button | ||
| 90 | + type="button" | ||
| 91 | + class="ghost danger push" | ||
| 92 | + disabled={remove.pending()} | ||
| 93 | + onClick={() => void remove.run()} | ||
| 94 | + > | ||
| 95 | + Delete course | ||
| 96 | + </button> | ||
| 97 | + </span> | ||
| 98 | + </form> | ||
| 99 | + </Show> | ||
| 100 | + | ||
| 101 | + <CourseExams courseId={params.id} canManage={canManage()} /> | ||
| 102 | + <Show when={can("teacher")}> | ||
| 103 | + <Roster courseId={params.id} canManage={canManage()} /> | ||
| 104 | + </Show> | ||
| 105 | + </section> | ||
| 106 | + )} | ||
| 107 | + </Show> | ||
| 108 | + ); | ||
| 109 | +} | ||
| 110 | + | ||
| 111 | +/** The course's exams; creator (or manager+) adds new ones here. */ | ||
| 112 | +function CourseExams(props: { courseId: string; canManage: boolean }) { | ||
| 113 | + const [list, { mutate }] = createResource(() => props.courseId, courses.exams); | ||
| 114 | + const [creating, setCreating] = createSignal(false); | ||
| 115 | + | ||
| 116 | + const create = createAction(async (form: HTMLFormElement) => { | ||
| 117 | + const data = new FormData(form); | ||
| 118 | + const created = await courses.createExam(props.courseId, { | ||
| 119 | + title: String(data.get("title")), | ||
| 120 | + description: String(data.get("description")), | ||
| 121 | + kind: String(data.get("kind")), | ||
| 122 | + weight: Number(data.get("weight")), | ||
| 123 | + }); | ||
| 124 | + mutate((current) => [created, ...(current ?? [])]); | ||
| 125 | + form.reset(); | ||
| 126 | + setCreating(false); | ||
| 127 | + }); | ||
| 128 | + | ||
| 129 | + return ( | ||
| 130 | + <article class="stack gap-top"> | ||
| 131 | + <div class="section-head"> | ||
| 132 | + <h2>Exams</h2> | ||
| 133 | + <Show when={props.canManage}> | ||
| 134 | + <button class="ghost" onClick={() => setCreating((open) => !open)}> | ||
| 135 | + <IconPlus /> New exam | ||
| 136 | + </button> | ||
| 137 | + </Show> | ||
| 138 | + </div> | ||
| 139 | + | ||
| 140 | + <Show when={creating()}> | ||
| 141 | + <form | ||
| 142 | + class="card stack" | ||
| 143 | + onSubmit={(e) => { | ||
| 144 | + e.preventDefault(); | ||
| 145 | + void create.run(e.currentTarget); | ||
| 146 | + }} | ||
| 147 | + > | ||
| 148 | + <input | ||
| 149 | + name="title" | ||
| 150 | + placeholder="Title" | ||
| 151 | + required | ||
| 152 | + maxLength={LIMITS.examTitle} | ||
| 153 | + ref={(el) => queueMicrotask(() => el.focus())} | ||
| 154 | + /> | ||
| 155 | + <textarea | ||
| 156 | + name="description" | ||
| 157 | + placeholder="Description" | ||
| 158 | + rows={2} | ||
| 159 | + maxLength={LIMITS.examDescription} | ||
| 160 | + /> | ||
| 161 | + <div class="row"> | ||
| 162 | + <label> | ||
| 163 | + Kind | ||
| 164 | + <select name="kind"> | ||
| 165 | + <For each={EXAM_KINDS}>{(kind) => <option value={kind}>{kind}</option>}</For> | ||
| 166 | + </select> | ||
| 167 | + </label> | ||
| 168 | + <label> | ||
| 169 | + Weight | ||
| 170 | + <input | ||
| 171 | + name="weight" | ||
| 172 | + type="number" | ||
| 173 | + value="1" | ||
| 174 | + required | ||
| 175 | + min={LIMITS.weight.min} | ||
| 176 | + max={LIMITS.weight.max} | ||
| 177 | + /> | ||
| 178 | + </label> | ||
| 179 | + </div> | ||
| 180 | + <ErrorLine error={create.error()} /> | ||
| 181 | + <span class="row-actions"> | ||
| 182 | + <button type="submit" disabled={create.pending()}> | ||
| 183 | + Add exam | ||
| 184 | + </button> | ||
| 185 | + <button type="button" class="ghost" onClick={() => setCreating(false)}> | ||
| 186 | + Cancel | ||
| 187 | + </button> | ||
| 188 | + </span> | ||
| 189 | + </form> | ||
| 190 | + </Show> | ||
| 191 | + | ||
| 192 | + <Show when={!list.loading} fallback={<Loading />}> | ||
| 193 | + <Show when={list()?.length} fallback={<Empty>No exams yet.</Empty>}> | ||
| 194 | + <div class="grid"> | ||
| 195 | + <For each={list()}> | ||
| 196 | + {(exam) => ( | ||
| 197 | + <A href={`/exams/${exam.id}`} class="card link-card stack"> | ||
| 198 | + <header class="row"> | ||
| 199 | + <h3>{exam.title}</h3> | ||
| 200 | + <span class="badge">{exam.kind}</span> | ||
| 201 | + <span class="badge" title="weight in the course average"> | ||
| 202 | + ×{exam.weight} | ||
| 203 | + </span> | ||
| 204 | + </header> | ||
| 205 | + <Show when={exam.description}> | ||
| 206 | + <p class="muted clamp">{exam.description}</p> | ||
| 207 | + </Show> | ||
| 208 | + </A> | ||
| 209 | + )} | ||
| 210 | + </For> | ||
| 211 | + </div> | ||
| 212 | + </Show> | ||
| 213 | + </Show> | ||
| 214 | + </article> | ||
| 215 | + ); | ||
| 216 | +} | ||
| 217 | + | ||
| 218 | +/** Enrollment roster. Teacher+ sees it; creator (or manager+) edits it. */ | ||
| 219 | +function Roster(props: { courseId: string; canManage: boolean }) { | ||
| 220 | + const { user } = useAuth(); | ||
| 221 | + const [roster, { mutate }] = createResource(() => props.courseId, courses.roster); | ||
| 222 | + | ||
| 223 | + const enroll = createAction(async (form: HTMLFormElement) => { | ||
| 224 | + const data = new FormData(form); | ||
| 225 | + const enrolled = await courses.enroll(props.courseId, String(data.get("user_id"))); | ||
| 226 | + // Enrolling is an idempotent upsert; one row per (course, user). | ||
| 227 | + mutate((current) => [ | ||
| 228 | + ...(current ?? []).filter((e) => e.user !== enrolled.user), | ||
| 229 | + enrolled, | ||
| 230 | + ]); | ||
| 231 | + form.reset(); | ||
| 232 | + }); | ||
| 233 | + | ||
| 234 | + const unenroll = createAction(async (entry: Enrollment) => { | ||
| 235 | + await courses.unenroll(props.courseId, entry.user); | ||
| 236 | + mutate((current) => current?.filter((e) => e.user !== entry.user)); | ||
| 237 | + }); | ||
| 238 | + | ||
| 239 | + return ( | ||
| 240 | + <article class="stack gap-top"> | ||
| 241 | + <h2>Roster</h2> | ||
| 242 | + | ||
| 243 | + <Show when={props.canManage}> | ||
| 244 | + <form | ||
| 245 | + class="row" | ||
| 246 | + onSubmit={(e) => { | ||
| 247 | + e.preventDefault(); | ||
| 248 | + void enroll.run(e.currentTarget); | ||
| 249 | + }} | ||
| 250 | + > | ||
| 251 | + <input name="user_id" placeholder="Student id" required /> | ||
| 252 | + <button type="submit" disabled={enroll.pending()}> | ||
| 253 | + Enroll | ||
| 254 | + </button> | ||
| 255 | + </form> | ||
| 256 | + <ErrorLine error={enroll.error() ?? unenroll.error()} /> | ||
| 257 | + </Show> | ||
| 258 | + | ||
| 259 | + <Show when={!roster.loading} fallback={<Loading />}> | ||
| 260 | + <Show when={roster()?.length} fallback={<Empty>Nobody enrolled yet.</Empty>}> | ||
| 261 | + <div class="table-wrap"> | ||
| 262 | + <table> | ||
| 263 | + <thead> | ||
| 264 | + <tr> | ||
| 265 | + <th>Student</th> | ||
| 266 | + <th>Enrolled by</th> | ||
| 267 | + <Show when={props.canManage}> | ||
| 268 | + <th /> | ||
| 269 | + </Show> | ||
| 270 | + </tr> | ||
| 271 | + </thead> | ||
| 272 | + <tbody> | ||
| 273 | + <For each={roster()}> | ||
| 274 | + {(entry) => ( | ||
| 275 | + <tr> | ||
| 276 | + <td class="mono">{entry.user === user()?.id ? "you" : entry.user}</td> | ||
| 277 | + <td class="mono">{entry.enrolled_by}</td> | ||
| 278 | + <Show when={props.canManage}> | ||
| 279 | + <td> | ||
| 280 | + <button | ||
| 281 | + class="ghost danger" | ||
| 282 | + disabled={unenroll.pending()} | ||
| 283 | + onClick={() => void unenroll.run(entry)} | ||
| 284 | + > | ||
| 285 | + Unenroll | ||
| 286 | + </button> | ||
| 287 | + </td> | ||
| 288 | + </Show> | ||
| 289 | + </tr> | ||
| 290 | + )} | ||
| 291 | + </For> | ||
| 292 | + </tbody> | ||
| 293 | + </table> | ||
| 294 | + </div> | ||
| 295 | + </Show> | ||
| 296 | + </Show> | ||
| 297 | + </article> | ||
| 298 | + ); | ||
| 299 | +} | ||
| 300 | + | ||
| 301 | +function NotFoundMessage(props: { error: unknown }) { | ||
| 302 | + const message = | ||
| 303 | + props.error instanceof ApiError && props.error.status === 404 | ||
| 304 | + ? "This course does not exist." | ||
| 305 | + : "Failed to load the course."; | ||
| 306 | + return <Empty>{message}</Empty>; | ||
| 307 | +} | ||
src/pages/Courses.tsxadded+101 -0
| @@ -0,0 +1,101 @@ | |||
| 1 | +// Course list plus creation (teacher+). Cards link to the detail page where | ||
| 2 | +// editing, the roster, and the course's exams live. | ||
| 3 | + | ||
| 4 | +import { A } from "@solidjs/router"; | ||
| 5 | +import { For, Show, createResource, createSignal } from "solid-js"; | ||
| 6 | +import { Empty, ErrorLine, Loading } from "../components/Feedback"; | ||
| 7 | +import { IconPlus } from "../components/Icons"; | ||
| 8 | +import { createAction } from "../lib/action"; | ||
| 9 | +import { courses } from "../lib/api"; | ||
| 10 | +import { useAuth } from "../lib/auth"; | ||
| 11 | +import { LIMITS } from "../lib/types"; | ||
| 12 | + | ||
| 13 | +export default function Courses() { | ||
| 14 | + const { can } = useAuth(); | ||
| 15 | + const [list, { mutate }] = createResource(courses.list); | ||
| 16 | + const [mine] = createResource(courses.mine); | ||
| 17 | + const enrolled = () => new Set(mine()?.map((course) => course.id)); | ||
| 18 | + const [creating, setCreating] = createSignal(false); | ||
| 19 | + | ||
| 20 | + const create = createAction(async (form: HTMLFormElement) => { | ||
| 21 | + const data = new FormData(form); | ||
| 22 | + const created = await courses.create({ | ||
| 23 | + title: String(data.get("title")), | ||
| 24 | + description: String(data.get("description")), | ||
| 25 | + }); | ||
| 26 | + mutate((current) => [created, ...(current ?? [])]); | ||
| 27 | + form.reset(); | ||
| 28 | + setCreating(false); | ||
| 29 | + }); | ||
| 30 | + | ||
| 31 | + return ( | ||
| 32 | + <section class="page"> | ||
| 33 | + <header class="page-head"> | ||
| 34 | + <div> | ||
| 35 | + <h1>Courses</h1> | ||
| 36 | + <p class="sub">Everything on offer — your enrollments are tagged.</p> | ||
| 37 | + </div> | ||
| 38 | + <Show when={can("teacher")}> | ||
| 39 | + <button onClick={() => setCreating((open) => !open)}> | ||
| 40 | + <IconPlus /> New course | ||
| 41 | + </button> | ||
| 42 | + </Show> | ||
| 43 | + </header> | ||
| 44 | + | ||
| 45 | + <Show when={creating()}> | ||
| 46 | + <form | ||
| 47 | + class="card stack" | ||
| 48 | + onSubmit={(e) => { | ||
| 49 | + e.preventDefault(); | ||
| 50 | + void create.run(e.currentTarget); | ||
| 51 | + }} | ||
| 52 | + > | ||
| 53 | + <input | ||
| 54 | + name="title" | ||
| 55 | + placeholder="Title" | ||
| 56 | + required | ||
| 57 | + maxLength={LIMITS.courseTitle} | ||
| 58 | + ref={(el) => queueMicrotask(() => el.focus())} | ||
| 59 | + /> | ||
| 60 | + <textarea | ||
| 61 | + name="description" | ||
| 62 | + placeholder="Description" | ||
| 63 | + rows={2} | ||
| 64 | + maxLength={LIMITS.courseDescription} | ||
| 65 | + /> | ||
| 66 | + <ErrorLine error={create.error()} /> | ||
| 67 | + <span class="row-actions"> | ||
| 68 | + <button type="submit" disabled={create.pending()}> | ||
| 69 | + Add course | ||
| 70 | + </button> | ||
| 71 | + <button type="button" class="ghost" onClick={() => setCreating(false)}> | ||
| 72 | + Cancel | ||
| 73 | + </button> | ||
| 74 | + </span> | ||
| 75 | + </form> | ||
| 76 | + </Show> | ||
| 77 | + | ||
| 78 | + <Show when={!list.loading} fallback={<Loading />}> | ||
| 79 | + <Show when={list()?.length} fallback={<Empty>No courses yet.</Empty>}> | ||
| 80 | + <div class="grid"> | ||
| 81 | + <For each={list()}> | ||
| 82 | + {(course) => ( | ||
| 83 | + <A href={`/courses/${course.id}`} class="card link-card stack"> | ||
| 84 | + <header class="row"> | ||
| 85 | + <h3>{course.title}</h3> | ||
| 86 | + <Show when={enrolled().has(course.id)}> | ||
| 87 | + <span class="badge">enrolled</span> | ||
| 88 | + </Show> | ||
| 89 | + </header> | ||
| 90 | + <Show when={course.description}> | ||
| 91 | + <p class="muted clamp">{course.description}</p> | ||
| 92 | + </Show> | ||
| 93 | + </A> | ||
| 94 | + )} | ||
| 95 | + </For> | ||
| 96 | + </div> | ||
| 97 | + </Show> | ||
| 98 | + </Show> | ||
| 99 | + </section> | ||
| 100 | + ); | ||
| 101 | +} | ||
src/pages/ExamDetail.tsxadded+317 -0
| @@ -0,0 +1,317 @@ | |||
| 1 | +// One exam: view-first, with an edit toggle for whoever manages its course | ||
| 2 | +// (creator or manager+), the caller's own mark, and — for teacher+ — grading, | ||
| 3 | +// results, statistics. | ||
| 4 | + | ||
| 5 | +import { A, useNavigate, useParams } from "@solidjs/router"; | ||
| 6 | +import { For, Show, createResource, createSignal } from "solid-js"; | ||
| 7 | +import { Empty, ErrorLine, Loading } from "../components/Feedback"; | ||
| 8 | +import { IconEdit } from "../components/Icons"; | ||
| 9 | +import { createAction } from "../lib/action"; | ||
| 10 | +import { ApiError, courses, exams } from "../lib/api"; | ||
| 11 | +import { useAuth } from "../lib/auth"; | ||
| 12 | +import { EXAM_KINDS, LIMITS, type ExamResult } from "../lib/types"; | ||
| 13 | + | ||
| 14 | +export default function ExamDetail() { | ||
| 15 | + const params = useParams<{ id: string }>(); | ||
| 16 | + const navigate = useNavigate(); | ||
| 17 | + const { user, can } = useAuth(); | ||
| 18 | + const [editing, setEditing] = createSignal(false); | ||
| 19 | + | ||
| 20 | + const [exam, { mutate: setExam }] = createResource(() => params.id, exams.get); | ||
| 21 | + // Management rights hang off the exam's course, not the exam row itself. | ||
| 22 | + const [course] = createResource(() => exam()?.course, courses.get); | ||
| 23 | + const canManage = () => { | ||
| 24 | + const parent = course(); | ||
| 25 | + return parent !== undefined && (parent.creator === user()?.id || can("manager")); | ||
| 26 | + }; | ||
| 27 | + | ||
| 28 | + const save = createAction(async (form: HTMLFormElement) => { | ||
| 29 | + const data = new FormData(form); | ||
| 30 | + setExam( | ||
| 31 | + await exams.update(params.id, { | ||
| 32 | + title: String(data.get("title")), | ||
| 33 | + description: String(data.get("description")), | ||
| 34 | + kind: String(data.get("kind")), | ||
| 35 | + weight: Number(data.get("weight")), | ||
| 36 | + }), | ||
| 37 | + ); | ||
| 38 | + setEditing(false); | ||
| 39 | + }); | ||
| 40 | + | ||
| 41 | + const remove = createAction(async () => { | ||
| 42 | + await exams.remove(params.id); | ||
| 43 | + navigate("/exams", { replace: true }); | ||
| 44 | + }); | ||
| 45 | + | ||
| 46 | + return ( | ||
| 47 | + <Show | ||
| 48 | + when={exam()} | ||
| 49 | + fallback={exam.error ? <NotFoundMessage error={exam.error} /> : <Loading />} | ||
| 50 | + > | ||
| 51 | + {(current) => ( | ||
| 52 | + <section class="page"> | ||
| 53 | + <header class="page-head"> | ||
| 54 | + <div> | ||
| 55 | + <div class="row"> | ||
| 56 | + <h1>{current().title}</h1> | ||
| 57 | + <span class="badge">{current().kind}</span> | ||
| 58 | + <span class="badge" title="weight in the course average"> | ||
| 59 | + ×{current().weight} | ||
| 60 | + </span> | ||
| 61 | + </div> | ||
| 62 | + <p class="sub"> | ||
| 63 | + Course:{" "} | ||
| 64 | + <A href={`/courses/${current().course}`}> | ||
| 65 | + {course()?.title ?? current().course} | ||
| 66 | + </A> | ||
| 67 | + </p> | ||
| 68 | + </div> | ||
| 69 | + <Show when={canManage()}> | ||
| 70 | + <button class="ghost" onClick={() => setEditing((open) => !open)}> | ||
| 71 | + <IconEdit /> {editing() ? "Close" : "Edit"} | ||
| 72 | + </button> | ||
| 73 | + </Show> | ||
| 74 | + </header> | ||
| 75 | + | ||
| 76 | + <Show when={!editing() && current().description}> | ||
| 77 | + <p class="prewrap">{current().description}</p> | ||
| 78 | + </Show> | ||
| 79 | + | ||
| 80 | + <Show when={editing()}> | ||
| 81 | + <form | ||
| 82 | + class="card stack" | ||
| 83 | + onSubmit={(e) => { | ||
| 84 | + e.preventDefault(); | ||
| 85 | + void save.run(e.currentTarget); | ||
| 86 | + }} | ||
| 87 | + > | ||
| 88 | + <input | ||
| 89 | + name="title" | ||
| 90 | + value={current().title} | ||
| 91 | + required | ||
| 92 | + maxLength={LIMITS.examTitle} | ||
| 93 | + /> | ||
| 94 | + <textarea name="description" rows={3} maxLength={LIMITS.examDescription}> | ||
| 95 | + {current().description} | ||
| 96 | + </textarea> | ||
| 97 | + <div class="row"> | ||
| 98 | + <label> | ||
| 99 | + Kind | ||
| 100 | + <select name="kind" value={current().kind}> | ||
| 101 | + <For each={EXAM_KINDS}> | ||
| 102 | + {(kind) => ( | ||
| 103 | + <option value={kind} selected={kind === current().kind}> | ||
| 104 | + {kind} | ||
| 105 | + </option> | ||
| 106 | + )} | ||
| 107 | + </For> | ||
| 108 | + </select> | ||
| 109 | + </label> | ||
| 110 | + <label> | ||
| 111 | + Weight | ||
| 112 | + <input | ||
| 113 | + name="weight" | ||
| 114 | + type="number" | ||
| 115 | + value={current().weight} | ||
| 116 | + required | ||
| 117 | + min={LIMITS.weight.min} | ||
| 118 | + max={LIMITS.weight.max} | ||
| 119 | + /> | ||
| 120 | + </label> | ||
| 121 | + </div> | ||
| 122 | + <ErrorLine error={save.error() ?? remove.error()} /> | ||
| 123 | + <span class="row-actions"> | ||
| 124 | + <button type="submit" disabled={save.pending()}> | ||
| 125 | + Save | ||
| 126 | + </button> | ||
| 127 | + <button type="button" class="ghost" onClick={() => setEditing(false)}> | ||
| 128 | + Cancel | ||
| 129 | + </button> | ||
| 130 | + <button | ||
| 131 | + type="button" | ||
| 132 | + class="ghost danger push" | ||
| 133 | + disabled={remove.pending()} | ||
| 134 | + onClick={() => void remove.run()} | ||
| 135 | + > | ||
| 136 | + Delete exam | ||
| 137 | + </button> | ||
| 138 | + </span> | ||
| 139 | + </form> | ||
| 140 | + </Show> | ||
| 141 | + | ||
| 142 | + <MyResult examId={params.id} /> | ||
| 143 | + <Show when={can("teacher")}> | ||
| 144 | + <Statistics examId={params.id} /> | ||
| 145 | + <Results examId={params.id} /> | ||
| 146 | + </Show> | ||
| 147 | + </section> | ||
| 148 | + )} | ||
| 149 | + </Show> | ||
| 150 | + ); | ||
| 151 | +} | ||
| 152 | + | ||
| 153 | +/** The caller's own mark; the backend answers 404 while ungraded. */ | ||
| 154 | +function MyResult(props: { examId: string }) { | ||
| 155 | + const [result] = createResource( | ||
| 156 | + () => props.examId, | ||
| 157 | + async (id) => { | ||
| 158 | + try { | ||
| 159 | + return await exams.myResult(id); | ||
| 160 | + } catch (err) { | ||
| 161 | + if (err instanceof ApiError && err.status === 404) return null; | ||
| 162 | + throw err; | ||
| 163 | + } | ||
| 164 | + }, | ||
| 165 | + ); | ||
| 166 | + | ||
| 167 | + return ( | ||
| 168 | + <article class="stack gap-top"> | ||
| 169 | + <h2>My result</h2> | ||
| 170 | + <Show when={!result.loading} fallback={<Loading />}> | ||
| 171 | + <Show when={result()} fallback={<Empty>Not graded yet.</Empty>}> | ||
| 172 | + {(mine) => ( | ||
| 173 | + <p class="mark"> | ||
| 174 | + {mine().mark} | ||
| 175 | + <small> / 100</small> | ||
| 176 | + </p> | ||
| 177 | + )} | ||
| 178 | + </Show> | ||
| 179 | + </Show> | ||
| 180 | + </article> | ||
| 181 | + ); | ||
| 182 | +} | ||
| 183 | + | ||
| 184 | +/** Mark statistics over the graded results. Teacher+ only. */ | ||
| 185 | +function Statistics(props: { examId: string }) { | ||
| 186 | + const [stats] = createResource(() => props.examId, exams.statistics); | ||
| 187 | + const round = (value: number) => (Math.round(value * 100) / 100).toString(); | ||
| 188 | + | ||
| 189 | + return ( | ||
| 190 | + <article class="stack gap-top"> | ||
| 191 | + <h2>Statistics</h2> | ||
| 192 | + <Show when={!stats.loading} fallback={<Loading />}> | ||
| 193 | + <Show when={stats()}> | ||
| 194 | + {(current) => ( | ||
| 195 | + <Show when={current().graded > 0} fallback={<Empty>Nothing graded yet.</Empty>}> | ||
| 196 | + <div class="stats"> | ||
| 197 | + <div class="card stat"> | ||
| 198 | + <span class="stat-label">Graded</span> | ||
| 199 | + <span class="stat-value">{current().graded}</span> | ||
| 200 | + </div> | ||
| 201 | + <div class="card stat"> | ||
| 202 | + <span class="stat-label">Average</span> | ||
| 203 | + <span class="stat-value">{round(current().average ?? 0)}</span> | ||
| 204 | + </div> | ||
| 205 | + <div class="card stat"> | ||
| 206 | + <span class="stat-label">Min</span> | ||
| 207 | + <span class="stat-value">{current().min}</span> | ||
| 208 | + </div> | ||
| 209 | + <div class="card stat"> | ||
| 210 | + <span class="stat-label">Max</span> | ||
| 211 | + <span class="stat-value">{current().max}</span> | ||
| 212 | + </div> | ||
| 213 | + </div> | ||
| 214 | + </Show> | ||
| 215 | + )} | ||
| 216 | + </Show> | ||
| 217 | + </Show> | ||
| 218 | + </article> | ||
| 219 | + ); | ||
| 220 | +} | ||
| 221 | + | ||
| 222 | +/** Full result table + grade form. Teacher+ only (the backend enforces course | ||
| 223 | + * management rights and target enrollment on write). */ | ||
| 224 | +function Results(props: { examId: string }) { | ||
| 225 | + const { user } = useAuth(); | ||
| 226 | + const [results, { mutate }] = createResource(() => props.examId, exams.results); | ||
| 227 | + | ||
| 228 | + const grade = createAction(async (form: HTMLFormElement) => { | ||
| 229 | + const data = new FormData(form); | ||
| 230 | + const graded = await exams.grade( | ||
| 231 | + props.examId, | ||
| 232 | + String(data.get("user_id")), | ||
| 233 | + Number(data.get("mark")), | ||
| 234 | + ); | ||
| 235 | + // One result per (exam, user); grading again overwrites. | ||
| 236 | + mutate((current) => [ | ||
| 237 | + ...(current ?? []).filter((r) => r.user !== graded.user), | ||
| 238 | + graded, | ||
| 239 | + ]); | ||
| 240 | + form.reset(); | ||
| 241 | + }); | ||
| 242 | + | ||
| 243 | + const removeResult = createAction(async (result: ExamResult) => { | ||
| 244 | + await exams.removeResult(props.examId, result.user); | ||
| 245 | + mutate((current) => current?.filter((r) => r.user !== result.user)); | ||
| 246 | + }); | ||
| 247 | + | ||
| 248 | + return ( | ||
| 249 | + <article class="stack gap-top"> | ||
| 250 | + <h2>Results</h2> | ||
| 251 | + <form | ||
| 252 | + class="row" | ||
| 253 | + onSubmit={(e) => { | ||
| 254 | + e.preventDefault(); | ||
| 255 | + void grade.run(e.currentTarget); | ||
| 256 | + }} | ||
| 257 | + > | ||
| 258 | + <input name="user_id" placeholder="Student id" required /> | ||
| 259 | + <input | ||
| 260 | + name="mark" | ||
| 261 | + type="number" | ||
| 262 | + placeholder="Mark" | ||
| 263 | + required | ||
| 264 | + min={LIMITS.mark.min} | ||
| 265 | + max={LIMITS.mark.max} | ||
| 266 | + /> | ||
| 267 | + <button type="submit" disabled={grade.pending()}> | ||
| 268 | + Grade | ||
| 269 | + </button> | ||
| 270 | + </form> | ||
| 271 | + <ErrorLine error={grade.error() ?? removeResult.error()} /> | ||
| 272 | + | ||
| 273 | + <Show when={!results.loading} fallback={<Loading />}> | ||
| 274 | + <Show when={results()?.length} fallback={<Empty>No results yet.</Empty>}> | ||
| 275 | + <div class="table-wrap"> | ||
| 276 | + <table> | ||
| 277 | + <thead> | ||
| 278 | + <tr> | ||
| 279 | + <th>Student</th> | ||
| 280 | + <th>Mark</th> | ||
| 281 | + <th /> | ||
| 282 | + </tr> | ||
| 283 | + </thead> | ||
| 284 | + <tbody> | ||
| 285 | + <For each={results()}> | ||
| 286 | + {(result) => ( | ||
| 287 | + <tr> | ||
| 288 | + <td class="mono">{result.user === user()?.id ? "you" : result.user}</td> | ||
| 289 | + <td>{result.mark}</td> | ||
| 290 | + <td> | ||
| 291 | + <button | ||
| 292 | + class="ghost danger" | ||
| 293 | + disabled={removeResult.pending()} | ||
| 294 | + onClick={() => void removeResult.run(result)} | ||
| 295 | + > | ||
| 296 | + Remove | ||
| 297 | + </button> | ||
| 298 | + </td> | ||
| 299 | + </tr> | ||
| 300 | + )} | ||
| 301 | + </For> | ||
| 302 | + </tbody> | ||
| 303 | + </table> | ||
| 304 | + </div> | ||
| 305 | + </Show> | ||
| 306 | + </Show> | ||
| 307 | + </article> | ||
| 308 | + ); | ||
| 309 | +} | ||
| 310 | + | ||
| 311 | +function NotFoundMessage(props: { error: unknown }) { | ||
| 312 | + const message = | ||
| 313 | + props.error instanceof ApiError && props.error.status === 404 | ||
| 314 | + ? "This exam does not exist." | ||
| 315 | + : "Failed to load the exam."; | ||
| 316 | + return <Empty>{message}</Empty>; | ||
| 317 | +} | ||
src/pages/Exams.tsxadded+53 -0
| @@ -0,0 +1,53 @@ | |||
| 1 | +// All exams across courses. Creation lives on the course page; cards link to | ||
| 2 | +// the detail page where editing, grading, results, and statistics live. | ||
| 3 | + | ||
| 4 | +import { A } from "@solidjs/router"; | ||
| 5 | +import { For, Show, createResource } from "solid-js"; | ||
| 6 | +import { Empty, Loading } from "../components/Feedback"; | ||
| 7 | +import { exams } from "../lib/api"; | ||
| 8 | +import { useAuth } from "../lib/auth"; | ||
| 9 | + | ||
| 10 | +export default function Exams() { | ||
| 11 | + const { can } = useAuth(); | ||
| 12 | + const [list] = createResource(exams.list); | ||
| 13 | + | ||
| 14 | + return ( | ||
| 15 | + <section class="page"> | ||
| 16 | + <header class="page-head"> | ||
| 17 | + <div> | ||
| 18 | + <h1>Exams</h1> | ||
| 19 | + <p class="sub"> | ||
| 20 | + All exams across courses. | ||
| 21 | + <Show when={can("teacher")}> | ||
| 22 | + {" "} | ||
| 23 | + New ones are created inside a <A href="/courses">course</A>. | ||
| 24 | + </Show> | ||
| 25 | + </p> | ||
| 26 | + </div> | ||
| 27 | + </header> | ||
| 28 | + | ||
| 29 | + <Show when={!list.loading} fallback={<Loading />}> | ||
| 30 | + <Show when={list()?.length} fallback={<Empty>No exams yet.</Empty>}> | ||
| 31 | + <div class="grid"> | ||
| 32 | + <For each={list()}> | ||
| 33 | + {(exam) => ( | ||
| 34 | + <A href={`/exams/${exam.id}`} class="card link-card stack"> | ||
| 35 | + <header class="row"> | ||
| 36 | + <h3>{exam.title}</h3> | ||
| 37 | + <span class="badge">{exam.kind}</span> | ||
| 38 | + <span class="badge" title="weight in the course average"> | ||
| 39 | + ×{exam.weight} | ||
| 40 | + </span> | ||
| 41 | + </header> | ||
| 42 | + <Show when={exam.description}> | ||
| 43 | + <p class="muted clamp">{exam.description}</p> | ||
| 44 | + </Show> | ||
| 45 | + </A> | ||
| 46 | + )} | ||
| 47 | + </For> | ||
| 48 | + </div> | ||
| 49 | + </Show> | ||
| 50 | + </Show> | ||
| 51 | + </section> | ||
| 52 | + ); | ||
| 53 | +} | ||
src/pages/Marks.tsxadded+151 -0
| @@ -0,0 +1,151 @@ | |||
| 1 | +// Weighted mark report: every enrolled course with its graded exams, the | ||
| 2 | +// course averages, and the overall average. Teacher+ can look up any student. | ||
| 3 | + | ||
| 4 | +import { A } from "@solidjs/router"; | ||
| 5 | +import { For, Show, createResource, createSignal } from "solid-js"; | ||
| 6 | +import { Empty, ErrorLine, Loading } from "../components/Feedback"; | ||
| 7 | +import { createAction } from "../lib/action"; | ||
| 8 | +import { marks } from "../lib/api"; | ||
| 9 | +import { useAuth } from "../lib/auth"; | ||
| 10 | +import type { MarksReport } from "../lib/types"; | ||
| 11 | + | ||
| 12 | +const round = (value: number) => (Math.round(value * 100) / 100).toString(); | ||
| 13 | + | ||
| 14 | +export default function Marks() { | ||
| 15 | + const { can } = useAuth(); | ||
| 16 | + const [mine] = createResource(marks.mine); | ||
| 17 | + | ||
| 18 | + return ( | ||
| 19 | + <section class="page"> | ||
| 20 | + <header class="page-head"> | ||
| 21 | + <div> | ||
| 22 | + <h1>My marks</h1> | ||
| 23 | + <p class="sub">Graded exams per course, weighted averages included.</p> | ||
| 24 | + </div> | ||
| 25 | + </header> | ||
| 26 | + | ||
| 27 | + <Show when={!mine.loading} fallback={<Loading />}> | ||
| 28 | + <Show when={mine()} fallback={<Empty>Failed to load your marks.</Empty>}> | ||
| 29 | + {(report) => <Report report={report()} />} | ||
| 30 | + </Show> | ||
| 31 | + </Show> | ||
| 32 | + | ||
| 33 | + <Show when={can("teacher")}> | ||
| 34 | + <Lookup /> | ||
| 35 | + </Show> | ||
| 36 | + </section> | ||
| 37 | + ); | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +/** Teacher+ tool: fetch any student's report by id. */ | ||
| 41 | +function Lookup() { | ||
| 42 | + const [report, setReport] = createSignal<MarksReport | null>(null); | ||
| 43 | + | ||
| 44 | + const fetch = createAction(async (form: HTMLFormElement) => { | ||
| 45 | + const data = new FormData(form); | ||
| 46 | + setReport(await marks.forUser(String(data.get("user_id")))); | ||
| 47 | + }); | ||
| 48 | + | ||
| 49 | + return ( | ||
| 50 | + <article class="card stack gap-top"> | ||
| 51 | + <h2>Student lookup</h2> | ||
| 52 | + <form | ||
| 53 | + class="row" | ||
| 54 | + onSubmit={(e) => { | ||
| 55 | + e.preventDefault(); | ||
| 56 | + void fetch.run(e.currentTarget); | ||
| 57 | + }} | ||
| 58 | + > | ||
| 59 | + <input name="user_id" placeholder="Student id" required /> | ||
| 60 | + <button type="submit" disabled={fetch.pending()}> | ||
| 61 | + Show marks | ||
| 62 | + </button> | ||
| 63 | + </form> | ||
| 64 | + <ErrorLine error={fetch.error()} /> | ||
| 65 | + | ||
| 66 | + <Show when={report()}> | ||
| 67 | + {(current) => ( | ||
| 68 | + <> | ||
| 69 | + <p class="meta"> | ||
| 70 | + Report for <span class="mono">{current().user}</span> | ||
| 71 | + </p> | ||
| 72 | + <Report report={current()} /> | ||
| 73 | + </> | ||
| 74 | + )} | ||
| 75 | + </Show> | ||
| 76 | + </article> | ||
| 77 | + ); | ||
| 78 | +} | ||
| 79 | + | ||
| 80 | +function Report(props: { report: MarksReport }) { | ||
| 81 | + return ( | ||
| 82 | + <Show | ||
| 83 | + when={props.report.courses.length} | ||
| 84 | + fallback={<Empty>Not enrolled in any course.</Empty>} | ||
| 85 | + > | ||
| 86 | + <div class="stack"> | ||
| 87 | + <div class="card row" title="mean of the course averages"> | ||
| 88 | + <span class="muted">Overall average</span> | ||
| 89 | + <p class="mark push"> | ||
| 90 | + {props.report.overall_average === null ? ( | ||
| 91 | + "—" | ||
| 92 | + ) : ( | ||
| 93 | + <> | ||
| 94 | + {round(props.report.overall_average)} | ||
| 95 | + <small> / 100</small> | ||
| 96 | + </> | ||
| 97 | + )} | ||
| 98 | + </p> | ||
| 99 | + </div> | ||
| 100 | + | ||
| 101 | + <For each={props.report.courses}> | ||
| 102 | + {(block) => ( | ||
| 103 | + <div class="card stack"> | ||
| 104 | + <header class="row"> | ||
| 105 | + <h3> | ||
| 106 | + <A href={`/courses/${block.course.id}`}>{block.course.title}</A> | ||
| 107 | + </h3> | ||
| 108 | + <span class="badge" title="weighted course average"> | ||
| 109 | + {block.average === null ? "—" : round(block.average)} | ||
| 110 | + </span> | ||
| 111 | + </header> | ||
| 112 | + <Show | ||
| 113 | + when={block.results.length} | ||
| 114 | + fallback={<Empty>Nothing graded yet.</Empty>} | ||
| 115 | + > | ||
| 116 | + <div class="table-wrap"> | ||
| 117 | + <table> | ||
| 118 | + <thead> | ||
| 119 | + <tr> | ||
| 120 | + <th>Exam</th> | ||
| 121 | + <th>Kind</th> | ||
| 122 | + <th>Weight</th> | ||
| 123 | + <th>Mark</th> | ||
| 124 | + </tr> | ||
| 125 | + </thead> | ||
| 126 | + <tbody> | ||
| 127 | + <For each={block.results}> | ||
| 128 | + {(entry) => ( | ||
| 129 | + <tr> | ||
| 130 | + <td> | ||
| 131 | + <A href={`/exams/${entry.exam}`}>{entry.title}</A> | ||
| 132 | + </td> | ||
| 133 | + <td> | ||
| 134 | + <span class="badge">{entry.kind}</span> | ||
| 135 | + </td> | ||
| 136 | + <td>×{entry.weight}</td> | ||
| 137 | + <td>{entry.mark}</td> | ||
| 138 | + </tr> | ||
| 139 | + )} | ||
| 140 | + </For> | ||
| 141 | + </tbody> | ||
| 142 | + </table> | ||
| 143 | + </div> | ||
| 144 | + </Show> | ||
| 145 | + </div> | ||
| 146 | + )} | ||
| 147 | + </For> | ||
| 148 | + </div> | ||
| 149 | + </Show> | ||
| 150 | + ); | ||
| 151 | +} | ||