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, theme } 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'];
|
|
|
|
|
correct: TextChoice['isCorrect'];
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
|
|
|
|
|
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 isMultipleAnswer = choices.filter(({ isCorrect }) => isCorrect === true).length === 0;
|
|
|
|
|
|
|
|
|
|
const prompt = `<span style="${ParagraphStyle(state.theme)}">Choisir une réponse${
|
|
|
|
|
isMultipleAnswer ? ` ou plusieurs` : ``
|
|
|
|
|
}:</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);
|
2024-03-29 20:08:34 -04:00
|
|
|
const isCorrectOption = isMultipleAnswer ? isPositiveWeight : isCorrect;
|
|
|
|
|
|
|
|
|
|
return `
|
|
|
|
|
<div class='multiple-choice-answers-container'>
|
|
|
|
|
<input class="gift-input" type="${
|
|
|
|
|
isMultipleAnswer ? 'checkbox' : 'radio'
|
|
|
|
|
}" id="${inputId}" name="${id}">
|
|
|
|
|
${AnswerWeight({ correct: isCorrectOption, weight: weight })}
|
|
|
|
|
<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, correct }: AnswerWeightOptions): string {
|
|
|
|
|
const Container = `
|
|
|
|
|
box-shadow: 0px 1px 1px ${theme(state.theme, 'gray400', 'black900')};
|
|
|
|
|
border-radius: 3px;
|
|
|
|
|
padding-left: 0.2rem;
|
|
|
|
|
padding-right: 0.2rem;
|
|
|
|
|
padding-top: 0.05rem;
|
|
|
|
|
padding-bottom: 0.05rem;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const CorrectWeight = `
|
|
|
|
|
color: ${theme(state.theme, 'green700', 'green100')};
|
|
|
|
|
background-color: ${theme(state.theme, 'green100', 'greenGray700')};
|
|
|
|
|
`;
|
|
|
|
|
const IncorrectWeight = `
|
|
|
|
|
color: ${theme(state.theme, 'beige600', 'beige100')};
|
|
|
|
|
background-color: ${theme(state.theme, 'beige300', 'beigeGray800')};
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
return weight
|
|
|
|
|
? `<span style="${Container} ${
|
|
|
|
|
correct ? `${CorrectWeight}` : `${IncorrectWeight}`
|
|
|
|
|
}">${weight}%</span>`
|
|
|
|
|
: ``;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-25 02:02:18 -05:00
|
|
|
function AnswerFeedback({ formattedFeedback }: AnswerFeedbackOptions): string {
|
2024-03-29 20:08:34 -04:00
|
|
|
const Container = `
|
|
|
|
|
color: ${theme(state.theme, 'teal700', 'gray700')};
|
|
|
|
|
`;
|
|
|
|
|
|
2025-01-26 09:33:42 -05:00
|
|
|
return formattedFeedback ? `<span style="${Container}">${FormattedTextTemplate(formattedFeedback)}</span>` : ``;
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|