diff --git a/common/lib/capa/capa/capa_problem.py b/common/lib/capa/capa/capa_problem.py index 5c4e642345..c13567c82d 100644 --- a/common/lib/capa/capa/capa_problem.py +++ b/common/lib/capa/capa/capa_problem.py @@ -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. diff --git a/common/lib/capa/capa/tests/test_capa_problem.py b/common/lib/capa/capa/tests/test_capa_problem.py index 828c5fdc5c..59ca1d4421 100644 --- a/common/lib/capa/capa/tests/test_capa_problem.py +++ b/common/lib/capa/capa/tests/test_capa_problem.py @@ -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( + """ + + + + over-suspicious + funny + + + + + The iPad + Napster + The iPod + + + + + + + """ + ) + self.assertEquals(problem.find_correct_answer_text(answer_id), answer_text) + def test_find_answer_text_textinput(self): problem = new_loncapa_problem( """ diff --git a/common/lib/xmodule/xmodule/capa_module.py b/common/lib/xmodule/xmodule/capa_module.py index a6077b2da3..2c9e73d101 100644 --- a/common/lib/xmodule/xmodule/capa_module.py +++ b/common/lib/xmodule/xmodule/capa_module.py @@ -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') diff --git a/lms/djangoapps/instructor_task/tests/test_tasks_helper.py b/lms/djangoapps/instructor_task/tests/test_tasks_helper.py index 9c11305af9..6f6fd348b9 100644 --- a/lms/djangoapps/instructor_task/tests/test_tasks_helper.py +++ b/lms/djangoapps/instructor_task/tests/test_tasks_helper.py @@ -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])