Add correct answer to response

This commit is contained in:
Kshitij Sobti
2018-07-05 05:16:39 +05:30
parent 491532c2db
commit c7f980c9de
4 changed files with 66 additions and 2 deletions

View File

@@ -495,6 +495,27 @@ class LoncapaProblem(object):
answer_ids.append(results.keys())
return answer_ids
def find_correct_answer_text(self, answer_id):
"""
Returns the correct answer(s) for the provided answer_id as a single string.
Arguments::
answer_id (str): a string like "98e6a8e915904d5389821a94e48babcf_13_1"
Returns:
str: A string containing the answer or multiple answers separated by commas.
"""
xml_elements = self.tree.xpath('//*[@id="' + answer_id + '"]')
if not xml_elements:
return
xml_element = xml_elements[0]
answer_text = xml_element.xpath('@answer')
if answer_text:
return answer_id[0]
if xml_element.tag == 'optioninput':
return xml_element.xpath('@correct')[0]
return ', '.join(xml_element.xpath('*[@correct="true"]/text()'))
def find_question_label(self, answer_id):
"""
Obtain the most relevant question text for a particular answer.

View File

@@ -659,6 +659,44 @@ class CAPAProblemReportHelpersTest(unittest.TestCase):
)
self.assertEquals(problem.find_answer_text(answer_id, choice_id), answer_text)
@ddt.data(
# Test for ChoiceResponse
('1_2_1', 'over-suspicious'),
# Test for MultipleChoiceResponse
('1_3_1', 'The iPad, Napster'),
# Test for OptionResponse
('1_4_1', 'blue'),
)
@ddt.unpack
def test_find_correct_answer_text_choices(self, answer_id, answer_text):
"""
Verify that ``find_correct_answer_text`` can find the correct answer for
ChoiceResponse, MultipleChoiceResponse and OptionResponse problems.
"""
problem = new_loncapa_problem(
"""
<problem>
<choiceresponse>
<checkboxgroup label="Select the correct synonym of paranoid?">
<choice correct="true">over-suspicious</choice>
<choice correct="false">funny</choice>
</checkboxgroup>
</choiceresponse>
<multiplechoiceresponse>
<choicegroup type="MultipleChoice">
<choice correct="true">The iPad</choice>
<choice correct="true">Napster</choice>
<choice correct="false">The iPod</choice>
</choicegroup>
</multiplechoiceresponse>
<optionresponse>
<optioninput options="('yellow','blue','green')" correct="blue" label="Color_1"/>
</optionresponse>
</problem>
"""
)
self.assertEquals(problem.find_correct_answer_text(answer_id), answer_text)
def test_find_answer_text_textinput(self):
problem = new_loncapa_problem(
"""

View File

@@ -393,13 +393,17 @@ class CapaDescriptor(CapaFields, RawDescriptor):
question_text = lcp.find_question_label(answer_id)
answer_text = lcp.find_answer_text(answer_id, current_answer=orig_answers)
correct_answer_text = lcp.find_correct_answer_text(answer_id)
count += 1
yield (user_state.username, {
report = {
_("Answer ID"): answer_id,
_("Question"): question_text,
_("Answer"): answer_text,
})
}
if correct_answer_text is not None:
report[_("Correct Answer")] = correct_answer_text
yield (user_state.username, report)
# Proxy to CapaModule for access to any of its attributes
answer_available = module_attr('answer_available')

View File

@@ -598,6 +598,7 @@ class TestProblemResponsesReport(TestReportMixin, InstructorTaskModuleTestCase):
'title': 'Problem1',
'Answer ID': 'i4x-edx-1_23x-problem-Problem1_2_1',
'Answer': 'Option 1',
'Correct Answer': u'Option 1',
'Question': u'The correct answer is Option 1',
}, student_data[0])
self.assertIn('state', student_data[0])