From 2aad6b8261d287c36c39af00df8966bbd8063479 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Wed, 3 Apr 2019 12:15:32 +0200 Subject: [PATCH] MDL-64641 calendar: New WS core_calendar_get_allowed_event_types --- calendar/externallib.php | 97 +++++++++++++++++++++-------- calendar/tests/externallib_test.php | 62 ++++++++++++++++++ lib/db/services.php | 8 +++ 3 files changed, 142 insertions(+), 25 deletions(-) diff --git a/calendar/externallib.php b/calendar/externallib.php index e2d20d19f39..8d4a3a93984 100644 --- a/calendar/externallib.php +++ b/calendar/externallib.php @@ -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(), + ] + ); + } } diff --git a/calendar/tests/externallib_test.php b/calendar/tests/externallib_test.php index a520af66a51..d3a7cf9e779 100644 --- a/calendar/tests/externallib_test.php +++ b/calendar/tests/externallib_test.php @@ -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']); + } } diff --git a/lib/db/services.php b/lib/db/services.php index 2e24cd2e660..206c2b97ae8 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -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', -- 2.43.0