Commit 4b58cb95 by Victor Shnayder

Fix attempt tracking, fix test

- increments attempts on any transition to DONE state
parent 0a1b731d
...@@ -160,6 +160,19 @@ class SelfAssessmentModule(XModule): ...@@ -160,6 +160,19 @@ class SelfAssessmentModule(XModule):
self.history[-1]['hint'] = hint self.history[-1]['hint'] = hint
def change_state(self, new_state):
"""
A centralized place for state changes--allows for hooks. If the
current state matches the old state, don't run any hooks.
"""
if self.state == new_state:
return
self.state = new_state
if self.state == self.DONE:
self.attempts += 1
@staticmethod @staticmethod
def convert_state_to_current_format(old_state): def convert_state_to_current_format(old_state):
""" """
...@@ -376,7 +389,7 @@ class SelfAssessmentModule(XModule): ...@@ -376,7 +389,7 @@ class SelfAssessmentModule(XModule):
# add new history element with answer and empty score and hint. # add new history element with answer and empty score and hint.
self.new_history_entry(get['student_answer']) self.new_history_entry(get['student_answer'])
self.state = self.ASSESSING self.change_state(self.ASSESSING)
return { return {
'success': True, 'success': True,
...@@ -411,11 +424,11 @@ class SelfAssessmentModule(XModule): ...@@ -411,11 +424,11 @@ class SelfAssessmentModule(XModule):
d = {'success': True,} d = {'success': True,}
if score == self.max_score(): if score == self.max_score():
self.state = self.DONE self.change_state(self.DONE)
d['message_html'] = self.get_message_html() d['message_html'] = self.get_message_html()
d['allow_reset'] = self._allow_reset() d['allow_reset'] = self._allow_reset()
else: else:
self.state = self.REQUEST_HINT self.change_state(self.REQUEST_HINT)
d['hint_html'] = self.get_hint_html() d['hint_html'] = self.get_hint_html()
d['state'] = self.state d['state'] = self.state
...@@ -438,10 +451,7 @@ class SelfAssessmentModule(XModule): ...@@ -438,10 +451,7 @@ class SelfAssessmentModule(XModule):
return self.out_of_sync_error(get) return self.out_of_sync_error(get)
self.record_latest_hint(get['hint']) self.record_latest_hint(get['hint'])
self.state = self.DONE self.change_state(self.DONE)
# increment attempts
self.attempts = self.attempts + 1
# To the tracking logs! # To the tracking logs!
event_info = { event_info = {
...@@ -473,7 +483,7 @@ class SelfAssessmentModule(XModule): ...@@ -473,7 +483,7 @@ class SelfAssessmentModule(XModule):
'success': False, 'success': False,
'error': 'Too many attempts.' 'error': 'Too many attempts.'
} }
self.state = self.INITIAL self.change_state(self.INITIAL)
return {'success': True} return {'success': True}
......
...@@ -33,7 +33,7 @@ class SelfAssessmentTest(unittest.TestCase): ...@@ -33,7 +33,7 @@ class SelfAssessmentTest(unittest.TestCase):
self.definition, self.descriptor, self.definition, self.descriptor,
state, {}, metadata=self.metadata) state, {}, metadata=self.metadata)
self.assertEqual(module.get_score(), 0) self.assertEqual(module.get_score()['score'], 0)
self.assertTrue('answer 3' in module.get_html()) self.assertTrue('answer 3' in module.get_html())
self.assertFalse('answer 2' in module.get_html()) self.assertFalse('answer 2' in module.get_html())
......
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