Note:

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

Enrolment API: Difference between revisions

From MoodleDocs
m (Text replacement - "<code>" to "<syntaxhighlight lang="php">")
m (Text replacement - "<code php>" to "<syntaxhighlight lang="php">")
Line 30: Line 30:
Some enrol methods has the support for sending welcome emails to users.  
Some enrol methods has the support for sending welcome emails to users.  
This method returns a list of all possible options for sending welcome email when the user enrol in a course and each option has a respective constant defined on '''enrollib.php''':  
This method returns a list of all possible options for sending welcome email when the user enrol in a course and each option has a respective constant defined on '''enrollib.php''':  
<code php>
<syntaxhighlight lang="php">
define('ENROL_DO_NOT_SEND_EMAIL', 0); // Do not send the welcome email.
define('ENROL_DO_NOT_SEND_EMAIL', 0); // Do not send the welcome email.
define('ENROL_SEND_EMAIL_FROM_COURSE_CONTACT', 1); // Send welcome email from course contact.
define('ENROL_SEND_EMAIL_FROM_COURSE_CONTACT', 1); // Send welcome email from course contact.
Line 39: Line 39:
The main idea is to make send email options consistent across enrolment methods that support this feature. See an example below:
The main idea is to make send email options consistent across enrolment methods that support this feature. See an example below:


<code php>
<syntaxhighlight lang="php">
$sendoptions = enrol_send_welcome_email_options();
$sendoptions = enrol_send_welcome_email_options();
print_object($sendoptions);
print_object($sendoptions);
Line 46: Line 46:
The example above will output:
The example above will output:


<code php>
<syntaxhighlight lang="php">
Array  
Array  
(
(
Line 59: Line 59:
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.
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.


<code php>
<syntaxhighlight lang="php">
function is_enrolled(context $context, $user = null, $withcapability = '', $onlyactive = false)
function is_enrolled(context $context, $user = null, $withcapability = '', $onlyactive = false)
</syntaxhighlight>
</syntaxhighlight>
Line 69: Line 69:
Sometimes you need to know the list of users that can participate in some activity.
Sometimes you need to know the list of users that can participate in some activity.


<code php>
<syntaxhighlight lang="php">
function get_enrolled_sql(context $context, $withcapability = '', $groupid = 0, $onlyactive = false)
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 get_enrolled_users(context $context, $withcapability = '', $groupid = 0, $userfields = 'u.*', $orderby = '', $limitfrom = 0, $limitnum = 0)
Line 76: Line 76:


For example you want to know who is able to summit assignment right now:
For example you want to know who is able to summit assignment right now:
<code php>
<syntaxhighlight lang="php">
$submissioncandidates = get_enrolled_users($modcontext, 'mod/assignment:submit');
$submissioncandidates = get_enrolled_users($modcontext, 'mod/assignment:submit');
</syntaxhighlight>
</syntaxhighlight>

Revision as of 13:36, 14 July 2021

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?

Enrolled users may fully participate in a course. Active user enrolment allows user to enter course. Only enrolled users may be group members. Grades are stored only for enrolled users.

Unenrolment

Unenrolment is irreversible operation that purges user participation information. Full unenrolment is suitable only if you do not need to preserve all course participation information including user grades.

Enrolment status

Instead of full unenrolment it is usually better to only suspend user enrolment. If there are other ways to enter the course (such guest access) it is recommended to remove user roles at the same time.

Activity participation

Activity developers decide the enrolment related behaviour of module.

There are some general guidelines:

  • Only users with active enrolments should receive notifications.
  • Activities should display enrolled users with some capability as participants.
  • By default only users with active enrolments should be displayed in reports.
  • There should be option to display all enrolled users including suspended enrolments.
  • For performance reasons invisible participation data should be purged on unenrolment.
  • Contributions visible by other participants should be kept after unenrolment (such as forum posts).

API functions

enrol_send_welcome_email_options()

Some enrol methods has the support for sending welcome emails to users. This method returns a list of all possible options for sending welcome email when the user enrol in a course and each option has a respective constant defined on enrollib.php:

define('ENROL_DO_NOT_SEND_EMAIL', 0); // Do not send the welcome email.
define('ENROL_SEND_EMAIL_FROM_COURSE_CONTACT', 1); // Send welcome email from course contact.
define('ENROL_SEND_EMAIL_FROM_KEY_HOLDER', 2); // Send welcome email from course key holder.
define('ENROL_SEND_EMAIL_FROM_NOREPLY', 3); // Send welcome email from no reply.

The main idea is to make send email options consistent across enrolment methods that support this feature. See an example below:

$sendoptions = enrol_send_welcome_email_options();
print_object($sendoptions);

The example above will output:

Array 
(
       [1] => 'No'
       [2] => 'From the course contact'
       [3] => 'From the key holder'
       [4] => 'From the no-reply address'
)

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');

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 enrolled users only.

See also