Commit 1d04a463 by Douglas Cerna

Added student_view_data methods to Problem Builder and MCQ blocks to export their data as JSON

parent 27e2f4ba
...@@ -167,6 +167,27 @@ class MCQBlock(SubmittingXBlockMixin, QuestionnaireAbstractBlock): ...@@ -167,6 +167,27 @@ class MCQBlock(SubmittingXBlockMixin, QuestionnaireAbstractBlock):
self._(u"A choice value listed as correct does not exist: {choice}").format(choice=choice_name(val)) self._(u"A choice value listed as correct does not exist: {choice}").format(choice=choice_name(val))
) )
def student_view_data(self):
"""
Returns a JSON representation of the student_view of this XBlock,
retrievable from the Course Block API.
"""
return {
'id': self.name,
'type': self.CATEGORY,
'question': self.question,
'message': self.message,
'choices': [
{'value': choice['value'], 'content': choice['display_name']}
for choice in self.human_readable_choices
],
'weight': self.weight,
'tips': [
{'content': tip.content, 'for_choices': tip.values}
for tip in self.get_tips()
],
}
class RatingBlock(MCQBlock): class RatingBlock(MCQBlock):
""" """
......
...@@ -905,6 +905,31 @@ class MentoringBlock(BaseMentoringBlock, StudioContainerWithNestedXBlocksMixin, ...@@ -905,6 +905,31 @@ class MentoringBlock(BaseMentoringBlock, StudioContainerWithNestedXBlocksMixin,
""" """
return loader.load_scenarios_from_path('templates/xml') return loader.load_scenarios_from_path('templates/xml')
def student_view_data(self):
"""
Returns a JSON representation of the student_view of this XBlock,
retrievable from the Course Block API.
"""
components = []
for child_id in self.children:
block = self.runtime.get_block(child_id)
if hasattr(block, 'student_view_data'):
components.append(block.student_view_data())
return {
'max_attempts': self.max_attempts,
'extended_feedback': self.extended_feedback,
'feedback_label': self.feedback_label,
'components': components,
'messages': {
message_type: self.get_message_content(message_type)
for message_type in (
'completed',
'incomplete',
'max_attempts_reached',
)
}
}
class MentoringWithExplicitStepsBlock(BaseMentoringBlock, StudioContainerWithNestedXBlocksMixin): class MentoringWithExplicitStepsBlock(BaseMentoringBlock, StudioContainerWithNestedXBlocksMixin):
""" """
......
...@@ -109,6 +109,42 @@ class TestMentoringBlock(BlockWithChildrenTestMixin, unittest.TestCase): ...@@ -109,6 +109,42 @@ class TestMentoringBlock(BlockWithChildrenTestMixin, unittest.TestCase):
(['pb-message'] if block.is_assessment else []) # Message type: "on-assessment-review" (['pb-message'] if block.is_assessment else []) # Message type: "on-assessment-review"
) )
def test_student_view_data(self):
def get_mock_components():
child_a = Mock(spec=['student_view_data'])
child_a.block_id = 'child_a'
child_a.student_view_data.return_value = 'child_a_json'
child_b = Mock(spec=[])
child_b.block_id = 'child_b'
return [child_a, child_b]
shared_data = {
'max_attempts': 3,
'extended_feedback': True,
'feedback_label': 'Feedback label',
}
children = get_mock_components()
children_by_id = {child.block_id: child for child in children}
block_data = {'children': children}
block_data.update(shared_data)
block = MentoringBlock(Mock(), DictFieldData(block_data), Mock())
block.runtime = Mock(
get_block=lambda block: children_by_id[block.block_id],
load_block_type=lambda block: Mock,
id_reader=Mock(get_definition_id=lambda block: block, get_block_type=lambda block: block),
)
expected = {
'components': [
'child_a_json',
],
'messages': {
'completed': None,
'incomplete': None,
'max_attempts_reached': None,
}
}
expected.update(shared_data)
self.assertEqual(block.student_view_data(), expected)
@ddt.ddt @ddt.ddt
class TestMentoringBlockTheming(unittest.TestCase): class TestMentoringBlockTheming(unittest.TestCase):
......
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