fix: adds default texts for missing answer texts in capa problems to fix response report generation

If an author has created a capa problem like an mcqs or something similar without providing answer text to an option and some learner selected that option then the response report generation will fail due to that missing answer text. The current commit will add default text to be substituted and prevents report generation crash.
This commit is contained in:
Hamza Farooq
2021-06-03 10:18:52 +05:00
committed by HamzaIbnFarooq
parent 883d223d59
commit 0e4d2ff823
2 changed files with 49 additions and 6 deletions

View File

@@ -669,11 +669,17 @@ class LoncapaProblem(object):
answer_id=answer_id,
choice_number=current_answer
))
assert len(elems) == 1
choicegroup = elems[0].getparent()
input_cls = inputtypes.registry.get_class_for_tag(choicegroup.tag)
choices_map = dict(input_cls.extract_choices(choicegroup, self.capa_system.i18n, text_only=True))
answer_text = choices_map[current_answer]
if len(elems) == 0:
log.warning("Answer Text Missing for answer id: %s and choice number: %s", answer_id, current_answer)
answer_text = "Answer Text Missing"
elif len(elems) == 1:
choicegroup = elems[0].getparent()
input_cls = inputtypes.registry.get_class_for_tag(choicegroup.tag)
choices_map = dict(input_cls.extract_choices(choicegroup, self.capa_system.i18n, text_only=True))
answer_text = choices_map.get(current_answer, "Answer Text Missing")
else:
log.warning("Multiple answers found for answer id: %s and choice number: %s", answer_id, current_answer)
answer_text = "Multiple answers found"
elif isinstance(current_answer, six.string_types):
# Already a string with the answer
@@ -682,7 +688,7 @@ class LoncapaProblem(object):
else:
raise NotImplementedError()
return answer_text
return answer_text or "Answer Text Missing"
def do_targeted_feedback(self, tree):
"""

View File

@@ -627,6 +627,43 @@ class CAPAProblemReportHelpersTest(unittest.TestCase):
)
assert problem.find_answer_text(answer_id, choice_id) == answer_text
@ddt.data(
# Test for ChoiceResponse
('1_2_1', 'choice_0', 'Answer Text Missing'),
('1_2_1', 'choice_1', 'funny'),
# Test for MultipleChoiceResponse
('1_3_1', 'choice_0', 'The iPad'),
('1_3_1', 'choice_2', 'Answer Text Missing'),
('1_3_1', ['choice_0', 'choice_1'], 'The iPad, Answer Text Missing'),
# Test for OptionResponse
('1_4_1', '', 'Answer Text Missing'),
)
@ddt.unpack
def test_find_answer_text_choices_with_missing_text(self, answer_id, choice_id, answer_text):
problem = new_loncapa_problem(
"""
<problem>
<choiceresponse>
<checkboxgroup label="Select the correct synonym of paranoid?">
<choice correct="true"></choice>
<choice correct="false">funny</choice>
</checkboxgroup>
</choiceresponse>
<multiplechoiceresponse>
<choicegroup type="MultipleChoice">
<choice correct="false">The iPad</choice>
<choice correct="false"></choice>
<choice correct="true"></choice>
</choicegroup>
</multiplechoiceresponse>
<optionresponse>
<optioninput options="('yellow','blue','green')" correct="blue" label="Color_1"/>
</optionresponse>
</problem>
"""
)
assert problem.find_answer_text(answer_id, choice_id) == answer_text
@ddt.data(
# Test for ChoiceResponse
('1_2_1', 'over-suspicious'),