diff --git a/courseware/capa/capa_problem.py b/courseware/capa/capa_problem.py index bedd58a33b..75a14ec82c 100644 --- a/courseware/capa/capa_problem.py +++ b/courseware/capa/capa_problem.py @@ -11,11 +11,12 @@ import calc, eia from util import contextualize_text from inputtypes import textline, schematic -from responsetypes import numericalresponse, formularesponse, customresponse +from responsetypes import numericalresponse, formularesponse, customresponse, schematicresponse response_types = {'numericalresponse':numericalresponse, 'formularesponse':formularesponse, - 'customresponse':customresponse} + 'customresponse':customresponse, + 'schematicresponse':schematicresponse} entry_types = ['textline', 'schematic'] response_properties = ["responseparam", "answer"] # How to convert from original XML to HTML @@ -23,6 +24,7 @@ response_properties = ["responseparam", "answer"] html_transforms = {'problem': {'tag':'div'}, "numericalresponse": {'tag':'span'}, "customresponse": {'tag':'span'}, + "schematicresponse": {'tag':'span'}, "formularesponse": {'tag':'span'}, "text": {'tag':'span'}} @@ -36,7 +38,7 @@ global_context={'random':random, # These should be removed from HTML output, including all subelements html_problem_semantics = ["responseparam", "answer", "script"] # These should be removed from HTML output, but keeping subelements -html_skip = ["numericalresponse", "customresponse", "formularesponse", "text"] +html_skip = ["numericalresponse", "customresponse", "schematicresponse", "formularesponse", "text"] # These should be transformed html_special_response = {"textline":textline.render, "schematic":schematic.render} diff --git a/courseware/capa/inputtypes.py b/courseware/capa/inputtypes.py index be0bdbcfbd..db98363872 100644 --- a/courseware/capa/inputtypes.py +++ b/courseware/capa/inputtypes.py @@ -17,7 +17,19 @@ class schematic(object): eid = element.get('id') height = element.get('height') width = element.get('width') - context = {'id':eid, 'value':value, 'state':state, 'width':width, 'height':height} + parts = element.get('parts') + analyses = element.get('analyses') + initial_value = element.get('initial_value') + context = { + 'id':eid, + 'value':value, + 'initial_value':initial_value, + 'state':state, + 'width':width, + 'height':height, + 'parts':parts, + 'analyses':analyses, + } html=render_to_string("schematicinput.html", context) return etree.XML(html) diff --git a/courseware/capa/responsetypes.py b/courseware/capa/responsetypes.py index 61ed2b30cb..3e3cefd8ab 100644 --- a/courseware/capa/responsetypes.py +++ b/courseware/capa/responsetypes.py @@ -1,4 +1,4 @@ -import random, numpy, math, scipy +import random, numpy, math, scipy, json from util import contextualize_text from calc import evaluator import random, math @@ -63,7 +63,6 @@ class customresponse(object): # be handled by capa_problem return {} - class formularesponse(object): def __init__(self, xml, context): self.xml = xml @@ -114,3 +113,28 @@ class formularesponse(object): def get_answers(self): return {self.answer_id:self.correct_answer} + +class schematicresponse(object): + def __init__(self, xml, context): + self.xml = xml + self.answer_ids = xml.xpath('//*[@id=$id]//schematic/@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): + submission = [json.loads(student_answers[k]) for k in sorted(self.answer_ids)] + self.context.update({'submission':submission}) + exec self.code in global_context, self.context + return zip(sorted(self.answer_ids), self.context['correct']) + + def get_answers(self): + # Since this is explicitly specified in the problem, this will + # be handled by capa_problem + return {}