Commit 9e4b0ab4 by Braden MacDonald

Clean up some of the components

parent 3d01658f
...@@ -23,36 +23,17 @@ ...@@ -23,36 +23,17 @@
# Imports ########################################################### # Imports ###########################################################
from xblock.core import XBlock from .common import BlockWithContent
from xblock.fields import Scope, String from xblock.fields import Scope, String
from xblock.fragment import Fragment
from xblockutils.resources import ResourceLoader
# Classes ########################################################### # Classes ###########################################################
class ChoiceBlock(XBlock): class ChoiceBlock(BlockWithContent):
""" """
Custom choice of an answer for a MCQ/MRQ Custom choice of an answer for a MCQ/MRQ
""" """
TEMPLATE = 'templates/html/choice.html'
value = String(help="Value of the choice when selected", scope=Scope.content, default="") value = String(help="Value of the choice when selected", scope=Scope.content, default="")
content = String(help="Human-readable version of the choice value", scope=Scope.content, default="") content = String(help="Human-readable version of the choice value", scope=Scope.content, default="")
has_children = True
def render(self):
"""
Returns a fragment containing the formatted choice
"""
fragment = Fragment()
child_content = u""
for child_id in self.children:
child = self.runtime.get_block(child_id)
child_fragment = child.render('mentoring_view', {})
fragment.add_frag_resources(child_fragment)
child_content += child_fragment.content
fragment.add_content(ResourceLoader(__name__).render_template('templates/html/choice.html', {
'self': self,
'child_content': child_content,
}))
return fragment # TODO: fragment_text_rewriting
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 OpenCraft
#
# This software's license gives you freedom; you can copy, convey,
# propagate, redistribute and/or modify this program under the terms of
# the GNU Affero General Public License (AGPL) as published by the Free
# Software Foundation (FSF), either version 3 of the License, or (at your
# option) any later version of the AGPL published by the FSF.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program in a file in the toplevel directory called
# "AGPLv3". If not, see <http://www.gnu.org/licenses/>.
#
# Imports ###########################################################
from xblock.core import XBlock
from xblock.fields import Scope, String
from xblock.fragment import Fragment
from xblockutils.resources import ResourceLoader
# Classes ###########################################################
class BlockWithContent(XBlock):
"""
A block that can contain simple text content OR <html> blocks
with rich HTML content.
"""
TEMPLATE = None # Override in subclass
content = String(help="Content", scope=Scope.content, default="")
has_children = True
def fallback_view(self, view_name, context=None):
"""
Returns a fragment containing the HTML
"""
fragment = Fragment()
child_content = u""
for child_id in self.children:
child = self.runtime.get_block(child_id)
child_fragment = child.render('mentoring_view', {})
fragment.add_frag_resources(child_fragment)
child_content += child_fragment.content
fragment.add_content(ResourceLoader(__name__).render_template(self.TEMPLATE, {
'self': self,
'content': self.content,
'child_content': child_content,
}))
return fragment # TODO: fragment_text_rewriting
def get_html(self):
""" Render as HTML - not as a Fragment """
return self.fallback_view(None, None).content
...@@ -53,15 +53,15 @@ class MCQBlock(QuestionnaireAbstractBlock): ...@@ -53,15 +53,15 @@ class MCQBlock(QuestionnaireAbstractBlock):
log.debug(u'Received MCQ submission: "%s"', submission) log.debug(u'Received MCQ submission: "%s"', submission)
correct = True correct = True
tips_fragments = [] tips_html = []
for tip in self.get_tips(): for tip in self.get_tips():
correct = correct and self.is_tip_correct(tip, submission) correct = correct and self.is_tip_correct(tip, submission)
if submission in tip.display_with_defaults: if submission in tip.display_with_defaults:
tips_fragments.append(tip.render()) tips_html.append(tip.get_html())
formatted_tips = ResourceLoader(__name__).render_template('templates/html/tip_choice_group.html', { formatted_tips = ResourceLoader(__name__).render_template('templates/html/tip_choice_group.html', {
'self': self, 'self': self,
'tips_fragments': tips_fragments, 'tips_html': tips_html,
'completed': correct, 'completed': correct,
}) })
......
...@@ -25,33 +25,17 @@ ...@@ -25,33 +25,17 @@
from xblock.core import XBlock from .common import BlockWithContent
from xblock.fields import Scope, String from xblock.fields import Scope, String
from xblock.fragment import Fragment
from xblockutils.resources import ResourceLoader
# Classes ########################################################### # Classes ###########################################################
class MentoringMessageBlock(XBlock): class MentoringMessageBlock(BlockWithContent):
""" """
A message which can be conditionally displayed at the mentoring block level, A message which can be conditionally displayed at the mentoring block level,
for example upon completion of the block for example upon completion of the block
""" """
TEMPLATE = 'templates/html/message.html'
content = String(help="Message to display upon completion", scope=Scope.content, default="") content = String(help="Message to display upon completion", scope=Scope.content, default="")
type = String(help="Type of message", scope=Scope.content, default="completed") type = String(help="Type of message", scope=Scope.content, default="completed")
has_children = True
def mentoring_view(self, context=None):
fragment = Fragment()
child_content = u""
for child_id in self.children:
child = self.runtime.get_block(child_id)
child_fragment = child.render('mentoring_view', context)
fragment.add_frag_resources(child_fragment)
child_content += child_fragment.content
fragment.add_content(ResourceLoader(__name__).render_template('templates/html/message.html', {
'self': self,
'child_content': child_content,
}))
return fragment
...@@ -52,11 +52,11 @@ class MRQBlock(QuestionnaireAbstractBlock): ...@@ -52,11 +52,11 @@ class MRQBlock(QuestionnaireAbstractBlock):
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_html = []
choice_selected = choice.value in submissions choice_selected = choice.value 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 in tip.display_with_defaults:
choice_tips_fragments.append(tip.render()) choice_tips_html.append(tip.get_html())
if ((not choice_selected and choice.value in tip.require_with_defaults) or if ((not choice_selected and choice.value in tip.require_with_defaults) or
(choice_selected and choice.value in tip.reject_with_defaults)): (choice_selected and choice.value in tip.reject_with_defaults)):
...@@ -74,7 +74,7 @@ class MRQBlock(QuestionnaireAbstractBlock): ...@@ -74,7 +74,7 @@ class MRQBlock(QuestionnaireAbstractBlock):
choice_result['completed'] = choice_completed choice_result['completed'] = choice_completed
choice_result['tips'] = ResourceLoader(__name__).render_template('templates/html/tip_choice_group.html', { choice_result['tips'] = ResourceLoader(__name__).render_template('templates/html/tip_choice_group.html', {
'self': self, 'self': self,
'tips_fragments': choice_tips_fragments, 'tips_html': choice_tips_html,
'completed': choice_completed, 'completed': choice_completed,
}) })
......
<span class="choice-text"> <span class="choice-text">
{% if self.content %}{{ self.content }}{% endif %} {{ self.content }}
{{ child_content|safe }} {{ child_content|safe }}
</span> </span>
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
<div class="choice-result fa icon-2x"></div> <div class="choice-result fa icon-2x"></div>
<label class="choice-label"> <label class="choice-label">
<input class="choice-selector" type="radio" name="{{ self.name }}" value="{{ choice.value }}"{% if self.student_choice == choice.value %} checked{% endif %} /> <input class="choice-selector" type="radio" name="{{ self.name }}" value="{{ choice.value }}"{% if self.student_choice == choice.value %} checked{% endif %} />
{{ choice.render.content|safe }} {{ choice.get_html|safe }}
</label> </label>
<div class="choice-tips"></div> <div class="choice-tips"></div>
</div> </div>
......
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
<div class="choice"> <div class="choice">
<div class="choice-result fa icon-2x"></div> <div class="choice-result fa icon-2x"></div>
<label><input type="radio" name="{{ self.name }}" value="{{ choice.value }}"{% if self.student_choice == '{{ choice.value }}' %} checked{% endif %} /> <label><input type="radio" name="{{ self.name }}" value="{{ choice.value }}"{% if self.student_choice == '{{ choice.value }}' %} checked{% endif %} />
{{ choice.render.content|safe }} {{ choice.get_html|safe }}
</label> </label>
<div class="choice-tips"></div> <div class="choice-tips"></div>
</div> </div>
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
<input class="choice-selector" type="checkbox" name="{{ self.name }}" <input class="choice-selector" type="checkbox" name="{{ self.name }}"
value="{{ choice.value }}" value="{{ choice.value }}"
{% if choice.value in self.student_choices %} checked{% endif %} /> {% if choice.value in self.student_choices %} checked{% endif %} />
{{ choice.render.content|safe }} {{ choice.get_html|safe }}
</label> </label>
<div class="choice-tips"></div> <div class="choice-tips"></div>
</div> </div>
......
<div class="tip-choice-group"> <div class="tip-choice-group">
{% for tip_fragment in tips_fragments %} {% for tip_html in tips_html %}
{{ tip_fragment.body_html|safe }} {{ tip_html|safe }}
{% endfor %} {% endfor %}
</div> </div>
<div class="close icon-remove-sign fa fa-times-circle"></div> <div class="close icon-remove-sign fa fa-times-circle"></div>
...@@ -23,11 +23,8 @@ ...@@ -23,11 +23,8 @@
# Imports ########################################################### # Imports ###########################################################
from .common import BlockWithContent
from xblock.core import XBlock
from xblock.fields import Scope, String from xblock.fields import Scope, String
from xblock.fragment import Fragment
from xblockutils.resources import ResourceLoader
# Functions ######################################################### # Functions #########################################################
...@@ -43,34 +40,18 @@ def commas_to_set(commas_str): ...@@ -43,34 +40,18 @@ def commas_to_set(commas_str):
# Classes ########################################################### # Classes ###########################################################
class TipBlock(XBlock): class TipBlock(BlockWithContent):
""" """
Each choice can define a tip depending on selection Each choice can define a tip depending on selection
""" """
TEMPLATE = 'templates/html/tip.html'
content = String(help="Text of the tip to provide if needed", scope=Scope.content, default="") content = String(help="Text of the tip to provide if needed", scope=Scope.content, default="")
display = String(help="List of choices to display the tip for", scope=Scope.content, default=None) display = String(help="List of choices to display the tip for", scope=Scope.content, default=None)
reject = String(help="List of choices to reject", scope=Scope.content, default=None) reject = String(help="List of choices to reject", scope=Scope.content, default=None)
require = String(help="List of choices to require", scope=Scope.content, default=None) require = String(help="List of choices to require", scope=Scope.content, default=None)
width = String(help="Width of the tip popup", scope=Scope.content, default='') width = String(help="Width of the tip popup", scope=Scope.content, default='')
height = String(help="Height of the tip popup", scope=Scope.content, default='') height = String(help="Height of the tip popup", scope=Scope.content, default='')
has_children = True
def render(self):
"""
Returns a fragment containing the formatted tip
"""
fragment = Fragment()
child_content = u""
for child_id in self.children:
child = self.runtime.get_block(child_id)
child_fragment = child.render('mentoring_view', {})
fragment.add_frag_resources(child_fragment)
child_content += child_fragment.content
fragment.add_content(ResourceLoader(__name__).render_template('templates/html/tip.html', {
'self': self,
'child_content': child_content,
}))
return fragment # TODO: fragment_text_rewriting
@property @property
def display_with_defaults(self): def display_with_defaults(self):
......
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