Commit 638a5059 by Bridger Maxwell

Added stores_state attribute for xmoduledescriptors as a way to declare if the…

Added stores_state attribute for xmoduledescriptors as a way to declare if the module stores state in the database.
parent f872e41d
...@@ -563,6 +563,8 @@ class CapaDescriptor(RawDescriptor): ...@@ -563,6 +563,8 @@ class CapaDescriptor(RawDescriptor):
module_class = CapaModule module_class = CapaModule
stores_state = True
# VS[compat] # VS[compat]
# TODO (cpennington): Delete this method once all fall 2012 course are being # TODO (cpennington): Delete this method once all fall 2012 course are being
# edited in the cms # edited in the cms
......
...@@ -108,6 +108,8 @@ class SequenceDescriptor(MakoModuleDescriptor, XmlDescriptor): ...@@ -108,6 +108,8 @@ class SequenceDescriptor(MakoModuleDescriptor, XmlDescriptor):
mako_template = 'widgets/sequence-edit.html' mako_template = 'widgets/sequence-edit.html'
module_class = SequenceModule module_class = SequenceModule
stores_state = True # For remembering where in the sequence the student is
@classmethod @classmethod
def definition_from_xml(cls, xml_object, system): def definition_from_xml(cls, xml_object, system):
return {'children': [ return {'children': [
......
...@@ -80,3 +80,5 @@ class VideoModule(XModule): ...@@ -80,3 +80,5 @@ class VideoModule(XModule):
class VideoDescriptor(RawDescriptor): class VideoDescriptor(RawDescriptor):
module_class = VideoModule module_class = VideoModule
stores_state = True
...@@ -304,6 +304,10 @@ class XModuleDescriptor(Plugin, HTMLSnippet): ...@@ -304,6 +304,10 @@ class XModuleDescriptor(Plugin, HTMLSnippet):
entry_point = "xmodule.v1" entry_point = "xmodule.v1"
module_class = XModule module_class = XModule
# Attributes for inpsection of the descriptor
stores_state = False # Indicates wether the xmodule state should be
# stored in a database (independent of shared state)
# A list of metadata that this module can inherit from its parent module # A list of metadata that this module can inherit from its parent module
inheritable_metadata = ( inheritable_metadata = (
'graded', 'start', 'due', 'graceperiod', 'showanswer', 'rerandomize', 'graded', 'start', 'due', 'graceperiod', 'showanswer', 'rerandomize',
......
...@@ -183,6 +183,10 @@ def get_score(user, problem, student_module_cache): ...@@ -183,6 +183,10 @@ def get_score(user, problem, student_module_cache):
problem: an XModule problem: an XModule
cache: A StudentModuleCache cache: A StudentModuleCache
""" """
if not problem.descriptor.stores_state:
# These are not problems, and do not store state
return (None, None)
correct = 0.0 correct = 0.0
# If the ID is not in the cache, add the item # If the ID is not in the cache, add the item
......
...@@ -69,10 +69,10 @@ class StudentModuleCache(object): ...@@ -69,10 +69,10 @@ class StudentModuleCache(object):
""" """
def __init__(self, user, descriptors): def __init__(self, user, descriptors):
''' '''
Find any StudentModule objects that are needed by any child modules of the Find any StudentModule objects that are needed by any descriptor
supplied descriptor, or caches only the StudentModule objects specifically in descriptors. Avoids making multiple queries to the database.
for every descriptor in descriptors. Avoids making multiple queries to the Note: Only modules that have store_state = True or have shared
database. state will have a StudentModule.
Arguments Arguments
user: The user for which to fetch maching StudentModules user: The user for which to fetch maching StudentModules
...@@ -134,6 +134,7 @@ class StudentModuleCache(object): ...@@ -134,6 +134,7 @@ class StudentModuleCache(object):
''' '''
keys = [] keys = []
for descriptor in descriptors: for descriptor in descriptors:
if descriptor.stores_state:
keys.append(descriptor.location.url()) keys.append(descriptor.location.url())
shared_state_key = getattr(descriptor, 'shared_state_key', None) shared_state_key = getattr(descriptor, 'shared_state_key', None)
......
...@@ -127,7 +127,10 @@ def get_module(user, request, location, student_module_cache, position=None): ...@@ -127,7 +127,10 @@ def get_module(user, request, location, student_module_cache, position=None):
descriptor = modulestore().get_item(location) descriptor = modulestore().get_item(location)
#TODO Only check the cache if this module can possibly have state #TODO Only check the cache if this module can possibly have state
instance_module = None
shared_module = None
if user.is_authenticated(): if user.is_authenticated():
if descriptor.stores_state:
instance_module = student_module_cache.lookup(descriptor.category, instance_module = student_module_cache.lookup(descriptor.category,
descriptor.location.url()) descriptor.location.url())
...@@ -135,11 +138,7 @@ def get_module(user, request, location, student_module_cache, position=None): ...@@ -135,11 +138,7 @@ def get_module(user, request, location, student_module_cache, position=None):
if shared_state_key is not None: if shared_state_key is not None:
shared_module = student_module_cache.lookup(descriptor.category, shared_module = student_module_cache.lookup(descriptor.category,
shared_state_key) shared_state_key)
else:
shared_module = None
else:
instance_module = None
shared_module = None
instance_state = instance_module.state if instance_module is not None else None instance_state = instance_module.state if instance_module is not None else None
...@@ -206,6 +205,11 @@ def get_instance_module(user, module, student_module_cache): ...@@ -206,6 +205,11 @@ def get_instance_module(user, module, student_module_cache):
or None if this is an anonymous user or None if this is an anonymous user
""" """
if user.is_authenticated(): if user.is_authenticated():
if not module.descriptor.stores_state:
log.exception("Attempted to get the instance_module for a module "
+ str(module.id) + " which does not store state.")
return None
instance_module = student_module_cache.lookup(module.category, instance_module = student_module_cache.lookup(module.category,
module.location.url()) module.location.url())
......
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