mirror of
https://github.com/moodle/moodle.git
synced 2025-04-16 14:02:32 +02:00
MDL-64641 calendar: New WS core_calendar_get_access_information
This commit is contained in:
parent
ef38c5a531
commit
a46980fa79
@ -1257,4 +1257,65 @@ class core_calendar_external extends external_api {
|
||||
public static function get_calendar_upcoming_view_returns() {
|
||||
return \core_calendar\external\calendar_upcoming_exporter::get_read_structure();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns description of method parameters.
|
||||
*
|
||||
* @return external_function_parameters.
|
||||
* @since Moodle 3.7
|
||||
*/
|
||||
public static function get_calendar_access_information_parameters() {
|
||||
return new external_function_parameters(
|
||||
[
|
||||
'courseid' => new external_value(PARAM_INT, 'Course to check, empty for site calendar events.', VALUE_DEFAULT, 0),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function to retrieve some permissions information for the given course calendar.
|
||||
*
|
||||
* @param int $courseid Course to check, empty for site.
|
||||
* @return array The access information
|
||||
* @throws moodle_exception
|
||||
* @since Moodle 3.7
|
||||
*/
|
||||
public static function get_calendar_access_information($courseid = 0) {
|
||||
|
||||
$params = self::validate_parameters(self::get_calendar_access_information_parameters(), ['courseid' => $courseid]);
|
||||
|
||||
if (empty($params['courseid']) || $params['courseid'] == SITEID) {
|
||||
$context = \context_system::instance();
|
||||
} else {
|
||||
$context = \context_course::instance($params['courseid']);
|
||||
}
|
||||
|
||||
self::validate_context($context);
|
||||
|
||||
return [
|
||||
'canmanageentries' => has_capability('moodle/calendar:manageentries', $context),
|
||||
'canmanageownentries' => has_capability('moodle/calendar:manageownentries', $context),
|
||||
'canmanagegroupentries' => has_capability('moodle/calendar:managegroupentries', $context),
|
||||
'warnings' => [],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value.
|
||||
*
|
||||
* @return external_description.
|
||||
* @since Moodle 3.7
|
||||
*/
|
||||
public static function get_calendar_access_information_returns() {
|
||||
|
||||
return new external_single_structure(
|
||||
[
|
||||
'canmanageentries' => new external_value(PARAM_BOOL, 'Whether the user can manage entries.'),
|
||||
'canmanageownentries' => new external_value(PARAM_BOOL, 'Whether the user can manage its own entries.'),
|
||||
'canmanagegroupentries' => new external_value(PARAM_BOOL, 'Whether the user can manage group entries.'),
|
||||
'warnings' => new external_warnings(),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -2552,4 +2552,86 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
|
||||
$this->assertCount(0, $data['events']);
|
||||
$this->assertEquals('nopermissions', $data['warnings'][0]['warningcode']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_calendar_access_information for admins.
|
||||
*/
|
||||
public function test_get_calendar_access_information_for_admins() {
|
||||
global $CFG;
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
|
||||
$CFG->calendar_adminseesall = 1;
|
||||
|
||||
$data = external_api::clean_returnvalue(
|
||||
core_calendar_external::get_calendar_access_information_returns(),
|
||||
core_calendar_external::get_calendar_access_information()
|
||||
);
|
||||
$this->assertTrue($data['canmanageownentries']);
|
||||
$this->assertTrue($data['canmanagegroupentries']);
|
||||
$this->assertTrue($data['canmanageentries']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_calendar_access_information for authenticated users.
|
||||
*/
|
||||
public function test_get_calendar_access_information_for_authenticated_users() {
|
||||
$this->resetAfterTest(true);
|
||||
$this->setUser($this->getDataGenerator()->create_user());
|
||||
|
||||
$data = external_api::clean_returnvalue(
|
||||
core_calendar_external::get_calendar_access_information_returns(),
|
||||
core_calendar_external::get_calendar_access_information()
|
||||
);
|
||||
$this->assertTrue($data['canmanageownentries']);
|
||||
$this->assertFalse($data['canmanagegroupentries']);
|
||||
$this->assertFalse($data['canmanageentries']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_calendar_access_information for student users.
|
||||
*/
|
||||
public function test_get_calendar_access_information_for_student_users() {
|
||||
global $DB;
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$role = $DB->get_record('role', array('shortname' => 'student'));
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
|
||||
|
||||
$this->setUser($user);
|
||||
|
||||
$data = external_api::clean_returnvalue(
|
||||
core_calendar_external::get_calendar_access_information_returns(),
|
||||
core_calendar_external::get_calendar_access_information($course->id)
|
||||
);
|
||||
$this->assertTrue($data['canmanageownentries']);
|
||||
$this->assertFalse($data['canmanagegroupentries']);
|
||||
$this->assertFalse($data['canmanageentries']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_calendar_access_information for teacher users.
|
||||
*/
|
||||
public function test_get_calendar_access_information_for_teacher_users() {
|
||||
global $DB;
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$course = $this->getDataGenerator()->create_course(['groupmode' => 1]);
|
||||
$role = $DB->get_record('role', array('shortname' => 'editingteacher'));
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
|
||||
$this->getDataGenerator()->create_group(['courseid' => $course->id]);
|
||||
|
||||
$this->setUser($user);
|
||||
|
||||
$data = external_api::clean_returnvalue(
|
||||
core_calendar_external::get_calendar_access_information_returns(),
|
||||
core_calendar_external::get_calendar_access_information($course->id)
|
||||
);
|
||||
$this->assertTrue($data['canmanageownentries']);
|
||||
$this->assertTrue($data['canmanagegroupentries']);
|
||||
$this->assertTrue($data['canmanageentries']);
|
||||
}
|
||||
}
|
||||
|
@ -218,6 +218,14 @@ $functions = array(
|
||||
'ajax' => true,
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
'core_calendar_get_calendar_access_information' => array(
|
||||
'classname' => 'core_calendar_external',
|
||||
'methodname' => 'get_calendar_access_information',
|
||||
'description' => 'Convenience function to retrieve some permissions/access information for the given course calendar.',
|
||||
'classpath' => 'calendar/externallib.php',
|
||||
'type' => 'read',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
'core_cohort_add_cohort_members' => array(
|
||||
'classname' => 'core_cohort_external',
|
||||
'methodname' => 'add_cohort_members',
|
||||
|
@ -29,7 +29,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$version = 2019040200.00; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
$version = 2019040200.01; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
// RR = release increments - 00 in DEV branches.
|
||||
// .XX = incremental changes.
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user