From dfe438dc732a472a08490c4b7ca5c0d9fce115e8 Mon Sep 17 00:00:00 2001 From: Diana Huang Date: Tue, 18 Dec 2012 13:02:17 -0500 Subject: [PATCH] Initial work on the rubric input type including the most basic parsing without error handling --- common/lib/capa/capa/inputtypes.py | 99 +++++++++++++++++++ .../lib/capa/capa/templates/rubricinput.html | 5 + 2 files changed, 104 insertions(+) create mode 100644 common/lib/capa/capa/templates/rubricinput.html diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index 73056bc09e..f19f38ce96 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -783,3 +783,102 @@ class OpenEndedInput(InputTypeBase): registry.register(OpenEndedInput) #----------------------------------------------------------------------------- + +class RubricInput(InputTypeBase): + """ + This is the logic for parsing and displaying a rubric of type + """ + + template = "rubricinput.html" + tags = ['rubric'] + + # pulled out for testing + submitted_msg = ("Feedback not yet available. Reload to check again. " + "Once the problem is graded, this message will be " + "replaced with the grader's feedback.") + + @classmethod + def get_attributes(cls): + """ + Convert options to a convenient format. + """ + return [ + ] + + def _extra_context(self): + """ + Add in the various bits and pieces of the + """ + return {} + + def setup(self): + + extract_categories(self.xml) + + @staticmethod + def extract_categories(element): + ''' + Contstruct a list of categories such that the structure looks like: + [ { category: "Category 1 Name", + options: [{text: "Option 1 Name", points: 0}, {text:"Option 2 Name", points: 5}] + }, + { category: "Category 2 Name", + options: [{text: "Option 1 Name", points: 0}, + {text: "Option 2 Name", points: 1}, + {text: "Option 3 Name", points: 2]}] + + ''' + categories = [] + for category in element: + if category.tag != 'category': + raise Exception("[capa.inputtypes.extract_categories] Expected a tag: got {0} instead".format(category.tag)) + else: + categories.append(extract_category(category)) + self.categories = categories + + + @staticmethod + def extract_category(category): + ''' + construct an individual category + {category: "Category 1 Name", + options: [{text: "Option 1 text", points: 1}, + {text: "Option 2 text", points: 2}]} + + all sorting and auto-point generation occurs in this function + ''' + descriptionxml = category[0] + optionsxml = category[1:] + + # parse description + if descriptionxml.tag != 'description': + raise Exception("[extract_category: expected description tag, got {0} instead".format(descriptionxml.tag)) + + description = descriptionxml.text + + cur_points = 0 + options = [] + # parse options + for option in optionsxml: + if option.tag != 'option': + raise Exception("[extract_category: expected option tag, got {0} instead".format(option.tag)) + else: + pointstr = option.get("points") + if(pointstr): + # try to parse this into an int + try: + points = int(pointstr) + except ValueError: + raise Exception("[extract_category: expected int to have points, got {0} instead".format(pointstr)) + else: + # use the generated one + points = cur_points + cur_points = cur_points + 1 + optiontext = option.text + options.append({'text': option.text, 'points': points}) + + return {'description': description, 'options': options} + +registry.register(RubricInput) + +#----------------------------------------------------------------------------- diff --git a/common/lib/capa/capa/templates/rubricinput.html b/common/lib/capa/capa/templates/rubricinput.html new file mode 100644 index 0000000000..e78ced62db --- /dev/null +++ b/common/lib/capa/capa/templates/rubricinput.html @@ -0,0 +1,5 @@ +
+ + +
+