Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/enrol/ -> locallib.php (source)

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]

This file contains the course_enrolment_manager class which is used to interface with the functions that exist in enrollib.php in relation to a single course.

Copyright: 2010 Sam Hemelryk
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 1653 lines (62 kb)
Included or required:0 times
Referenced: 9 times
Includes or requires: 0 files

Defines 6 classes

course_enrolment_manager:: (0 methods):

uses:: (43 methods):
  __construct()
  get_moodlepage()
  get_total_users()
  get_total_other_users()
  get_users()
  get_filter_sql()
  get_other_users()
  get_basic_search_conditions()
  execute_search_queries()
  get_potential_users()
  search_other_users()
  search_users()
  get_instance_sql()
  get_enrolment_instances()
  get_enrolment_instance_names()
  get_enrolment_plugins()
  get_all_roles()
  get_viewable_roles()
  get_assignable_roles()
  get_assignable_roles_for_json()
  get_all_groups()
  unenrol_user()
  get_user_enrolment_components()
  unassign_role_from_user()
  assign_role_to_user()
  add_user_to_group()
  remove_user_from_group()
  get_group()
  edit_enrolment()
  get_enrolment_filter()
  get_user_roles()
  get_user_enrolments()
  get_user_groups()
  get_url_params()
  get_course()
  get_context()
  get_other_users_for_display()
  get_users_for_display()
  prepare_user_for_display()
  get_manual_enrol_buttons()
  has_instance()
  get_filtered_enrolment_plugin()
  get_users_enrolments()

enrol_user_button:: (5 methods):
  __construct()
  require_yui_module()
  require_js_init_call()
  strings_for_js()
  initialise_js()

user_enrolment_action:: (5 methods):
  __construct()
  get_icon()
  get_title()
  get_url()
  get_attributes()

enrol_ajax_exception:: (1 method):
  __construct()

enrol_bulk_enrolment_operation:: (2 methods):
  __construct()
  get_form()


Class: course_enrolment_manager  - X-Ref

This class provides a targeted tied together means of interfacing the enrolment
tasks together with a course.

It is provided as a convenience more than anything else.

Class: uses  - X-Ref

__construct(moodle_page $moodlepage, $course, $instancefilter = null,$rolefilter = 0, $searchfilter = '', $groupfilter = 0, $statusfilter = -1)   X-Ref
Constructs the course enrolment manager

param: moodle_page $moodlepage
param: stdClass $course
param: string $instancefilter
param: int $rolefilter If non-zero, filters to users with specified role
param: string $searchfilter If non-blank, filters to users with search text
param: int $groupfilter if non-zero, filter users with specified group
param: int $statusfilter if not -1, filter users with active/inactive enrollment.

get_moodlepage()   X-Ref
Returns the current moodle page

return: moodle_page

get_total_users()   X-Ref
Returns the total number of enrolled users in the course.

If a filter was specificed this will be the total number of users enrolled
in this course by means of that instance.

return: int

get_total_other_users()   X-Ref
Returns the total number of enrolled users in the course.

If a filter was specificed this will be the total number of users enrolled
in this course by means of that instance.

return: int

get_users($sort, $direction='ASC', $page=0, $perpage=25)   X-Ref
Gets all of the users enrolled in this course.

If a filter was specified this will be the users who were enrolled
in this course by means of that instance. If role or search filters were
specified then these will also be applied.

param: string $sort
param: string $direction ASC or DESC
param: int $page First page should be 0
param: int $perpage Defaults to 25
return: array

get_filter_sql()   X-Ref
Obtains WHERE clause to filter results by defined search and role filter
(instance filter is handled separately in JOIN clause, see
get_instance_sql).

return: array Two-element array with SQL and params for WHERE clause

get_other_users($sort, $direction='ASC', $page=0, $perpage=25)   X-Ref
Gets and array of other users.

Other users are users who have been assigned roles or inherited roles
within this course but who have not been enrolled in the course

param: string $sort
param: string $direction
param: int $page
param: int $perpage
return: array

get_basic_search_conditions($search, $searchanywhere)   X-Ref
Helper method used by {@link get_potential_users()} and {@link search_other_users()}.

param: string $search the search term, if any.
param: bool $searchanywhere Can the search term be anywhere, or must it be at the start.
return: array with three elements:

execute_search_queries($search, $fields, $countfields, $sql, array $params, $page, $perpage,$addedenrollment = 0, $returnexactcount = false)   X-Ref
Helper method used by {@link get_potential_users()} and {@link search_other_users()}.

param: string $search the search string, if any.
param: string $fields the first bit of the SQL when returning some users.
param: string $countfields fhe first bit of the SQL when counting the users.
param: string $sql the bulk of the SQL statement.
param: array $params query parameters.
param: int $page which page number of the results to show.
param: int $perpage number of users per page.
param: int $addedenrollment number of users added to enrollment.
param: bool $returnexactcount Return the exact total users using count_record or not.
return: array with two or three elements:

