diff --git a/client/src/components/LiveResults/LiveResults.tsx b/client/src/components/LiveResults/LiveResults.tsx index 1a88a9e..a19518d 100644 --- a/client/src/components/LiveResults/LiveResults.tsx +++ b/client/src/components/LiveResults/LiveResults.tsx @@ -114,6 +114,45 @@ const LiveResults: React.FC = ({ questions, showSelectedQuesti return correctAnswers; }; + const totalParticipation: number = useMemo(() => { + let classTotal = 0; + + students.forEach((student) => { + classTotal += getTotalAnswers(student); + }); + + return (classTotal / (students.length * maxQuestions)) * 100; + }, [students]); + + const getTotalAnswers = (student: StudentType): number => { + if (student.answers.length === 0) { + return 0; + } + + const uniqueQuestions = new Set(); + let answers = 0; + + for (const answer of student.answers) { + const { idQuestion } = answer; + + if (!uniqueQuestions.has(idQuestion)) { + uniqueQuestions.add(idQuestion); + answers++; + } + } + return answers; + }; + + const getParticipationPerQuestion = (index: number): number => { + return ( + (students.filter((student) => + student.answers.some( + (answer) => + parseInt(answer.idQuestion.toString()) === index + 1) + ).length / students.length) * 100 + ); + }; + return (
@@ -317,6 +356,41 @@ const LiveResults: React.FC = ({ questions, showSelectedQuesti {students.length > 0 ? `${totalCorrectAnswers.toFixed()}` : '-'} + + +
% réussite
+
+ {Array.from({ length: maxQuestions }, (_, index) => ( + + {students.length > 0 + ? `${getParticipationPerQuestion(index).toFixed()} %` + : '-'} + + ))} + + {students.length > 0 ? `${totalParticipation.toFixed()} %` : '-'} + +