diff --git a/courseware/capa/capa_problem.py b/courseware/capa/capa_problem.py index 2cf1716537..9a1b9afa54 100644 --- a/courseware/capa/capa_problem.py +++ b/courseware/capa/capa_problem.py @@ -10,11 +10,11 @@ from content_parser import xpath_remove from util import contextualize_text from inputtypes import textline, schematic -from responsetypes import numericalresponse, formularesponse +from responsetypes import numericalresponse, formularesponse, customresponse response_types = {'numericalresponse':numericalresponse, 'formularesponse':formularesponse, - 'customresponse':None} + 'customresponse':customresponse} entry_types = ['textline', 'schematic'] response_properties = ["responseparam", "answer"] # How to convert from original XML to HTML @@ -124,6 +124,12 @@ class LoncapaProblem(object): responder = response_types[response.tag](response, self.context) results = responder.get_answers() answer_map.update(results) + + for entry in problems_simple.xpath("//"+"|//".join(response_properties+entry_types)): + answer = entry.get('correct_answer') + if answer != None: + answer_map[entry.get('id')] = contextualize_text(answer, self.context()) + return answer_map # ======= Private ======== diff --git a/courseware/capa/responsetypes.py b/courseware/capa/responsetypes.py index 9c1108d6e7..efc1ba3ced 100644 --- a/courseware/capa/responsetypes.py +++ b/courseware/capa/responsetypes.py @@ -1,6 +1,14 @@ +import random, numpy, math, scipy from util import contextualize_text from calc import evaluator import random, math +from django.conf import settings + +# TODO: Should be the same object as in capa_problem +global_context={'random':random, + 'numpy':numpy, + 'math':math, + 'scipy':scipy} class numericalresponse(object): def __init__(self, xml, context): @@ -30,15 +38,31 @@ class numericalresponse(object): class customresponse(object): def __init__(self, xml, context): self.xml = xml - self.answer_id = xml.xpath('//*[@id=$id]//textline/@id', - id=xml.get('id'))[0] - return {self.answer_id:'correct'} + ## CRITICAL TODO: Should cover all entrytypes + self.answer_ids = xml.xpath('//*[@id=$id]//textline/@id', + id=xml.get('id')) + self.context = context + answer = xml.xpath('//*[@id=$id]//answer', + id=xml.get('id'))[0] + answer_src = answer.get('src') + if answer_src != None: + self.code = open(settings.DATA_DIR+'src/'+answer_src).read() + else: + self.code = answer.text def grade(self, student_answers): - return {self.answer_id:'correct'} + print "YY", self.answer_ids + print "XX", student_answers + submission = [student_answers[k] for k in sorted(self.answer_ids)] + self.context.update({'submission':submission}) + print self.code + exec self.code in global_context, self.context + return zip(sorted(self.answer_ids), self.context['correct']) def get_answers(self): - return {self.answer_id:'correct'} + # Since this is explicitly specified in the problem, this will + # be handled by capa_problem + return {} class formularesponse(object): diff --git a/courseware/capa_module.py b/courseware/capa_module.py index 85ec7f0060..47740567d6 100644 --- a/courseware/capa_module.py +++ b/courseware/capa_module.py @@ -237,14 +237,15 @@ class LoncapaModule(XModule): for key in get: answers['_'.join(key.split('_')[1:])]=get[key] - try: + #try: + if True: old_state = self.lcp.get_state() lcp_id = self.lcp.problem_id filename = self.lcp.filename correct_map = self.lcp.grade_answers(answers) - except: - self.lcp = LoncapaProblem(filename, id=lcp_id, state=old_state) - return json.dumps({'success':'syntax'}) + #except: + # self.lcp = LoncapaProblem(filename, id=lcp_id, state=old_state) + # return json.dumps({'success':'syntax'}) self.attempts = self.attempts + 1 self.lcp.done=True