get_potential_users($enrolid, $search = '', $searchanywhere = false, $page = 0, $perpage = 25,$addedenrollment = 0, $returnexactcount = false)   X-Ref
Gets an array of the users that can be enrolled in this course.

param: int $enrolid
param: string $search
param: bool $searchanywhere
param: int $page Defaults to 0
param: int $perpage Defaults to 25
param: int $addedenrollment Defaults to 0
param: bool $returnexactcount Return the exact total users using count_record or not.
return: array with two or three elements:

search_other_users($search = '', $searchanywhere = false, $page = 0, $perpage = 25, $returnexactcount = false)   X-Ref
Searches other users and returns paginated results

param: string $search
param: bool $searchanywhere
param: int $page Starting at 0
param: int $perpage
param: bool $returnexactcount Return the exact total users using count_record or not.
return: array with two or three elements:

search_users(string $search = '', bool $searchanywhere = false, int $page = 0, int $perpage = 25,bool $returnexactcount = false)   X-Ref
Searches through the enrolled users in this course.

param: string $search The search term.
param: bool $searchanywhere Can the search term be anywhere, or must it be at the start.
param: int $page Starting at 0.
param: int $perpage Number of users returned per page.
param: bool $returnexactcount Return the exact total users using count_record or not.
return: array with two or three elements:

get_instance_sql()   X-Ref
Gets an array containing some SQL to user for when selecting, params for
that SQL, and the filter that was used in constructing the sql.

return: string

get_enrolment_instances($onlyenabled = false)   X-Ref
Returns all of the enrolment instances for this course.

param: bool $onlyenabled Whether to return data from enabled enrolment instance names only.
return: array

get_enrolment_instance_names($onlyenabled = false)   X-Ref
Returns the names for all of the enrolment instances for this course.

param: bool $onlyenabled Whether to return data from enabled enrolment instance names only.
return: array

get_enrolment_plugins($onlyenabled = true)   X-Ref
Gets all of the enrolment plugins that are available for this course.

param: bool $onlyenabled return only enabled enrol plugins
return: array

get_all_roles()   X-Ref
Gets all of the roles this course can contain.

return: array

get_viewable_roles()   X-Ref
Gets all of the roles this course can contain.

return: array

get_assignable_roles($otherusers = false)   X-Ref
Gets all of the assignable roles for this course.

return: array

get_assignable_roles_for_json($otherusers = false)   X-Ref
Gets all of the assignable roles for this course, wrapped in an array to ensure
role sort order is not lost during json deserialisation.

param: boolean $otherusers whether to include the assignable roles for other users
return: array

get_all_groups()   X-Ref
Gets all of the groups for this course.

return: array

unenrol_user($ue)   X-Ref
Unenrols a user from the course given the users ue entry

param: stdClass $ue
return: bool

get_user_enrolment_components($userenrolment)   X-Ref
Given a user enrolment record this method returns the plugin and enrolment
instance that relate to it.

param: stdClass|int $userenrolment
return: array array($instance, $plugin)

unassign_role_from_user($userid, $roleid)   X-Ref
Removes an assigned role from a user.

param: int $userid
param: int $roleid
return: bool

assign_role_to_user($roleid, $userid)   X-Ref
Assigns a role to a user.

param: int $roleid
param: int $userid
return: int|false

add_user_to_group($user, $groupid)   X-Ref
Adds a user to a group

param: stdClass $user
param: int $groupid
return: bool

remove_user_from_group($user, $groupid)   X-Ref
Removes a user from a group

param: StdClass $user
param: int $groupid
return: bool

get_group($groupid)   X-Ref
Gets the requested group

param: int $groupid
return: stdClass|int

edit_enrolment($userenrolment, $data)   X-Ref
Edits an enrolment

param: stdClass $userenrolment
param: stdClass $data
return: bool

get_enrolment_filter()   X-Ref
Returns the current enrolment filter that is being applied by this class

return: string

get_user_roles($userid)   X-Ref
Gets the roles assigned to this user that are applicable for this course.

param: int $userid
return: array

get_user_enrolments($userid)   X-Ref
Gets the enrolments this user has in the course - including all suspended plugins and instances.

param: int $userid
return: array

get_user_groups($userid)   X-Ref
Gets the groups this user belongs to

param: int $userid
return: array

get_url_params()   X-Ref
Retursn an array of params that would go into the URL to return to this
exact page.

return: array

get_course()   X-Ref
Returns the course this object is managing enrolments for

return: stdClass

get_context()   X-Ref
Returns the course context

return: context

get_other_users_for_display(core_enrol_renderer $renderer, moodle_url $pageurl, $sort, $direction, $page, $perpage)   X-Ref
Gets an array of other users in this course ready for display.

