EvalueTonSavoir/client/src/components/GiftTemplate/templates/MultipleChoiceAnswersTemplate.ts

64 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-03-29 20:08:34 -04:00
import { nanoid } from 'nanoid';
2025-01-25 02:02:18 -05:00
import { TemplateOptions } from './types';
2025-01-26 09:33:42 -05:00
import {FormattedTextTemplate} from './TextTypeTemplate';
2025-01-25 02:02:18 -05:00
import AnswerIcon from './AnswerIconTemplate';
2024-03-29 20:08:34 -04:00
import { state } from '.';
import { ParagraphStyle } from '../constants';
2025-01-25 02:02:18 -05:00
import { MultipleChoiceQuestion, TextChoice } from 'gift-pegjs';
2024-03-29 20:08:34 -04:00
2025-01-25 02:02:18 -05:00
type MultipleChoiceAnswerOptions = TemplateOptions & Pick<MultipleChoiceQuestion, 'choices'>;
2024-03-29 20:08:34 -04:00
2025-01-25 02:02:18 -05:00
type AnswerFeedbackOptions = TemplateOptions & Pick<TextChoice, 'formattedFeedback'>;
2024-03-29 20:08:34 -04:00
interface AnswerWeightOptions extends TemplateOptions {
2025-01-25 02:02:18 -05:00
weight: TextChoice['weight'];
2024-03-29 20:08:34 -04:00
}
// careful -- this template is re-used by True/False questions!
2025-01-25 02:02:18 -05:00
export default function MultipleChoiceAnswersTemplate({ choices }: MultipleChoiceAnswerOptions) {
2024-03-29 20:08:34 -04:00
const id = `id${nanoid(8)}`;
const hasManyCorrectChoices = choices.filter(({ isCorrect }) => isCorrect === true).length > 1;
2024-03-29 20:08:34 -04:00
const prompt = `<span style="${ParagraphStyle(state.theme)}">Choisir une réponse${
hasManyCorrectChoices ? ` ou plusieurs` : ``
2024-03-29 20:08:34 -04:00
}:</span>`;
const result = choices
2025-01-25 02:02:18 -05:00
.map(({ weight, isCorrect, formattedText, formattedFeedback }) => {
2024-03-29 20:08:34 -04:00
const CustomLabel = `
display: inline-block;
padding: 0.2em 0 0.2em 0;
`;
const inputId = `id${nanoid(6)}`;
2025-01-25 02:02:18 -05:00
const isPositiveWeight = (weight != undefined) && (weight > 0);
const isCorrectOption = hasManyCorrectChoices ? isPositiveWeight || isCorrect : isCorrect;
2024-03-29 20:08:34 -04:00
return `
<div class='multiple-choice-answers-container'>
<input class="gift-input" type="${
hasManyCorrectChoices ? 'checkbox' : 'radio'
2024-03-29 20:08:34 -04:00
}" id="${inputId}" name="${id}">
${AnswerWeight({ weight: weight })}
2024-03-29 20:08:34 -04:00
<label style="${CustomLabel} ${ParagraphStyle(state.theme)}" for="${inputId}">
2025-01-26 09:33:42 -05:00
${FormattedTextTemplate(formattedText)}
2024-03-29 20:08:34 -04:00
</label>
${AnswerIcon({ correct: isCorrectOption })}
2025-01-25 02:02:18 -05:00
${AnswerFeedback({ formattedFeedback: formattedFeedback })}
2024-03-29 20:08:34 -04:00
</input>
</div>
`;
})
.join('');
return `${prompt}${result}`;
}
function AnswerWeight({ weight }: AnswerWeightOptions): string {
return weight ? `<span class="answer-weight-container ${weight > 0 ? 'answer-positive-weight' : 'answer-zero-or-less-weight'}">${weight}%</span>` : ``;
2024-03-29 20:08:34 -04:00
}
2025-01-25 02:02:18 -05:00
function AnswerFeedback({ formattedFeedback }: AnswerFeedbackOptions): string {
return formattedFeedback ? `<span class="feedback-container">${FormattedTextTemplate(formattedFeedback)}</span>` : ``;
2024-03-29 20:08:34 -04:00
}