Commit e31356b1 by Piotr Mitros

Better/more AJAXy problem rendering

parent 0fec3df8
...@@ -30,7 +30,19 @@ class LoncapaModule(XModule): ...@@ -30,7 +30,19 @@ class LoncapaModule(XModule):
def max_score(self): def max_score(self):
return len(self.lcp.questions) return len(self.lcp.questions)
def get_html(self, encapsulate=True): def get_html(self):
return render_to_string('problem_ajax.html',
{'id':self.filename,
'ajax_url':self.ajax_url,
})
def get_init_js(self):
return render_to_string('problem.js',
{'id':self.filename,
'ajax_url':self.ajax_url,
})
def get_problem_html(self, encapsulate=True):
html = self.lcp.get_html() html = self.lcp.get_html()
content={'name':self.name, content={'name':self.name,
'html':html} 'html':html}
...@@ -47,11 +59,9 @@ class LoncapaModule(XModule): ...@@ -47,11 +59,9 @@ class LoncapaModule(XModule):
}) })
if encapsulate: if encapsulate:
html = '<div id="main_{id}">'.format(id=self.item_id)+html+"</div>" html = '<div id="main_{id}">'.format(id=self.item_id)+html+"</div>"
return html return html
def get_init_js(self):
return ""
def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None): def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None):
XModule.__init__(self, xml, item_id, ajax_url, track_url, state) XModule.__init__(self, xml, item_id, ajax_url, track_url, state)
dom=parseString(xml) dom=parseString(xml)
...@@ -63,17 +73,25 @@ class LoncapaModule(XModule): ...@@ -63,17 +73,25 @@ class LoncapaModule(XModule):
def handle_ajax(self, dispatch, get): def handle_ajax(self, dispatch, get):
if dispatch=='problem_check': if dispatch=='problem_check':
html = self.check_problem(get) response = self.check_problem(get)
elif dispatch=='problem_reset': elif dispatch=='problem_reset':
html = self.reset_problem(get) response = self.reset_problem(get)
elif dispatch=='problem_get':
response = self.get_problem(get)
else: else:
return "Error" return "Error"
return html return response
# Temporary -- move to capa_problem # Figure out if we should move these to capa_problem?
def get_problem(self, get):
''' Same as get_problem_html -- if we want to reconfirm we have the right
thing e.g. after several AJAX calls. '''
return self.get_problem_html(encapsulate=False)
def check_problem(self, get): def check_problem(self, get):
''' Checks whether answers to a problem are correct, and returns
a map of correct/incorrect answers '''
self.lcp.done=True self.lcp.done=True
answer=dict() answer=dict()
# input_resistor_1 ==> resistor_1 # input_resistor_1 ==> resistor_1
...@@ -85,6 +103,8 @@ class LoncapaModule(XModule): ...@@ -85,6 +103,8 @@ class LoncapaModule(XModule):
return js return js
def reset_problem(self, get): def reset_problem(self, get):
''' Changes problem state to unfinished -- removes student answers,
and causes problem to rerender itself. '''
self.lcp.done=False self.lcp.done=False
self.lcp.answers=dict() self.lcp.answers=dict()
self.lcp.context=dict() self.lcp.context=dict()
...@@ -92,9 +112,6 @@ class LoncapaModule(XModule): ...@@ -92,9 +112,6 @@ class LoncapaModule(XModule):
self.lcp.answers=dict() # Student answers self.lcp.answers=dict() # Student answers
self.lcp.correct_map=dict() self.lcp.correct_map=dict()
self.lcp.seed=None self.lcp.seed=None
# Minor cleanup would be nice
# We recreate the capa_problem on a reset
filename=settings.DATA_DIR+self.filename+".xml" filename=settings.DATA_DIR+self.filename+".xml"
self.lcp=LoncapaProblem(filename, self.item_id, self.lcp.get_state()) self.lcp=LoncapaProblem(filename, self.item_id, self.lcp.get_state())
# self.lcp.__init__(filename, self.item_id, self.lcp.get_state()) return json.dumps(self.get_problem_html(encapsulate=False))
return json.dumps(self.get_html(encapsulate=False))
...@@ -188,7 +188,7 @@ def render_x_module(request, xml_module): ...@@ -188,7 +188,7 @@ def render_x_module(request, xml_module):
return content return content
def modx_dispatch(request, module=None, dispatch=None, id=None): def modx_dispatch(request, module=None, dispatch=None, id=None):
''' Generic module for extensions. This handles AJAX. ''' ''' Generic module for extensions. '''
s = StudentModule.objects.filter(module_type=module, student=request.user, module_id=id) s = StudentModule.objects.filter(module_type=module, student=request.user, module_id=id)
if len(s) == 0: if len(s) == 0:
raise Http404 raise Http404
...@@ -229,7 +229,8 @@ def index(request, course="6.002 Spring 2012", chapter="Using the System", secti ...@@ -229,7 +229,8 @@ def index(request, course="6.002 Spring 2012", chapter="Using the System", secti
if not request.user.is_authenticated(): if not request.user.is_authenticated():
return redirect('/') return redirect('/')
# Fix URLs # Fixes URLs -- we don't get funny encoding characters from spaces
# so they remain readable
course=course.replace("_"," ") course=course.replace("_"," ")
chapter=chapter.replace("_"," ") chapter=chapter.replace("_"," ")
section=section.replace("_"," ") section=section.replace("_"," ")
......
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