MDL-66077 course: Add WS to get list of users in a cmid

Part of MDL-66074
This commit is contained in:
Mathew May 2019-10-05 11:33:05 +08:00
parent af9ca2a658
commit 06e50afd5e
6 changed files with 158 additions and 3 deletions

View File

@ -1,2 +1,2 @@
define ("core_course/repository",["jquery","core/ajax"],function(a,b){return{getEnrolledCoursesByTimelineClassification:function getEnrolledCoursesByTimelineClassification(a,c,d,e){var f={classification:a};if("undefined"!=typeof c){f.limit=c}if("undefined"!=typeof d){f.offset=d}if("undefined"!=typeof e){f.sort=e}return b.call([{methodname:"core_course_get_enrolled_courses_by_timeline_classification",args:f}])[0]},getLastAccessedCourses:function getLastAccessedCourses(a,c,d,e){var f={};if("undefined"!=typeof a){f.userid=a}if("undefined"!=typeof c){f.limit=c}if("undefined"!=typeof d){f.offset=d}if("undefined"!=typeof e){f.sort=e}return b.call([{methodname:"core_course_get_recent_courses",args:f}])[0]}}});
define ("core_course/repository",["jquery","core/ajax"],function(a,b){return{getEnrolledCoursesByTimelineClassification:function getEnrolledCoursesByTimelineClassification(a,c,d,e){var f={classification:a};if("undefined"!=typeof c){f.limit=c}if("undefined"!=typeof d){f.offset=d}if("undefined"!=typeof e){f.sort=e}return b.call([{methodname:"core_course_get_enrolled_courses_by_timeline_classification",args:f}])[0]},getLastAccessedCourses:function getLastAccessedCourses(a,c,d,e){var f={};if("undefined"!=typeof a){f.userid=a}if("undefined"!=typeof c){f.limit=c}if("undefined"!=typeof d){f.offset=d}if("undefined"!=typeof e){f.sort=e}return b.call([{methodname:"core_course_get_recent_courses",args:f}])[0]},getUsersFromCourseModuleID:function getEnrolledUsersFromCourseModuleID(a){return b.call([{methodname:"core_course_get_enrolled_users_by_cmid",args:{cmid:a}}])[0]}}});
//# sourceMappingURL=repository.min.js.map

File diff suppressed because one or more lines are too long

View File

@ -94,8 +94,26 @@ define(['jquery', 'core/ajax'], function($, Ajax) {
return Ajax.call([request])[0];
};
/**
* Get the list of users enrolled in this cmid.
*
* @param {Number} cmid Course Module from which the users will be obtained
* @returns {Promise} Promise containing a list of users
*/
var getEnrolledUsersFromCourseModuleID = function(cmid) {
var request = {
methodname: 'core_course_get_enrolled_users_by_cmid',
args: {
cmid: cmid,
},
};
return Ajax.call([request])[0];
};
return {
getEnrolledCoursesByTimelineClassification: getEnrolledCoursesByTimelineClassification,
getLastAccessedCourses: getLastAccessedCourses
getLastAccessedCourses: getLastAccessedCourses,
getUsersFromCourseModuleID: getEnrolledUsersFromCourseModuleID,
};
});

View File

@ -4053,4 +4053,82 @@ class core_course_external extends external_api {
public static function get_recent_courses_returns() {
return new external_multiple_structure(course_summary_exporter::get_read_structure(), 'Courses');
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
*/
public static function get_enrolled_users_by_cmid_parameters() {
return new external_function_parameters([
'cmid' => new external_value(PARAM_INT, 'id of the course module', VALUE_REQUIRED),
]);
}
/**
* Get all users in a course for a given cmid.
*
* @param int $cmid Course Module id from which the users will be obtained
* @return array List of users
* @throws invalid_parameter_exception
*/
public static function get_enrolled_users_by_cmid(int $cmid) {
$warnings = [];
[
'cmid' => $cmid,
] = self::validate_parameters(self::get_enrolled_users_by_cmid_parameters(), [
'cmid' => $cmid,
]);
list($course, $cm) = get_course_and_cm_from_cmid($cmid);
$coursecontext = context_course::instance($course->id);
self::validate_context($coursecontext);
$enrolledusers = get_enrolled_users($coursecontext);
$users = array_map(function ($user) {
$user->fullname = fullname($user);
return $user;
}, $enrolledusers);
sort($users);
return [
'users' => $users,
'warnings' => $warnings,
];
}
/**
* Returns description of method result value
*
* @return external_description
*/
public static function get_enrolled_users_by_cmid_returns() {
return new external_single_structure([
'users' => new external_multiple_structure(self::user_description()),
'warnings' => new external_warnings(),
]);
}
/**
* Create user return value description.
*
* @return single_structure_description
*/
public static function user_description() {
$userfields = array(
'id' => new external_value(core_user::get_property_type('id'), 'ID of the user'),
'fullname' => new external_value(PARAM_TEXT, 'The full name of the user', VALUE_OPTIONAL),
'firstname' => new external_value(
core_user::get_property_type('firstname'),
'The first name(s) of the user',
VALUE_OPTIONAL),
'lastname' => new external_value(
core_user::get_property_type('lastname'),
'The family name of the user',
VALUE_OPTIONAL),
);
return new external_single_structure($userfields);
}
}

View File

@ -2988,4 +2988,55 @@ class core_course_externallib_testcase extends externallib_advanced_testcase {
$this->assertCount(1, $result);
$this->assertEquals($courses[0]->id, array_shift($result)->id);
}
/**
* Test get enrolled users by cmid function.
*/
public function test_get_enrolled_users_by_cmid() {
$this->resetAfterTest(true);
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// Set the first created user to the test user.
self::setUser($user1);
// Create course to add the module.
$course1 = self::getDataGenerator()->create_course();
// Forum with tracking off.
$record = new stdClass();
$record->course = $course1->id;
$forum1 = self::getDataGenerator()->create_module('forum', $record);
// Following lines enrol and assign default role id to the users.
$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
// Create what we expect to be returned when querying the course module.
$expectedusers = array(
'users' => array(),
'warnings' => array(),
);
$expectedusers['users'][0] = [
'id' => $user1->id,
'fullname' => fullname($user1),
'firstname' => $user1->firstname,
'lastname' => $user1->lastname,
];
$expectedusers['users'][1] = [
'id' => $user2->id,
'fullname' => fullname($user2),
'firstname' => $user2->firstname,
'lastname' => $user2->lastname,
];
// Test getting the users in a given context.
$users = core_course_external::get_enrolled_users_by_cmid($forum1->cmid);
$users = external_api::clean_returnvalue(core_course_external::get_enrolled_users_by_cmid_returns(), $users);
$this->assertEquals(2, count($users['users']));
$this->assertEquals($expectedusers, $users);
}
}

View File

@ -613,6 +613,14 @@ $functions = array(
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_course_get_enrolled_users_by_cmid' => array(
'classname' => 'core_course_external',
'methodname' => 'get_enrolled_users_by_cmid',
'classpath' => 'course/externallib.php',
'description' => 'List users bycourse module id.',
'type' => 'read',
'ajax' => true,
),
'core_enrol_get_course_enrolment_methods' => array(
'classname' => 'core_enrol_external',
'methodname' => 'get_course_enrolment_methods',