feat(error-book): implement error book module with SM2 spaced repetition

- Add SM2 algorithm implementation with tests for spaced repetition review scheduling

- Add data-access, schema, types, and server actions for error book CRUD

- Add components: add dialog, class overview, filters, item card, stats cards, review buttons, top wrong questions

- Add error-book routes for admin, teacher, parent, and student roles

- Add i18n messages (en, zh-CN) for error book module
This commit is contained in:
SpecialX
2026-06-23 17:36:42 +08:00
parent 396c2c568d
commit bf056399c6
26 changed files with 3613 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
"use client"
import { BarChart3 } from "lucide-react"
import { EmptyState } from "@/shared/components/ui/empty-state"
export default function TeacherErrorBookError() {
return (
<div className="p-8">
<EmptyState
icon={BarChart3}
title="加载错题分析失败"
description="发生了一些错误,请刷新页面重试。"
action={{ label: "刷新页面", onClick: () => window.location.reload() }}
className="border-none shadow-none"
/>
</div>
)
}

View File

@@ -0,0 +1,26 @@
import { Skeleton } from "@/shared/components/ui/skeleton"
export default function TeacherErrorBookLoading() {
return (
<div className="flex h-full flex-col space-y-8 p-8">
<div className="space-y-2">
<Skeleton className="h-8 w-[180px]" />
<Skeleton className="h-4 w-[300px]" />
</div>
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
{Array.from({ length: 4 }).map((_, idx) => (
<Skeleton key={idx} className="h-[120px] w-full rounded-md" />
))}
</div>
<div className="grid gap-4 md:grid-cols-2">
{Array.from({ length: 2 }).map((_, idx) => (
<Skeleton key={idx} className="h-[300px] w-full rounded-md" />
))}
</div>
<Skeleton className="h-[400px] w-full rounded-md" />
</div>
)
}

View File

@@ -0,0 +1,119 @@
import type { JSX } from "react"
import { BarChart3 } from "lucide-react"
import { requirePermission } from "@/shared/lib/auth-guard"
import { Permissions } from "@/shared/types/permissions"
import { EmptyState } from "@/shared/components/ui/empty-state"
import { getStudentIdsByClassIds, getClassIdsByGradeIds } from "@/modules/classes/data-access"
import {
getStudentErrorBookSummaries,
getTopWrongQuestionsByStudentIds,
getKnowledgePointWeakness,
getSubjectErrorDistribution,
getStudentNameMap,
} from "@/modules/error-book/data-access"
import { ClassErrorBookOverview, StudentErrorTable } from "@/modules/error-book/components/class-error-overview"
import { TopWrongQuestions } from "@/modules/error-book/components/top-wrong-questions"
export const dynamic = "force-dynamic"
export default async function TeacherErrorBookPage(): Promise<JSX.Element> {
const ctx = await requirePermission(Permissions.ERROR_BOOK_ANALYTICS_READ)
// 教师的 dataScope 为 class_taught年级主任/教研组长为 grade_managed管理员为 all
const classIds = ctx.dataScope.type === "class_taught" ? ctx.dataScope.classIds : []
const gradeIds = ctx.dataScope.type === "grade_managed" ? ctx.dataScope.gradeIds : []
if (classIds.length === 0 && gradeIds.length === 0 && ctx.dataScope.type !== "all") {
return (
<div className="flex h-full flex-col space-y-8 p-8">
<div>
<h1 className="text-2xl font-bold tracking-tight"></h1>
<p className="text-muted-foreground"></p>
</div>
<EmptyState
icon={BarChart3}
title="暂无可查看的班级"
description="您还未被分配到任何班级,无法查看错题分析数据。"
className="h-[360px] bg-card"
/>
</div>
)
}
// 年级主任/教研组长:先根据 gradeIds 查询班级,再查询学生
let targetClassIds = classIds
if (gradeIds.length > 0) {
const gradeClassIds = await getClassIdsByGradeIds(gradeIds)
targetClassIds = [...new Set([...classIds, ...gradeClassIds])]
}
const studentIds = await getStudentIdsByClassIds(targetClassIds)
if (studentIds.length === 0) {
return (
<div className="flex h-full flex-col space-y-8 p-8">
<div>
<h1 className="text-2xl font-bold tracking-tight"></h1>
<p className="text-muted-foreground"></p>
</div>
<EmptyState
icon={BarChart3}
title="班级暂无学生"
description="班级中没有学生,无法查看错题分析数据。"
className="h-[360px] bg-card"
/>
</div>
)
}
// 并行查询所有统计数据
const [summaries, topWrongQuestions, weakKps, subjectDist, nameMap] = await Promise.all([
getStudentErrorBookSummaries(studentIds),
getTopWrongQuestionsByStudentIds(studentIds, 10),
getKnowledgePointWeakness(studentIds, 10),
getSubjectErrorDistribution(studentIds),
getStudentNameMap(studentIds),
])
const studentsWithErrorBook = summaries.filter((s) => s.totalCount > 0)
const totalErrorItems = summaries.reduce((sum, s) => sum + s.totalCount, 0)
const averageMasteryRate = studentsWithErrorBook.length > 0
? studentsWithErrorBook.reduce((sum, s) => sum + s.masteredRate, 0) / studentsWithErrorBook.length
: 0
// 按错题数降序排列
const sortedSummaries = [...summaries].sort((a, b) => b.totalCount - a.totalCount)
return (
<div className="flex h-full flex-col space-y-8 p-8">
<div>
<h1 className="text-2xl font-bold tracking-tight"></h1>
<p className="text-muted-foreground">
</p>
</div>
<ClassErrorBookOverview
totalStudents={studentIds.length}
studentsWithErrorBook={studentsWithErrorBook.length}
totalErrorItems={totalErrorItems}
averageMasteryRate={averageMasteryRate}
topWeakKnowledgePoints={weakKps}
subjectDistribution={subjectDist}
/>
<div className="space-y-4">
<h2 className="text-lg font-semibold"></h2>
<StudentErrorTable
students={sortedSummaries}
studentNames={nameMap}
basePath="/teacher/error-book"
/>
</div>
<TopWrongQuestions questions={topWrongQuestions} />
</div>
)
}