Commit 8b308e49 by Alan Boudreault

Fix xml attribute set and modified the load_student_data

parent ba90e14d
......@@ -139,6 +139,12 @@ class LightChildrenMixin(XBlockWithChildrenFragmentsMixin):
"""
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.content = u'<div class="xblock-light-child" name="{}" data-type="{}">{}</div>'.format(
child.name, child.__class__.__name__, frag.content)
......@@ -173,6 +179,7 @@ class XBlockWithLightChildren(LightChildrenMixin, XBlock):
"""
Current HTML view of the XBlock, for refresh by client
"""
frag = self.student_view({})
frag = self.fragment_text_rewriting(frag)
......@@ -208,6 +215,7 @@ class LightChild(Plugin, LightChildrenMixin):
entry_point = 'xblock.light_children'
def __init__(self, parent):
self.__dict__['_field_data'] = {} # Keep a track of the field class type, for setattr
self.parent = parent
self.xblock_container = parent.xblock_container
......@@ -217,6 +225,7 @@ class LightChild(Plugin, LightChildrenMixin):
fields = [(attr, value) for attr, value in self.__class__.__dict__.iteritems() if \
isinstance(value, LightChildField)]
for attr, value in fields:
self._field_data[attr] = value.__class__
self.__dict__[attr] = value.get() # set the default value
@lazy
......@@ -225,7 +234,6 @@ class LightChild(Plugin, LightChildrenMixin):
Use lazy property instead of XBlock field, as __init__() doesn't support
overwriting field values
"""
if not self.name:
return ''
......@@ -311,6 +319,13 @@ class LightChild(Plugin, LightChildrenMixin):
)
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):
"""
......@@ -326,6 +341,10 @@ class LightChildField(object):
def get(self):
return self.value
@classmethod
def set(cls, value):
return value
class String(LightChildField):
def __init__(self, *args, **kwargs):
......@@ -345,8 +364,13 @@ class Integer(LightChildField):
def __str__(self):
return str(self.value)
def get(self):
return int(self.value)
@classmethod
def set(cls, value):
try:
value = int(value)
except (TypeError, ValueError): # not an integer
return 0
return value
def __nonzero__(self):
try:
......@@ -356,6 +380,7 @@ class Integer(LightChildField):
return self.value is not None
class Boolean(LightChildField):
pass
......
......@@ -33,7 +33,7 @@ from xblock.core import XBlock
from xblock.fields import Boolean, Scope, String
from xblock.fragment import Fragment
from .light_children import XBlockWithLightChildren
from .light_children import XBlockWithLightChildren, LightChild
from .message import MentoringMessageBlock
from .utils import get_scenarios_from_path, load_resource, render_template
......@@ -114,6 +114,8 @@ class MentoringBlock(XBlockWithLightChildren):
completed = True
for child in self.get_children_objects():
if child.name and child.name in submissions:
if isinstance(child, LightChild):
child.load_student_data()
submission = submissions[child.name]
child_result = child.submit(submission)
submit_results.append([child.name, child_result])
......
......@@ -56,10 +56,6 @@ class MRQBlock(QuestionnaireAbstractBlock):
def submit(self, 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
results = []
......@@ -88,15 +84,11 @@ class MRQBlock(QuestionnaireAbstractBlock):
})
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
if not completed:
setattr(self, 'num_attempts', self.num_attempts + 1)
# TODO do a proper fix, max_attempts is set as 'str' with the xml loading code.
# 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):
if self.num_attempts >= self.max_attempts:
completed = True
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.'
......@@ -108,7 +100,7 @@ class MRQBlock(QuestionnaireAbstractBlock):
'completed': completed,
'choices': results,
'message': self.message,
'max_attempts': max_attempts,
'max_attempts': self.max_attempts,
'num_attempts': self.num_attempts
}
......
......@@ -73,10 +73,6 @@ class QuestionnaireAbstractBlock(LightChild):
def mentoring_view(self, context=None):
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:
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