Skip to content

Commit 11a7e78

Browse files
authored
fix: parsing text input problems (#1679)
* Parse incorrect answer fields in text input problems * Fix feedback order
1 parent 56b7a7b commit 11a7e78

File tree

4 files changed

+100
-4
lines changed

4 files changed

+100
-4
lines changed

src/editors/containers/ProblemEditor/components/EditProblemView/hooks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const checkForNoAnswers = ({ openSaveWarningModal, problem }) => {
9393
const titles = [];
9494
answers.forEach(answer => {
9595
const title = simpleTextAreaProblems.includes(problemType) ? answer.title : answerTitles[answer.id];
96-
if (title.length > 0) {
96+
if (title?.length > 0) {
9797
titles.push(title);
9898
}
9999
});
@@ -108,7 +108,7 @@ export const checkForNoAnswers = ({ openSaveWarningModal, problem }) => {
108108
answers.forEach(answer => {
109109
if (answer.correct) {
110110
const title = simpleTextAreaProblems.includes(problemType) ? answer.title : answerTitles[answer.id];
111-
if (title.length > 0) {
111+
if (title?.length > 0) {
112112
correctAnswer = true;
113113
}
114114
}

src/editors/containers/ProblemEditor/data/OLXParser.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,12 @@ export class OLXParser {
140140
[correctAnswerFeedbackTag, incorrectAnswerFeedbackTag] = option;
141141
}
142142
const problemBodyArr = problemBody[problemType];
143+
let hasCorrectAnswerFeedback = isChoiceProblem;
143144
problemBodyArr.forEach(subtag => {
144145
const tagNames = Object.keys(subtag);
145146
if (!isChoiceProblem && tagNames.includes(correctAnswerFeedbackTag)) {
146147
preservedAnswers.unshift(subtag[correctAnswerFeedbackTag]);
148+
hasCorrectAnswerFeedback = true;
147149
}
148150
if (problemType === ProblemTypeKeys.TEXTINPUT && tagNames.includes(incorrectAnswerFeedbackTag)) {
149151
preservedAnswers.push(subtag);
@@ -157,6 +159,11 @@ export class OLXParser {
157159
});
158160
}
159161
});
162+
// Since the first feedback is taken as correct answer feedback by non-choice problems,
163+
// we insert an empty array to preserve feedback order.
164+
if (!hasCorrectAnswerFeedback) {
165+
preservedAnswers.unshift([]);
166+
}
160167
return preservedAnswers;
161168
}
162169

@@ -360,7 +367,7 @@ export class OLXParser {
360367
const stringEqualHint = _.get(stringresponse, 'stringequalhint', []);
361368
if (_.isArray(stringEqualHint)) {
362369
stringEqualHint.forEach((newAnswer, indx) => {
363-
answerFeedback = this.richTextBuilder.build(stringEqualHintFeedback[indx].stringequalhint);
370+
answerFeedback = this.getFeedback(stringEqualHintFeedback[indx]?.stringequalhint);
364371
answers.push({
365372
id: indexToLetterMap[answers.length],
366373
title: newAnswer['@_answer'],
@@ -369,7 +376,7 @@ export class OLXParser {
369376
});
370377
});
371378
} else {
372-
answerFeedback = this.richTextBuilder.build(stringEqualHintFeedback[0].stringequalhint);
379+
answerFeedback = this.getFeedback(stringEqualHintFeedback[0]?.stringequalhint);
373380
answers.push({
374381
id: indexToLetterMap[answers.length],
375382
title: stringEqualHint['@_answer'],

src/editors/containers/ProblemEditor/data/OLXParser.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
dropdownOLXWithFeedbackAndHintsOLX,
55
numericInputWithFeedbackAndHintsOLX,
66
textInputWithFeedbackAndHintsOLX,
7+
textInputWithFeedbackInIncorrectAnswerOnlyOLX,
78
multipleChoiceWithoutAnswers,
89
multipleChoiceSingleAnswer,
910
multipleChoiceWithFeedbackAndHintsOLX,
@@ -39,6 +40,7 @@ const multipleChoiceOlxParser = new OLXParser(multipleChoiceWithFeedbackAndHints
3940
const multipleChoiceWithoutAnswersOlxParser = new OLXParser(multipleChoiceWithoutAnswers.rawOLX);
4041
const multipleChoiceSingleAnswerOlxParser = new OLXParser(multipleChoiceSingleAnswer.rawOLX);
4142
const textInputOlxParser = new OLXParser(textInputWithFeedbackAndHintsOLX.rawOLX);
43+
const textInputIncorrectFeedbackOlxParser = new OLXParser(textInputWithFeedbackInIncorrectAnswerOnlyOLX.rawOLX);
4244
const textInputMultipleAnswersOlxParser = new OLXParser(textInputWithFeedbackAndHintsOLXWithMultipleAnswers.rawOLX);
4345
const advancedOlxParser = new OLXParser(advancedProblemOlX.rawOLX);
4446
const multipleTextInputOlxParser = new OLXParser(multipleTextInputProblemOlX.rawOLX);
@@ -286,6 +288,13 @@ describe('OLXParser', () => {
286288
expect(answers).toHaveLength(3);
287289
});
288290
});
291+
describe('given text input olx with feedback for incorrect answer only and hints', () => {
292+
const { answers } = textInputIncorrectFeedbackOlxParser.parseStringResponse();
293+
it('should equal an array of objects with length three', () => {
294+
expect(answers).toEqual(textInputWithFeedbackInIncorrectAnswerOnlyOLX.data.answers);
295+
expect(answers).toHaveLength(4);
296+
});
297+
});
289298
describe('given text input olx with feedback and hints with multiple answers', () => {
290299
const { answers } = textInputMultipleAnswersOlxParser.parseStringResponse();
291300
it('should equal an array of objects with length four', () => {

src/editors/containers/ProblemEditor/data/mockData/olxTestData.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,86 @@ export const textInputWithFeedbackAndHintsOLX = {
562562
`,
563563
};
564564

565+
export const textInputWithFeedbackInIncorrectAnswerOnlyOLX = {
566+
rawOLX: `<problem>
567+
<stringresponse answer="the correct answer" type="ci">
568+
<p>You can use this template as a guide to the simple editor markdown and OLX markup to use for text input with hints and feedback problems. Edit this component to replace this template with your own assessment.</p>
569+
<label>Add the question text, or prompt, here. This text is required.</label>
570+
<description>You can add an optional tip or note related to the prompt like this. </description>
571+
<additional_answer answer="optional acceptable variant of the correct answer"/>
572+
<stringequalhint answer="optional incorrect answer such as a frequent misconception"><p>You can specify optional feedback for none, a subset, or all of the answers.</p></stringequalhint>
573+
<stringequalhint answer="another optional incorrect answer such as a frequent misconception"><p>You can specify feedback again.</p></stringequalhint>
574+
<textline size="20"/>
575+
</stringresponse>
576+
<demandhint>
577+
<hint><p>You can add an optional hint like this. Problems that have a hint include a hint button, and this text appears the first time learners select the button.</p></hint>
578+
<hint><p>If you add more than one hint, a different hint appears each time learners select the hint button.</p></hint>
579+
</demandhint>
580+
</problem>`,
581+
hints: [{
582+
id: 0,
583+
value: '<p>You can add an optional hint like this. Problems that have a hint include a hint button, and this text appears the first time learners select the button.</p>',
584+
},
585+
{
586+
id: 1,
587+
value: '<p>If you add more than one hint, a different hint appears each time learners select the hint button.</p>',
588+
},
589+
],
590+
data: {
591+
answers: [
592+
{
593+
id: 'A',
594+
title: 'the correct answer',
595+
correct: true,
596+
selectedFeedback: '',
597+
},
598+
{
599+
id: 'B',
600+
title: 'optional acceptable variant of the correct answer',
601+
correct: true,
602+
selectedFeedback: '',
603+
},
604+
{
605+
id: 'C',
606+
title: 'optional incorrect answer such as a frequent misconception',
607+
correct: false,
608+
selectedFeedback: '<p>You can specify optional feedback for none, a subset, or all of the answers.</p>',
609+
},
610+
{
611+
id: 'D',
612+
title: 'another optional incorrect answer such as a frequent misconception',
613+
correct: false,
614+
selectedFeedback: '<p>You can specify feedback again.</p>',
615+
},
616+
],
617+
additionalStringAttributes: {
618+
type: 'ci',
619+
textline: {
620+
size: '20',
621+
},
622+
},
623+
},
624+
question: `<p>You can use this template as a guide to the simple editor markdown and OLX markup to use for text input with hints and feedback problems. Edit this component to replace this template with your own assessment.</p>
625+
<label>Add the question text, or prompt, here. This text is required.</label>
626+
<em class="olx_description">You can add an optional tip or note related to the prompt like this. </em>`,
627+
buildOLX: `<problem>
628+
<stringresponse answer="the correct answer" type="ci">
629+
<p>You can use this template as a guide to the simple editor markdown and OLX markup to use for text input with hints and feedback problems. Edit this component to replace this template with your own assessment.</p>
630+
<label>Add the question text, or prompt, here. This text is required.</label>
631+
<em>You can add an optional tip or note related to the prompt like this.</em>
632+
<correcthint><p>You can specify optional feedback like this, which appears after this answer is submitted.</p></correcthint>
633+
<additional_answer answer="optional acceptable variant of the correct answer"></additional_answer>
634+
<stringequalhint answer="optional incorrect answer such as a frequent misconception"><p>You can specify optional feedback for none, a subset, or all of the answers.</p></stringequalhint>
635+
<textline size="20"></textline>
636+
</stringresponse>
637+
<demandhint>
638+
<hint><p>You can add an optional hint like this. Problems that have a hint include a hint button, and this text appears the first time learners select the button.</p></hint>
639+
<hint><p>If you add more than one hint, a different hint appears each time learners select the hint button.</p></hint>
640+
</demandhint>
641+
</problem>
642+
`,
643+
};
644+
565645
export const textInputWithFeedbackAndHintsOLXWithMultipleAnswers = {
566646
rawOLX: `<problem>
567647
<stringresponse answer="the correct answer" type="ci">

0 commit comments

Comments
 (0)