diff --git a/src/api/initial-assessment.api.ts b/src/api/initial-assessment.api.ts index 1488cab..972362b 100644 --- a/src/api/initial-assessment.api.ts +++ b/src/api/initial-assessment.api.ts @@ -211,31 +211,49 @@ function unwrapQuestionSets(body: unknown): Array> { return [] } -function pickStandalonePlacement(sets: Array>): Record | null { - const publishedStandalone = sets.find((set) => { +const CEFR_LEVELS: CefrLevel[] = ["A1", "A2", "B1", "B2", "C1", "C2"] + +function isCefrLevel(value: string): value is CefrLevel { + return (CEFR_LEVELS as string[]).includes(value) +} + +function setLevel(set: Record): CefrLevel | null { + const level = String(set.description ?? "").trim().toUpperCase() + return isCefrLevel(level) ? level : null +} + +function pickSetForLevel( + sets: Array>, + level: CefrLevel, +): Record | null { + const matching = sets.filter((set) => setLevel(set) === level) + if (matching.length === 0) return null + + const publishedStandalone = matching.find((set) => { const status = String(set.status ?? "").toUpperCase() const owner = String(set.owner_type ?? "STANDALONE").toUpperCase() return status === "PUBLISHED" && (owner === "STANDALONE" || owner === "") }) if (publishedStandalone) return publishedStandalone - const anyStandalone = sets.find((set) => { + const anyStandalone = matching.find((set) => { const owner = String(set.owner_type ?? "STANDALONE").toUpperCase() return owner === "STANDALONE" || owner === "" }) if (anyStandalone) return anyStandalone - return sets[0] ?? null + const published = matching.find((set) => String(set.status ?? "").toUpperCase() === "PUBLISHED") + return published ?? matching[0] ?? null } -export async function findPlacementAssessmentSet(): Promise { +export async function findPlacementAssessmentSet(level: CefrLevel): Promise { const res = await getQuestionSets({ set_type: "INITIAL_ASSESSMENT", - limit: 50, + limit: 100, offset: 0, }) const sets = unwrapQuestionSets(res.data) - const picked = pickStandalonePlacement(sets) + const picked = pickSetForLevel(sets, level) if (!picked) return null const id = Number(picked.id) if (!Number.isFinite(id)) return null @@ -245,11 +263,15 @@ export async function findPlacementAssessmentSet(): Promise { + const level = input?.level + if (!level) { + throw new Error("Placement set level is required") + } const payload: CreateQuestionSetRequest = { - title: input?.title?.trim() || "Initial Placement Assessment", - description: input?.description?.trim() || "PLACEMENT", + title: input?.title?.trim() || `Initial Placement Assessment — ${level}`, + description: level, set_type: "INITIAL_ASSESSMENT", owner_type: "STANDALONE", owner_id: null, @@ -269,10 +291,10 @@ export async function createPlacementAssessmentSet(input?: { return detailRes.data.data } -export async function ensurePlacementAssessmentSet(): Promise { - const existing = await findPlacementAssessmentSet() +export async function ensurePlacementAssessmentSet(level: CefrLevel): Promise { + const existing = await findPlacementAssessmentSet(level) if (existing) return existing - return createPlacementAssessmentSet() + return createPlacementAssessmentSet({ level }) } export async function savePlacementAssessmentSet( diff --git a/src/pages/content-management/InitialAssessmentPage.tsx b/src/pages/content-management/InitialAssessmentPage.tsx index 8052652..be60009 100644 --- a/src/pages/content-management/InitialAssessmentPage.tsx +++ b/src/pages/content-management/InitialAssessmentPage.tsx @@ -19,6 +19,7 @@ import { getPlacementQuestionDetail, listPlacementQuestions, savePlacementAssessmentSet, + type CefrLevel, type PlacementQuestionDetail, type PlacementQuestionRow, } from "../../api/initial-assessment.api" @@ -177,13 +178,15 @@ function normalizeTimeLimitValue(raw: number | null | undefined): string { return raw != null && raw > 0 ? String(raw) : "" } +const CEFR_LEVELS: CefrLevel[] = ["A1", "A2", "B1", "B2", "C1", "C2"] + export function InitialAssessmentPage() { const [tab, setTab] = useState("set") + const [selectedLevel, setSelectedLevel] = useState("A1") const [loading, setLoading] = useState(true) const [savingSet, setSavingSet] = useState(false) const [setDetail, setSetDetail] = useState(null) const [title, setTitle] = useState("") - const [description, setDescription] = useState("PLACEMENT") const [status, setStatus] = useState("DRAFT") const [shuffle, setShuffle] = useState(false) const [timeLimit, setTimeLimit] = useState("") @@ -206,27 +209,24 @@ export function InitialAssessmentPage() { const setFormDirty = useMemo(() => { if (!setDetail) return false - const savedTitle = setDetail.title || "Initial Placement Assessment" - const savedDescription = setDetail.description || "PLACEMENT" + const savedTitle = setDetail.title || `Initial Placement Assessment — ${selectedLevel}` const savedShuffle = Boolean(setDetail.shuffle_questions) const savedTimeLimit = normalizeTimeLimitValue(setDetail.time_limit_minutes) return ( title.trim() !== savedTitle.trim() || - (description.trim() || "PLACEMENT") !== savedDescription.trim() || shuffle !== savedShuffle || timeLimit.trim() !== savedTimeLimit ) - }, [setDetail, title, description, shuffle, timeLimit]) + }, [setDetail, title, shuffle, timeLimit, selectedLevel]) const questionFormDirty = useMemo(() => !draftsEqual(draft, draftBaseline), [draft, draftBaseline]) const loadSet = useCallback(async () => { setLoading(true) try { - const detail = await ensurePlacementAssessmentSet() + const detail = await ensurePlacementAssessmentSet(selectedLevel) setSetDetail(detail) - setTitle(detail.title || "Initial Placement Assessment") - setDescription(detail.description || "PLACEMENT") + setTitle(detail.title || `Initial Placement Assessment — ${selectedLevel}`) setStatus(String(detail.status || "DRAFT").toUpperCase()) setShuffle(Boolean(detail.shuffle_questions)) setTimeLimit(normalizeTimeLimitValue(detail.time_limit_minutes)) @@ -237,7 +237,7 @@ export function InitialAssessmentPage() { } finally { setLoading(false) } - }, []) + }, [selectedLevel]) const loadQuestions = useCallback(async (setId: number) => { setQuestionsLoading(true) @@ -322,7 +322,7 @@ export function InitialAssessmentPage() { try { const updated = await savePlacementAssessmentSet(setDetail.id, { title: title.trim(), - description: description.trim() || "PLACEMENT", + description: selectedLevel, status, shuffle_questions: shuffle, time_limit_minutes: timeLimit.trim() ? Number(timeLimit) : null, @@ -359,7 +359,7 @@ export function InitialAssessmentPage() { const nextStatus = nextPublished ? "PUBLISHED" : "DRAFT" const updated = await savePlacementAssessmentSet(setDetail.id, { title: title.trim() || setDetail.title, - description: description.trim() || "PLACEMENT", + description: selectedLevel, status: nextStatus, shuffle_questions: shuffle, time_limit_minutes: timeLimit.trim() ? Number(timeLimit) : null, @@ -368,7 +368,11 @@ export function InitialAssessmentPage() { }) setSetDetail(updated) setStatus(nextStatus) - toast.success(nextPublished ? "Placement assessment published" : "Placement assessment unpublished") + toast.success( + nextPublished + ? `${selectedLevel} placement set published` + : `${selectedLevel} placement set unpublished`, + ) } catch (e) { notifyApiError(e, "Failed to update publish status") } finally { @@ -577,13 +581,13 @@ export function InitialAssessmentPage() { Initial placement assessment

- Manage the single learner placement test, its questions, and the A1–C2 score thresholds used - when grading submissions. + Manage one question set per CEFR level (A1–C2). Learners progress upward when they pass + each level's pass score; failing stops and assigns the highest level they passed.

- {published ? "Published" : "Draft"} + {selectedLevel} · {published ? "Published" : "Draft"}
Live for learners @@ -591,7 +595,7 @@ export function InitialAssessmentPage() { checked={published} onCheckedChange={() => void handlePublishToggle(!published)} disabled={savingSet} - aria-label="Publish placement assessment" + aria-label={`Publish ${selectedLevel} placement assessment`} />
+
+ {CEFR_LEVELS.map((level) => { + const selected = selectedLevel === level + return ( + + ) + })} +
+
@@ -627,9 +658,9 @@ export function InitialAssessmentPage() {
{( [ - { id: "set", label: "Placement set" }, + { id: "set", label: `${selectedLevel} set` }, { id: "questions", label: "Questions" }, - { id: "thresholds", label: "Score thresholds" }, + { id: "thresholds", label: "Pass thresholds" }, ] as const ).map((item) => (