diff --git a/course/externallib.php b/course/externallib.php index 2af1e72a154..d39f1b7c534 100644 --- a/course/externallib.php +++ b/course/externallib.php @@ -4177,22 +4177,28 @@ class core_course_external extends external_api { * @param int $groupid Group id from which the users will be obtained * @param bool $onlyactive Whether to return only the active enrolled users or all enrolled users in the course. * @return array List of users - * @throws invalid_parameter_exception */ public static function get_enrolled_users_by_cmid(int $cmid, int $groupid = 0, bool $onlyactive = false) { - global $PAGE; + global $PAGE; + $warnings = []; - self::validate_parameters(self::get_enrolled_users_by_cmid_parameters(), [ - 'cmid' => $cmid, - 'groupid' => $groupid, - 'onlyactive' => $onlyactive, + [ + 'cmid' => $cmid, + 'groupid' => $groupid, + 'onlyactive' => $onlyactive, + ] = self::validate_parameters(self::get_enrolled_users_by_cmid_parameters(), [ + 'cmid' => $cmid, + 'groupid' => $groupid, + 'onlyactive' => $onlyactive, ]); list($course, $cm) = get_course_and_cm_from_cmid($cmid); $coursecontext = context_course::instance($course->id); self::validate_context($coursecontext); + course_require_view_participants($coursecontext); + $enrolledusers = get_enrolled_users($coursecontext, '', $groupid, 'u.*', null, 0, 0, $onlyactive); $users = array_map(function ($user) use ($PAGE) { diff --git a/course/tests/externallib_test.php b/course/tests/externallib_test.php index 47fbc00ff2c..d49801ead09 100644 --- a/course/tests/externallib_test.php +++ b/course/tests/externallib_test.php @@ -3742,6 +3742,12 @@ final class externallib_test extends externallib_advanced_testcase { $this->assertEquals(2, count($users['users'])); $this->assertEquals($expectedusers, $users); + + // Prohibit the capability for viewing course participants. + $this->unassignUserCapability('moodle/course:viewparticipants', null, null, $course1->id); + $this->expectException(required_capability_exception::class); + $this->expectExceptionMessage('Sorry, but you do not currently have permissions to do that (View participants)'); + core_course_external::get_enrolled_users_by_cmid($forum1->cmid); } /**