Commit ac7fe063 by John Hess

fixed excess_draggable behavior so that true means it is actually excess

parent d265c255
...@@ -111,7 +111,7 @@ class DragAndDrop(object): ...@@ -111,7 +111,7 @@ class DragAndDrop(object):
Returns: bool. Returns: bool.
''' '''
for draggable in self.excess_draggables: for draggable in self.excess_draggables:
if not self.excess_draggables[draggable]: if self.excess_draggables[draggable]:
return False # user answer has more draggables than correct answer return False # user answer has more draggables than correct answer
# Number of draggables in user_groups may be differ that in # Number of draggables in user_groups may be differ that in
...@@ -304,8 +304,13 @@ class DragAndDrop(object): ...@@ -304,8 +304,13 @@ class DragAndDrop(object):
user_answer = json.loads(user_answer) user_answer = json.loads(user_answer)
# check if we have draggables that are not in correct answer: # This dictionary will hold a key for each draggable the user placed on
self.excess_draggables = {} # the image. The value is True if that draggable is not mentioned in any
# correct_answer entries. If the draggable is mentioned in at least one
# correct_answer entry, the value is False.
# default to consider every user answer excess until proven otherwise.
self.excess_draggables = dict((users_draggable.keys()[0],True)
for users_draggable in user_answer['draggables'])
# create identical data structures from user answer and correct answer # create identical data structures from user answer and correct answer
for i in xrange(0, len(correct_answer)): for i in xrange(0, len(correct_answer)):
...@@ -322,15 +327,8 @@ class DragAndDrop(object): ...@@ -322,15 +327,8 @@ class DragAndDrop(object):
self.user_groups[groupname].append(draggable_name) self.user_groups[groupname].append(draggable_name)
self.user_positions[groupname]['user'].append( self.user_positions[groupname]['user'].append(
draggable_dict[draggable_name]) draggable_dict[draggable_name])
self.excess_draggables[draggable_name] = True # proved that this is not excess
else: self.excess_draggables[draggable_name] = False
self.excess_draggables[draggable_name] = \
self.excess_draggables.get(draggable_name, False)
if len(correct_answer)==0:
for draggable_dict in user_answer['draggables']:
# draggable_dict is 1-to-1 {draggable_name: position}
draggable_name = draggable_dict.keys()[0]
self.excess_draggables[draggable_name] = False
def grade(user_input, correct_answer): def grade(user_input, correct_answer):
""" Creates DragAndDrop instance from user_input and correct_answer and """ Creates DragAndDrop instance from user_input and correct_answer and
......
...@@ -46,6 +46,18 @@ class Test_DragAndDrop_Grade(unittest.TestCase): ...@@ -46,6 +46,18 @@ class Test_DragAndDrop_Grade(unittest.TestCase):
correct_answer = {'1': 't1', 'name_with_icon': 't2'} correct_answer = {'1': 't1', 'name_with_icon': 't2'}
self.assertTrue(draganddrop.grade(user_input, correct_answer)) self.assertTrue(draganddrop.grade(user_input, correct_answer))
def test_expect_no_actions_wrong(self):
user_input = '{"draggables": [{"1": "t1"}, \
{"name_with_icon": "t2"}]}'
correct_answer = []
self.assertFalse(draganddrop.grade(user_input, correct_answer))
def test_expect_no_actions_right(self):
user_input = '{"draggables": []}'
correct_answer = []
self.assertTrue(draganddrop.grade(user_input, correct_answer))
def test_targets_false(self): def test_targets_false(self):
user_input = '{"draggables": [{"1": "t1"}, \ user_input = '{"draggables": [{"1": "t1"}, \
{"name_with_icon": "t2"}]}' {"name_with_icon": "t2"}]}'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment