EvalueTonSavoir/client/src/components/QuestionsDisplay/NumericalQuestionDisplay/NumericalQuestionDisplay.tsx

155 lines
7.1 KiB
TypeScript
Raw Normal View History

2024-03-29 20:08:34 -04:00
// NumericalQuestion.tsx
import React, { useState, useEffect } from 'react';
2024-03-29 20:08:34 -04:00
import '../questionStyle.css';
import { Button, TextField } from '@mui/material';
2025-01-26 09:33:42 -05:00
import { FormattedTextTemplate } from '../../GiftTemplate/templates/TextTypeTemplate';
2025-01-24 14:51:58 -05:00
import { NumericalQuestion, SimpleNumericalAnswer, RangeNumericalAnswer, HighLowNumericalAnswer } from 'gift-pegjs';
import { isSimpleNumericalAnswer, isRangeNumericalAnswer, isHighLowNumericalAnswer, isMultipleNumericalAnswer } from 'gift-pegjs/typeGuards';
import { StudentType } from 'src/Types/StudentType';
2025-03-08 11:05:25 -05:00
import { AnswerType } from 'src/pages/Student/JoinRoom/JoinRoom';
2024-03-29 20:08:34 -04:00
interface Props {
2025-01-24 14:51:58 -05:00
question: NumericalQuestion;
2025-03-08 11:05:25 -05:00
handleOnSubmitAnswer?: (answer: AnswerType) => void;
2024-03-29 20:08:34 -04:00
showAnswer?: boolean;
passedAnswer?: AnswerType;
students?: StudentType[];
showResults?: boolean;
2024-03-29 20:08:34 -04:00
}
2025-01-24 14:51:58 -05:00
const NumericalQuestionDisplay: React.FC<Props> = (props) => {
const { question, showAnswer, handleOnSubmitAnswer, students, showResults, passedAnswer } =
2024-03-29 20:08:34 -04:00
props;
2025-03-21 00:25:25 -04:00
const [answer, setAnswer] = useState<AnswerType>(passedAnswer || []);
2025-01-24 14:51:58 -05:00
const correctAnswers = question.choices;
let correctAnswer = '';
const [correctAnswerRate, setCorrectAnswerRate] = useState<number>(0);
2025-03-16 21:25:21 -04:00
const [submissionCounts, setSubmissionCounts] = useState({
correctSubmissions: 0,
totalSubmissions: 0
});
useEffect(() => {
if (passedAnswer !== null && passedAnswer !== undefined) {
setAnswer(passedAnswer);
}
if (showResults && students) {
calculateCorrectAnswerRate();
}
}, [passedAnswer, showResults, students]);
const calculateCorrectAnswerRate = () => {
if (!students || students.length === 0) {
2025-03-16 21:25:21 -04:00
setSubmissionCounts({ correctSubmissions: 0, totalSubmissions: 0 });
return;
}
const totalSubmissions = students.length;
const correctSubmissions = students.filter(student =>
student.answers.some(ans =>
ans.idQuestion === Number(question.id) && ans.isCorrect
)
).length;
2025-03-16 21:25:21 -04:00
setSubmissionCounts({
correctSubmissions,
totalSubmissions
});
setCorrectAnswerRate((correctSubmissions / totalSubmissions) * 100);
};
2025-01-24 14:51:58 -05:00
//const isSingleAnswer = correctAnswers.length === 1;
2024-03-29 20:08:34 -04:00
2025-01-24 14:51:58 -05:00
if (isSimpleNumericalAnswer(correctAnswers[0])) {
correctAnswer = `${(correctAnswers[0] as SimpleNumericalAnswer).number}`;
} else if (isRangeNumericalAnswer(correctAnswers[0])) {
const choice = correctAnswers[0] as RangeNumericalAnswer;
correctAnswer = `Entre ${choice.number - choice.range} et ${choice.number + choice.range}`;
2025-01-24 14:51:58 -05:00
} else if (isHighLowNumericalAnswer(correctAnswers[0])) {
const choice = correctAnswers[0] as HighLowNumericalAnswer;
correctAnswer = `Entre ${choice.numberLow} et ${choice.numberHigh}`;
2025-01-24 14:51:58 -05:00
} else if (isMultipleNumericalAnswer(correctAnswers[0])) {
correctAnswer = `MultipleNumericalAnswer is not supported yet`;
2025-01-24 14:51:58 -05:00
} else {
throw new Error('Unknown numerical answer type');
2025-01-24 14:51:58 -05:00
}
2024-03-29 20:08:34 -04:00
return (
<>
<div className="container question-wrapper">
<div className="row justify-content-center">
<div className="col-auto">
<div>
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(question.formattedStem) }} />
2025-01-24 14:51:58 -05:00
</div>
{showAnswer ? (
<>
<div className="correct-answer-text mb-2">
<strong>La bonne réponse est: </strong>
{correctAnswer}</div>
<span>
<strong>Votre réponse est: </strong>{answer.toString()}
</span>
{question.formattedGlobalFeedback && <div className="global-feedback mb-2">
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(question.formattedGlobalFeedback) }} />
</div>}
</>
) : (
<>
<div className="answer-wrapper mb-1">
<TextField
type="number"
id={question.formattedStem.text}
name={question.formattedStem.text}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
setAnswer([e.target.valueAsNumber]);
}}
inputProps={{ 'data-testid': 'number-input' }}
/>
</div>
{question.formattedGlobalFeedback && showAnswer && (
<div className="global-feedback mb-2">
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(question.formattedGlobalFeedback) }} />
</div>
)}
{handleOnSubmitAnswer && (
<div className="col-auto d-flex flex-column align-items-center">
<Button
variant="contained"
onClick={() =>
answer !== undefined &&
handleOnSubmitAnswer &&
handleOnSubmitAnswer(answer)
}
disabled={answer === undefined || answer === null || isNaN(answer[0] as number)}
>
Répondre
</Button>
</div>
)}
</>
)}
</div>
{showResults && (
<div className="col-auto">
<div>
Taux de réponse correcte: {submissionCounts.correctSubmissions}/{submissionCounts.totalSubmissions}
</div>
<div className="progress-bar-container">
<div className="progress-bar-fill" style={{ width: `${correctAnswerRate}%` }}></div>
<div className="progress-bar-text">
{correctAnswerRate.toFixed(1)}%
</div>
</div>
</div>
)}
</div>
</div>
</>
2024-03-29 20:08:34 -04:00
);
};
2025-01-24 14:51:58 -05:00
export default NumericalQuestionDisplay;