Enrolment plugins

Jump to: navigation, search

Since Moodle 2.0 all enrolment plugins must extend enrol_plugin base class which is defined at the end of lib/enrollib.php. This base class contains all standard methods together with developer documentation.

Course enrolment information is stored in tables
enrol
and
user_enrolments
and optionally other custom database tables defined by individual enrolment plugins. Each plugin has complete total over own instance record and user enrolments, by defaults user enrolments are protected and can not be modified manually by teachers.

Enrolment gives users following privileges:

  • User with active enrolment may enter course, other users need either temporary guest access right or moodle/course:view capability.
  • "My courses" shows list of active enrolments for current user.
  • Course participation - some activities restrict participation to enrolled users only. The behaviour is defined independently by each activity, for example only enrolled users with submit capability may submit assignments, the capability alone is not enough.
  • Only enrolled users may be members of groups.
  • Gradebook tracks grades of all enrolled users, visibility of grades is controlled by role membership.

Enrolments and role assignments are now separate concepts, you may be enrolled and not have any role and you may have a role in course and not be enrolled. Roles at course context level and below may be controlled by enrolment plugins.

User enrolment process

Manual enrolment plugins are the simplest way to handle user enrolments. This simplest plugin is enrol_manual, users with necessary permissions may enrol or unenrol users manually. enrol_flatfile plugin allows automation of enrolment and unernolment actions.

Fully automatic plugins are configured at the system level, they synchronise user enrolments with information stored in external systems (ex.: enrol_ldap, enrol_database and enrol_category). Some non-interactive plugins may require configuration of enrolment instances (ex.: enrol_cohort and enrol_meta).

Interactive enrolment plugins require user interaction during enrolment (ex.: enrol_self and enrol_paypal). These plugins need to override
enrol_plugin::show_enrolme_link()
,
enrol_plugin::enrol_page_hook()
and to implement adding and editing of enrol instance.

Enrolment expiration and suspending

User has active enrolment if all following conditions are met:

  • user has record in
    user_enrolments
    table,
  • user enrolment already started,
  • user enrolment is not past timeend,
  • user enrolment has active status,
  • enrol instance has active status in
    enrol
    table,
  • enrol plugin is enabled.

Most synchronisation plugins include a setting called External unenrol action. It is used to decide what happens when previously enrolled user is not supposed to be enrolled any more. Synchronisation is usually executed from cli/sync.php or as part of standard cron.

Time based expiration was implemented in Moodle 2.5. Plugins that set timeend in user_enrolments table may want to specify expiration action and optional expiration notification, see
enrol_plugin::process_expirations()
and
enrol_plugin::send_expiry_notifications()
.

Manual enrolment modifications

The standard user enrolment UI does not use the following methods directly, so developers need to describe all possible UI enrolments actions via
enrol_plugin::get_user_enrolment_actions()
. It is very important to obey these restrictions in all externallib methods!

The following methods describe the allowed modifications for each enrolment instance:

  • enrol_plugin::roles_protected()
    - True means that protected roles (nonempty component+itemid) cannot be modified by any other plugin. Return false if you want to allow users to remove all roles assigned by this plugin. Since Moodle 2.5 it is allowed to assign roles with component+itemid even if roles are not protected.
  • enrol_plugin::allow_enrol()
    - True means other code may call
    enrol_plugin::enrol_user()
    , false means only plugin may enrol users. Each plugin is responsible for implementing its own UI for enrolment. See
    enrol_plugin::get_manual_enrol_link()
    .
  • enrol_plugin::allow_unenrol()
    and
    enrol_plugin::allow_unenrol_user()
    - Is other code allowed to unenrol everybody from one instance or one specific user? True is required for course reset and manual user unenrolment.
  • enrol_plugin:allow_manage()
    - Return true if plugin allows manual modification of user enrolments from other code. False is usually returned from plugins that synchronise data with external systems, otherwise the manual changes would be reverted immediately upon synchronisation.

Standard capabilities

  • enrol/xxx:enrol - Must be defined when
    enrol_plugin::allow_enrol()
    returns true.
  • enrol/xxx:unenrol - Must be implemented when
    enrol_plugin::allow_unenrol()
    or
    enrol_plugin::allow_unenrol_user()
    returns true.
  • enrol/xxx:manage - Must be implemented when
    enrol_plugin::allow_manage()
    returns true.
  • enrol/xxx:unenrolself - Usually implemented when plugin support self-unenrolment.
  • enrol/xxx:config - Implemented when plugin allows user to modify instance properties. Automatic synchronisation plugins do not usually need this capability.

Standard Editing UI

In Moodle 3.1 changes were made to the enrolment plugin base class so that plugins do not have to define their own add/edit interface. In order to use this new logic, the enrolment plugin class must override and return true from this function:

    /**                                                                                                                            
     * We are a good plugin and don't invent our own UI/validation code path.                                                       
     *                                                                                                                              
     * @return boolean                                                                                                              
     */                                                                                                                             
    public function use_standard_editing_ui() {                                                                                     
        return true;                                                                                                                
    }

This means that the following functions from the plugin will be called to build the add/edit form, perform validation of the data and add standard navigation links to the manage enrolments page and course navigation.

    /**                                                                                                                             
     * Add elements to the edit instance form.                                                                                      
     *                                                                                                                              
     * @param stdClass $instance                                                                                                    
     * @param MoodleQuickForm $mform                                                                                                
     * @param context $context                                                                                                      
     * @return bool                                                                                                                 
     */                                                                                                                             
    public function edit_instance_form($instance, MoodleQuickForm $mform, $context) {
    /**                                                                                                                             
     * Perform custom validation of the data used to edit the instance.                                                             
     *                                                                                                                              
     * @param array $data array of ("fieldname"=>value) of submitted data                                                           
     * @param array $files array of uploaded files "element_name"=>tmp_file_path                                                    
     * @param object $instance The instance loaded from the DB                                                                      
     * @param context $context The context of the instance we are editing                                                           
     * @return array of "element_name"=>"error_description" if there are errors,                                                    
     *         or an empty array if everything is OK.                                                                               
     * @return void                                                                                                                 
     */                                                                                                                             
    public function edit_instance_validation($data, $files, $instance, $context) {
    /**                                                                                                                             
     * Return true if we can add a new instance to this course.                                                                     
     *                                                                                                                              
     * @param int $courseid                                                                                                         
     * @return boolean                                                                                                              
     */                                                                                                                             
    public function can_add_instance($courseid) {                                                                                   

In future - only plugins using this standard ui code path will provide a useful API for e.g. tool_uploadcourse to add/edit enrolment instances with complete validation of the submitted data.

See also