107 lines
3.4 KiB
TypeScript
107 lines
3.4 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { createClient } from '@/lib/supabase/server'
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const supabase = await createClient()
|
|
const db = supabase as any
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const participationId = searchParams.get('participation_id')
|
|
|
|
if (!participationId) {
|
|
return NextResponse.json({ error: 'participation_id est requis' }, { status: 400 })
|
|
}
|
|
|
|
const { data: participation, error: partError } = await db
|
|
.from('student_participations')
|
|
.select('id, status, score, first_name, last_name, session_id')
|
|
.eq('id', participationId)
|
|
.single()
|
|
|
|
if (partError || !participation) {
|
|
return NextResponse.json({ error: 'Participation introuvable' }, { status: 404 })
|
|
}
|
|
|
|
if (participation.status !== 'completed') {
|
|
return NextResponse.json(
|
|
{ error: 'Les résultats ne sont disponibles qu\'après avoir terminé le quiz' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
const { data: session } = await db
|
|
.from('sessions')
|
|
.select('quiz_id')
|
|
.eq('id', participation.session_id)
|
|
.single()
|
|
|
|
if (!session) {
|
|
return NextResponse.json({ error: 'Session introuvable' }, { status: 404 })
|
|
}
|
|
|
|
const { data: questions, error: questionsError } = await db
|
|
.from('questions')
|
|
.select(`
|
|
id,
|
|
question_text,
|
|
explanation,
|
|
order,
|
|
answers(id, answer_text, is_correct)
|
|
`)
|
|
.eq('quiz_id', session.quiz_id)
|
|
.order('order')
|
|
|
|
if (questionsError) {
|
|
return NextResponse.json({ error: 'Erreur récupération des questions' }, { status: 500 })
|
|
}
|
|
|
|
const { data: studentAnswers } = await db
|
|
.from('student_answers')
|
|
.select('question_id, answer_id')
|
|
.eq('participation_id', participationId)
|
|
|
|
const studentAnswerMap = new Map(
|
|
(studentAnswers ?? []).map((sa: any) => [sa.question_id, sa.answer_id])
|
|
)
|
|
|
|
const detailedResults = (questions ?? []).map((q: any) => {
|
|
const studentAnswerId = studentAnswerMap.get(q.id) ?? null
|
|
const correctAnswer = q.answers.find((a: any) => a.is_correct)
|
|
const studentAnswer = q.answers.find((a: any) => a.id === studentAnswerId)
|
|
const isCorrect = studentAnswerId === correctAnswer?.id
|
|
|
|
return {
|
|
question_id: q.id,
|
|
question_text: q.question_text,
|
|
explanation: q.explanation,
|
|
student_answer: studentAnswer ? { id: studentAnswer.id, text: studentAnswer.answer_text } : null,
|
|
correct_answer: correctAnswer ? { id: correctAnswer.id, text: correctAnswer.answer_text } : null,
|
|
is_correct: isCorrect,
|
|
all_answers: q.answers.map((a: any) => ({ id: a.id, text: a.answer_text, is_correct: a.is_correct })),
|
|
}
|
|
})
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
results: {
|
|
student: {
|
|
first_name: participation.first_name,
|
|
last_name: participation.last_name,
|
|
score: participation.score,
|
|
},
|
|
questions: detailedResults,
|
|
summary: {
|
|
total: detailedResults.length,
|
|
correct: detailedResults.filter((r: any) => r.is_correct).length,
|
|
score: participation.score,
|
|
},
|
|
},
|
|
})
|
|
} catch (error) {
|
|
console.error('[student/results]', error)
|
|
return NextResponse.json({ error: 'Erreur serveur interne' }, { status: 500 })
|
|
}
|
|
}
|