Other users are users who have been assigned or inherited roles within this
course but have not been enrolled.

param: core_enrol_renderer $renderer
param: moodle_url $pageurl
param: string $sort
param: string $direction ASC | DESC
param: int $page Starting from 0
param: int $perpage
return: array

get_users_for_display(course_enrolment_manager $manager, $sort, $direction, $page, $perpage)   X-Ref
Gets an array of users for display, this includes minimal user information
as well as minimal information on the users roles, groups, and enrolments.

param: core_enrol_renderer $renderer
param: moodle_url $pageurl
param: int $sort
param: string $direction ASC or DESC
param: int $page
param: int $perpage
return: array

prepare_user_for_display($user, $extrafields, $now)   X-Ref
Prepare a user record for display

This function is called by both {@link get_users_for_display} and {@link get_other_users_for_display} to correctly
prepare user fields for display

Please note that this function does not check capability for moodle/coures:viewhiddenuserfields

param: object $user The user record
param: array $extrafields The list of fields as returned from \core_user\fields::get_identity_fields used to determine which
param: int $now The time used for lastaccess calculation
return: array The fields to be displayed including userid, courseid, picture, firstname, lastcourseaccess, lastaccess and any

get_manual_enrol_buttons()   X-Ref
No description

has_instance($enrolpluginname)   X-Ref
No description

get_filtered_enrolment_plugin()   X-Ref
Returns the enrolment plugin that the course manager was being filtered to.

If no filter was being applied then this function returns false.

return: enrol_plugin

get_users_enrolments(array $userids)   X-Ref
Returns and array of users + enrolment details.

Given an array of user id's this function returns and array of user enrolments for those users
as well as enough user information to display the users name and picture for each enrolment.

param: array $userids
return: array

Class: enrol_user_button  - X-Ref

A button that is used to enrol users in a course

__construct(moodle_url $url, $label, $method = 'post')   X-Ref
Initialises the new enrol_user_button

param: moodle_url $url
param: string $label The text to display in the button
param: string $method Either post or get

require_yui_module($modules, $function, array $arguments = null, $galleryversion = null, $ondomready = false)   X-Ref
Adds a YUI module call that will be added to the page when the button is used.

param: string|array $modules One or more modules to require
param: string $function The JS function to call
param: array $arguments An array of arguments to pass to the function
param: string $galleryversion Deprecated: The gallery version to use
param: bool $ondomready If true the call is postponed until the DOM is finished loading

require_js_init_call($function, array $extraarguments = null, $ondomready = false, array $module = null)   X-Ref
Adds a JS initialisation call to the page when the button is used.

param: string $function The function to call
param: array $extraarguments An array of arguments to pass to the function
param: bool $ondomready If true the call is postponed until the DOM is finished loading
param: array $module A module definition

strings_for_js($identifiers, $component = 'moodle', $a = null)   X-Ref
Requires strings for JS that will be loaded when the button is used.

param: type $identifiers
param: string $component
param: mixed $a

initialise_js(moodle_page $page)   X-Ref
Initialises the JS that is required by this button

param: moodle_page $page

Class: user_enrolment_action  - X-Ref

User enrolment action

This class is used to manage a renderable ue action such as editing an user enrolment or deleting
a user enrolment.

__construct(pix_icon $icon, $title, $url, array $attributes = null)   X-Ref
Constructor

param: pix_icon $icon
param: string $title
param: moodle_url $url
param: array $attributes

get_icon()   X-Ref
Returns the icon for this action

return: pix_icon

get_title()   X-Ref
Returns the title for this action

return: string

get_url()   X-Ref
Returns the URL for this action

return: moodle_url

get_attributes()   X-Ref
Returns the attributes to use for this action

return: array

Class: enrol_ajax_exception  - X-Ref

__construct($errorcode, $link = '', $a = NULL, $debuginfo = null)   X-Ref
Constructor

param: string $errorcode The name of the string from error.php to print
param: string $module name of module
param: string $link The url where the user will be prompted to continue. If no url is provided the user will be directed to the site index page.
param: object $a Extra words and phrases that might be required in the error string
param: string $debuginfo optional debugging information

Class: enrol_bulk_enrolment_operation  - X-Ref

This class is used to manage a bulk operations for enrolment plugins.

__construct(course_enrolment_manager $manager, enrol_plugin $plugin = null)   X-Ref
Contructor

param: course_enrolment_manager $manager
param: stdClass $plugin

get_form($defaultaction = null, $defaultcustomdata = null)   X-Ref
Returns a moodleform used for this operation, or false if no form is required and the action
should be immediatly processed.

param: moodle_url|string $defaultaction
param: mixed $defaultcustomdata
return: enrol_bulk_enrolment_change_form|moodleform|false