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,
};
});
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);
+ }
}
$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);
+ }
}
'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',