Commit 9037e116 by Piotr Mitros

X Module fully refactored

parent a077249a
...@@ -28,7 +28,7 @@ class LoncapaModule(XModule): ...@@ -28,7 +28,7 @@ class LoncapaModule(XModule):
return self.lcp.get_score() return self.lcp.get_score()
def max_score(self): def max_score(self):
return len(lcp.questions) return len(self.lcp.questions)
def get_html(self, encapsulate=True): def get_html(self, encapsulate=True):
html = self.lcp.get_html() html = self.lcp.get_html()
...@@ -40,6 +40,9 @@ class LoncapaModule(XModule): ...@@ -40,6 +40,9 @@ class LoncapaModule(XModule):
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_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)
......
...@@ -71,7 +71,7 @@ function updateHTML(elmId, value) { ...@@ -71,7 +71,7 @@ function updateHTML(elmId, value) {
} }
function setytplayerState(newState) { function setytplayerState(newState) {
updateHTML("playerstate", newState); // updateHTML("playerstate", newState);
} }
function onYouTubePlayerReady(playerId) { function onYouTubePlayerReady(playerId) {
...@@ -143,10 +143,10 @@ function updateytplayerInfo() { ...@@ -143,10 +143,10 @@ function updateytplayerInfo() {
update_captions(getCurrentTime()); update_captions(getCurrentTime());
} }
updateHTML("videoduration", getDuration()); // updateHTML("videoduration", getDuration());
updateHTML("videotime", getCurrentTime()); // updateHTML("videotime", getCurrentTime());
updateHTML("startbytes", getStartBytes()); // updateHTML("startbytes", getStartBytes());
updateHTML("volume", getVolume()); // updateHTML("volume", getVolume());
} }
// functions for the api calls // functions for the api calls
......
...@@ -17,6 +17,7 @@ from django.http import Http404 ...@@ -17,6 +17,7 @@ from django.http import Http404
import urllib import urllib
import capa_module import capa_module
import video_module
from models import StudentModule from models import StudentModule
...@@ -55,7 +56,7 @@ def profile(request): ...@@ -55,7 +56,7 @@ def profile(request):
correct=response.grade correct=response.grade
else: else:
correct=0 correct=0
total=capa_module.LoncapaModule(p, id=id).max_score() total=capa_module.LoncapaModule(p.toxml(), "id").max_score() # TODO: Add state. Not useful now, but maybe someday problems will have randomized max scores?
scores.append((int(correct),total)) scores.append((int(correct),total))
score={'course':course.getAttribute('name'), score={'course':course.getAttribute('name'),
'section':s.getAttribute("name"), 'section':s.getAttribute("name"),
...@@ -71,7 +72,8 @@ def profile(request): ...@@ -71,7 +72,8 @@ def profile(request):
'location':user_info.location, 'location':user_info.location,
'language':user_info.language, 'language':user_info.language,
'email':request.user.email, 'email':request.user.email,
'homeworks':hw 'homeworks':hw,
'csrf':csrf(request)['csrf_token']
} }
return render_to_response('profile.html', context) return render_to_response('profile.html', context)
...@@ -94,12 +96,12 @@ def render_accordion(request,course,chapter,section): ...@@ -94,12 +96,12 @@ def render_accordion(request,course,chapter,section):
return {'js':render_to_string('accordion_init.js',context), return {'js':render_to_string('accordion_init.js',context),
'content':render_to_string('accordion.html',context)} 'content':render_to_string('accordion.html',context)}
def video_module(request, module): def video_mod(request, module):
''' Shows a video, with subtitles. ''' Shows a video, with subtitles.
''' '''
id=module.getAttribute('youtube') id=module.getAttribute('youtube')
return {'js':render_to_string('video_init.js',{'id':id}), return {'js':render_to_string('video_init.js',{'id':id}),
'content':render_to_string('video.html',{})} 'content':render_to_string('video.html',{'id':id})}
def html_module(request, module): def html_module(request, module):
''' Show basic text ''' Show basic text
...@@ -150,33 +152,36 @@ def seq_module(request, module): ...@@ -150,33 +152,36 @@ def seq_module(request, module):
'content':render_to_string('seq_module.html',{'items':contents})} 'content':render_to_string('seq_module.html',{'items':contents})}
modx_modules={'problem':capa_module.LoncapaModule} modx_modules={'problem':capa_module.LoncapaModule}#, 'video1':video_module.VideoModule}
def render_x_module(request, xml_module): def render_x_module(request, xml_module):
''' Generic module for extensions. This renders to HTML. ''' ''' Generic module for extensions. This renders to HTML. '''
# Check if problem has an instance in DB # Check if problem has an instance in DB
module_id=xml_module.getAttribute(capa_module.LoncapaModule.id_attribute) module_type=xml_module.nodeName
module_class=modx_modules[module_type]
module_id=xml_module.getAttribute(module_class.id_attribute)
s = StudentModule.objects.filter(student=request.user, module_id=module_id) s = StudentModule.objects.filter(student=request.user, module_id=module_id)
if len(s) == 0: if len(s) == 0:
# If not, create one, and save it # If not, create one, and save it
problem=capa_module.LoncapaModule(xml_module.toxml(), module_id) instance=module_class(xml_module.toxml(), module_id)
smod=StudentModule(student=request.user, smod=StudentModule(student=request.user,
module_id=module_id, module_id=module_id,
state=problem.get_state(), state=instance.get_state(),
xml=problem.xml) xml=instance.xml)
smod.save() smod.save()
elif len(s) == 1: elif len(s) == 1:
# If so, render it # If so, render it
s=s[0] s=s[0]
problem=capa_module.LoncapaModule(xml_module.toxml(), instance=module_class(xml_module.toxml(),
module_id, module_id,
state=s.state) state=s.state)
s.state=problem.get_state() s.state=instance.get_state()
s.save() s.save()
else: else:
raise Exception("Database is inconsistent (1).") raise Exception("Database is inconsistent (1).")
return {'content':problem.get_html()} return {'content':instance.get_html(),
'js':instance.get_js()}
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. This handles AJAX. '''
...@@ -187,14 +192,14 @@ def modx_dispatch(request, module=None, dispatch=None, id=None): ...@@ -187,14 +192,14 @@ def modx_dispatch(request, module=None, dispatch=None, id=None):
s=s[0] s=s[0]
dispatch=dispatch.split('?')[0] dispatch=dispatch.split('?')[0]
problem=modx_modules[module](s.xml, s.module_id, state=s.state) instance=modx_modules[module](s.xml, s.module_id, state=s.state)
html=problem.handle_ajax(dispatch, request.GET) html=instance.handle_ajax(dispatch, request.GET)
s.state=problem.get_state() s.state=instance.get_state()
s.grade=problem.get_score()['score'] s.grade=instance.get_score()['score']
s.save() s.save()
return HttpResponse(html) return HttpResponse(html)
module_types={'video':video_module, module_types={'video':video_mod,
'html':html_module, 'html':html_module,
'tab':tab_module, 'tab':tab_module,
'vertical':vertical_module, 'vertical':vertical_module,
......
...@@ -23,6 +23,13 @@ class XModule: ...@@ -23,6 +23,13 @@ class XModule:
def get_html(self): def get_html(self):
return "Unimplemented" return "Unimplemented"
def get_js(self):
''' JavaScript code to be run when problem is shown. Be aware
that this may happen several times on the same page
(e.g. student switching tabs). Common functions should be put
in the main course .js files for now. '''
return ""
def handle_ajax(self, dispatch, get): def handle_ajax(self, dispatch, get):
''' dispatch is last part of the URL. ''' dispatch is last part of the URL.
get is a dictionary-like object ''' get is a dictionary-like object '''
......
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