xblocks.rst 5.7 KB
Newer Older
1 2 3 4 5 6
Integrating XBlocks with edx-platform
=====================================

The edX LMS and Studio have several features that are extensions of the core XBlock
libraries (https://xblock.readthedocs.org). These features are listed below.

7 8 9 10 11
* `LMS`_
* `Studio`_
* `Testing`_
* `Deploying your XBlock`_

12 13 14 15 16 17 18 19 20 21 22 23
LMS
---

Runtime Features
~~~~~~~~~~~~~~~~

These are properties and methods available on ``self.runtime`` when a view or handler is executed by the LMS.

* anonymous_student_id: An identifier unique to the student in the particular course
  that the block is being executed in. The same student in two different courses
  will have two different ids.

24
* publish(block, event_type, event): Emit events to the surrounding system. Events are dictionaries that can contain arbitrary data.
25 26 27 28 29 30
  XBlocks can publish events by calling ``self.runtime.publish(self, event_type, event)``. The ``event_type`` parameter
  enables downstream processing of the event since it uniquely identifies the schema. This call will cause the runtime
  to save the event data in the application event stream. XBlocks should publish events whenever a significant state
  change occurs. Post-hoc analysis of the event stream can yield insight about how the XBlock is used in the context of
  the application. Ideally interesting state of the XBlock could be reconstructed at any point in history through
  careful analysis of the event stream.
31 32 33 34 35 36 37 38 39 40 41 42

  TODO: Link to the authoritive list of event types.

In the future, these are likely to become more formal XBlock services (one related to users,
and the other to event publishing).

Class Features
~~~~~~~~~~~~~~

These are class attributes or functions that can be provided by an XBlock to customize behaviour
in the LMS.

43 44 45
* student_view (XBlock view): This is the view that will be rendered to display the XBlock
  in the LMS. It will also be used to render the block in "preview" mode in Studio, unless
  the XBlock also implements author_view.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
* has_score (class property): True if this block should appear in the LMS progress page.
* get_progress (method): See documentation in x_module.py:XModuleMixin.get_progress.
* icon_class (class property): This can be one of (``other``, ``video``, or ``problem``), and
  determines which icon appears in edx sequence headers. There is currently no way to provide
  a different icon.

Grading
~~~~~~~

To participate in the course grade, an XBlock should set ``has_score`` to ``True``, and
should ``publish`` a ``grade`` event whenever the grade changes. The ``grade`` event is a
dictionary of the following form::

    {
        'value': <number>,
        'max_value': <number>,
62
        'user_id': <number>,
63 64
    }

65 66 67
The grade event represents a grade of ``value/max_value`` for the current user. The
``user_id`` field is optional, the currently logged in user's ID will be used if it is
omitted.
68 69 70 71 72 73 74 75 76 77 78 79 80

Restrictions
~~~~~~~~~~~~

* A block can't modify the value of any field with a scope where the ``user`` property
  is ``UserScope.NONE``.

Studio
------

Class Features
~~~~~~~~~~~~~~

81
* studio_view (XBlock.view): The view used to render an editor in Studio. The editor rendering can
82 83
be completely different from the LMS student_view, and it is only shown when the author selects "Edit".
* author_view (XBlock.view): An optional view of the XBlock similar to student_view, but with possible inline
84
editing capabilities. This view differs from studio_view in that it should be as similar to student_view
85
as possible. When previewing XBlocks within Studio, Studio will prefer author_view to student_view.
86

87
* non_editable_metadata_fields (property): A list of :class:`~xblock.fields.Field` objects that
88 89 90 91 92 93
  shouldn't be displayed in the default editing view for Studio.

Restrictions
~~~~~~~~~~~~

* A block can't modify the value of any field with a scope where the ``user`` property
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  is not ``UserScope.NONE``.


Testing
-------

These instructions are temporary. Once XBlocks are fully supported by edx-platform
(both the LMS and Studio), installation and testing will be much more straightforward.

To enable an XBlock for testing in your devstack (https://github.com/edx/configuration/wiki/edX-Developer-Stack):

#.  Install your block::

        $ vagrant ssh
        vagrant@precise64:~$ sudo -u edxapp /edx/bin/pip.edxapp install /path/to/your/block

#.  Enable the block

    #.  In ``edx-platform/lms/envs/common.py``, uncomment::

        # from xmodule.x_module import prefer_xmodules
        # XBLOCK_SELECT_FUNCTION = prefer_xmodules

    #.  In ``edx-platform/cms/envs/common.py``, uncomment::

        # from xmodule.x_module import prefer_xmodules
        # XBLOCK_SELECT_FUNCTION = prefer_xmodules

    #.  In ``edx-platform/cms/envs/common.py``, change::

            'ALLOW_ALL_ADVANCED_COMPONENTS': False,

        to::

            'ALLOW_ALL_ADVANCED_COMPONENTS': True,

#.  Add the block to your courses' advanced settings in Studio

    #. Log in to Studio, and open your course
    #. Settings -> Advanced Settings
    #. Change the value for the key ``"advanced_modules"`` to ``["your-block"]``

#.  Add your block into your course

    #. Edit a unit
    #. Advanced -> your-block

Note the name ``your-block`` used in Studio must exactly match the key you used to add your
142
block to your ``setup.py`` ``entry_points`` list (if you are still discovering Xblocks and simply used the ``startnew.py`` script as described at https://github.com/edx/XBlock/blob/master/doc/getting_started.rst , look in the ``setup.py`` file that was created)
143 144 145 146 147 148 149 150


Deploying your XBlock
---------------------

To deploy your block to your own hosted version of edx-platform, you need to install it
into the virtualenv that the platform is running out of, and add to the list of ``ADVANCED_COMPONENT_TYPES``
in ``edx-platform/cms/djangoapps/contentstore/views/component.py``.