diff --git a/common/lib/capa/capa/responsetypes.py b/common/lib/capa/capa/responsetypes.py index 8ab716735c..2035c42661 100644 --- a/common/lib/capa/capa/responsetypes.py +++ b/common/lib/capa/capa/responsetypes.py @@ -1961,9 +1961,10 @@ class ImageResponse(LoncapaResponse): self.ielements = self.inputfields self.answer_ids = [ie.get('id') for ie in self.ielements] + def get_score(self, student_answers): correct_map = CorrectMap() - expectedset = self.get_answers() + expectedset = self.get_mapped_answers() for aid in self.answer_ids: # loop through IDs of # fields in our stanza given = student_answers[ @@ -2018,11 +2019,42 @@ class ImageResponse(LoncapaResponse): break return correct_map - def get_answers(self): - return ( + def get_mapped_answers(self): + ''' + Returns the internal representation of the answers + + Input: + None + Returns: + tuple (dict, dict) - + rectangles (dict) - a map of inputs to the defined rectangle for that input + regions (dict) - a map of inputs to the defined region for that input + ''' + answers = ( dict([(ie.get('id'), ie.get( 'rectangle')) for ie in self.ielements]), dict([(ie.get('id'), ie.get('regions')) for ie in self.ielements])) + return answers + + def get_answers(self): + ''' + Returns the external representation of the answers + + Input: + None + Returns: + dict (str, (str, str)) - a map of inputs to a tuple of their rectange + and their regions + ''' + answers = {} + for ie in self.ielements: + ie_id = ie.get('id') + answers[ie_id] = (ie.get('rectangle'), ie.get('regions')) + + return answers + + + #----------------------------------------------------------------------------- @@ -2087,8 +2119,8 @@ class AnnotationResponse(LoncapaResponse): correct_option = self._find_option_with_choice( inputfield, 'correct') if correct_option is not None: - answer_map[inputfield.get( - 'id')] = correct_option.get('description') + input_id = inputfield.get('id') + answer_map[input_id] = correct_option.get('description') return answer_map def _get_max_points(self): diff --git a/common/lib/capa/capa/tests/test_responsetypes.py b/common/lib/capa/capa/tests/test_responsetypes.py index 0c007f83b2..e009c26aef 100644 --- a/common/lib/capa/capa/tests/test_responsetypes.py +++ b/common/lib/capa/capa/tests/test_responsetypes.py @@ -36,6 +36,10 @@ class ResponseTest(unittest.TestCase): correct_map = problem.grade_answers(input_dict) self.assertEquals(correct_map.get_correctness('1_2_1'), expected_correctness) + def assert_answer_format(self, problem): + answers = problem.get_question_answers() + self.assertTrue(answers['1_2_1'] is not None) + def assert_multiple_grade(self, problem, correct_answers, incorrect_answers): for input_str in correct_answers: result = problem.grade_answers({'1_2_1': input_str}).get_correctness('1_2_1') @@ -166,6 +170,13 @@ class ImageResponseTest(ResponseTest): incorrect_inputs = ["[0,0]", "[600,300]"] self.assert_multiple_grade(problem, correct_inputs, incorrect_inputs) + def test_show_answer(self): + rectangle_str = "(100,100)-(200,200)" + region_str = "[[10,10], [20,10], [20, 30]]" + + problem = self.build_problem(regions=region_str, rectangle=rectangle_str) + self.assert_answer_format(problem) + class SymbolicResponseTest(unittest.TestCase): def test_sr_grade(self):