Note: You are currently viewing documentation for Moodle 2.3. Up-to-date documentation for the latest stable version is available here: Customlabel subtype development framework.

Customlabel subtype development framework: Difference between revisions

From MoodleDocs
Line 68: Line 68:


====Field type : htmlarea or editor====
====Field type : htmlarea or editor====
        unset($field);
        $field->name = 'textareafieldname';
        $field->type = 'textarea';
        $field->size = 80;
        $this->fields['textareafieldname'] = $field;


Provides an editable textarea or html editor (depending on site settings) for inpting a formatted multiline content.
Provides an editable textarea or html editor (depending on site settings) for inpting a formatted multiline content.

Revision as of 19:10, 14 December 2012

The customlabel subtype development framework is a set of writting and development rules which will make easy making new subtypes for use in course contents.

The customlabel concept is to centralize all mechanical aspects in the customlabel module, thus keeping writing of a subtype minimalist.

What is a subtype?

A subtype is a cutomlabel subplugin handled by the generic subplugin API of Moodle.

A subtype is essentially :

  • a directory in the mod/customlabel/type directory
  • a subclass of the customlabel_type (@see /mod/customlabel/type/customtype.class.php)
  • a file version.php to handle subplugin verisonning
  • a lang directory for storing templates and language dependant strings
  • a local css file for preseting default styling

The customlabel_type class

The customlabel_type class describes what is the internal information model of the customlabel. The custom type must require the geenric customlabel_type implementation to extend the parent class :

  require_once ($CFG->dirroot."/mod/customlabel/type/customtype.class.php");
  
  class customlabel_type_coursedata extends customlabel_type{
  ... implementation ...
  }


Just three methods are enough to build the customtype :

The class constructor

Defines all information fields used by the customlabel templates. The class stores the type information and builds the $this->fields array that defines each field.

  function __construct($data){
     parent::__construct($data);
     $this->type = 'coursedata';
     $this->fields = array();

The constructor will define fields using the following code template :

  unset($field);
       $field->name = 'fieldname';
       $field->type = 'textfield';
       $this->fields['fieldname'] = $field;

Field type drives how the instance setting form will require information from the editing teacher. Depending on the type accessory attributes can be used to control specific aspects of the settings form.

Field type : choiceyesno

       unset($field);
       $field->name = 'choiceyesnoname';
       $field->type = 'choiceyesno';
       $this->fields['choiceyesnoname'] = $field;

Provides a boolean selector as a yes/no list

Field type : textfield

       unset($field);
       $field->name = 'textfieldname';
       $field->type = 'textfield';
       $field->size = 40;  // optional
       $field->mawlength = 40;  // optional
       $this->fields['textfieldname'] = $field;

Provides a simple textfield to get a simple string

Field type : htmlarea or editor

       unset($field);
       $field->name = 'textareafieldname';
       $field->type = 'textarea';
       $field->size = 80;
       $this->fields['textareafieldname'] = $field;

Provides an editable textarea or html editor (depending on site settings) for inpting a formatted multiline content.

Field type : list

  unset($field);
  $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;

Provides a select list keyed by the option list and labelled with option key tranlsations in the active language. Translations must be provided in lang files of the subtype plugin.

Field type : datasource

  unset($field);
  $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
  $field->constraintson = 'level1,level2';
  $field->mandatory = true;
  $this->fields['datafieldname'] = $field;