Commit cd67ec8f by Piotr Mitros

Modular refactor: Input types look more like x_module

parent b8b9928a
...@@ -25,6 +25,7 @@ from mako.template import Template ...@@ -25,6 +25,7 @@ from mako.template import Template
from util import contextualize_text from util import contextualize_text
import inputtypes import inputtypes
from responsetypes import NumericalResponse, FormulaResponse, CustomResponse, SchematicResponse, MultipleChoiceResponse, StudentInputError, TrueFalseResponse, ExternalResponse,ImageResponse,OptionResponse from responsetypes import NumericalResponse, FormulaResponse, CustomResponse, SchematicResponse, MultipleChoiceResponse, StudentInputError, TrueFalseResponse, ExternalResponse,ImageResponse,OptionResponse
import calc import calc
...@@ -239,7 +240,7 @@ class LoncapaProblem(object): ...@@ -239,7 +240,7 @@ class LoncapaProblem(object):
# used to be # used to be
# if problemtree.tag in html_special_response: # if problemtree.tag in html_special_response:
if hasattr(inputtypes, problemtree.tag): if problemtree.tag in inputtypes.SimpleInput.get_xml_tags():
# status is currently the answer for the problem ID for the input element, # status is currently the answer for the problem ID for the input element,
# but it will turn into a dict containing both the answer and any associated message # but it will turn into a dict containing both the answer and any associated message
# for the problem ID for the input element. # for the problem ID for the input element.
...@@ -266,9 +267,15 @@ class LoncapaProblem(object): ...@@ -266,9 +267,15 @@ class LoncapaProblem(object):
# print "[courseware.capa.capa_problem.extract_html] msg = ",msg # print "[courseware.capa.capa_problem.extract_html] msg = ",msg
# do the rendering # do the rendering
#render_function = html_special_response[problemtree.tag] render_object = inputtypes.SimpleInput(system = self.system,
render_function = getattr(inputtypes, problemtree.tag) xml = problemtree,
return render_function(problemtree, value, status, msg) # render the special response (textline, schematic,...) state = {'value':value,
'status': status,
'id':problemtree.get('id'),
'feedback':{'message':msg}
},
use = 'capa_input')
return render_object.get_html() #function(problemtree, value, status, msg) # render the special response (textline, schematic,...)
tree=Element(problemtree.tag) tree=Element(problemtree.tag)
for item in problemtree: for item in problemtree:
......
...@@ -32,8 +32,15 @@ from lxml import etree ...@@ -32,8 +32,15 @@ from lxml import etree
from mitxmako.shortcuts import render_to_string from mitxmako.shortcuts import render_to_string
def simpleinput(fn):
def wrapped():
print "XXXXXXXXXXXXX", fn
return fn
return wrapped
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
@simpleinput
def optioninput(element, value, status, msg=''): def optioninput(element, value, status, msg=''):
''' '''
Select option input type. Select option input type.
...@@ -254,3 +261,61 @@ def imageinput(element, value, status, msg=''): ...@@ -254,3 +261,61 @@ def imageinput(element, value, status, msg=''):
html=render_to_string("imageinput.html", context) html=render_to_string("imageinput.html", context)
return etree.XML(html) return etree.XML(html)
class SimpleInput():# XModule
''' Type for simple inputs
State is a dictionary with optional keys:
* Value
* ID
* Status (answered, unanswered, unsubmitted)
* Feedback (dictionary containing keys for hints, errors, or other
feedback from previous attempt)
'''
# We should populate this with a decorator on the specific types
simple_types = {'choicegroup':choicegroup,
'imageinput':imageinput,
'js_textline':js_textline,
'math':math,
'optioninput':optioninput,
'schematic':schematic,
'solution':solution,
'textbox':textbox,
'textline':textline}
@classmethod
def get_xml_tags(c):
return c.simple_types.keys()
@classmethod
def get_uses(c):
return ['capa_input']
def get_html(self):
return self.simple_types[self.tag](self.xml, self.value, self.status, self.msg)
def __init__(self, system, xml, item_id = None, track_url=None, state=None, use = 'capa_input'):
self.xml = xml
self.tag = xml.tag
if not state:
state = {}
if item_id:
self.id = item_id
if xml.get('id'):
self.id = xml.get('id')
if 'id' in state:
self.id = state['id']
self.system = system
self.value = ''
if 'value' in state:
self.value = state['value']
self.msg = ''
if 'feedback' in state and 'message' in state['feedback']:
self.msg = state['feedback']['message']
self.status = 'unanswered'
if 'status' in state:
self.status = state['status']
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