MDL-64641 calendar: New WS core_calendar_get_allowed_event_types
authorJuan Leyva <juanleyvadelgado@gmail.com>
Wed, 3 Apr 2019 10:15:32 +0000 (12:15 +0200)
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

index e2d20d1..8d4a3a9 100644 (file)
@@ -28,6 +28,7 @@
 defined('MOODLE_INTERNAL') || die;
 
 require_once("$CFG->libdir/externallib.php");
+require_once($CFG->dirroot . '/calendar/lib.php');
 
 use \core_calendar\local\api as local_api;
 use \core_calendar\local\event\container as event_container;
@@ -79,8 +80,7 @@ class core_calendar_external extends external_api {
      * @since Moodle 2.5
      */
     public static function delete_calendar_events($events) {
-        global $CFG, $DB;
-        require_once($CFG->dirroot."/calendar/lib.php");
+        global $DB;
 
         // Parameter validation.
         $params = self::validate_parameters(self:: delete_calendar_events_parameters(), array('events' => $events));
@@ -173,8 +173,7 @@ class core_calendar_external extends external_api {
      * @since Moodle 2.5
      */
     public static function get_calendar_events($events = array(), $options = array()) {
-        global $SITE, $DB, $USER, $CFG;
-        require_once($CFG->dirroot."/calendar/lib.php");
+        global $SITE, $DB, $USER;
 
         // Parameter validation.
         $params = self::validate_parameters(self::get_calendar_events_parameters(), array('events' => $events, 'options' => $options));
@@ -426,9 +425,7 @@ class core_calendar_external extends external_api {
     public static function get_calendar_action_events_by_timesort($timesortfrom = 0, $timesortto = null,
                                                        $aftereventid = 0, $limitnum = 20, $limittononsuspendedevents = false,
                                                        $userid = null) {
-        global $CFG, $PAGE, $USER;
-
-        require_once($CFG->dirroot . '/calendar/lib.php');
+        global $PAGE, $USER;
 
         $params = self::validate_parameters(
             self::get_calendar_action_events_by_timesort_parameters(),
@@ -511,9 +508,7 @@ class core_calendar_external extends external_api {
     public static function get_calendar_action_events_by_course(
         $courseid, $timesortfrom = null, $timesortto = null, $aftereventid = 0, $limitnum = 20) {
 
-        global $CFG, $PAGE, $USER;
-
-        require_once($CFG->dirroot . '/calendar/lib.php');
+        global $PAGE, $USER;
 
         $user = null;
         $params = self::validate_parameters(
@@ -596,9 +591,7 @@ class core_calendar_external extends external_api {
     public static function get_calendar_action_events_by_courses(
         array $courseids, $timesortfrom = null, $timesortto = null, $limitnum = 10) {
 
-        global $CFG, $PAGE, $USER;
-
-        require_once($CFG->dirroot . '/calendar/lib.php');
+        global $PAGE, $USER;
 
         $user = null;
         $params = self::validate_parameters(
@@ -691,8 +684,7 @@ class core_calendar_external extends external_api {
      * @throws moodle_exception if user doesnt have the permission to create events.
      */
     public static function create_calendar_events($events) {
-        global $CFG, $DB, $USER;
-        require_once($CFG->dirroot."/calendar/lib.php");
+        global $DB, $USER;
 
         // Parameter validation.
         $params = self::validate_parameters(self::create_calendar_events_parameters(), array('events' => $events));
@@ -797,8 +789,7 @@ class core_calendar_external extends external_api {
      * @return array Array of event details
      */
     public static function get_calendar_event_by_id($eventid) {
-        global $CFG, $PAGE, $USER;
-        require_once($CFG->dirroot."/calendar/lib.php");
+        global $PAGE, $USER;
 
         $params = self::validate_parameters(self::get_calendar_event_by_id_parameters(), ['eventid' => $eventid]);
         $context = \context_user::instance($USER->id);
@@ -868,8 +859,7 @@ class core_calendar_external extends external_api {
      * @throws moodle_exception
      */
     public static function submit_create_update_form($formdata) {
-        global $CFG, $USER, $PAGE;
-        require_once($CFG->dirroot."/calendar/lib.php");
+        global $USER, $PAGE, $CFG;
         require_once($CFG->libdir."/filelib.php");
 
         // Parameter validation.
@@ -995,8 +985,7 @@ class core_calendar_external extends external_api {
      * @return  array
      */
     public static function get_calendar_monthly_view($year, $month, $courseid, $categoryid, $includenavigation, $mini) {
-        global $CFG, $DB, $USER, $PAGE;
-        require_once($CFG->dirroot."/calendar/lib.php");
+        global $DB, $USER, $PAGE;
 
         // Parameter validation.
         $params = self::validate_parameters(self::get_calendar_monthly_view_parameters(), [
@@ -1073,8 +1062,7 @@ class core_calendar_external extends external_api {
      * @return  array
      */
     public static function get_calendar_day_view($year, $month, $day, $courseid, $categoryid) {
-        global $CFG, $DB, $USER, $PAGE;
-        require_once($CFG->dirroot."/calendar/lib.php");
+        global $DB, $USER, $PAGE;
 
         // Parameter validation.
         $params = self::validate_parameters(self::get_calendar_day_view_parameters(), [
@@ -1214,8 +1202,7 @@ class core_calendar_external extends external_api {
      * @return  array
      */
     public static function get_calendar_upcoming_view($courseid, $categoryid) {
-        global $CFG, $DB, $USER, $PAGE;
-        require_once($CFG->dirroot."/calendar/lib.php");
+        global $DB, $USER, $PAGE;
 
         // Parameter validation.
         $params = self::validate_parameters(self::get_calendar_upcoming_view_parameters(), [
@@ -1318,4 +1305,64 @@ class core_calendar_external extends external_api {
             ]
         );
     }
+
+    /**
+     * Returns description of method parameters.
+     *
+     * @return external_function_parameters.
+     * @since  Moodle 3.7
+     */
+    public static function get_allowed_event_types_parameters() {
+        return new external_function_parameters(
+            [
+                'courseid' => new external_value(PARAM_INT, 'Course to check, empty for site.', VALUE_DEFAULT, 0),
+            ]
+        );
+    }
+
+    /**
+     * Get the type of events a user can create in the given course.
+     *
+     * @param int $courseid Course to check, empty for site.
+     * @return array The types allowed
+     * @throws moodle_exception
+     * @since  Moodle 3.7
+     */
+    public static function get_allowed_event_types($courseid = 0) {
+
+        $params = self::validate_parameters(self::get_allowed_event_types_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);
+
+        $allowedeventtypes = array_filter(calendar_get_allowed_event_types($params['courseid']));
+
+        return [
+            'allowedeventtypes' => array_keys($allowedeventtypes),
+            'warnings' => [],
+        ];
+    }
+
+    /**
+     * Returns description of method result value.
+     *
+     * @return external_description.
+     * @since  Moodle 3.7
+     */
+    public static function  get_allowed_event_types_returns() {
+
+        return new external_single_structure(
+            [
+                'allowedeventtypes' => new external_multiple_structure(
+                    new external_value(PARAM_NOTAGS, 'Allowed event types to be created in the given course.')
+                ),
+                'warnings' => new external_warnings(),
+            ]
+        );
+    }
 }
index a520af6..d3a7cf9 100644 (file)
@@ -2634,4 +2634,66 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
         $this->assertTrue($data['canmanagegroupentries']);
         $this->assertTrue($data['canmanageentries']);
     }
+
+    /**
+     * Test get_allowed_event_types for admins.
+     */
+    public function test_get_allowed_event_types_for_admins() {
+        global $CFG;
+        $this->resetAfterTest(true);
+        $this->setAdminUser();
+        $CFG->calendar_adminseesall = 1;
+        $data = external_api::clean_returnvalue(
+            core_calendar_external::get_allowed_event_types_returns(),
+            core_calendar_external::get_allowed_event_types()
+        );
+        $this->assertEquals(['user', 'site', 'course', 'category'], $data['allowedeventtypes']);
+    }
+    /**
+     * Test get_allowed_event_types for authenticated users.
+     */
+    public function test_get_allowed_event_types_for_authenticated_users() {
+        $this->resetAfterTest(true);
+        $this->setUser($this->getDataGenerator()->create_user());
+        $data = external_api::clean_returnvalue(
+            core_calendar_external::get_allowed_event_types_returns(),
+            core_calendar_external::get_allowed_event_types()
+        );
+        $this->assertEquals(['user'], $data['allowedeventtypes']);
+    }
+    /**
+     * Test get_allowed_event_types for student users.
+     */
+    public function test_get_allowed_event_types_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_allowed_event_types_returns(),
+            core_calendar_external::get_allowed_event_types($course->id)
+        );
+        $this->assertEquals(['user'], $data['allowedeventtypes']);
+    }
+    /**
+     * Test get_allowed_event_types for teacher users.
+     */
+    public function test_get_allowed_event_types_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_allowed_event_types_returns(),
+            core_calendar_external::get_allowed_event_types($course->id)
+        );
+        $this->assertEquals(['user', 'course', 'group'], $data['allowedeventtypes']);
+    }
 }
index 2e24cd2..206c2b9 100644 (file)
@@ -226,6 +226,14 @@ $functions = array(
         'type' => 'read',
         'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
     ),
+    'core_calendar_get_allowed_event_types' => array(
+        'classname' => 'core_calendar_external',
+        'methodname' => 'get_allowed_event_types',
+        'description' => 'Get the type of events a user can create in the given course.',
+        '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',