Commit dbf5365b by Alan Boudreault

Fix LightChild fields instanciation issue

parent 522da359
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
import logging import logging
import json import json
import copy
from lazy import lazy from lazy import lazy
...@@ -102,6 +103,14 @@ class LightChildrenMixin(XBlockWithChildrenFragmentsMixin): ...@@ -102,6 +103,14 @@ class LightChildrenMixin(XBlockWithChildrenFragmentsMixin):
child = child_class(block) child = child_class(block)
child.name = u'{}_{}'.format(block.name, child_id) child.name = u'{}_{}'.format(block.name, child_id)
# Instanciate our LightChild fields
# TODO HACK, Since we are not replacing the fields attribute with directly, we need to
# instanciate new fields for our LightChild.
fields = [(attr, value) for attr, value in child_class.__dict__.iteritems() if \
isinstance(value, LightChildField)]
for attr, value in fields:
child.__dict__[attr] = copy.deepcopy(value)
# Add any children the child may itself have # Add any children the child may itself have
child_class.init_block_from_node(child, xml_child, xml_child.items()) child_class.init_block_from_node(child, xml_child, xml_child.items())
...@@ -257,13 +266,13 @@ class LightChild(Plugin, LightChildrenMixin): ...@@ -257,13 +266,13 @@ class LightChild(Plugin, LightChildrenMixin):
return return
student_data = json.loads(self.student_data) student_data = json.loads(self.student_data)
fields = self.get_fields() fields = self.get_fields_to_save()
for field in fields: for field in fields:
if field in student_data: if field in student_data:
setattr(self, field, student_data[field]) setattr(self, field, student_data[field])
@classmethod @classmethod
def get_fields(cls): def get_fields_to_save(cls):
""" """
Returns a list of all LightChildField of the class. Used for saving student data. Returns a list of all LightChildField of the class. Used for saving student data.
""" """
...@@ -281,7 +290,7 @@ class LightChild(Plugin, LightChildrenMixin): ...@@ -281,7 +290,7 @@ class LightChild(Plugin, LightChildrenMixin):
self.student_data = {} self.student_data = {}
# Get All LightChild fields to save # Get All LightChild fields to save
for field in self.get_fields(): for field in self.get_fields_to_save():
self.student_data[field] = getattr(self, field).to_json() self.student_data[field] = getattr(self, field).to_json()
if self.name: if self.name:
......
...@@ -48,7 +48,7 @@ class MRQBlock(QuestionnaireAbstractBlock): ...@@ -48,7 +48,7 @@ class MRQBlock(QuestionnaireAbstractBlock):
# TODO REMOVE THIS, ONLY NEEDED FOR LIGHTCHILDREN # TODO REMOVE THIS, ONLY NEEDED FOR LIGHTCHILDREN
@classmethod @classmethod
def get_fields(cls): def get_fields_to_save(cls):
return [ return [
'num_attempts' 'num_attempts'
] ]
...@@ -57,19 +57,23 @@ class MRQBlock(QuestionnaireAbstractBlock): ...@@ -57,19 +57,23 @@ 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 = []
for choice in self.custom_choices: for choice in self.custom_choices:
choice_completed = True choice_completed = True
choice_tips_fragments = [] choice_tips_fragments = []
choice_selected = choice.value in submissions choice_selected = choice.value.get() in submissions
for tip in self.get_tips(): for tip in self.get_tips():
if choice.value in tip.display_with_defaults: if choice.value.get() in tip.display_with_defaults:
choice_tips_fragments.append(tip.render()) choice_tips_fragments.append(tip.render())
if ((not choice_selected and choice.value in tip.require_with_defaults) or if ((not choice_selected and choice.value.get() in tip.require_with_defaults) or
(choice_selected and choice.value in tip.reject_with_defaults)): (choice_selected and choice.value.get() in tip.reject_with_defaults)):
choice_completed = False choice_completed = False
completed = completed and choice_completed completed = completed and choice_completed
...@@ -86,8 +90,6 @@ class MRQBlock(QuestionnaireAbstractBlock): ...@@ -86,8 +90,6 @@ 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.'
# What's the proper way to get my value saved? it doesn't work without '.value'
# this is incorrect and the num_attempts is resetted if we restart the server.
num_attempts = self.num_attempts.get() + 1 if self.max_attempts else 0 num_attempts = self.num_attempts.get() + 1 if self.max_attempts else 0
setattr(self, 'num_attempts', num_attempts) setattr(self, 'num_attempts', num_attempts)
...@@ -99,9 +101,9 @@ class MRQBlock(QuestionnaireAbstractBlock): ...@@ -99,9 +101,9 @@ class MRQBlock(QuestionnaireAbstractBlock):
if max_attempts_reached and (not completed or num_attempts > max_attempts): if max_attempts_reached and (not completed or num_attempts > max_attempts):
completed = True completed = True
self.message += u' You have reached the maximum number of attempts for this question. ' \ self.message = self.message.get() + u' You have reached the maximum number of attempts for this question. ' \
'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.'
else: # only save the student_choices if there was a attempt left, might be incorrect or unuseful
self.student_choices = submissions self.student_choices = submissions
result = { result = {
......
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