Table des matières
Element Type Development Model
Module: Course Elements
Course elements are editorial building blocks that come very close to the “business view” of the content, by providing this “business” with pre-formatted models.
The module provides as standard a battery of elements addressing the recurring and classic needs of educational publishing. In some cases, it may be desirable, for a specific pedagogical project, to create your own course elements, rather than redeveloping complete Moodle plugins.
Basic elements of a type
A course element type is a sub-plugin in the Moodle sense. It has its own version, its own language strings, its own rights. It is therefore built as a directory in the base path /mod/customlabel/type.
The mandatory content of a subtype is:
- [db]
- access.php: defines the content visibility control capabilities
- [lang]
- [en]
- customlabeltype_newtype.php: the strings file
- customlabel.class.php: the implementation class
- customlabel.css: the base stylesheet of the widget
- version.php: the version file
Optional resources:
- [pix]: default images directory (> 3.0)
The directory bears the name of the sub-plugin that will serve as the basis for the formal names in its code. In what follows, the plugin name is assumed to be newtype.
The plugin is represented by a subclass of the base class customlabel_type
require_once $CFG->dirroot.'/mod/customlabel/type/customtype.class.php'; class customlabel_newtype extends customlabel_type { }
A course element subtype must define
- a type
- an array of fields
- possible processing hooks.
Three methods are enough to establish the course element type:
The class constructor
It defines all the information fields supported by this specific course element model. The class stores as members the subtype name and an array $this→fields of field definitions.
function __construct($data){ parent::__construct($data); $this->type = 'coursedata'; $this->fields = array();
Fields are defined according to the type of data they ask the user for when editing the label. The following models show the different supported data type possibilities:
$field = StdClass(); $field->name = '<fieldname>'; $field->type = '<fieldtype>'; $this->fields['<fieldname>'] = $field; ... additional attributes ...
The data field type will determine how the data will be requested from the course editor. Depending on this type, additional attributes may be accepted (or are required) to complete the description.
Field type: Simple yes/no choice (choiceyesno)
$field = new StdClass(); $field->name = 'choiceyesnoname'; $field->type = 'choiceyesno'; $this->fields['choiceyesnoname'] = $field;
Is rendered as a drop-down list with two options “Yes” and “No”.
Field type: text field (textfield)
$field = new StdClass; $field->name = 'textfieldname'; $field->type = 'textfield'; $field->size = 40; // optional $field->mawlength = 40; // optional $this->fields['textfieldname'] = $field;
Produces a simple text input field.
Field type: text area (htmlarea or editor)
$field = new Stdclass(); $field->name = 'textareafieldname'; $field->type = 'textarea'; $field->size = 80; $this->fields['textareafieldname'] = $field;
Produces as output a simple text area (textarea) or a WYSIWYG editor (depending on the general site settings) to enter multi-line text or formatted text.
Field type: Drop-down list (list)
$field = new StdClass(); $field->name = 'datafieldname'; $field->type = 'list'; $field->options = 'comma,separated,list,of,option,keys'; $field->multiple = 'multiple'; // adds multiple choice capability $this->fields['datafieldname'] = $field;
Produces a drop-down list whose values are the keys mentioned in the options attribute and whose visible labels are the translations of the keys in the current language. The translations of the keys must be provided in the language files of the sub-plugin.
A number of optional attributes can bring some behaviour modifications to resolve similar situations:
$field->straightoptions = true;
The effect of this attribute is that the list will present identical values and labels, built from the keys given in the options attribute without any translation.
Field type: Data source (datasource)
$field = new StdClass(); $field->name = 'datafieldname'; $field->type = 'datasource'; $field->source = 'dbfieldkeyed'; // source mode $field->table = 'tablename where to get data from'; $field->field = 'sourcefieldname'; $field->select = 'some select clause'; $field->multiple = 'multiple'; // adds multiple choice capability (defaults to : no) $field->constraintson = 'level1,level2'; $field->mandatory = true; // (defaults to : false) $this->fields['datafieldname'] = $field;
The data source appears as a drop-down list, but built from dynamic data stored elsewhere in Moodle. The data source can provide the keys and/or the values of the list. This choice is driven by the source attribute, which can be:
- dbfieldkey: Gets the labels in a specific field (
fieldattribute) of the specified table (tableattribute); the keys are necessarily the values of theidcolumn of the same table. The results can be sorted on a field of the table (sortorderattribute) and they can be filtered by a SELECT clause on the same table (selectattribute).
Example:
$field = new StdClass(); $field->name = 'courseselectorbyid'; $field->type = 'datasource'; $field->source = 'dbfieldkey'; $field->table = 'course'; $field->field = 'fullname'; $field->ordering = 'sortorder'; $field->select = ' visible = 1 '; $this->fields['courseselectorbyid'] = $field;
- dbfieldkeyed: This is a more flexible variant than the first, which gets the list options from a table, but with the freedom to choose both the keys field and the list labels field.
Example:
$field = new Stdclass(); $field->name = 'courseselectorbyshortname'; $field->type = 'datasource'; $field->source = 'dbfieldkeyed'; $field->table = 'course'; $field->field = 'fullname'; $field->key = 'shortname'; $field->select = ' visible = 1 '; $field->ordering = 'sortorder'; $this->fields['courseselectorbyshortname'] = $field;
- function: Loads the drop-down list from the result of a PHP function that must return an associative array of keys/values. It is currently not possible to send parameters to this function. This form is to be used if getting the options requires a join or complex processing.
$field = new StdClass(); $field->name = 'selectorbyfunction'; $field->type = 'datasource'; $field->source = 'function'; $field->file = 'sourcefile/path/from/dirroot'; $field->function = 'functionname'; $this->fields['selectorbyfunction'] = $field;
Field type: File upload (filepicker)
$field = new StdClass(); $field->name = 'datafieldname'; $field->type = 'filepicker'; $field->acceptedtypes = 'jpg,png,gif'; $field->default = '/theme/mytheme/pix_mod/customlabels/coureheading/default.jpg'; $field->mandatory = true; // (defaults to : false) $this->fields['datafieldname'] = $field;
The acceptedtypes attribute defaults to '*'. This field type will allow the teacher to upload an image that can be used in the template. You will probably need to “post-process” the data a little to adapt the URL and build an IMG tag, or directly in a SRC or HREF attribute. The default attribute makes it possible to define a default url if no file has been uploaded by the user.
Content production
When the course element is prepared (when it is saved), the following production sequence is activated:
- Specific pre-processing (if present in the sub-plugin)
- preparation of list options
- Retrieval of data sources
- Specific post-processing (if present in the sub-plugin)
- Template computation
Pre-processing can be used to bring additional data to the course element, coming for example from external sources (ldap, other databases, computation). Post-processing can be used to format the final data or compute template variables not provided by the data sources or by the sub-plugin description. The two methods to override are:
public function preprocess_data() { }
public function postprocess_data() { }
In these methods, all the data available for the course element is in the $this→data structure
Templates and formatting
Templates are written as language strings, in order to be able to localise the production of the course element and allow easier modification by the administrator.
Template example:
$string['template'] = '
<div class="custombox-commentbox">
<%%comment%%>
</div>
<%if %%readmorecontent%% %>
<div class="custombox-commentbox readmorelink">
<a href="javascript:togglecustomstring(\'<%%customid%%>\', \'Read more...\', \'Read less...\')"><span id="customctl<%%customid%%>"><%%initialstring%%></span></a>
</div>
<div class="custombox-commentbox readmore" id="custom<%%customid%%>">
<%%readmorecontent%%>
</div>
<%endif %>
<script type="text/javascript">
setupcustomstring(\'<%%customid%%>\', \'<%%initiallyvisible%%>\', \'Read more...\', \'Read less...\');
</script>
';
Variable injection
The variables of the $this→data structure can be injected by means of the <varname> tags. In the example above, the variable $this→data→readmorecontent must be fed if it has content provided by the teacher.
Simple conditional syntaxes
To resolve simple combinatorial cases (formatting alternatives), a simple conditional syntax is offered:
<%if %%varname%% %> <%endif %>
Multilingualism
Course elements support multilingual resolution, by offering one template version per language enabled in Moodle. The English version of the template is therefore mandatory. As a result, the template texts can contain literal mentions. The version coming from the language file corresponding to the active language will be offered as output. All the language versions are precompiled in advance when the course element is saved so that they can be delivered according to the user's dynamic choice of language.
To obtain total multilingualism, teachers will also have to produce the content of the course element fields with Moodle's multilingual syntax.
REMINDER: To correctly filter course elements that may contain HTML with complex structure, the “Enhanced multilingual” filter (filter_multilangenhanced) must be installed and used as a replacement for the standard multilingual filter.
Back to the technical guide - Back to the component index - Back to the plugins index - Back to the catalogue
