mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
MDL-57503 calendar: add api function for action events by course
Add get_action_events_by_course to calendar api. Part of MDL-55611 epic.
This commit is contained in:
parent
1ef06b4335
commit
e62cd85fc5
@ -2348,4 +2348,31 @@ class api {
|
||||
return $mapper->from_event_to_legacy_event($event);
|
||||
}, $events);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of action events for the logged in user by the given
|
||||
* course and timesort values.
|
||||
*
|
||||
* @param \stdClass $course The course the events must belong to
|
||||
* @param int|null $timesortfrom The start timesort value (inclusive)
|
||||
* @param int|null $timesortto The end timesort value (inclusive)
|
||||
* @param int|null $aftereventid Only return events after this one
|
||||
* @param int $limitnum Limit results to this amount (between 1 and 50)
|
||||
* @return array A list of action_event_interface objects
|
||||
*/
|
||||
public static function get_action_events_by_course(
|
||||
$course,
|
||||
$timesortfrom = null,
|
||||
$timesortto = null,
|
||||
$aftereventid = null,
|
||||
$limitnum = 20
|
||||
) {
|
||||
$mapper = \core_calendar\local\event\core_container::get_event_mapper();
|
||||
$events = local_api::get_action_events_by_course(
|
||||
$course, $timesortfrom, $timesortto, $aftereventid, $limitnum);
|
||||
|
||||
return array_map(function($event) use ($mapper) {
|
||||
return $mapper->from_event_to_legacy_event($event);
|
||||
}, $events);
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,8 @@ namespace core_calendar\local;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use core_calendar\local\event\exceptions\limit_invalid_parameter_exception;
|
||||
|
||||
/**
|
||||
* Class containing the local calendar API.
|
||||
*
|
||||
@ -70,4 +72,40 @@ class api {
|
||||
|
||||
return $vault->get_action_events_by_timesort($USER, $timesortfrom, $timesortto, $afterevent, $limitnum);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of action events for the logged in user by the given
|
||||
* course and timesort values.
|
||||
*
|
||||
* @param \stdClass $course The course the events must belong to
|
||||
* @param int|null $timesortfrom The start timesort value (inclusive)
|
||||
* @param int|null $timesortto The end timesort value (inclusive)
|
||||
* @param int|null $aftereventid Only return events after this one
|
||||
* @param int $limitnum Limit results to this amount (between 1 and 50)
|
||||
* @return array A list of action_event_interface objects
|
||||
*/
|
||||
public static function get_action_events_by_course(
|
||||
$course,
|
||||
$timesortfrom = null,
|
||||
$timesortto = null,
|
||||
$aftereventid = null,
|
||||
$limitnum = 20
|
||||
) {
|
||||
global $USER;
|
||||
|
||||
if ($limitnum < 1 || $limitnum > 50) {
|
||||
throw new limit_invalid_parameter_exception(
|
||||
"Limit must be between 1 and 50 (inclusive)");
|
||||
}
|
||||
|
||||
$vault = \core_calendar\local\event\core_container::get_event_vault();
|
||||
|
||||
$afterevent = null;
|
||||
if ($aftereventid && $event = $vault->get_event_by_id($aftereventid)) {
|
||||
$afterevent = $event;
|
||||
}
|
||||
|
||||
return $vault->get_action_events_by_course(
|
||||
$USER, $course, $timesortfrom, $timesortto, $afterevent, $limitnum);
|
||||
}
|
||||
}
|
||||
|
@ -147,6 +147,83 @@ class event_vault implements event_vault_interface {
|
||||
return $events;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an array of events for the given user filtered by the course and time constraints.
|
||||
*
|
||||
* @param \stdClass $user The user for whom the events belong
|
||||
* @param array $courses The courses to filter by
|
||||
* @param int|null $timesortfrom Events with timesort from this value (inclusive)
|
||||
* @param int|null $timesortto Events with timesort until this value (inclusive)
|
||||
* @param event_interface|null $afterevent Only return events after this one
|
||||
* @param int $limitnum Return at most this number of events
|
||||
* @return action_event_interface
|
||||
*/
|
||||
public function get_action_events_by_course(
|
||||
\stdClass $user,
|
||||
\stdClass $course,
|
||||
int $timesortfrom = null,
|
||||
int $timesortto = null,
|
||||
event_interface $afterevent = null,
|
||||
int $limitnum = 20
|
||||
) {
|
||||
global $DB;
|
||||
|
||||
if ($limitnum < 1 || $limitnum > 50) {
|
||||
throw new limit_invalid_parameter_exception("Limit must be between 1 and 50 (inclusive)");
|
||||
}
|
||||
|
||||
$params = [
|
||||
'type' => CALENDAR_EVENT_TYPE_ACTION,
|
||||
'courseid' => $course->id,
|
||||
];
|
||||
$where = [
|
||||
'type = :type',
|
||||
'courseid = :courseid',
|
||||
];
|
||||
|
||||
if ($timesortfrom) {
|
||||
$where[] = 'timesort >= :timesortfrom';
|
||||
$params['timesortfrom'] = $timesortfrom;
|
||||
}
|
||||
|
||||
if ($timesortto) {
|
||||
$where[] = 'timesort <= :timesortto';
|
||||
$params['timesortto'] = $timesortto;
|
||||
}
|
||||
|
||||
if (!is_null($afterevent)) {
|
||||
$where[] = 'id > :id';
|
||||
$params['id'] = $afterevent->get_id();
|
||||
}
|
||||
|
||||
$wheresql = implode(' AND ', $where);
|
||||
$sql = sprintf("SELECT * FROM {event} WHERE %s ORDER BY timesort ASC, id ASC", $wheresql);
|
||||
|
||||
$offset = 0;
|
||||
$events = [];
|
||||
// We need to continue to pull records from the database until we reach
|
||||
// the requested amount of events because not all records in the database
|
||||
// will be visible for the current user.
|
||||
while ($records = array_values($DB->get_records_sql($sql, $params, $offset, $limitnum))) {
|
||||
foreach ($records as $record) {
|
||||
if ($event = $this->transform_from_database_record($record)) {
|
||||
if ($event instanceof action_event_interface) {
|
||||
$events[] = $event;
|
||||
}
|
||||
|
||||
if (count($events) == $limitnum) {
|
||||
// We've got all of the events so break both loops.
|
||||
break 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$offset += $limitnum;
|
||||
}
|
||||
|
||||
return $events;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an event from a database record.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user