Commit b1b08d29 by ichuang

made "position" a generic parameter passed to all modules, from courseware.views.index

parent 2e2bf13e
...@@ -38,6 +38,11 @@ class I4xSystem(object): ...@@ -38,6 +38,11 @@ class I4xSystem(object):
self.render_function = render_function self.render_function = render_function
self.exception404 = Http404 self.exception404 = Http404
self.DEBUG = settings.DEBUG self.DEBUG = settings.DEBUG
def get(self,attr): # uniform access to attributes (like etree)
return self.__dict__.get(attr)
def set(self,attr,val): # uniform access to attributes (like etree)
self.__dict__[attr] = val
def __repr__(self): def __repr__(self):
return repr(self.__dict__) return repr(self.__dict__)
def __str__(self): def __str__(self):
...@@ -100,7 +105,7 @@ def get_state_from_module_object_preload(user, xml_module, module_object_preload ...@@ -100,7 +105,7 @@ def get_state_from_module_object_preload(user, xml_module, module_object_preload
return smod, state return smod, state
def render_x_module(user, request, xml_module, module_object_preload): def render_x_module(user, request, xml_module, module_object_preload,position=None):
''' Generic module for extensions. This renders to HTML. ''' Generic module for extensions. This renders to HTML.
modules include sequential, vertical, problem, video, html modules include sequential, vertical, problem, video, html
...@@ -113,6 +118,11 @@ def render_x_module(user, request, xml_module, module_object_preload): ...@@ -113,6 +118,11 @@ def render_x_module(user, request, xml_module, module_object_preload):
- request : current django HTTPrequest - request : current django HTTPrequest
- xml_module : lxml etree of xml subtree for the current module - xml_module : lxml etree of xml subtree for the current module
- module_object_preload : list of StudentModule objects, one of which may match this module type and id - module_object_preload : list of StudentModule objects, one of which may match this module type and id
- position : extra information from URL for user-specified position within module
Returns:
- dict which is context for HTML rendering of the specified module
''' '''
module_type=xml_module.tag module_type=xml_module.tag
...@@ -138,6 +148,7 @@ def render_x_module(user, request, xml_module, module_object_preload): ...@@ -138,6 +148,7 @@ def render_x_module(user, request, xml_module, module_object_preload):
ajax_url = ajax_url, ajax_url = ajax_url,
filestore = OSFS(data_root), filestore = OSFS(data_root),
) )
system.set('position',position) # pass URL specified position along to module, through I4xSystem
instance=module_class(system, instance=module_class(system,
etree.tostring(xml_module), etree.tostring(xml_module),
module_id, module_id,
...@@ -176,8 +187,22 @@ def render_x_module(user, request, xml_module, module_object_preload): ...@@ -176,8 +187,22 @@ def render_x_module(user, request, xml_module, module_object_preload):
return content return content
def render_module(user, request, module, module_object_preload): def render_module(user, request, module, module_object_preload, position=None):
''' Generic dispatch for internal modules. ''' ''' Generic dispatch for internal modules.
Args:
- user : django User
- request : HTTP request
- module : ElementTree (xml) for this module
- module_object_preload : list of StudentModule objects, one of which may match this module type and id
- position : extra information from URL for user-specified position within module
Returns:
- dict which is context for HTML rendering of the specified module
'''
if module==None : if module==None :
return {"content":""} return {"content":""}
return render_x_module(user, request, module, module_object_preload) return render_x_module(user, request, module, module_object_preload, position)
...@@ -113,4 +113,8 @@ class Module(XModule): ...@@ -113,4 +113,8 @@ class Module(XModule):
state = json.loads(state) state = json.loads(state)
if 'position' in state: self.position = int(state['position']) if 'position' in state: self.position = int(state['position'])
# if position is specified in system, then use that instead
if system.get('position'):
self.position = int(system.get('position'))
self.rendered = False self.rendered = False
...@@ -68,6 +68,7 @@ class XModule(object): ...@@ -68,6 +68,7 @@ class XModule(object):
self.xml = xml self.xml = xml
self.item_id = item_id self.item_id = item_id
self.state = state self.state = state
self.DEBUG = False
if system: if system:
## These are temporary; we really should go ## These are temporary; we really should go
...@@ -76,4 +77,5 @@ class XModule(object): ...@@ -76,4 +77,5 @@ class XModule(object):
self.tracker = system.track_function self.tracker = system.track_function
self.filestore = system.filestore self.filestore = system.filestore
self.render_function = system.render_function self.render_function = system.render_function
self.DEBUG = system.DEBUG
self.system = system self.system = system
...@@ -160,7 +160,11 @@ def index(request, course=None, chapter="Using the System", section="Hints",posi ...@@ -160,7 +160,11 @@ def index(request, course=None, chapter="Using the System", section="Hints",posi
- course : coursename (str) - course : coursename (str)
- chapter : chapter name (str) - chapter : chapter name (str)
- section : section name (str) - section : section name (str)
- position : position in sequence, ie of <sequential> module (int) - position : position in module, eg of <sequential> module (str)
Returns:
- HTTPresponse
''' '''
user = request.user user = request.user
...@@ -215,21 +219,6 @@ def index(request, course=None, chapter="Using the System", section="Hints",posi ...@@ -215,21 +219,6 @@ def index(request, course=None, chapter="Using the System", section="Hints",posi
else: else:
module_object_preload = [] module_object_preload = []
if position and module and module.tag=='sequential':
smod, state = get_state_from_module_object_preload(user, module, module_object_preload)
newstate = json.dumps({ 'position':position })
if smod:
smod.state = newstate
elif user.is_authenticated():
smod=StudentModule(student=user,
module_type = module.tag,
module_id= module.get('id'),
state = newstate)
smod.save()
# now regenerate module_object_preload
module_object_preload = list(StudentModule.objects.filter(student=user,
module_id__in=module_ids))
context = { context = {
'csrf': csrf(request)['csrf_token'], 'csrf': csrf(request)['csrf_token'],
'accordion': render_accordion(request, course, chapter, section), 'accordion': render_accordion(request, course, chapter, section),
...@@ -237,7 +226,7 @@ def index(request, course=None, chapter="Using the System", section="Hints",posi ...@@ -237,7 +226,7 @@ def index(request, course=None, chapter="Using the System", section="Hints",posi
} }
try: try:
module = render_module(user, request, module, module_object_preload) # ugh - shouldn't overload module module_context = render_module(user, request, module, module_object_preload, position)
except: except:
log.exception("Unable to load module") log.exception("Unable to load module")
context.update({ context.update({
...@@ -247,8 +236,8 @@ def index(request, course=None, chapter="Using the System", section="Hints",posi ...@@ -247,8 +236,8 @@ def index(request, course=None, chapter="Using the System", section="Hints",posi
return render_to_response('courseware.html', context) return render_to_response('courseware.html', context)
context.update({ context.update({
'init': module.get('init_js', ''), 'init': module_context.get('init_js', ''),
'content': module['content'], 'content': module_context['content'],
}) })
result = render_to_response('courseware.html', context) result = render_to_response('courseware.html', context)
......
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