2025-01-23 22:38:22 -05:00
|
|
|
// MultipleChoiceQuestionDisplay.tsx
|
2025-03-06 03:29:49 -05:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2024-03-29 20:08:34 -04:00
|
|
|
import '../questionStyle.css';
|
|
|
|
|
import { Button } from '@mui/material';
|
2025-01-26 09:33:42 -05:00
|
|
|
import { FormattedTextTemplate } from '../../GiftTemplate/templates/TextTypeTemplate';
|
2025-01-23 22:38:22 -05:00
|
|
|
import { MultipleChoiceQuestion } from 'gift-pegjs';
|
2025-03-06 03:29:49 -05:00
|
|
|
import { StudentType } from 'src/Types/StudentType';
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
interface Props {
|
2025-01-23 22:38:22 -05:00
|
|
|
question: MultipleChoiceQuestion;
|
2024-03-29 20:08:34 -04:00
|
|
|
handleOnSubmitAnswer?: (answer: string) => void;
|
|
|
|
|
showAnswer?: boolean;
|
2025-03-06 03:29:49 -05:00
|
|
|
students?: StudentType[];
|
|
|
|
|
isDisplayOnly?: boolean;
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-23 22:38:22 -05:00
|
|
|
const MultipleChoiceQuestionDisplay: React.FC<Props> = (props) => {
|
2025-03-06 03:29:49 -05:00
|
|
|
const { question, showAnswer, handleOnSubmitAnswer, students, isDisplayOnly } = props;
|
2024-03-29 20:08:34 -04:00
|
|
|
const [answer, setAnswer] = useState<string>();
|
2025-03-06 03:29:49 -05:00
|
|
|
const [pickRates, setPickRates] = useState<number[]>([]);
|
|
|
|
|
const [showCorrectAnswers, setShowCorrectAnswers] = useState(false);
|
2024-04-16 12:14:20 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
const handleOnClickAnswer = (choice: string) => {
|
|
|
|
|
setAnswer(choice);
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-06 03:29:49 -05:00
|
|
|
const toggleShowCorrectAnswers = () => {
|
|
|
|
|
setShowCorrectAnswers(!showCorrectAnswers);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const calculatePickRates = () => {
|
|
|
|
|
if (!students || students.length === 0) {
|
|
|
|
|
setPickRates(new Array(question.choices.length).fill(0)); // Fill with 0 for each choice
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rates = question.choices.map(choice => {
|
|
|
|
|
const choiceAnswers = students.filter(student =>
|
|
|
|
|
student.answers.some(ans =>
|
|
|
|
|
ans.idQuestion === Number(question.id) && ans.answer === choice.formattedText.text
|
|
|
|
|
)
|
|
|
|
|
).length;
|
|
|
|
|
return (choiceAnswers / students.length) * 100;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setPickRates(rates);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setAnswer(undefined);
|
|
|
|
|
calculatePickRates();
|
|
|
|
|
}, [students, question, showCorrectAnswers]);
|
|
|
|
|
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
const alpha = Array.from(Array(26)).map((_e, i) => i + 65);
|
|
|
|
|
const alphabet = alpha.map((x) => String.fromCharCode(x));
|
|
|
|
|
return (
|
|
|
|
|
<div className="question-container">
|
2024-04-08 21:37:11 -04:00
|
|
|
<div className="question content">
|
2025-01-26 09:33:42 -05:00
|
|
|
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(question.formattedStem) }} />
|
2024-03-29 20:08:34 -04:00
|
|
|
</div>
|
|
|
|
|
<div className="choices-wrapper mb-1">
|
2025-01-23 22:38:22 -05:00
|
|
|
{question.choices.map((choice, i) => {
|
|
|
|
|
const selected = answer === choice.formattedText.text ? 'selected' : '';
|
2025-03-06 03:29:49 -05:00
|
|
|
const rateStyle = showCorrectAnswers ? {
|
|
|
|
|
backgroundImage: `linear-gradient(to right, ${choice.isCorrect ? 'lightgreen' : 'lightcoral'} ${pickRates[i]}%, transparent ${pickRates[i]}%)`,
|
|
|
|
|
color: 'black'
|
|
|
|
|
} : {};
|
2024-03-29 20:08:34 -04:00
|
|
|
return (
|
2025-01-23 22:38:22 -05:00
|
|
|
<div key={choice.formattedText.text + i} className="choice-container">
|
2025-03-06 03:29:49 -05:00
|
|
|
{/* <Button
|
2024-03-29 20:08:34 -04:00
|
|
|
variant="text"
|
|
|
|
|
className="button-wrapper"
|
2025-01-30 23:01:04 -05:00
|
|
|
onClick={() => !showAnswer && handleOnClickAnswer(choice.formattedText.text)}>
|
2025-01-31 15:55:37 -05:00
|
|
|
{showAnswer? (<div> {(choice.isCorrect ? '✅' : '❌')}</div>)
|
|
|
|
|
:``}
|
2024-03-29 20:08:34 -04:00
|
|
|
<div className={`circle ${selected}`}>{alphabet[i]}</div>
|
|
|
|
|
<div className={`answer-text ${selected}`}>
|
2025-01-26 09:33:42 -05:00
|
|
|
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(choice.formattedText) }} />
|
2024-03-29 20:08:34 -04:00
|
|
|
</div>
|
2025-01-30 23:01:04 -05:00
|
|
|
{choice.formattedFeedback && showAnswer && (
|
2024-03-29 20:08:34 -04:00
|
|
|
<div className="feedback-container mb-1 mt-1/2">
|
2025-01-26 09:33:42 -05:00
|
|
|
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(choice.formattedFeedback) }} />
|
2024-03-29 20:08:34 -04:00
|
|
|
</div>
|
|
|
|
|
)}
|
2025-03-06 03:29:49 -05:00
|
|
|
</Button> */}
|
|
|
|
|
<Button
|
|
|
|
|
variant="text"
|
|
|
|
|
className={`button-wrapper ${selected}`}
|
|
|
|
|
onClick={() => !showAnswer && handleOnClickAnswer(choice.formattedText.text)}
|
|
|
|
|
>
|
|
|
|
|
<div className={`circle ${selected}`}>{alphabet[i]}</div>
|
|
|
|
|
<div className={`answer-text ${selected}`}
|
|
|
|
|
style={rateStyle}>
|
|
|
|
|
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(choice.formattedText) }} />
|
|
|
|
|
</div>
|
|
|
|
|
{choice.formattedFeedback && showAnswer && (
|
|
|
|
|
<div className="feedback-container mb-1 mt-1/2">
|
|
|
|
|
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(choice.formattedFeedback) }} />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{showCorrectAnswers && <div>{choice.isCorrect ? '✅' : '❌'}</div>}
|
|
|
|
|
{showCorrectAnswers && (
|
|
|
|
|
<div className="pick-rate">
|
|
|
|
|
{pickRates[i].toFixed(1)}%
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-01-30 23:01:04 -05:00
|
|
|
</Button>
|
2024-03-29 20:08:34 -04:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
2025-01-23 22:38:22 -05:00
|
|
|
{question.formattedGlobalFeedback && showAnswer && (
|
|
|
|
|
<div className="global-feedback mb-2">
|
2025-01-26 09:33:42 -05:00
|
|
|
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate(question.formattedGlobalFeedback) }} />
|
2025-01-25 02:02:18 -05:00
|
|
|
</div>
|
2024-03-29 20:08:34 -04:00
|
|
|
)}
|
2025-01-21 15:35:07 -05:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
{!showAnswer && handleOnSubmitAnswer && (
|
2025-01-21 15:35:07 -05:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
<Button
|
|
|
|
|
variant="contained"
|
|
|
|
|
onClick={() =>
|
|
|
|
|
answer !== undefined && handleOnSubmitAnswer && handleOnSubmitAnswer(answer)
|
|
|
|
|
}
|
|
|
|
|
disabled={answer === undefined}
|
|
|
|
|
>
|
|
|
|
|
Répondre
|
2025-01-21 15:35:07 -05:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
</Button>
|
|
|
|
|
)}
|
2025-03-06 03:29:49 -05:00
|
|
|
|
|
|
|
|
{isDisplayOnly && (
|
|
|
|
|
<div>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outlined"
|
|
|
|
|
onClick={toggleShowCorrectAnswers}
|
|
|
|
|
color="primary"
|
|
|
|
|
>
|
|
|
|
|
{showCorrectAnswers ? "Masquer les résultats" : "Afficher les résultats"}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2024-03-29 20:08:34 -04:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-23 22:38:22 -05:00
|
|
|
export default MultipleChoiceQuestionDisplay;
|