diff --git a/common/lib/xmodule/xmodule/capa_module.py b/common/lib/xmodule/xmodule/capa_module.py index 8bf1a56404..a891474581 100644 --- a/common/lib/xmodule/xmodule/capa_module.py +++ b/common/lib/xmodule/xmodule/capa_module.py @@ -1,3 +1,4 @@ +import cgi import datetime import dateutil import dateutil.parser @@ -131,17 +132,17 @@ class CapaModule(XModule): self.weight = None if self.rerandomize == 'never': - seed = 1 + self.seed = 1 elif self.rerandomize == "per_student" and hasattr(self.system, 'id'): - seed = system.id + self.seed = system.id else: - seed = None + self.seed = None try: # TODO (vshnayder): move as much as possible of this work and error # checking to descriptor load time self.lcp = LoncapaProblem(self.definition['data'], self.location.html_id(), - instance_state, seed=seed, system=self.system) + instance_state, seed=self.seed, system=self.system) except Exception as err: msg = 'cannot create LoncapaProblem {loc}: {err}'.format( loc=self.location.url(), err=err) @@ -160,7 +161,7 @@ class CapaModule(XModule): (self.location.url(), msg)) self.lcp = LoncapaProblem( problem_text, self.location.html_id(), - instance_state, seed=seed, system=self.system) + instance_state, seed=self.seed, system=self.system) else: # add extra info and raise raise Exception(msg), None, sys.exc_info()[2] @@ -220,9 +221,10 @@ class CapaModule(XModule): try: html = self.lcp.get_html() except Exception, err: + log.exception(err) + # TODO (vshnayder): another switch on DEBUG. if self.system.DEBUG: - log.exception(err) msg = ( '[courseware.capa.capa_module] ' 'Failed to generate HTML for problem %s' % @@ -231,7 +233,47 @@ class CapaModule(XModule): msg += '

%s

' % traceback.format_exc().replace('<', '<') html = msg else: - raise + # We're in non-debug mode, and possibly even in production. We want + # to avoid bricking of problem as much as possible + + # Presumably, student submission has corrupted LoncapaProblem HTML. + # First, pull down all student answers + student_answers = self.lcp.student_answers + answer_ids = student_answers.keys() + + # Some inputtypes, such as dynamath, have additional "hidden" state that + # is not exposed to the student. Keep those hidden + # TODO: Use regex, e.g. 'dynamath' is suffix at end of answer_id + hidden_state_keywords = ['dynamath'] + for answer_id in answer_ids: + for hidden_state_keyword in hidden_state_keywords: + if answer_id.find(hidden_state_keyword) >= 0: + student_answers.pop(answer_id) + + # Next, generate a fresh LoncapaProblem + self.lcp = LoncapaProblem(self.definition['data'], self.location.html_id(), + state=None, # Tabula rasa + seed=self.seed, system=self.system) + + # Prepend a scary warning to the student + warning = '
'\ + '

Warning: The problem has been reset to its initial state!

'\ + 'The problem\'s state was corrupted by an invalid submission. ' \ + 'The submission consisted of:'\ + ''\ + 'If this error persists, please contact the course staff.'\ + '
' + + html = warning + try: + html += self.lcp.get_html() + except Exception, err: # Couldn't do it. Give up + log.exception(err) + raise content = {'name': self.display_name, 'html': html, diff --git a/common/lib/xmodule/xmodule/css/capa/display.scss b/common/lib/xmodule/xmodule/css/capa/display.scss index 0591a01843..f349de31b5 100644 --- a/common/lib/xmodule/xmodule/css/capa/display.scss +++ b/common/lib/xmodule/xmodule/css/capa/display.scss @@ -465,6 +465,22 @@ section.problem { margin-top: 10px; } + div.capa_reset { + padding: 25px; + border: 1px solid $error-red; + background-color: lighten($error-red, 25%); + border-radius: 3px; + font-size: 1em; + margin-top: 10px; + margin-bottom: 10px; + } + .capa_reset>h2 { + color: #AA0000; + } + .capa_reset li { + font-size: 0.9em; + } + .hints { border: 1px solid #ccc;