In ~Palm and earlier, all built-in XBlock Sass was included into LMS and CMS
styles before being compiled. The generated CSS was coupled together with
broader LMS/CMS CSS. This means that comprehensive themes have been able to
modify built-in XBlock appearance by setting certain Sass variables. We say that
built-in XBlock Sass was, and is expected to be, "theme-aware".
Shortly after Palm, we decoupled XBlock Sass from LMS and CMS Sass [1]. Each
built-in block's Sass is now compiled into two separate CSS targets, one for
block editing and one for block display. The CSS, now located at
`common/static/css/xmodule`, is injected into the running Webpack context with
the new `XModuleWebpackLoader`. Built-in XBlocks already used
`add_webpack_to_fragment` in order to add JS Webpack bundles to their view
fragments, so when CSS was added to Webpack, it Just Worked.
This unlocked a slieu of simplifications for static asset processing [2];
however, it accidentally made XBlock Sass theme-*unaware*, or perhaps
theme-confused, since the CSS was targeted at `common/static/css/xmodule`
regardless of the theme. The result of this is that **built-in XBlock views will
use CSS based on the Sass variables _last theme to be compiled._** Sass
variables are only used in a handful of places in XBlocks, so the bug is subtle,
but it is there for those running off of master. For example, using edX.org's
theme on master, we can see that there is a default blue underline in the Studio
sequence nav [3]. With this bugfix, it becomes the standard edX.org
greenish-black [4].
This commit makes several changes, firstly to fix the bug, and secondly to leave
ourselves with a more comprehensible asset setup in the `xmodule/` directory.
* We remove the `XModuleWebpackLoader`, thus taking built-in XBlock Sass back
out of Webpack.
* We compile XBlock Sass not to `common/static/css/xmodule`, but to:
* `[lms|cms]/static/css` for the default theme, and
* `<THEME_ROOT>/[lms|cms]/static/css`, for any custom theme.
This is where the comprehensive theming system expects to find themable
assets. Unfortunately, this does mean that the Sass is compiled twice, both
for LMS and CMS. We would have liked to compile it once to somewhere in the
`common/`, but comprehensive theming does not consider `common/` assets to be
themable.
* We split `add_webpack_to_fragment` into two more specialized functions:
* `add_webpack_js_to_fragment` , for adding *just* JS from a Webpack bundle,
and
* `add_sass_to_fragment`, for adding static links to CSS compiled themable
Sass (not Webpack). Both these functions are moved to a new module
`xmodule/util/builtin_assets.py`, since the original module
(`xmodule/util/xmodule_django.py`) didn't make a ton of sense.
* In an orthogonal bugfix, we merge Sass `CourseInfoBlock`, `StaticTabBlock`,
`AboutBlock` into the `HtmlBlock` Sass files. The first three were never used,
as their styling was handled by `HtmlBlock` (their shared parent class).
* As a refactoring, we change Webpack bundle names and Sass module names to be
less misleading:
* student_view, public_view, and author_view: was `<Name>BlockPreview`, is now
`<Name>BlockDisplay`.
* studio_view: was `<Name>BlockStudio`, is now `<Name>BlockEditor`.
* As a refactoring, we move the contents of `xmodule/static` into the existing
`xmodule/assets` directory, and adopt its simper structure. We now have:
* `xmodule/assets/*.scss`: Top-level compiled Sass modules. These could be
collapsed away in a future refactoring.
* `xmodule/assets/<blocktype>/*`: Resources for each block, including both JS
modules and Sass includes (underscore-prefixed so that they aren't
compiled). This structure maps closely with what externally-defined XBlocks
do.
* `xmodule/js` still exists, but it will soon be folded into the
`xmodule/assets`.
* We add a new README [4] to explain the new structure, and also update a
docstring in `openedx/lib/xblock/utils` which had fallen out of date with
reality.
* Side note: We avoid the term "XModule" in all of this, because that's
(thankfully) become a much less useful/accurate way to describe these blocks.
Instead, we say "built-in XBlocks".
Refs:
1. https://github.com/openedx/edx-platform/pull/32018
2. https://github.com/openedx/edx-platform/issues/32292
3. https://github.com/openedx/edx-platform/assets/3628148/8b44545d-0f71-4357-9385-69d6e1cca86f
4. https://github.com/openedx/edx-platform/assets/3628148/d0b7b309-b8a4-4697-920a-8a520e903e06
5. https://github.com/openedx/edx-platform/tree/master/xmodule/assets#readme
Part of: https://github.com/openedx/edx-platform/issues/32292
266 lines
8.8 KiB
Python
266 lines
8.8 KiB
Python
"""Poll block is ungraded xmodule used by students to
|
|
to do set of polls.
|
|
|
|
On the client side we show:
|
|
If student does not yet anwered - Question with set of choices.
|
|
If student have answered - Question with statistics for each answers.
|
|
"""
|
|
|
|
|
|
import html
|
|
import json
|
|
import logging
|
|
from collections import OrderedDict
|
|
from copy import deepcopy
|
|
|
|
from pkg_resources import resource_filename
|
|
from web_fragments.fragment import Fragment
|
|
|
|
from lxml import etree
|
|
from xblock.core import XBlock
|
|
from xblock.fields import Boolean, Dict, List, Scope, String # lint-amnesty, pylint: disable=wrong-import-order
|
|
from openedx.core.djangolib.markup import Text, HTML
|
|
from xmodule.mako_block import MakoTemplateBlockBase
|
|
from xmodule.stringify import stringify_children
|
|
from xmodule.util.builtin_assets import add_webpack_js_to_fragment, add_sass_to_fragment
|
|
from xmodule.x_module import (
|
|
HTMLSnippet,
|
|
ResourceTemplates,
|
|
shim_xmodule_js,
|
|
XModuleMixin,
|
|
XModuleToXBlockMixin,
|
|
)
|
|
from xmodule.xml_block import XmlMixin
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
_ = lambda text: text
|
|
|
|
|
|
@XBlock.needs('mako')
|
|
class PollBlock(
|
|
MakoTemplateBlockBase,
|
|
XmlMixin,
|
|
XModuleToXBlockMixin,
|
|
HTMLSnippet,
|
|
ResourceTemplates,
|
|
XModuleMixin,
|
|
): # pylint: disable=abstract-method
|
|
"""Poll Block"""
|
|
# Name of poll to use in links to this poll
|
|
display_name = String(
|
|
help=_("The display name for this component."),
|
|
scope=Scope.settings
|
|
)
|
|
|
|
voted = Boolean(
|
|
help=_("Whether this student has voted on the poll"),
|
|
scope=Scope.user_state,
|
|
default=False
|
|
)
|
|
poll_answer = String(
|
|
help=_("Student answer"),
|
|
scope=Scope.user_state,
|
|
default=''
|
|
)
|
|
poll_answers = Dict(
|
|
help=_("Poll answers from all students"),
|
|
scope=Scope.user_state_summary
|
|
)
|
|
|
|
# List of answers, in the form {'id': 'some id', 'text': 'the answer text'}
|
|
answers = List(
|
|
help=_("Poll answers from xml"),
|
|
scope=Scope.content,
|
|
default=[]
|
|
)
|
|
|
|
question = String(
|
|
help=_("Poll question"),
|
|
scope=Scope.content,
|
|
default=''
|
|
)
|
|
|
|
resources_dir = None
|
|
uses_xmodule_styles_setup = True
|
|
|
|
preview_view_js = {
|
|
'js': [
|
|
resource_filename(__name__, 'js/src/javascript_loader.js'),
|
|
resource_filename(__name__, 'js/src/poll/poll.js'),
|
|
resource_filename(__name__, 'js/src/poll/poll_main.js')
|
|
],
|
|
'xmodule_js': resource_filename(__name__, 'js/src/xmodule.js'),
|
|
}
|
|
|
|
# There is no studio_view() for this XBlock but this is needed to make the
|
|
# the static_content command happy.
|
|
studio_view_js = {
|
|
'js': [],
|
|
'xmodule_js': resource_filename(__name__, 'js/src/xmodule.js')
|
|
}
|
|
|
|
def handle_ajax(self, dispatch, data): # lint-amnesty, pylint: disable=unused-argument
|
|
"""Ajax handler.
|
|
|
|
Args:
|
|
dispatch: string request slug
|
|
data: dict request data parameters
|
|
|
|
Returns:
|
|
json string
|
|
"""
|
|
if dispatch in self.poll_answers and not self.voted:
|
|
# FIXME: fix this, when xblock will support mutable types.
|
|
# Now we use this hack.
|
|
temp_poll_answers = self.poll_answers
|
|
temp_poll_answers[dispatch] += 1
|
|
self.poll_answers = temp_poll_answers
|
|
|
|
self.voted = True
|
|
self.poll_answer = dispatch
|
|
return json.dumps({'poll_answers': self.poll_answers,
|
|
'total': sum(self.poll_answers.values()),
|
|
'callback': {'objectName': 'Conditional'}
|
|
})
|
|
elif dispatch == 'get_state':
|
|
return json.dumps({'poll_answer': self.poll_answer,
|
|
'poll_answers': self.poll_answers,
|
|
'total': sum(self.poll_answers.values())
|
|
})
|
|
elif dispatch == 'reset_poll' and self.voted and \
|
|
self.xml_attributes.get('reset', 'True').lower() != 'false':
|
|
self.voted = False
|
|
|
|
# FIXME: fix this, when xblock will support mutable types.
|
|
# Now we use this hack.
|
|
temp_poll_answers = self.poll_answers
|
|
temp_poll_answers[self.poll_answer] -= 1
|
|
self.poll_answers = temp_poll_answers
|
|
|
|
self.poll_answer = ''
|
|
return json.dumps({'status': 'success'})
|
|
else: # return error message
|
|
return json.dumps({'error': 'Unknown Command!'})
|
|
|
|
def student_view(self, _context):
|
|
"""
|
|
Renders the student view.
|
|
"""
|
|
fragment = Fragment()
|
|
params = {
|
|
'element_id': self.location.html_id(),
|
|
'element_class': self.location.block_type,
|
|
'ajax_url': self.ajax_url,
|
|
'configuration_json': self.dump_poll(),
|
|
}
|
|
fragment.add_content(self.runtime.service(self, 'mako').render_template('poll.html', params))
|
|
add_sass_to_fragment(fragment, 'PollBlockDisplay.scss')
|
|
add_webpack_js_to_fragment(fragment, 'PollBlockDisplay')
|
|
shim_xmodule_js(fragment, 'Poll')
|
|
return fragment
|
|
|
|
def dump_poll(self):
|
|
"""Dump poll information.
|
|
|
|
Returns:
|
|
string - Serialize json.
|
|
"""
|
|
# FIXME: hack for resolving caching `default={}` during definition
|
|
# poll_answers field
|
|
if self.poll_answers is None:
|
|
self.poll_answers = {}
|
|
|
|
answers_to_json = OrderedDict()
|
|
|
|
# FIXME: fix this, when xblock support mutable types.
|
|
# Now we use this hack.
|
|
temp_poll_answers = self.poll_answers
|
|
|
|
# Fill self.poll_answers, prepare data for template context.
|
|
for answer in self.answers:
|
|
# Set default count for answer = 0.
|
|
if answer['id'] not in temp_poll_answers:
|
|
temp_poll_answers[answer['id']] = 0
|
|
answers_to_json[answer['id']] = html.escape(answer['text'], quote=False)
|
|
self.poll_answers = temp_poll_answers
|
|
|
|
return json.dumps({
|
|
'answers': answers_to_json,
|
|
'question': html.escape(self.question, quote=False),
|
|
# to show answered poll after reload:
|
|
'poll_answer': self.poll_answer,
|
|
'poll_answers': self.poll_answers if self.voted else {},
|
|
'total': sum(self.poll_answers.values()) if self.voted else 0,
|
|
'reset': str(self.xml_attributes.get('reset', 'true')).lower()
|
|
})
|
|
|
|
_tag_name = 'poll_question'
|
|
_child_tag_name = 'answer'
|
|
|
|
@classmethod
|
|
def definition_from_xml(cls, xml_object, system):
|
|
"""Pull out the data into dictionary.
|
|
|
|
Args:
|
|
xml_object: xml from file.
|
|
system: `system` object.
|
|
|
|
Returns:
|
|
(definition, children) - tuple
|
|
definition - dict:
|
|
{
|
|
'answers': <List of answers>,
|
|
'question': <Question string>
|
|
}
|
|
"""
|
|
# Check for presense of required tags in xml.
|
|
if len(xml_object.xpath(cls._child_tag_name)) == 0:
|
|
raise ValueError("Poll_question definition must include \
|
|
at least one 'answer' tag")
|
|
|
|
xml_object_copy = deepcopy(xml_object)
|
|
answers = []
|
|
for element_answer in xml_object_copy.findall(cls._child_tag_name):
|
|
answer_id = element_answer.get('id', None)
|
|
if answer_id:
|
|
answers.append({
|
|
'id': answer_id,
|
|
'text': stringify_children(element_answer)
|
|
})
|
|
xml_object_copy.remove(element_answer)
|
|
|
|
definition = {
|
|
'answers': answers,
|
|
'question': stringify_children(xml_object_copy)
|
|
}
|
|
children = []
|
|
|
|
return (definition, children)
|
|
|
|
def definition_to_xml(self, resource_fs):
|
|
"""Return an xml element representing to this definition."""
|
|
poll_str = HTML('<{tag_name}>{text}</{tag_name}>').format(
|
|
tag_name=self._tag_name, text=self.question)
|
|
xml_object = etree.fromstring(poll_str)
|
|
xml_object.set('display_name', self.display_name)
|
|
|
|
def add_child(xml_obj, answer): # lint-amnesty, pylint: disable=unused-argument
|
|
# Escape answer text before adding to xml tree.
|
|
answer_text = str(answer['text'])
|
|
child_str = Text('{tag_begin}{text}{tag_end}').format(
|
|
tag_begin=HTML('<{tag_name} id="{id}">').format(
|
|
tag_name=self._child_tag_name,
|
|
id=answer['id']
|
|
),
|
|
text=answer_text,
|
|
tag_end=HTML('</{tag_name}>').format(tag_name=self._child_tag_name)
|
|
)
|
|
child_node = etree.fromstring(child_str)
|
|
xml_object.append(child_node)
|
|
|
|
for answer in self.answers:
|
|
add_child(xml_object, answer)
|
|
|
|
return xml_object
|