diff --git a/src/api/deleteExamQuestionById.ts b/src/api/deleteExamQuestionById.ts new file mode 100644 index 0000000..1388c97 --- /dev/null +++ b/src/api/deleteExamQuestionById.ts @@ -0,0 +1,5 @@ +import { client } from "./client"; + +export function deleteExamQuestionById(examId: string, questionId: string): Promise { + return client(`/exams/${examId}/questions/${questionId}`, { method: "DELETE" }); +} diff --git a/src/api/getExamQuestions.ts b/src/api/getExamQuestions.ts new file mode 100644 index 0000000..d3c1b8d --- /dev/null +++ b/src/api/getExamQuestions.ts @@ -0,0 +1,6 @@ +import { client } from "./client"; +import type { ExamQuestion } from "./types"; + +export function getExamQuestions(examId: string, signal?: AbortSignal): Promise { + return client(`/exams/${examId}/questions`, { signal }); +} diff --git a/src/api/patchExamQuestionById.ts b/src/api/patchExamQuestionById.ts new file mode 100644 index 0000000..e8d51d1 --- /dev/null +++ b/src/api/patchExamQuestionById.ts @@ -0,0 +1,18 @@ +import { client } from "./client"; +import type { ExamQuestion, QuestionKind } from "./types"; + +export type PatchExamQuestionBody = { + text?: string; + kind?: QuestionKind; + points?: number; + choices?: string[] | null; + correct?: number | null; +}; + +export function patchExamQuestionById( + examId: string, + questionId: string, + body: PatchExamQuestionBody, +): Promise { + return client(`/exams/${examId}/questions/${questionId}`, { method: "PATCH", body }); +} diff --git a/src/api/postExamQuestion.ts b/src/api/postExamQuestion.ts new file mode 100644 index 0000000..9404eef --- /dev/null +++ b/src/api/postExamQuestion.ts @@ -0,0 +1,14 @@ +import { client } from "./client"; +import type { ExamQuestion, QuestionKind } from "./types"; + +export type ExamQuestionBody = { + text: string; + kind: QuestionKind; + points: number; + choices?: string[] | null; + correct?: number | null; +}; + +export function postExamQuestion(examId: string, body: ExamQuestionBody): Promise { + return client(`/exams/${examId}/questions`, { method: "POST", body }); +} diff --git a/src/api/types.ts b/src/api/types.ts index cbe30ec..69847ae 100644 --- a/src/api/types.ts +++ b/src/api/types.ts @@ -2,6 +2,7 @@ export type Role = "student" | "teacher" | "manager" | "admin"; export type AttendanceStatus = "present" | "absent" | "late" | "excused"; export type ExamKind = "homework" | "quiz" | "midterm" | "final" | "project" | "oral"; export type ExamMode = "sync" | "async"; +export type QuestionKind = "choice" | "text"; export type User = { id: string; @@ -74,6 +75,16 @@ export type ExamResult = { graded_by: string; }; +export type ExamQuestion = { + id: string; + exam: string; + text: string; + kind: QuestionKind; + points: number; + choices: string[] | null; + correct: number | null; +}; + export type MarkEntry = { exam: string; title: string; @@ -105,3 +116,5 @@ export const EXAM_KINDS: ExamKind[] = [ ]; export const EXAM_MODES: ExamMode[] = ["sync", "async"]; + +export const QUESTION_KINDS: QuestionKind[] = ["choice", "text"]; diff --git a/src/components/exams/exam-questions-panel.tsx b/src/components/exams/exam-questions-panel.tsx new file mode 100644 index 0000000..876faad --- /dev/null +++ b/src/components/exams/exam-questions-panel.tsx @@ -0,0 +1,271 @@ +import { For, Show, Suspense, createEffect, createMemo, createResource, createSignal } from "solid-js"; +import { deleteExamQuestionById } from "@/api/deleteExamQuestionById"; +import { getExamQuestions } from "@/api/getExamQuestions"; +import { patchExamQuestionById } from "@/api/patchExamQuestionById"; +import { postExamQuestion } from "@/api/postExamQuestion"; +import { ApiError, formatApiError } from "@/api/client"; +import type { ExamQuestion, QuestionKind } from "@/api/types"; +import { QUESTION_KINDS } from "@/api/types"; +import { Badge } from "@/components/ui/badge"; +import { Button } from "@/components/ui/button"; +import { ConfirmDialog } from "@/components/ui/confirm-dialog"; +import { IconPlus, IconTrash } from "@/components/ui/icons"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { PageSpinner } from "@/components/ui/page-spinner"; +import { Select } from "@/components/ui/select"; +import { Textarea } from "@/components/ui/textarea"; +import { useT } from "@/stores/preferences-context"; + +type QuestionValues = { + text: string; + kind: QuestionKind; + points: number; + choices: string[] | null; + correct: number | null; +}; + +export function ExamQuestionsPanel(props: { examId: string }) { + const t = useT(); + const [questions, { refetch }] = createResource(() => props.examId, async (examId) => { + try { + return await getExamQuestions(examId); + } catch (err) { + if (err instanceof ApiError && err.status === 404) return []; + throw err; + } + }); + const [showForm, setShowForm] = createSignal(false); + const [editing, setEditing] = createSignal(null); + const [removeQuestion, setRemoveQuestion] = createSignal(null); + const [error, setError] = createSignal(""); + + const formInitial = createMemo(() => editing() ?? undefined); + + const submit = async (values: QuestionValues) => { + setError(""); + try { + const q = editing(); + if (q) { + await patchExamQuestionById(props.examId, q.id, values); + } else { + await postExamQuestion(props.examId, values); + } + setEditing(null); + setShowForm(false); + await refetch(); + } catch (err) { + setError(formatApiError(err)); + } + }; + + return ( +
+
+

{t("questions.title")}

+ +
+ + + { + setEditing(null); + setShowForm(false); + }} + onSubmit={submit} + /> + + + {error() &&

{error()}

} + + }> + 0} + fallback={

{t("questions.empty")}

} + > +
    + + {(q, index) => ( +
  1. +
    +
    +
    + #{index() + 1} + + {q.kind === "choice" ? t("questions.kind.choice") : t("questions.kind.text")} + + {q.points} {t("questions.points")} +
    +

    {q.text}

    + +
      + + {(choice, choiceIndex) => ( +
    1. + {choiceIndex()}. {choice} +
    2. + )} +
      +
    +
    +
    +
    + + +
    +
    +
  2. + )} +
    +
+
+
+ + { + if (!open) setRemoveQuestion(null); + }} + title={t("confirm.deleteTitle")} + variant="destructive" + summary={removeQuestion()?.text ?? ""} + onConfirm={async () => { + const q = removeQuestion(); + if (!q) return; + setError(""); + try { + await deleteExamQuestionById(props.examId, q.id); + await refetch(); + } catch (err) { + setError(formatApiError(err)); + } finally { + setRemoveQuestion(null); + } + }} + /> +
+ ); +} + +function QuestionForm(props: { + initial?: ExamQuestion; + onSubmit: (values: QuestionValues) => Promise; + onCancel: () => void; +}) { + const t = useT(); + const [text, setText] = createSignal(props.initial?.text ?? ""); + const [kind, setKind] = createSignal(props.initial?.kind ?? "choice"); + const [points, setPoints] = createSignal(String(props.initial?.points ?? 1)); + const [choicesText, setChoicesText] = createSignal((props.initial?.choices ?? ["", ""]).join("\n")); + const [correct, setCorrect] = createSignal(props.initial?.correct != null ? String(props.initial.correct) : "0"); + const [error, setError] = createSignal(""); + const [pending, setPending] = createSignal(false); + + createEffect(() => { + const initial = props.initial; + setText(initial?.text ?? ""); + setKind(initial?.kind ?? "choice"); + setPoints(String(initial?.points ?? 1)); + setChoicesText((initial?.choices ?? ["", ""]).join("\n")); + setCorrect(initial?.correct != null ? String(initial.correct) : "0"); + setError(""); + }); + + const validate = (): QuestionValues | string => { + const body = text().trim(); + if (!body) return t("questions.textRequired"); + if (body.length > 2000) return t("form.descriptionMax"); + const p = Number(points()); + if (!Number.isInteger(p) || p < 1 || p > 100) return t("questions.pointsRange"); + if (kind() === "text") { + return { text: body, kind: "text", points: p, choices: null, correct: null }; + } + const choices = choicesText().split("\n").map((choice) => choice.trim()).filter(Boolean); + if (choices.length < 2 || choices.length > 10 || choices.some((choice) => choice.length > 500)) { + return t("questions.choicesRange"); + } + const c = Number(correct()); + if (!Number.isInteger(c) || c < 0 || c >= choices.length) return t("questions.correctRange"); + return { text: body, kind: "choice", points: p, choices, correct: c }; + }; + + const submit = async (e: SubmitEvent) => { + e.preventDefault(); + const values = validate(); + if (typeof values === "string") { + setError(values); + return; + } + setError(""); + setPending(true); + try { + await props.onSubmit(values); + } catch (err) { + setError(formatApiError(err)); + } finally { + setPending(false); + } + }; + + return ( +
void submit(e)}> +
+ +