Commit e53bc6a1 authored by ilya's avatar ilya
Browse files

MDL-69869 enrol: Add ability to get suspended users through

                 core_enrol_get_enrolled_users webservice.
parent 7fa836cf
......@@ -693,7 +693,13 @@ class core_enrol_external extends external_api {
isn\'t defined, returns all the viewable users.
This option requires \'moodle/site:accessallgroups\' on the course context if the
user doesn\'t belong to the group.
* onlyactive (integer) return only users with active enrolments and matching time restrictions. This option requires \'moodle/course:enrolreview\' on the course context.
* onlyactive (integer) return only users with active enrolments and matching time restrictions.
This option requires \'moodle/course:enrolreview\' on the course context.
Please note that this option can\'t
be used together with onlysuspended (only one can be active).
* onlysuspended (integer) return only suspended users. This option requires
\'moodle/course:enrolreview\' on the course context. Please note that this option can\'t
be used together with onlyactive (only one can be active).
* userfields (\'string, string, ...\') return only the values of these user fields.
* limitfrom (integer) sql limit from.
* limitnumber (integer) maximum number of returned users.
......@@ -730,6 +736,7 @@ class core_enrol_external extends external_api {
$withcapability = '';
$groupid = 0;
$onlyactive = false;
$onlysuspended = false;
$userfields = array();
$limitfrom = 0;
$limitnumber = 0;
......@@ -747,6 +754,9 @@ class core_enrol_external extends external_api {
case 'onlyactive':
$onlyactive = !empty($option['value']);
break;
case 'onlysuspended':
$onlysuspended = !empty($option['value']);
break;
case 'userfields':
$thefields = explode(',', $option['value']);
foreach ($thefields as $f) {
......@@ -809,11 +819,12 @@ class core_enrol_external extends external_api {
require_capability('moodle/site:accessallgroups', $coursecontext);
}
// to overwrite this option, you need course:enrolereview permission
if ($onlyactive) {
if ($onlyactive || $onlysuspended) {
require_capability('moodle/course:enrolreview', $coursecontext);
}
list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive,
$onlysuspended);
$ctxselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ctxjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
$enrolledparams['contextlevel'] = CONTEXT_USER;
......
......@@ -355,6 +355,95 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase {
}
}
/**
* Verify get_enrolled_users() returned users according to their status.
*/
public function test_get_enrolled_users_active_suspended() {
global $USER;
$this->resetAfterTest();
// Create the course and the users.
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
$user0 = $this->getDataGenerator()->create_user(array('username' => 'user0active'));
$user1 = $this->getDataGenerator()->create_user(array('username' => 'user1active'));
$user2 = $this->getDataGenerator()->create_user(array('username' => 'user2active'));
$user2su = $this->getDataGenerator()->create_user(array('username' => 'user2suspended')); // Suspended user.
$user3 = $this->getDataGenerator()->create_user(array('username' => 'user3active'));
$user3su = $this->getDataGenerator()->create_user(array('username' => 'user3suspended')); // Suspended user.
// Enrol the users in the course.
$this->getDataGenerator()->enrol_user($user0->id, $course->id);
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id);
$this->getDataGenerator()->enrol_user($user2su->id, $course->id, null, 'manual', 0, 0, ENROL_USER_SUSPENDED);
$this->getDataGenerator()->enrol_user($user3->id, $course->id);
$this->getDataGenerator()->enrol_user($user3su->id, $course->id, null, 'manual', 0, 0, ENROL_USER_SUSPENDED);
// Create a role to add the allowedcaps. Users will have this role assigned.
$roleid = $this->getDataGenerator()->create_role();
// Allow the specified capabilities.
assign_capability('moodle/course:enrolreview', CAP_ALLOW, $roleid, $coursecontext);
assign_capability('moodle/user:viewalldetails', CAP_ALLOW, $roleid, $coursecontext);
// Switch to the user and assign the role.
$this->setUser($user0);
role_assign($roleid, $USER->id, $coursecontext);
// Suspended users.
$options = array(
array('name' => 'onlysuspended', 'value' => true),
array('name' => 'userfields', 'value' => 'id,username')
);
$suspendedusers = core_enrol_external::get_enrolled_users($course->id, $options);
// We need to execute the return values cleaning process to simulate the web service server.
$suspendedusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $suspendedusers);
$this->assertCount(2, $suspendedusers);
foreach ($suspendedusers as $suspendeduser) {
$this->assertStringContainsString('suspended', $suspendeduser['username']);
}
// Active users.
$options = array(
array('name' => 'onlyactive', 'value' => true),
array('name' => 'userfields', 'value' => 'id,username')
);
$activeusers = core_enrol_external::get_enrolled_users($course->id, $options);
// We need to execute the return values cleaning process to simulate the web service server.
$activeusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $activeusers);
$this->assertCount(4, $activeusers);
foreach ($activeusers as $activeuser) {
$this->assertStringContainsString('active', $activeuser['username']);
}
// All enrolled users.
$options = array(
array('name' => 'userfields', 'value' => 'id,username')
);
$allusers = core_enrol_external::get_enrolled_users($course->id, $options);
// We need to execute the return values cleaning process to simulate the web service server.
$allusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $allusers);
$this->assertCount(6, $allusers);
// Active and suspended. Test exception is thrown.
$options = [
['name' => 'onlyactive', 'value' => true],
['name' => 'onlysuspended', 'value' => true],
['name' => 'userfields', 'value' => 'id,username']
];
$this->expectException('coding_exception');
$message = 'Coding error detected, it must be fixed by a programmer: Both onlyactive ' .
'and onlysuspended are set, this is probably not what you want!';
$this->expectExceptionMessage($message);
core_enrol_external::get_enrolled_users($course->id, $options);
}
/**
* Test get_users_courses
*/
......
This files describes API changes in /enrol/* - plugins,
information provided here is intended especially for developers.
=== 3.11 ===
* Added onlysuspended option to core_enrol_get_enrolled_users webservice to retrieve only suspended users.
=== 3.8 ===
* Function enrol_manual_plugin::enrol_cohort now return the number of enrolled cohort users.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment