Skip to content

Commit

Permalink
MDL-64641 calendar: New WS core_calendar_get_access_information
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Apr 5, 2019
1 parent ef38c5a commit a46980f
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 1 deletion.
61 changes: 61 additions & 0 deletions calendar/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1257,4 +1257,65 @@ public static function get_calendar_upcoming_view_parameters() {
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(),
]
);
}
}
82 changes: 82 additions & 0 deletions calendar/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2552,4 +2552,86 @@ public function test_get_calendar_events_hidden_category() {
$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']);
}
}
8 changes: 8 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@
'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',
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit a46980f

Please sign in to comment.