Commit 8b248375 by Piotr Mitros

Refactor working

parent 24bf20e1
......@@ -17,7 +17,6 @@ class LoncapaModule(XModule):
id_attribute="filename"
def get_state(self):
print "got"
return self.lcp.get_state()
def get_score(self):
......@@ -40,10 +39,19 @@ class LoncapaModule(XModule):
self.filename=node.getAttribute("filename")
filename=settings.DATA_DIR+self.filename+".xml"
self.name=node.getAttribute("name")
print state
self.lcp=LoncapaProblem(filename, item_id, state)
# Temporary
def handle_ajax(self, dispatch, get):
if dispatch=='problem_check':
html = self.check_problem(get)
elif dispatch=='problem_reset':
html = self.reset_problem(get)
else:
return "Error"
return html
# Temporary -- move to capa_problem
def check_problem(self, get):
answer=dict()
......@@ -54,3 +62,12 @@ class LoncapaModule(XModule):
js=json.dumps(self.lcp.grade_answers(answer))
return js
def reset_problem(self, get):
self.lcp.answers=dict()
self.lcp.context=dict()
self.lcp.questions=dict() # Detailed info about questions in problem instance. TODO: Should be by id and not lid.
self.lcp.answers=dict() # Student answers
self.lcp.correct_map=dict()
self.lcp.seed=None
return "{}"
......@@ -42,7 +42,7 @@ class LoncapaProblem():
state={}
self.gid=id
if 'seed' in state:
if 'seed' in state and state['seed']!=None and state['seed']!="":
self.seed=state['seed']
else:
# TODO: Check performance of urandom -- depending on
......
......@@ -119,8 +119,11 @@ def vertical_module(request, module):
return {'js':js,
'content':render_to_string('vert_module.html',{'items':contents})}
modx_modules={'problem':capa_module.LoncapaModule}
def render_x_module(request, xml_module):
# Check if problem has an instance in DB
print xml_module
module_id=xml_module.getAttribute(capa_module.LoncapaModule.id_attribute)
s = StudentModule.objects.filter(student=request.user, module_id=module_id)
if len(s) == 0:
......@@ -133,9 +136,12 @@ def render_x_module(request, xml_module):
smod.save()
elif len(s) == 1:
# If so, render it
s=s[0]
problem=capa_module.LoncapaModule(xml_module.toxml(),
module_id,
state=s[0].state)
state=s.state)
s.state=problem.get_state()
s.save()
else:
raise Exception("Database is inconsistent (1).")
......@@ -149,24 +155,12 @@ def modx_dispatch(request, module=None, dispatch=None, id=None):
s=s[0]
dispatch=dispatch.split('?')[0]
if dispatch=='problem_check':
problem=capa_module.LoncapaModule(s.xml, s.module_id, state=s.state)
html = problem.check_problem(request.GET)
s.state=problem.get_state()
s.grade=problem.get_score()['score']
s.save()
return HttpResponse(html) #check_problem(s, request.GET)
elif dispatch=='problem_reset':
return reset_problem(request,id)
else:
raise Http404
def reset_problem(request,id):
s = StudentModule.objects.filter(student=request.user, module_id=id)[0]
s.state="{}"
problem=modx_modules[module](s.xml, s.module_id, state=s.state)
html=problem.handle_ajax(dispatch, request.GET)
s.state=problem.get_state()
s.grade=problem.get_score()['score']
s.save()
return HttpResponse(json.dumps({}), mimetype="application/json")
return HttpResponse(html)
module_types={'video':video_module,
'html':html_module,
......
......@@ -23,8 +23,10 @@ class XModule:
def get_html(self):
return "Unimplemented"
def handle_ajax(self, json):
return
def handle_ajax(self, dispatch, get):
''' dispatch is last part of the URL.
get is a dictionary-like object '''
return ""
def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None):
''' In most cases, you must pass state or xml'''
......
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