MDL-57503 calendar: test for api function for action events by course
[moodle.git] / calendar / classes / local / api.php
CommitLineData
392d6a49
RW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Contains class containing the internal calendar API.
19 *
20 * @package core_calendar
21 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_calendar\local;
26
27defined('MOODLE_INTERNAL') || die();
28
e62cd85f
RW
29use core_calendar\local\event\exceptions\limit_invalid_parameter_exception;
30
392d6a49
RW
31/**
32 * Class containing the local calendar API.
33 *
34 * @package core_calendar
35 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class api {
39
40 /**
41 * Get a list of action events for the logged in user by the given
42 * timesort values.
43 *
44 * @param int|null $timesortfrom The start timesort value (inclusive)
45 * @param int|null $timesortto The end timesort value (inclusive)
46 * @param int|null $aftereventid Only return events after this one
47 * @param int $limitnum Limit results to this amount (between 1 and 50)
48 * @return array A list of action_event_interface objects
49 */
50 public static function get_action_events_by_timesort(
51 $timesortfrom = null,
52 $timesortto = null,
53 $aftereventid = null,
54 $limitnum = 20
55 ) {
56 global $USER;
57
58 if (is_null($timesortfrom) && is_null($timesortto)) {
59 throw new \moodle_exception("Must provide a timesort to and/or from value");
60 }
61
62 if ($limitnum < 1 || $limitnum > 50) {
63 throw new \moodle_exception("Limit must be between 1 and 50 (inclusive)");
64 }
65
66 $vault = \core_calendar\local\event\core_container::get_event_vault();
67
68 $afterevent = null;
69 if ($aftereventid && $event = $vault->get_event_by_id($aftereventid)) {
70 $afterevent = $event;
71 }
72
73 return $vault->get_action_events_by_timesort($USER, $timesortfrom, $timesortto, $afterevent, $limitnum);
74 }
e62cd85f
RW
75
76 /**
77 * Get a list of action events for the logged in user by the given
78 * course and timesort values.
79 *
80 * @param \stdClass $course The course the events must belong to
81 * @param int|null $timesortfrom The start timesort value (inclusive)
82 * @param int|null $timesortto The end timesort value (inclusive)
83 * @param int|null $aftereventid Only return events after this one
84 * @param int $limitnum Limit results to this amount (between 1 and 50)
85 * @return array A list of action_event_interface objects
86 */
87 public static function get_action_events_by_course(
88 $course,
89 $timesortfrom = null,
90 $timesortto = null,
91 $aftereventid = null,
92 $limitnum = 20
93 ) {
94 global $USER;
95
96 if ($limitnum < 1 || $limitnum > 50) {
97 throw new limit_invalid_parameter_exception(
98 "Limit must be between 1 and 50 (inclusive)");
99 }
100
101 $vault = \core_calendar\local\event\core_container::get_event_vault();
102
103 $afterevent = null;
104 if ($aftereventid && $event = $vault->get_event_by_id($aftereventid)) {
105 $afterevent = $event;
106 }
107
108 return $vault->get_action_events_by_course(
109 $USER, $course, $timesortfrom, $timesortto, $afterevent, $limitnum);
110 }
392d6a49 111}