MDL-64641 calendar: New WS core_calendar_get_access_information
authorJuan Leyva <juanleyvadelgado@gmail.com>
Wed, 13 Feb 2019 10:47:57 +0000 (11:47 +0100)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Fri, 5 Apr 2019 07:56:39 +0000 (09:56 +0200)
calendar/externallib.php
calendar/tests/externallib_test.php
lib/db/services.php
version.php

index a942122..e2d20d1 100644 (file)
@@ -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(),
+            ]
+        );
+    }
 }
index 4692bbd..a520af6 100644 (file)
@@ -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']);
+    }
 }
index 3cb9079..2e24cd2 100644 (file)
@@ -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',
index 728235c..b698e24 100644 (file)
@@ -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.