Manage one initial assessment question set per CEFR level.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-08-03 11:09:51 -07:00
parent de9766be7b
commit 8e7226b370
3 changed files with 102 additions and 55 deletions

View File

@ -211,31 +211,49 @@ function unwrapQuestionSets(body: unknown): Array<Record<string, unknown>> {
return [] return []
} }
function pickStandalonePlacement(sets: Array<Record<string, unknown>>): Record<string, unknown> | null { const CEFR_LEVELS: CefrLevel[] = ["A1", "A2", "B1", "B2", "C1", "C2"]
const publishedStandalone = sets.find((set) => {
function isCefrLevel(value: string): value is CefrLevel {
return (CEFR_LEVELS as string[]).includes(value)
}
function setLevel(set: Record<string, unknown>): CefrLevel | null {
const level = String(set.description ?? "").trim().toUpperCase()
return isCefrLevel(level) ? level : null
}
function pickSetForLevel(
sets: Array<Record<string, unknown>>,
level: CefrLevel,
): Record<string, unknown> | 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 status = String(set.status ?? "").toUpperCase()
const owner = String(set.owner_type ?? "STANDALONE").toUpperCase() const owner = String(set.owner_type ?? "STANDALONE").toUpperCase()
return status === "PUBLISHED" && (owner === "STANDALONE" || owner === "") return status === "PUBLISHED" && (owner === "STANDALONE" || owner === "")
}) })
if (publishedStandalone) return publishedStandalone if (publishedStandalone) return publishedStandalone
const anyStandalone = sets.find((set) => { const anyStandalone = matching.find((set) => {
const owner = String(set.owner_type ?? "STANDALONE").toUpperCase() const owner = String(set.owner_type ?? "STANDALONE").toUpperCase()
return owner === "STANDALONE" || owner === "" return owner === "STANDALONE" || owner === ""
}) })
if (anyStandalone) return anyStandalone 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<QuestionSetDetail | null> { export async function findPlacementAssessmentSet(level: CefrLevel): Promise<QuestionSetDetail | null> {
const res = await getQuestionSets({ const res = await getQuestionSets({
set_type: "INITIAL_ASSESSMENT", set_type: "INITIAL_ASSESSMENT",
limit: 50, limit: 100,
offset: 0, offset: 0,
}) })
const sets = unwrapQuestionSets(res.data) const sets = unwrapQuestionSets(res.data)
const picked = pickStandalonePlacement(sets) const picked = pickSetForLevel(sets, level)
if (!picked) return null if (!picked) return null
const id = Number(picked.id) const id = Number(picked.id)
if (!Number.isFinite(id)) return null if (!Number.isFinite(id)) return null
@ -245,11 +263,15 @@ export async function findPlacementAssessmentSet(): Promise<QuestionSetDetail |
export async function createPlacementAssessmentSet(input?: { export async function createPlacementAssessmentSet(input?: {
title?: string title?: string
description?: string level: CefrLevel
}): Promise<QuestionSetDetail> { }): Promise<QuestionSetDetail> {
const level = input?.level
if (!level) {
throw new Error("Placement set level is required")
}
const payload: CreateQuestionSetRequest = { const payload: CreateQuestionSetRequest = {
title: input?.title?.trim() || "Initial Placement Assessment", title: input?.title?.trim() || `Initial Placement Assessment — ${level}`,
description: input?.description?.trim() || "PLACEMENT", description: level,
set_type: "INITIAL_ASSESSMENT", set_type: "INITIAL_ASSESSMENT",
owner_type: "STANDALONE", owner_type: "STANDALONE",
owner_id: null, owner_id: null,
@ -269,10 +291,10 @@ export async function createPlacementAssessmentSet(input?: {
return detailRes.data.data return detailRes.data.data
} }
export async function ensurePlacementAssessmentSet(): Promise<QuestionSetDetail> { export async function ensurePlacementAssessmentSet(level: CefrLevel): Promise<QuestionSetDetail> {
const existing = await findPlacementAssessmentSet() const existing = await findPlacementAssessmentSet(level)
if (existing) return existing if (existing) return existing
return createPlacementAssessmentSet() return createPlacementAssessmentSet({ level })
} }
export async function savePlacementAssessmentSet( export async function savePlacementAssessmentSet(

View File

@ -19,6 +19,7 @@ import {
getPlacementQuestionDetail, getPlacementQuestionDetail,
listPlacementQuestions, listPlacementQuestions,
savePlacementAssessmentSet, savePlacementAssessmentSet,
type CefrLevel,
type PlacementQuestionDetail, type PlacementQuestionDetail,
type PlacementQuestionRow, type PlacementQuestionRow,
} from "../../api/initial-assessment.api" } from "../../api/initial-assessment.api"
@ -177,13 +178,15 @@ function normalizeTimeLimitValue(raw: number | null | undefined): string {
return raw != null && raw > 0 ? String(raw) : "" return raw != null && raw > 0 ? String(raw) : ""
} }
const CEFR_LEVELS: CefrLevel[] = ["A1", "A2", "B1", "B2", "C1", "C2"]
export function InitialAssessmentPage() { export function InitialAssessmentPage() {
const [tab, setTab] = useState<TabId>("set") const [tab, setTab] = useState<TabId>("set")
const [selectedLevel, setSelectedLevel] = useState<CefrLevel>("A1")
const [loading, setLoading] = useState(true) const [loading, setLoading] = useState(true)
const [savingSet, setSavingSet] = useState(false) const [savingSet, setSavingSet] = useState(false)
const [setDetail, setSetDetail] = useState<QuestionSetDetail | null>(null) const [setDetail, setSetDetail] = useState<QuestionSetDetail | null>(null)
const [title, setTitle] = useState("") const [title, setTitle] = useState("")
const [description, setDescription] = useState("PLACEMENT")
const [status, setStatus] = useState("DRAFT") const [status, setStatus] = useState("DRAFT")
const [shuffle, setShuffle] = useState(false) const [shuffle, setShuffle] = useState(false)
const [timeLimit, setTimeLimit] = useState<string>("") const [timeLimit, setTimeLimit] = useState<string>("")
@ -206,27 +209,24 @@ export function InitialAssessmentPage() {
const setFormDirty = useMemo(() => { const setFormDirty = useMemo(() => {
if (!setDetail) return false if (!setDetail) return false
const savedTitle = setDetail.title || "Initial Placement Assessment" const savedTitle = setDetail.title || `Initial Placement Assessment — ${selectedLevel}`
const savedDescription = setDetail.description || "PLACEMENT"
const savedShuffle = Boolean(setDetail.shuffle_questions) const savedShuffle = Boolean(setDetail.shuffle_questions)
const savedTimeLimit = normalizeTimeLimitValue(setDetail.time_limit_minutes) const savedTimeLimit = normalizeTimeLimitValue(setDetail.time_limit_minutes)
return ( return (
title.trim() !== savedTitle.trim() || title.trim() !== savedTitle.trim() ||
(description.trim() || "PLACEMENT") !== savedDescription.trim() ||
shuffle !== savedShuffle || shuffle !== savedShuffle ||
timeLimit.trim() !== savedTimeLimit timeLimit.trim() !== savedTimeLimit
) )
}, [setDetail, title, description, shuffle, timeLimit]) }, [setDetail, title, shuffle, timeLimit, selectedLevel])
const questionFormDirty = useMemo(() => !draftsEqual(draft, draftBaseline), [draft, draftBaseline]) const questionFormDirty = useMemo(() => !draftsEqual(draft, draftBaseline), [draft, draftBaseline])
const loadSet = useCallback(async () => { const loadSet = useCallback(async () => {
setLoading(true) setLoading(true)
try { try {
const detail = await ensurePlacementAssessmentSet() const detail = await ensurePlacementAssessmentSet(selectedLevel)
setSetDetail(detail) setSetDetail(detail)
setTitle(detail.title || "Initial Placement Assessment") setTitle(detail.title || `Initial Placement Assessment — ${selectedLevel}`)
setDescription(detail.description || "PLACEMENT")
setStatus(String(detail.status || "DRAFT").toUpperCase()) setStatus(String(detail.status || "DRAFT").toUpperCase())
setShuffle(Boolean(detail.shuffle_questions)) setShuffle(Boolean(detail.shuffle_questions))
setTimeLimit(normalizeTimeLimitValue(detail.time_limit_minutes)) setTimeLimit(normalizeTimeLimitValue(detail.time_limit_minutes))
@ -237,7 +237,7 @@ export function InitialAssessmentPage() {
} finally { } finally {
setLoading(false) setLoading(false)
} }
}, []) }, [selectedLevel])
const loadQuestions = useCallback(async (setId: number) => { const loadQuestions = useCallback(async (setId: number) => {
setQuestionsLoading(true) setQuestionsLoading(true)
@ -322,7 +322,7 @@ export function InitialAssessmentPage() {
try { try {
const updated = await savePlacementAssessmentSet(setDetail.id, { const updated = await savePlacementAssessmentSet(setDetail.id, {
title: title.trim(), title: title.trim(),
description: description.trim() || "PLACEMENT", description: selectedLevel,
status, status,
shuffle_questions: shuffle, shuffle_questions: shuffle,
time_limit_minutes: timeLimit.trim() ? Number(timeLimit) : null, time_limit_minutes: timeLimit.trim() ? Number(timeLimit) : null,
@ -359,7 +359,7 @@ export function InitialAssessmentPage() {
const nextStatus = nextPublished ? "PUBLISHED" : "DRAFT" const nextStatus = nextPublished ? "PUBLISHED" : "DRAFT"
const updated = await savePlacementAssessmentSet(setDetail.id, { const updated = await savePlacementAssessmentSet(setDetail.id, {
title: title.trim() || setDetail.title, title: title.trim() || setDetail.title,
description: description.trim() || "PLACEMENT", description: selectedLevel,
status: nextStatus, status: nextStatus,
shuffle_questions: shuffle, shuffle_questions: shuffle,
time_limit_minutes: timeLimit.trim() ? Number(timeLimit) : null, time_limit_minutes: timeLimit.trim() ? Number(timeLimit) : null,
@ -368,7 +368,11 @@ export function InitialAssessmentPage() {
}) })
setSetDetail(updated) setSetDetail(updated)
setStatus(nextStatus) setStatus(nextStatus)
toast.success(nextPublished ? "Placement assessment published" : "Placement assessment unpublished") toast.success(
nextPublished
? `${selectedLevel} placement set published`
: `${selectedLevel} placement set unpublished`,
)
} catch (e) { } catch (e) {
notifyApiError(e, "Failed to update publish status") notifyApiError(e, "Failed to update publish status")
} finally { } finally {
@ -577,13 +581,13 @@ export function InitialAssessmentPage() {
Initial placement assessment Initial placement assessment
</h1> </h1>
<p className="mt-1.5 max-w-2xl text-sm text-grayScale-500"> <p className="mt-1.5 max-w-2xl text-sm text-grayScale-500">
Manage the single learner placement test, its questions, and the A1–C2 score thresholds used Manage one question set per CEFR level (A1–C2). Learners progress upward when they pass
when grading submissions. each level&apos;s pass score; failing stops and assigns the highest level they passed.
</p> </p>
</div> </div>
<div className="flex flex-wrap items-center gap-3"> <div className="flex flex-wrap items-center gap-3">
<Badge variant={published ? "success" : "secondary"}> <Badge variant={published ? "success" : "secondary"}>
{published ? "Published" : "Draft"} {selectedLevel} · {published ? "Published" : "Draft"}
</Badge> </Badge>
<div className="flex items-center gap-2 rounded-xl border border-grayScale-200 bg-white px-3 py-2"> <div className="flex items-center gap-2 rounded-xl border border-grayScale-200 bg-white px-3 py-2">
<span className="text-xs font-medium text-grayScale-500">Live for learners</span> <span className="text-xs font-medium text-grayScale-500">Live for learners</span>
@ -591,7 +595,7 @@ export function InitialAssessmentPage() {
checked={published} checked={published}
onCheckedChange={() => void handlePublishToggle(!published)} onCheckedChange={() => void handlePublishToggle(!published)}
disabled={savingSet} disabled={savingSet}
aria-label="Publish placement assessment" aria-label={`Publish ${selectedLevel} placement assessment`}
/> />
</div> </div>
<Button type="button" variant="outline" size="sm" onClick={() => void loadSet()} disabled={loading}> <Button type="button" variant="outline" size="sm" onClick={() => void loadSet()} disabled={loading}>
@ -601,6 +605,33 @@ export function InitialAssessmentPage() {
</div> </div>
</div> </div>
<div
className="mb-6 flex flex-wrap gap-2"
role="tablist"
aria-label="CEFR placement level"
>
{CEFR_LEVELS.map((level) => {
const selected = selectedLevel === level
return (
<button
key={level}
type="button"
role="tab"
aria-selected={selected}
onClick={() => setSelectedLevel(level)}
className={cn(
"rounded-full px-3.5 py-1.5 text-sm font-semibold transition",
selected
? "bg-brand-500 text-white"
: "border border-grayScale-200 bg-white text-grayScale-600 hover:border-grayScale-300",
)}
>
{level}
</button>
)
})}
</div>
<div className="mb-6 grid gap-3 sm:grid-cols-2"> <div className="mb-6 grid gap-3 sm:grid-cols-2">
<Card className="rounded-xl border-grayScale-200/70 shadow-sm"> <Card className="rounded-xl border-grayScale-200/70 shadow-sm">
<CardContent className="flex items-center gap-3 p-4"> <CardContent className="flex items-center gap-3 p-4">
@ -627,9 +658,9 @@ export function InitialAssessmentPage() {
<div className="mb-6 flex gap-1 overflow-x-auto border-b border-grayScale-100"> <div className="mb-6 flex gap-1 overflow-x-auto border-b border-grayScale-100">
{( {(
[ [
{ id: "set", label: "Placement set" }, { id: "set", label: `${selectedLevel} set` },
{ id: "questions", label: "Questions" }, { id: "questions", label: "Questions" },
{ id: "thresholds", label: "Score thresholds" }, { id: "thresholds", label: "Pass thresholds" },
] as const ] as const
).map((item) => ( ).map((item) => (
<button <button
@ -669,14 +700,12 @@ export function InitialAssessmentPage() {
</label> </label>
<label className="space-y-1.5 sm:col-span-2"> <label className="space-y-1.5 sm:col-span-2">
<span className="text-xs font-semibold uppercase tracking-wide text-grayScale-400"> <span className="text-xs font-semibold uppercase tracking-wide text-grayScale-400">
Description CEFR level
</span>
<Input value={selectedLevel} disabled readOnly />
<span className="text-xs text-grayScale-400">
Each level has its own published set. Switch levels with the chips above.
</span> </span>
<Textarea
rows={3}
value={description}
onChange={(e) => setDescription(e.target.value)}
placeholder="Short description for admins"
/>
</label> </label>
<label className="space-y-1.5"> <label className="space-y-1.5">
<span className="text-xs font-semibold uppercase tracking-wide text-grayScale-400"> <span className="text-xs font-semibold uppercase tracking-wide text-grayScale-400">

View File

@ -16,12 +16,12 @@ import { SpinnerIcon } from "../../components/ui/spinner-icon"
const LEVEL_ORDER = ["A1", "A2", "B1", "B2", "C1", "C2"] as const const LEVEL_ORDER = ["A1", "A2", "B1", "B2", "C1", "C2"] as const
const DEFAULTS: InitialAssessmentLevelThreshold[] = [ const DEFAULTS: InitialAssessmentLevelThreshold[] = [
{ level: "A1", min_percent: 0, display_order: 1 }, { level: "A1", min_percent: 70, display_order: 1 },
{ level: "A2", min_percent: 17, display_order: 2 }, { level: "A2", min_percent: 70, display_order: 2 },
{ level: "B1", min_percent: 34, display_order: 3 }, { level: "B1", min_percent: 70, display_order: 3 },
{ level: "B2", min_percent: 51, display_order: 4 }, { level: "B2", min_percent: 70, display_order: 4 },
{ level: "C1", min_percent: 68, display_order: 5 }, { level: "C1", min_percent: 70, display_order: 5 },
{ level: "C2", min_percent: 85, display_order: 6 }, { level: "C2", min_percent: 70, display_order: 6 },
] ]
function ensureAllLevels(rows: InitialAssessmentLevelThreshold[]): InitialAssessmentLevelThreshold[] { function ensureAllLevels(rows: InitialAssessmentLevelThreshold[]): InitialAssessmentLevelThreshold[] {
@ -37,14 +37,10 @@ function ensureAllLevels(rows: InitialAssessmentLevelThreshold[]): InitialAssess
} }
function validateRows(rows: InitialAssessmentLevelThreshold[]): string | null { function validateRows(rows: InitialAssessmentLevelThreshold[]): string | null {
if (rows[0]?.min_percent !== 0) return "A1 minimum score must be 0%."
for (let i = 0; i < rows.length; i++) { for (let i = 0; i < rows.length; i++) {
const value = rows[i].min_percent const value = rows[i].min_percent
if (!Number.isFinite(value) || value < 0 || value > 100) { if (!Number.isFinite(value) || value < 0 || value > 100) {
return `${rows[i].level} minimum must be between 0 and 100.` return `${rows[i].level} pass score must be between 0 and 100.`
}
if (i > 0 && value <= rows[i - 1].min_percent) {
return `${rows[i].level} minimum must be greater than ${rows[i - 1].level}.`
} }
} }
return null return null
@ -143,8 +139,9 @@ export function InitialAssessmentThresholdsTab() {
</CardTitle> </CardTitle>
</div> </div>
<p className="text-sm text-grayScale-500"> <p className="text-sm text-grayScale-500">
Map placement score percentage to CEFR levels A1–C2. Learners receive the highest Pass score (%) required to clear each CEFR level&apos;s question set and continue to the
level whose minimum they meet. next level. Failing a level finalizes the learner at the highest level they already
passed (A1 if none).
</p> </p>
</div> </div>
<div className="flex gap-2"> <div className="flex gap-2">
@ -166,14 +163,13 @@ export function InitialAssessmentThresholdsTab() {
className="flex flex-col gap-1.5 rounded-xl border border-grayScale-200 bg-white p-4" className="flex flex-col gap-1.5 rounded-xl border border-grayScale-200 bg-white p-4"
> >
<span className="text-sm font-semibold text-grayScale-800">{row.level}</span> <span className="text-sm font-semibold text-grayScale-800">{row.level}</span>
<span className="text-xs text-grayScale-400">Minimum score (%)</span> <span className="text-xs text-grayScale-400">Pass score (%)</span>
<Input <Input
type="number" type="number"
min={0} min={0}
max={100} max={100}
step={1} step={1}
value={row.min_percent} value={row.min_percent}
disabled={row.level === "A1"}
onChange={(e) => updateMin(row.level, e.target.value)} onChange={(e) => updateMin(row.level, e.target.value)}
className="h-9" className="h-9"
/> />
@ -181,8 +177,8 @@ export function InitialAssessmentThresholdsTab() {
))} ))}
</div> </div>
<p className="mt-2 text-xs text-grayScale-400"> <p className="mt-2 text-xs text-grayScale-400">
Learners get the highest level whose minimum score they meet. A1 starts at 0%. Manage the Learners start at A1 and advance only when they meet that level&apos;s pass score. Configure
placement test and its questions under{" "} one question set per level under{" "}
<Link to="/new-content/initial-assessment" className="font-medium text-brand-600 hover:underline"> <Link to="/new-content/initial-assessment" className="font-medium text-brand-600 hover:underline">
Content → Initial assessment Content → Initial assessment
</Link> </Link>