Commit 9cf9f05a by Waheed Ahmed

Merge pull request #6983 from edx/waheed/tnl1419-fix-optioninput-if-options-created-with-variables

Fixed optioninput problem, option was not remain selected after submitting.
parents dca2b95e 0086893d
...@@ -710,12 +710,14 @@ class LoncapaProblem(object): ...@@ -710,12 +710,14 @@ class LoncapaProblem(object):
hint = '' hint = ''
hintmode = None hintmode = None
input_id = problemtree.get('id') input_id = problemtree.get('id')
answervariable = None
if problemid in self.correct_map: if problemid in self.correct_map:
pid = input_id pid = input_id
status = self.correct_map.get_correctness(pid) status = self.correct_map.get_correctness(pid)
msg = self.correct_map.get_msg(pid) msg = self.correct_map.get_msg(pid)
hint = self.correct_map.get_hint(pid) hint = self.correct_map.get_hint(pid)
hintmode = self.correct_map.get_hintmode(pid) hintmode = self.correct_map.get_hintmode(pid)
answervariable = self.correct_map.get_property(pid, 'answervariable')
value = "" value = ""
if self.student_answers and problemid in self.student_answers: if self.student_answers and problemid in self.student_answers:
...@@ -730,6 +732,7 @@ class LoncapaProblem(object): ...@@ -730,6 +732,7 @@ class LoncapaProblem(object):
'status': status, 'status': status,
'id': input_id, 'id': input_id,
'input_state': self.input_state[input_id], 'input_state': self.input_state[input_id],
'answervariable': answervariable,
'feedback': { 'feedback': {
'message': msg, 'message': msg,
'hint': hint, 'hint': hint,
......
...@@ -46,6 +46,7 @@ class CorrectMap(object): ...@@ -46,6 +46,7 @@ class CorrectMap(object):
hint='', hint='',
hintmode=None, hintmode=None,
queuestate=None, queuestate=None,
answervariable=None, # pylint: disable=C0330
**kwargs **kwargs
): ):
...@@ -57,6 +58,7 @@ class CorrectMap(object): ...@@ -57,6 +58,7 @@ class CorrectMap(object):
'hint': hint, 'hint': hint,
'hintmode': hintmode, 'hintmode': hintmode,
'queuestate': queuestate, 'queuestate': queuestate,
'answervariable': answervariable,
} }
def __repr__(self): def __repr__(self):
......
...@@ -213,6 +213,7 @@ class InputTypeBase(object): ...@@ -213,6 +213,7 @@ class InputTypeBase(object):
self.hint = feedback.get('hint', '') self.hint = feedback.get('hint', '')
self.hintmode = feedback.get('hintmode', None) self.hintmode = feedback.get('hintmode', None)
self.input_state = state.get('input_state', {}) self.input_state = state.get('input_state', {})
self.answervariable = state.get("answervariable", None)
# put hint above msg if it should be displayed # put hint above msg if it should be displayed
if self.hintmode == 'always': if self.hintmode == 'always':
...@@ -310,6 +311,8 @@ class InputTypeBase(object): ...@@ -310,6 +311,8 @@ class InputTypeBase(object):
(a, v) for (a, v) in self.loaded_attributes.iteritems() if a in self.to_render (a, v) for (a, v) in self.loaded_attributes.iteritems() if a in self.to_render
) )
context.update(self._extra_context()) context.update(self._extra_context())
if self.answervariable:
context.update({'answervariable': self.answervariable})
return context return context
def _extra_context(self): def _extra_context(self):
......
...@@ -1097,6 +1097,9 @@ class OptionResponse(LoncapaResponse): ...@@ -1097,6 +1097,9 @@ class OptionResponse(LoncapaResponse):
cmap.set(aid, 'correct') cmap.set(aid, 'correct')
else: else:
cmap.set(aid, 'incorrect') cmap.set(aid, 'incorrect')
answer_variable = self.get_student_answer_variable_name(student_answers, aid)
if answer_variable:
cmap.set_property(aid, 'answervariable', answer_variable)
return cmap return cmap
def get_answers(self): def get_answers(self):
...@@ -1105,6 +1108,18 @@ class OptionResponse(LoncapaResponse): ...@@ -1105,6 +1108,18 @@ class OptionResponse(LoncapaResponse):
# log.debug('%s: expected answers=%s' % (unicode(self),amap)) # log.debug('%s: expected answers=%s' % (unicode(self),amap))
return amap return amap
def get_student_answer_variable_name(self, student_answers, aid):
"""
Return student answers variable name if exist in context else None.
"""
if aid in student_answers:
for key, val in self.context.iteritems():
# convert val into unicode because student answer always be a unicode string
# even it is a list, dict etc.
if unicode(val) == student_answers[aid]:
return '$' + key
return None
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<option value="option_${id}_dummy_default"> </option> <option value="option_${id}_dummy_default"> </option>
% for option_id, option_description in options: % for option_id, option_description in options:
<option value="${option_id}" <option value="${option_id}"
% if (option_id==value): % if (option_id==value or option_id==answervariable):
selected="true" selected="true"
% endif % endif
> ${option_description}</option> > ${option_description}</option>
......
...@@ -347,6 +347,26 @@ class OptionResponseTest(ResponseTest): ...@@ -347,6 +347,26 @@ class OptionResponseTest(ResponseTest):
self.assert_grade(problem, "hasn\'t", "correct") self.assert_grade(problem, "hasn\'t", "correct")
self.assert_grade(problem, "has'nt", "incorrect") self.assert_grade(problem, "has'nt", "incorrect")
def test_variable_options(self):
"""
Test that if variable are given in option response then correct map must contain answervariable value.
"""
script = textwrap.dedent("""\
a = 1000
b = a*2
c = a*3
""")
problem = self.build_problem(
options=['$a', '$b', '$c'],
correct_option='$a',
script=script
)
input_dict = {'1_2_1': '1000'}
correct_map = problem.grade_answers(input_dict)
self.assertEqual(correct_map.get_correctness('1_2_1'), 'correct')
self.assertEqual(correct_map.get_property('1_2_1', 'answervariable'), '$a')
class FormulaResponseTest(ResponseTest): class FormulaResponseTest(ResponseTest):
""" """
......
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