Commit 7d4c3458 authored by Mark Johnson's avatar Mark Johnson
Browse files

MDL-64886 enrol: Make enrolledusercount optional in enrol_get_users_courses

parent 07203d34
......@@ -280,6 +280,11 @@ class core_enrol_external extends external_api {
return new external_function_parameters(
array(
'userid' => new external_value(PARAM_INT, 'user id'),
'returnusercount' => new external_value(PARAM_BOOL,
'Include count of enrolled users for each course? This can add several seconds to the response time'
. ' if a user is on several large courses, so set this to false if the value will not be used to'
. ' improve performance.',
VALUE_DEFAULT, true),
)
);
}
......@@ -289,9 +294,10 @@ class core_enrol_external extends external_api {
* Please note the current user must be able to access the course, otherwise the course is not included.
*
* @param int $userid
* @param bool $returnusercount
* @return array of courses
*/
public static function get_users_courses($userid) {
public static function get_users_courses($userid, $returnusercount = true) {
global $CFG, $USER, $DB;
require_once($CFG->dirroot . '/course/lib.php');
......@@ -299,8 +305,10 @@ class core_enrol_external extends external_api {
// Do basic automatic PARAM checks on incoming data, using params description
// If any problems are found then exceptions are thrown with helpful error messages
$params = self::validate_parameters(self::get_users_courses_parameters(), array('userid'=>$userid));
$params = self::validate_parameters(self::get_users_courses_parameters(),
['userid' => $userid, 'returnusercount' => $returnusercount]);
$userid = $params['userid'];
$returnusercount = $params['returnusercount'];
$courses = enrol_get_users_courses($userid, true, '*');
$result = array();
......@@ -337,9 +345,11 @@ class core_enrol_external extends external_api {
continue;
}
list($enrolledsqlselect, $enrolledparams) = get_enrolled_sql($context);
$enrolledsql = "SELECT COUNT('x') FROM ($enrolledsqlselect) enrolleduserids";
$enrolledusercount = $DB->count_records_sql($enrolledsql, $enrolledparams);
if ($returnusercount) {
list($enrolledsqlselect, $enrolledparams) = get_enrolled_sql($context);
$enrolledsql = "SELECT COUNT('x') FROM ($enrolledsqlselect) enrolleduserids";
$enrolledusercount = $DB->count_records_sql($enrolledsql, $enrolledparams);
}
$displayname = external_format_string(get_course_display_name_for_list($course), $context->id);
list($course->summary, $course->summaryformat) =
......@@ -395,14 +405,13 @@ class core_enrol_external extends external_api {
);
}
$result[] = array(
$courseresult = [
'id' => $course->id,
'shortname' => $course->shortname,
'fullname' => $course->fullname,
'displayname' => $displayname,
'idnumber' => $course->idnumber,
'visible' => $course->visible,
'enrolledusercount' => $enrolledusercount,
'summary' => $course->summary,
'summaryformat' => $course->summaryformat,
'format' => $course->format,
......@@ -420,7 +429,11 @@ class core_enrol_external extends external_api {
'isfavourite' => isset($favouritecourseids[$course->id]),
'hidden' => $hidden,
'overviewfiles' => $overviewfiles,
);
];
if ($returnusercount) {
$courseresult['enrolledusercount'] = $enrolledusercount;
}
$result[] = $courseresult;
}
return $result;
......@@ -439,7 +452,8 @@ class core_enrol_external extends external_api {
'shortname' => new external_value(PARAM_RAW, 'short name of course'),
'fullname' => new external_value(PARAM_RAW, 'long name of course'),
'displayname' => new external_value(PARAM_TEXT, 'course display name for lists.', VALUE_OPTIONAL),
'enrolledusercount' => new external_value(PARAM_INT, 'Number of enrolled users in this course'),
'enrolledusercount' => new external_value(PARAM_INT, 'Number of enrolled users in this course',
VALUE_OPTIONAL),
'idnumber' => new external_value(PARAM_RAW, 'id number of course'),
'visible' => new external_value(PARAM_INT, '1 means visible, 0 means not yet visible course'),
'summary' => new external_value(PARAM_RAW, 'summary', VALUE_OPTIONAL),
......
......@@ -425,7 +425,7 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase {
$this->setUser($student);
// Call the external function.
$enrolledincourses = core_enrol_external::get_users_courses($student->id);
$enrolledincourses = core_enrol_external::get_users_courses($student->id, true);
// We need to execute the return values cleaning process to simulate the web service server.
$enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses);
......@@ -455,6 +455,7 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase {
$this->assertTrue($courseenrol['completionhascriteria']);
$this->assertTrue($courseenrol['hidden']);
$this->assertTrue($courseenrol['isfavourite']);
$this->assertEquals(2, $courseenrol['enrolledusercount']);
} else {
// Check language pack. Should be empty since an incorrect one was used when creating the course.
$this->assertEmpty($courseenrol['lang']);
......@@ -466,13 +467,21 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase {
$this->assertFalse($courseenrol['completionhascriteria']);
$this->assertFalse($courseenrol['hidden']);
$this->assertFalse($courseenrol['isfavourite']);
$this->assertEquals(1, $courseenrol['enrolledusercount']);
}
}
// Check that returnusercount works correctly.
$enrolledincourses = core_enrol_external::get_users_courses($student->id, false);
$enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses);
foreach ($enrolledincourses as $courseenrol) {
$this->assertFalse(isset($courseenrol['enrolledusercount']));
}
// Now check that admin users can see all the info.
$this->setAdminUser();
$enrolledincourses = core_enrol_external::get_users_courses($student->id);
$enrolledincourses = core_enrol_external::get_users_courses($student->id, true);
$enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses);
$this->assertEquals(2, count($enrolledincourses));
foreach ($enrolledincourses as $courseenrol) {
......@@ -493,7 +502,7 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase {
// Check other users can't see private info.
$this->setUser($otherstudent);
$enrolledincourses = core_enrol_external::get_users_courses($student->id);
$enrolledincourses = core_enrol_external::get_users_courses($student->id, true);
$enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses);
$this->assertEquals(1, count($enrolledincourses));
......@@ -502,7 +511,7 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase {
// Change some global profile visibility fields.
$CFG->hiddenuserfields = 'lastaccess';
$enrolledincourses = core_enrol_external::get_users_courses($student->id);
$enrolledincourses = core_enrol_external::get_users_courses($student->id, true);
$enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses);
$this->assertEquals(0, $enrolledincourses[0]['lastaccess']); // I can't see this, hidden by global setting.
......
......@@ -7,6 +7,8 @@ information provided here is intended especially for developers.
- users: List of user objects returned by the query.
- moreusers: True if there are still more users, otherwise is False.
- totalusers: Number users matching the search. (This element only exists if the function is called with $returnexactcount param set to true).
* enrolledusercount is now optional in the return value of get_users_courses() for performance reasons. This is controlled with the new
optional returnusercount parameter (default true).
=== 3.6 ===
......
Supports Markdown
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