Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Note: This page is a work-in-progress. Feedback and suggested improvements are welcome. Please join the discussion on moodle.org or use the talk.


This is a draft specification outlining changes to the custom user profile fields to extend the functionality to generic custom data fields that may be applied to moodle objects

Description

We are seeing an increasing number of requests for adding additional fields to be associated with users and courses. Information to be stored against users can already be achieved through the custom user profile fields, although the interface is sadly lacking. However associated custom data with other areas of moodle, eg courses, currently requires hacking of the code. It is the goal of this document to provide a specification to allow the addition of custom data fields to be associated with any moodle objects with a consistent interface and API functions that allows for easy integration with existing and 3rd party plugins.

Moodle Objects

In this specification a moodle object refers to any one of the following: user, course, course category, file, group, cohort, activity module, block

Specification

Class Definitions

Data Field Definition Class

abstract class data_field_define {
    /// Constructor
    function __construct() {
    }
    /// Define the applicable formats
    abstract public function applicable_formats {
        return array(EXTCONTEXT_COURSE, EXTCONTEXT_GROUP);
    }
 ...
 }

Data Field Class

abstract class data_field_base {
    function __construct();
    abstract public edit_field_add();
    public function display_data();
    public function edit_field();
    public function edit_after_data();
    public function edit_save_data();
    public function edit_validate_field();
    public function edit_field_set_default();
    public function edit_field_set_required();
    public function edit_field_set_locked();
    public_function edit_save_data_preprocess();
    public function edit_load_data_object();
    protected function set_instanceid();
    protected function set_fieldid();
    protected function load_data();
    protected function is_visible();
    protected function is_empty();
    protected function is_required();
    protected function is_locked();
    protected function is_unique();
    protected function is_signup_field();
}


API functions

function data_load_data();
function data_defintion();
function data_definition_after_data();
function data_validation();
function data_save_data();
function data_display_fields();
function data_signup_fields();
...

Database tables

The database tables will closely reflect the existing custom user profile field tables

data_category

field type null key default extra description
id int(10) no primary null auto_increment
extendedcontextid int(10) no the (extended) context of data eg user, course, group, ...
name char(255) no
sortorder int(10) no 0


data_data

field type null key default extra description
id int(10) no primary null auto_increment
extendedcontextid int(10) no the (extended) context of data eg user, course, group, ...
instanceid int(10) no foreign 0 id for the particular extended context type instance
fieldid int(10) no 0 id from the data_field table
data text no the stored data for a given user
dataformat int(2) no 0


data_field

field type null key default extra description
id int(10) no primary null auto_increment
shortname char(255) no
name text no
datatype char(255) no the data field plugin
description text no yes
descriptionformat int(2) no 0
categoryid int(10) no foreign 0 id from the data_category table
sortorder int(10) no 0
required int(2) no 0 is the field required
locked int(2) no 0 lock status of the data
visible int(4) no 0 visibility status
forceunique int(2) no 0 uniqueness requirement
signup int(2) no 0 should the field appear on the signup page?
defaultdata text yes
defaultdataformat int(2) no 0
param1 text yes generic field for the plugin to store information
param2 text yes generic field for the plugin to store information
param3 text yes generic field for the plugin to store information
param4 text yes generic field for the plugin to store information
param5 text yes generic field for the plugin to store information

Interfaces

Upgrade Issues

Standard User Profile Fields

Custom User Profile Fields

User fields for condition acitivities

See Also

Notes from Hackfest
Jenny Gray's blog post