Commit 314c5728 by ichuang

fix capa_problem to call each responder's get_answers() just once

parent 7a5d645b
...@@ -147,7 +147,7 @@ class LoncapaProblem(object): ...@@ -147,7 +147,7 @@ class LoncapaProblem(object):
used to give complex problems (eg programming questions) multiple points. used to give complex problems (eg programming questions) multiple points.
''' '''
maxscore = 0 maxscore = 0
for responder in self.responders.values(): for response, responder in self.responders.iteritems():
if hasattr(responder,'get_max_score'): if hasattr(responder,'get_max_score'):
try: try:
maxscore += responder.get_max_score() maxscore += responder.get_max_score()
...@@ -155,11 +155,7 @@ class LoncapaProblem(object): ...@@ -155,11 +155,7 @@ class LoncapaProblem(object):
log.debug('responder %s failed to properly return from get_max_score()' % responder) # FIXME log.debug('responder %s failed to properly return from get_max_score()' % responder) # FIXME
raise raise
else: else:
try: maxscore += len(self.responder_answers[response])
maxscore += len(responder.get_answers())
except:
log.debug('responder %s failed to properly return get_answers()' % responder) # FIXME
raise
return maxscore return maxscore
def get_score(self): def get_score(self):
...@@ -211,8 +207,8 @@ class LoncapaProblem(object): ...@@ -211,8 +207,8 @@ class LoncapaProblem(object):
(see capa_module) (see capa_module)
""" """
answer_map = dict() answer_map = dict()
for responder in self.responders.values(): for response in self.responders.keys():
results = responder.get_answers() results = self.responder_answers[response]
answer_map.update(results) # dict of (id,correct_answer) answer_map.update(results) # dict of (id,correct_answer)
# include solutions from <solution>...</solution> stanzas # include solutions from <solution>...</solution> stanzas
...@@ -228,8 +224,9 @@ class LoncapaProblem(object): ...@@ -228,8 +224,9 @@ class LoncapaProblem(object):
the dicts returned by grade_answers and get_question_answers. (Though the dicts returned by grade_answers and get_question_answers. (Though
get_question_answers may only return a subset of these.""" get_question_answers may only return a subset of these."""
answer_ids = [] answer_ids = []
for responder in self.responders.values(): for response in self.responders.keys():
answer_ids.append(responder.get_answers().keys()) results = self.responder_answers[response]
answer_ids.append(results.keys())
return answer_ids return answer_ids
def get_html(self): def get_html(self):
...@@ -382,6 +379,8 @@ class LoncapaProblem(object): ...@@ -382,6 +379,8 @@ class LoncapaProblem(object):
In-place transformation In-place transformation
Also create capa Response instances for each responsetype and save as self.responders Also create capa Response instances for each responsetype and save as self.responders
Obtain all responder answers and save as self.responder_answers dict (key = response)
''' '''
response_id = 1 response_id = 1
self.responders = {} self.responders = {}
...@@ -402,6 +401,15 @@ class LoncapaProblem(object): ...@@ -402,6 +401,15 @@ class LoncapaProblem(object):
responder = response_tag_dict[response.tag](response, inputfields, self.context, self.system) # instantiate capa Response responder = response_tag_dict[response.tag](response, inputfields, self.context, self.system) # instantiate capa Response
self.responders[response] = responder # save in list in self self.responders[response] = responder # save in list in self
# get responder answers (do this only once, since there may be a performance cost, eg with externalresponse)
self.responder_answers = {}
for response in self.responders.keys():
try:
self.responder_answers[response] = responder.get_answers()
except:
log.debug('responder %s failed to properly return get_answers()' % self.responders[response]) # FIXME
raise
# <solution>...</solution> may not be associated with any specific response; give IDs for those separately # <solution>...</solution> may not be associated with any specific response; give IDs for those separately
# TODO: We should make the namespaces consistent and unique (e.g. %s_problem_%i). # TODO: We should make the namespaces consistent and unique (e.g. %s_problem_%i).
solution_id = 1 solution_id = 1
......
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