XBlockAsides Hello world example
This commit is contained in:
@@ -119,6 +119,20 @@ class PreviewModuleSystem(ModuleSystem): # pylint: disable=abstract-method
|
||||
"""
|
||||
return self.wrap_xblock(block, view_name, Fragment(), context)
|
||||
|
||||
def layout_asides(self, block, context, frag, view_name, aside_frag_fns):
|
||||
position_for_asides = '<!-- footer for xblock_aside -->'
|
||||
result = Fragment()
|
||||
result.add_frag_resources(frag)
|
||||
|
||||
for aside, aside_fn in aside_frag_fns:
|
||||
aside_frag = self.wrap_aside(block, aside, view_name, aside_fn(block, context), context)
|
||||
aside.save()
|
||||
result.add_frag_resources(aside_frag)
|
||||
frag.content = frag.content.replace(position_for_asides, position_for_asides + aside_frag.content)
|
||||
|
||||
result.add_content(frag.content)
|
||||
return result
|
||||
|
||||
|
||||
class StudioPermissionsService(object):
|
||||
"""
|
||||
|
||||
92
cms/lib/xblock/tagging.py
Normal file
92
cms/lib/xblock/tagging.py
Normal file
@@ -0,0 +1,92 @@
|
||||
"""
|
||||
Example implementation of Structured Tagging based on XBlockAsides
|
||||
"""
|
||||
|
||||
from xblock.core import XBlockAside
|
||||
from xblock.fragment import Fragment
|
||||
from xblock.fields import Scope, Dict
|
||||
from xmodule.x_module import STUDENT_VIEW
|
||||
from xmodule.capa_module import CapaModule
|
||||
from abc import ABCMeta, abstractproperty
|
||||
from edxmako.shortcuts import render_to_string
|
||||
|
||||
|
||||
_ = lambda text: text
|
||||
|
||||
|
||||
class AbstractTag(object):
|
||||
"""
|
||||
Abstract class for tags
|
||||
"""
|
||||
__metaclass__ = ABCMeta
|
||||
|
||||
@abstractproperty
|
||||
def key(self):
|
||||
"""
|
||||
Subclasses must implement key
|
||||
"""
|
||||
raise NotImplementedError('Subclasses must implement key')
|
||||
|
||||
@abstractproperty
|
||||
def name(self):
|
||||
"""
|
||||
Subclasses must implement name
|
||||
"""
|
||||
raise NotImplementedError('Subclasses must implement name')
|
||||
|
||||
@abstractproperty
|
||||
def allowed_values(self):
|
||||
"""
|
||||
Subclasses must implement allowed_values
|
||||
"""
|
||||
raise NotImplementedError('Subclasses must implement allowed_values')
|
||||
|
||||
|
||||
class LearningOutcomeTag(AbstractTag):
|
||||
"""
|
||||
Particular implementation tags for learning outcomes
|
||||
"""
|
||||
@property
|
||||
def key(self):
|
||||
""" Identifier for the learning outcome selector """
|
||||
return 'learning_outcome_tag'
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" Label for the learning outcome selector """
|
||||
return _('Learning outcomes')
|
||||
|
||||
@property
|
||||
def allowed_values(self):
|
||||
""" Allowed values for the learning outcome selector """
|
||||
return {'test1': 'Test 1', 'test2': 'Test 2', 'test3': 'Test 3'}
|
||||
|
||||
|
||||
class StructuredTagsAside(XBlockAside):
|
||||
"""
|
||||
Aside that allows tagging blocks
|
||||
"""
|
||||
saved_tags = Dict(help=_("Dictionary with the available tags"),
|
||||
scope=Scope.content,
|
||||
default={},)
|
||||
available_tags = [LearningOutcomeTag()]
|
||||
|
||||
@XBlockAside.aside_for(STUDENT_VIEW)
|
||||
def student_view_aside(self, block, context):
|
||||
"""
|
||||
Display the tag selector with specific categories and allowed values,
|
||||
depending on the context.
|
||||
"""
|
||||
if isinstance(block, CapaModule):
|
||||
tags = []
|
||||
for tag in self.available_tags:
|
||||
tags.append({
|
||||
'key': tag.key,
|
||||
'title': tag.name,
|
||||
'values': tag.allowed_values,
|
||||
'current_value': self.saved_tags.get(tag.key, None),
|
||||
})
|
||||
return Fragment(render_to_string('structured_tags_block.html', {'tags': tags}))
|
||||
#return Fragment(u'<div class="xblock-render">Hello world!!!</div>')
|
||||
else:
|
||||
return Fragment(u'')
|
||||
15
cms/templates/structured_tags_block.html
Normal file
15
cms/templates/structured_tags_block.html
Normal file
@@ -0,0 +1,15 @@
|
||||
<div class="xblock-render">
|
||||
% for tag in tags:
|
||||
<label for="problem_tags_${tag['key']}">${tag['title']}</label>:
|
||||
<select id="problem_tags_${tag['key']}" name="problem_tags_${tag['key']}">
|
||||
% for k,v in tag['values'].iteritems():
|
||||
<%
|
||||
selected = ''
|
||||
if k == tag['current_value']:
|
||||
selected = 'selected'
|
||||
%>
|
||||
<option value="${k}" ${selected}>${v}</option>
|
||||
% endfor
|
||||
% endfor
|
||||
</select>
|
||||
</div>
|
||||
@@ -150,7 +150,9 @@ messages = xblock.validate().to_json()
|
||||
% endif
|
||||
|
||||
% if not is_root:
|
||||
<!-- footer for xblock_aside -->
|
||||
</section>
|
||||
|
||||
% if is_reorderable:
|
||||
</li>
|
||||
% else:
|
||||
|
||||
@@ -44,6 +44,9 @@ XBLOCKS = [
|
||||
"vertical = xmodule.vertical_block:VerticalBlock",
|
||||
"wrapper = xmodule.wrapper_module:WrapperBlock",
|
||||
]
|
||||
XBLOCKS_ASIDES = [
|
||||
'tagging_aside = cms.lib.xblock.tagging:StructuredTagsAside',
|
||||
]
|
||||
|
||||
setup(
|
||||
name="XModule",
|
||||
@@ -66,6 +69,7 @@ setup(
|
||||
entry_points={
|
||||
'xblock.v1': XMODULES + XBLOCKS,
|
||||
'xmodule.v1': XMODULES,
|
||||
'xblock_asides.v1': XBLOCKS_ASIDES,
|
||||
'console_scripts': [
|
||||
'xmodule_assets = xmodule.static_content:main',
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user