From 60774b28c3435f37ff5088b9211b99740cae4fc3 Mon Sep 17 00:00:00 2001 From: Ryan Wyllie Date: Tue, 28 Feb 2017 03:25:23 +0000 Subject: [PATCH] MDL-57503 calendar: external function get action events by course Added an external function to get action events by course and timesort. Part of MDL-55611 epic. --- .../external/events_related_objects_cache.php | 11 ++- calendar/externallib.php | 86 +++++++++++++++++++ lib/db/services.php | 9 ++ 3 files changed, 105 insertions(+), 1 deletion(-) diff --git a/calendar/classes/external/events_related_objects_cache.php b/calendar/classes/external/events_related_objects_cache.php index 894c6fb16b9..ca9f97fba69 100644 --- a/calendar/classes/external/events_related_objects_cache.php +++ b/calendar/classes/external/events_related_objects_cache.php @@ -67,9 +67,18 @@ class events_related_objects_cache { * Constructor. * * @param array $event Array of event_interface events + * @param array $courses Array of courses to populate the cache with */ - public function __construct(array $events) { + public function __construct(array $events, array $courses = null) { $this->events = $events; + + if (!is_null($courses)) { + $this->courses = []; + + foreach ($courses as $course) { + $this->courses[$course->id] = $course; + } + } } /** diff --git a/calendar/externallib.php b/calendar/externallib.php index 20638f43813..94ae949bdc6 100644 --- a/calendar/externallib.php +++ b/calendar/externallib.php @@ -31,6 +31,7 @@ require_once("$CFG->libdir/externallib.php"); use \core_calendar\local\api as local_api; use \core_calendar\external\events_exporter; +use \core_calendar\external\events_grouped_by_course_exporter; use \core_calendar\external\events_related_objects_cache; /** @@ -383,6 +384,91 @@ class core_calendar_external extends external_api { return events_exporter::get_read_structure(); } + /** + * Returns description of method parameters. + * + * @return external_function_parameters + */ + public static function get_calendar_action_events_by_course_parameters() { + return new external_function_parameters( + array( + 'courseid' => new external_value(PARAM_INT, 'Course id'), + 'timesortfrom' => new external_value(PARAM_INT, 'Time sort from', VALUE_DEFAULT, null), + 'timesortto' => new external_value(PARAM_INT, 'Time sort to', VALUE_DEFAULT, null), + 'aftereventid' => new external_value(PARAM_INT, 'The last seen event id', VALUE_DEFAULT, 0), + 'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 20) + ) + ); + } + + /** + * Get calendar action events for the given course. + * + * @since Moodle 3.3 + * @param int $courseid Only events in this course + * @param null|int $timesortfrom Events after this time (inclusive) + * @param null|int $timesortto Events before this time (inclusive) + * @param null|int $aftereventid Get events with ids greater than this one + * @param int $limitnum Limit the number of results to this value + * @return array + */ + 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'); + + $user = null; + $params = self::validate_parameters( + self::get_calendar_action_events_by_course_parameters(), + [ + 'courseid' => $courseid, + 'timesortfrom' => $timesortfrom, + 'timesortto' => $timesortto, + 'aftereventid' => $aftereventid, + 'limitnum' => $limitnum, + ] + ); + $context = \context_user::instance($USER->id); + self::validate_context($context); + + if (empty($params['aftereventid'])) { + $params['aftereventid'] = null; + } + + $courses = enrol_get_my_courses('*', 'visible DESC,sortorder ASC', 0, [$courseid]); + $courses = array_values($courses); + + if (empty($courses)) { + return []; + } + + $course = $courses[0]; + $renderer = $PAGE->get_renderer('core_calendar'); + $events = local_api::get_action_events_by_course( + $course, + $params['timesortfrom'], + $params['timesortto'], + $params['aftereventid'], + $params['limitnum'] + ); + + $exportercache = new events_related_objects_cache($events, $courses); + $exporter = new events_exporter($events, ['cache' => $exportercache]); + + return $exporter->export($renderer); + } + + /** + * Returns description of method result value. + * + * @return external_description + */ + public static function get_calendar_action_events_by_course_returns() { + return events_exporter::get_read_structure(); + } + /** * Returns description of method parameters. * diff --git a/lib/db/services.php b/lib/db/services.php index c7dbb155395..73176c6a835 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -84,6 +84,15 @@ $functions = array( 'capabilities' => 'moodle/calendar:manageentries, moodle/calendar:manageownentries, moodle/calendar:managegroupentries', 'ajax' => true, ), + 'core_calendar_get_action_events_by_course' => array( + 'classname' => 'core_calendar_external', + 'methodname' => 'get_calendar_action_events_by_course', + 'description' => 'Get calendar action events by course', + 'classpath' => 'calendar/externallib.php', + 'type' => 'read', + 'capabilities' => 'moodle/calendar:manageentries, moodle/calendar:manageownentries, moodle/calendar:managegroupentries', + 'ajax' => true, + ), 'core_cohort_add_cohort_members' => array( 'classname' => 'core_cohort_external', 'methodname' => 'add_cohort_members', -- 2.43.0