Commit 7af6528e authored by David Monllaó's avatar David Monllaó
Browse files

MDL-60340 calendar: New param to specify a user

parent ec819146
......@@ -119,6 +119,7 @@ class api {
* @param int|null $aftereventid Only return events after this one
* @param int $limitnum Limit results to this amount (between 1 and 50)
* @param bool $lmittononsuspendedevents Limit course events to courses the user is active in (not suspended).
* @param \stdClass|null $user The user id or false for $USER
* @return array A list of action_event_interface objects
* @throws \moodle_exception
*/
......@@ -127,10 +128,15 @@ class api {
$timesortto = null,
$aftereventid = null,
$limitnum = 20,
$limittononsuspendedevents = false
$limittononsuspendedevents = false,
?\stdClass $user = null
) {
global $USER;
if (!$user) {
$user = $USER;
}
if (is_null($timesortfrom) && is_null($timesortto)) {
throw new \moodle_exception("Must provide a timesort to and/or from value");
}
......@@ -139,6 +145,7 @@ class api {
throw new \moodle_exception("Limit must be between 1 and 50 (inclusive)");
}
\core_calendar\local\event\container::set_requesting_user($user->id);
$vault = \core_calendar\local\event\container::get_event_vault();
$afterevent = null;
......@@ -146,7 +153,7 @@ class api {
$afterevent = $event;
}
return $vault->get_action_events_by_timesort($USER, $timesortfrom, $timesortto, $afterevent, $limitnum,
return $vault->get_action_events_by_timesort($user, $timesortfrom, $timesortto, $afterevent, $limitnum,
$limittononsuspendedevents);
}
......
......@@ -406,7 +406,8 @@ class core_calendar_external extends external_api {
'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),
'limittononsuspendedevents' => new external_value(PARAM_BOOL,
'Limit the events to courses the user is not suspended in', VALUE_DEFAULT, false)
'Limit the events to courses the user is not suspended in', VALUE_DEFAULT, false),
'userid' => new external_value(PARAM_INT, 'The user id', VALUE_DEFAULT, null),
)
);
}
......@@ -419,15 +420,16 @@ class core_calendar_external extends external_api {
* @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
* @param null|int $userid The user id
* @return array
*/
public static function get_calendar_action_events_by_timesort($timesortfrom = 0, $timesortto = null,
$aftereventid = 0, $limitnum = 20, $limittononsuspendedevents = false) {
$aftereventid = 0, $limitnum = 20, $limittononsuspendedevents = false,
$userid = null) {
global $CFG, $PAGE, $USER;
require_once($CFG->dirroot . '/calendar/lib.php');
$user = null;
$params = self::validate_parameters(
self::get_calendar_action_events_by_timesort_parameters(),
[
......@@ -435,10 +437,17 @@ class core_calendar_external extends external_api {
'timesortto' => $timesortto,
'aftereventid' => $aftereventid,
'limitnum' => $limitnum,
'limittononsuspendedevents' => $limittononsuspendedevents
'limittononsuspendedevents' => $limittononsuspendedevents,
'userid' => $userid,
]
);
$context = \context_user::instance($USER->id);
if ($params['userid']) {
$user = \core_user::get_user($params['userid']);
} else {
$user = $USER;
}
$context = \context_user::instance($user->id);
self::validate_context($context);
if (empty($params['aftereventid'])) {
......@@ -451,7 +460,8 @@ class core_calendar_external extends external_api {
$params['timesortto'],
$params['aftereventid'],
$params['limitnum'],
$params['limittononsuspendedevents']
$params['limittononsuspendedevents'],
$user
);
$exportercache = new events_related_objects_cache($events);
......
......@@ -679,6 +679,23 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
$this->assertEmpty($result['events']);
$this->assertNull($result['firstid']);
$this->assertNull($result['lastid']);
// Requesting action events on behalf of another user.
$this->setAdminUser();
$result = core_calendar_external::get_calendar_action_events_by_timesort(5, null, 0, 20, false, $user->id);
$result = external_api::clean_returnvalue(
core_calendar_external::get_calendar_action_events_by_timesort_returns(),
$result
);
$events = $result['events'];
$this->assertCount(4, $events);
$this->assertEquals('Event 5', $events[0]['name']);
$this->assertEquals('Event 6', $events[1]['name']);
$this->assertEquals('Event 7', $events[2]['name']);
$this->assertEquals('Event 8', $events[3]['name']);
$this->assertEquals($event5->id, $result['firstid']);
$this->assertEquals($event8->id, $result['lastid']);
}
/**
......@@ -739,6 +756,24 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
$this->assertEmpty($result['events']);
$this->assertNull($result['firstid']);
$this->assertNull($result['lastid']);
// Requesting action events on behalf of another user.
$this->setAdminUser();
$result = core_calendar_external::get_calendar_action_events_by_timesort(null, 5, 0, 20, false, $user->id);
$result = external_api::clean_returnvalue(
core_calendar_external::get_calendar_action_events_by_timesort_returns(),
$result
);
$events = $result['events'];
$this->assertCount(4, $events);
$this->assertEquals('Event 1', $events[0]['name']);
$this->assertEquals('Event 2', $events[1]['name']);
$this->assertEquals('Event 3', $events[2]['name']);
$this->assertEquals('Event 4', $events[3]['name']);
$this->assertEquals($event1->id, $result['firstid']);
$this->assertEquals($event4->id, $result['lastid']);
}
/**
......
......@@ -125,6 +125,10 @@ class core_calendar_local_api_testcase extends advanced_testcase {
$result = \core_calendar\local\api::get_action_events_by_timesort(9);
$this->assertEmpty($result);
$this->setAdminUser();
$result = \core_calendar\local\api::get_action_events_by_timesort(5, null, null, 20, false, $user);
$this->assertCount(4, $result);
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment