Commit 8b308e49 by Alan Boudreault

Fix xml attribute set and modified the load_student_data

parent ba90e14d
...@@ -139,6 +139,12 @@ class LightChildrenMixin(XBlockWithChildrenFragmentsMixin): ...@@ -139,6 +139,12 @@ class LightChildrenMixin(XBlockWithChildrenFragmentsMixin):
""" """
Replacement for ```self.runtime.render_child()``` Replacement for ```self.runtime.render_child()```
""" """
# load student_data
for lightchild in self.get_children_objects():
if isinstance(lightchild, LightChild):
lightchild.load_student_data()
frag = getattr(child, view_name)(context) frag = getattr(child, view_name)(context)
frag.content = u'<div class="xblock-light-child" name="{}" data-type="{}">{}</div>'.format( frag.content = u'<div class="xblock-light-child" name="{}" data-type="{}">{}</div>'.format(
child.name, child.__class__.__name__, frag.content) child.name, child.__class__.__name__, frag.content)
...@@ -173,6 +179,7 @@ class XBlockWithLightChildren(LightChildrenMixin, XBlock): ...@@ -173,6 +179,7 @@ class XBlockWithLightChildren(LightChildrenMixin, XBlock):
""" """
Current HTML view of the XBlock, for refresh by client Current HTML view of the XBlock, for refresh by client
""" """
frag = self.student_view({}) frag = self.student_view({})
frag = self.fragment_text_rewriting(frag) frag = self.fragment_text_rewriting(frag)
...@@ -208,6 +215,7 @@ class LightChild(Plugin, LightChildrenMixin): ...@@ -208,6 +215,7 @@ class LightChild(Plugin, LightChildrenMixin):
entry_point = 'xblock.light_children' entry_point = 'xblock.light_children'
def __init__(self, parent): def __init__(self, parent):
self.__dict__['_field_data'] = {} # Keep a track of the field class type, for setattr
self.parent = parent self.parent = parent
self.xblock_container = parent.xblock_container self.xblock_container = parent.xblock_container
...@@ -217,6 +225,7 @@ class LightChild(Plugin, LightChildrenMixin): ...@@ -217,6 +225,7 @@ class LightChild(Plugin, LightChildrenMixin):
fields = [(attr, value) for attr, value in self.__class__.__dict__.iteritems() if \ fields = [(attr, value) for attr, value in self.__class__.__dict__.iteritems() if \
isinstance(value, LightChildField)] isinstance(value, LightChildField)]
for attr, value in fields: for attr, value in fields:
self._field_data[attr] = value.__class__
self.__dict__[attr] = value.get() # set the default value self.__dict__[attr] = value.get() # set the default value
@lazy @lazy
...@@ -225,7 +234,6 @@ class LightChild(Plugin, LightChildrenMixin): ...@@ -225,7 +234,6 @@ class LightChild(Plugin, LightChildrenMixin):
Use lazy property instead of XBlock field, as __init__() doesn't support Use lazy property instead of XBlock field, as __init__() doesn't support
overwriting field values overwriting field values
""" """
if not self.name: if not self.name:
return '' return ''
...@@ -311,6 +319,13 @@ class LightChild(Plugin, LightChildrenMixin): ...@@ -311,6 +319,13 @@ class LightChild(Plugin, LightChildrenMixin):
) )
return lightchild_data return lightchild_data
def __setattr__(self, name, value):
if name in self._field_data:
self.__dict__[name] = self._field_data[name].set(value) # use the class type setter
else:
super(LightChild, self).__setattr__(name, value)
class LightChildField(object): class LightChildField(object):
""" """
...@@ -326,6 +341,10 @@ class LightChildField(object): ...@@ -326,6 +341,10 @@ class LightChildField(object):
def get(self): def get(self):
return self.value return self.value
@classmethod
def set(cls, value):
return value
class String(LightChildField): class String(LightChildField):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
...@@ -345,8 +364,13 @@ class Integer(LightChildField): ...@@ -345,8 +364,13 @@ class Integer(LightChildField):
def __str__(self): def __str__(self):
return str(self.value) return str(self.value)
def get(self): @classmethod
return int(self.value) def set(cls, value):
try:
value = int(value)
except (TypeError, ValueError): # not an integer
return 0
return value
def __nonzero__(self): def __nonzero__(self):
try: try:
...@@ -356,6 +380,7 @@ class Integer(LightChildField): ...@@ -356,6 +380,7 @@ class Integer(LightChildField):
return self.value is not None return self.value is not None
class Boolean(LightChildField): class Boolean(LightChildField):
pass pass
......
...@@ -33,7 +33,7 @@ from xblock.core import XBlock ...@@ -33,7 +33,7 @@ from xblock.core import XBlock
from xblock.fields import Boolean, Scope, String from xblock.fields import Boolean, Scope, String
from xblock.fragment import Fragment from xblock.fragment import Fragment
from .light_children import XBlockWithLightChildren from .light_children import XBlockWithLightChildren, LightChild
from .message import MentoringMessageBlock from .message import MentoringMessageBlock
from .utils import get_scenarios_from_path, load_resource, render_template from .utils import get_scenarios_from_path, load_resource, render_template
...@@ -114,6 +114,8 @@ class MentoringBlock(XBlockWithLightChildren): ...@@ -114,6 +114,8 @@ class MentoringBlock(XBlockWithLightChildren):
completed = True completed = True
for child in self.get_children_objects(): for child in self.get_children_objects():
if child.name and child.name in submissions: if child.name and child.name in submissions:
if isinstance(child, LightChild):
child.load_student_data()
submission = submissions[child.name] submission = submissions[child.name]
child_result = child.submit(submission) child_result = child.submit(submission)
submit_results.append([child.name, child_result]) submit_results.append([child.name, child_result])
......
...@@ -56,10 +56,6 @@ class MRQBlock(QuestionnaireAbstractBlock): ...@@ -56,10 +56,6 @@ class MRQBlock(QuestionnaireAbstractBlock):
def submit(self, submissions): def submit(self, submissions):
log.debug(u'Received MRQ submissions: "%s"', submissions) log.debug(u'Received MRQ submissions: "%s"', submissions)
# Ensure our data are loaded from the db
# TODO HACK, TO REMOVE.
self.load_student_data()
completed = True completed = True
results = [] results = []
...@@ -88,15 +84,11 @@ class MRQBlock(QuestionnaireAbstractBlock): ...@@ -88,15 +84,11 @@ class MRQBlock(QuestionnaireAbstractBlock):
}) })
self.message = u'Your answer is correct!' if completed else u'Your answer is incorrect.' self.message = u'Your answer is correct!' if completed else u'Your answer is incorrect.'
# Do not increase the counter is the answer is correct # Do not increase the counter is the answer is correct
if not completed: if not completed:
setattr(self, 'num_attempts', self.num_attempts + 1) setattr(self, 'num_attempts', self.num_attempts + 1)
# TODO do a proper fix, max_attempts is set as 'str' with the xml loading code. if self.num_attempts >= self.max_attempts:
# will find a better solution tomorrow
max_attempts = int(self.max_attempts)
if self.num_attempts >= max_attempts and (not completed or self.num_attempts > max_attempts):
completed = True completed = True
self.message += u' You have reached the maximum number of attempts for this question. ' \ self.message += u' You have reached the maximum number of attempts for this question. ' \
u'Your next answers won''t be saved. You can check the answer(s) using the "Show Answer(s)" button.' u'Your next answers won''t be saved. You can check the answer(s) using the "Show Answer(s)" button.'
...@@ -108,7 +100,7 @@ class MRQBlock(QuestionnaireAbstractBlock): ...@@ -108,7 +100,7 @@ class MRQBlock(QuestionnaireAbstractBlock):
'completed': completed, 'completed': completed,
'choices': results, 'choices': results,
'message': self.message, 'message': self.message,
'max_attempts': max_attempts, 'max_attempts': self.max_attempts,
'num_attempts': self.num_attempts 'num_attempts': self.num_attempts
} }
......
...@@ -73,10 +73,6 @@ class QuestionnaireAbstractBlock(LightChild): ...@@ -73,10 +73,6 @@ class QuestionnaireAbstractBlock(LightChild):
def mentoring_view(self, context=None): def mentoring_view(self, context=None):
name = self.__class__.__name__ name = self.__class__.__name__
# Ensure our data are loaded from the db
# TODO HACK, TO REMOVE.
self.load_student_data()
if str(self.type) not in self.valid_types: if str(self.type) not in self.valid_types:
raise ValueError, u'Invalid value for {}.type: `{}`'.format(name, self.type) raise ValueError, u'Invalid value for {}.type: `{}`'.format(name, self.type)
......
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