Add quiz feature: student UI, admin editor, lesson page integration
- QuizSection component: shows questions as text inputs, read-only after submission - QuizEditor component: admin CRUD for quiz questions with type selector - saveQuiz/deleteQuiz server actions for admin - submitQuizAttempt server action: idempotent, auto-marks lesson complete - Student lesson page: renders QuizSection, updates complete button logic - Admin lesson page: renders QuizEditor below homework section Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -94,6 +94,43 @@ export async function submitHomework(
|
||||
revalidatePath(`/courses/${slug}/lessons/${lessonId}`);
|
||||
}
|
||||
|
||||
// ── Quiz ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
export async function submitQuizAttempt(
|
||||
quizId: string,
|
||||
lessonId: string,
|
||||
slug: string,
|
||||
answers: Record<string, string>
|
||||
) {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
if (!session) throw new Error("Unauthorized");
|
||||
|
||||
const existing = await prisma.quizAttempt.findFirst({
|
||||
where: { quizId, userId: session.user.id },
|
||||
});
|
||||
if (existing) return;
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await tx.quizAttempt.create({
|
||||
data: {
|
||||
userId: session.user.id,
|
||||
quizId,
|
||||
score: 0,
|
||||
answers: answers as object,
|
||||
},
|
||||
});
|
||||
await tx.lessonProgress.upsert({
|
||||
where: { userId_lessonId: { userId: session.user.id, lessonId } },
|
||||
create: { userId: session.user.id, lessonId },
|
||||
update: {},
|
||||
});
|
||||
});
|
||||
|
||||
revalidatePath(`/courses/${slug}/lessons/${lessonId}`);
|
||||
revalidatePath(`/courses/${slug}`);
|
||||
revalidatePath("/dashboard");
|
||||
}
|
||||
|
||||
// ── Comments ──────────────────────────────────────────────────────────────────
|
||||
|
||||
export async function addComment(lessonId: string, slug: string, text: string) {
|
||||
|
||||
Reference in New Issue
Block a user