Note:

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

Enrolment API

From MoodleDocs

work in progress... (skodak)


Since Moodle 2.0 there is a new concept of user enrolments, they are fully independent from the roles and capabilities. Capabilities are very often used in combination with enrolment status.

What is enrolment?

Unenrolment

Enrolment status

Activity participation

API functions

is_enrolled()

Is user participating in the course? Returns true for students and teachers, false for administrators and other managers. User enrolments can be either active or suspended, suspended users can not enter the course (unless there is some kind of guest access allowed) or have moodle/course:view capability and are usually hidden in the UI.

function is_enrolled(context $context, $user = null, $withcapability = , $onlyactive = false)

Good example is choice module where we have one slot for each participant, people that are not enrolled are not allowed to vote is_enrolled($context, $USER, 'mod/choice:choose'). Another example is assignment where users need to be enrolled and have capability to submit assignemnts is_enrolled($this->context, $USER, 'mod/assignment:submit').

get_enrolled_users()

Sometimes you need to know the list of users that can participate in some activity.

function get_enrolled_sql(context $context, $withcapability = , $groupid = 0, $onlyactive = false) function get_enrolled_users(context $context, $withcapability = , $groupid = 0, $userfields = 'u.*', $orderby = , $limitfrom = 0, $limitnum = 0) function count_enrolled_users(context $context, $withcapability = , $groupid = 0)

For example you want to know who is able to summit assignment right now: $submissioncandidates = get_enrolled_users($modcontext, 'mod/assignment:submit', 0, true);

The assignment module needs to hold data for all users that are enrolled including the users with suspended enrolments and without any roles. Module developers may decide to purge all user data when user is fully unenrolled.

SQL select from get_enrolled_sql() is often used for performance reasons - you can use it in joins to get specific information for only enrolled users.


See also