$courseid = $this->page->course->id;
$categoryid = ($this->page->context->contextlevel === CONTEXT_COURSECAT) ? $this->page->category->id : null;
$calendar = \calendar_information::create(time(), $courseid, $categoryid);
- list($data, $template) = calendar_get_view($calendar, 'mini', isloggedin());
+ list($data, $template) = calendar_get_view($calendar, 'mini', isloggedin(), isloggedin());
$renderer = $this->page->get_renderer('core_calendar');
$this->content->text .= $renderer->render_from_template($template, $data);
};
return {
- init: function(root) {
+ init: function(root, loadOnInit) {
root = $(root);
CalendarViewManager.init(root);
registerEventListeners(root);
registerCalendarEventListeners(root);
+
+ if (loadOnInit) {
+ // The calendar hasn't yet loaded it's events so we
+ // should load them as soon as we've initialised.
+ CalendarViewManager.reloadCurrentMonth(root);
+ }
+
}
};
});
*/
protected $includenavigation = true;
+ /**
+ * @var bool $initialeventsloaded Whether the events have been loaded for this month.
+ */
+ protected $initialeventsloaded = true;
+
/**
* Constructor for month_exporter.
*
'type' => PARAM_BOOL,
'default' => true,
],
+ // Tracks whether the first set of events have been loaded and provided
+ // to the exporter.
+ 'initialeventsloaded' => [
+ 'type' => PARAM_BOOL,
+ 'default' => true,
+ ],
'previousperiod' => [
'type' => date_exporter::read_properties_definition(),
],
'larrow' => $output->larrow(),
'rarrow' => $output->rarrow(),
'includenavigation' => $this->includenavigation,
+ 'initialeventsloaded' => $this->initialeventsloaded,
];
if ($context = $this->get_default_add_context()) {
return $this;
}
+ /**
+ * Set whether the initial events have already been loaded and
+ * provided to the exporter.
+ *
+ * @param bool $loaded
+ * @return $this
+ */
+ public function set_initialeventsloaded(bool $loaded) {
+ $this->initialeventsloaded = $loaded;
+
+ return $this;
+ }
+
/**
* Get the default context for use when adding a new event.
*
* @param \calendar_information $calendar The calendar being represented
* @param string $view The type of calendar to have displayed
* @param bool $includenavigation Whether to include navigation
+ * @param bool $skipevents Whether to load the events or not
* @return array[array, string]
*/
-function calendar_get_view(\calendar_information $calendar, $view, $includenavigation = true) {
+function calendar_get_view(\calendar_information $calendar, $view, $includenavigation = true, bool $skipevents = false) {
global $PAGE, $CFG;
$renderer = $PAGE->get_renderer('core_calendar');
return $param;
}, [$calendar->users, $calendar->groups, $calendar->courses, $calendar->categories]);
- $events = \core_calendar\local\api::get_events(
- $tstart,
- $tend,
- null,
- null,
- null,
- null,
- $eventlimit,
- null,
- $userparam,
- $groupparam,
- $courseparam,
- $categoryparam,
- true,
- true,
- function ($event) {
- if ($proxy = $event->get_course_module()) {
- $cminfo = $proxy->get_proxied_instance();
- return $cminfo->uservisible;
- }
+ if ($skipevents) {
+ $events = [];
+ } else {
+ $events = \core_calendar\local\api::get_events(
+ $tstart,
+ $tend,
+ null,
+ null,
+ null,
+ null,
+ $eventlimit,
+ null,
+ $userparam,
+ $groupparam,
+ $courseparam,
+ $categoryparam,
+ true,
+ true,
+ function ($event) {
+ if ($proxy = $event->get_course_module()) {
+ $cminfo = $proxy->get_proxied_instance();
+ return $cminfo->uservisible;
+ }
- if ($proxy = $event->get_category()) {
- $category = $proxy->get_proxied_instance();
+ if ($proxy = $event->get_category()) {
+ $category = $proxy->get_proxied_instance();
- return $category->is_uservisible();
- }
+ return $category->is_uservisible();
+ }
- return true;
- }
- );
+ return true;
+ }
+ );
+ }
$related = [
'events' => $events,
if ($view == "month" || $view == "mini" || $view == "minithree") {
$month = new \core_calendar\external\month_exporter($calendar, $type, $related);
$month->set_includenavigation($includenavigation);
+ $month->set_initialeventsloaded(!$skipevents);
$data = $month->export($renderer);
} else if ($view == "day") {
$day = new \core_calendar\external\calendar_day_exporter($calendar, $related);
// Previous.
$calendar->set_time($prev);
- list($previousmonth, ) = calendar_get_view($calendar, 'minithree', false);
+ list($previousmonth, ) = calendar_get_view($calendar, 'minithree', false, true);
// Current month.
$calendar->set_time($current);
- list($currentmonth, ) = calendar_get_view($calendar, 'minithree', false);
+ list($currentmonth, ) = calendar_get_view($calendar, 'minithree', false, true);
// Next month.
$calendar->set_time($next);
- list($nextmonth, ) = calendar_get_view($calendar, 'minithree', false);
+ list($nextmonth, ) = calendar_get_view($calendar, 'minithree', false, true);
// Reset the time back.
$calendar->set_time($current);
</div>
{{#js}}
require(['jquery', 'core_calendar/calendar_mini'], function($, CalendarMini) {
- CalendarMini.init($("#calendar-month-{{date.year}}-{{date.month}}-{{uniqid}}"));
+ CalendarMini.init($("#calendar-month-{{date.year}}-{{date.month}}-{{uniqid}}"), !{{initialeventsloaded}});
});
{{/js}}
$this->assertCount(1, $courses);
}
+
+ /**
+ * Confirm that the skip events flag causes the calendar_get_view function
+ * to avoid querying for the calendar events.
+ */
+ public function test_calendar_get_view_skip_events() {
+ $this->resetAfterTest(true);
+ $this->setAdminUser();
+
+ $generator = $this->getDataGenerator();
+ $user = $generator->create_user();
+ $skipnavigation = true;
+ $skipevents = true;
+ $event = create_event([
+ 'eventtype' => 'user',
+ 'userid' => $user->id
+ ]);
+
+ $this->setUser($user);
+ $calendar = \calendar_information::create(time() - 10, SITEID, null);
+
+ list($data, $template) = calendar_get_view($calendar, 'day', $skipnavigation, $skipevents);
+ $this->assertEmpty($data->events);
+
+ $skipevents = false;
+ list($data, $template) = calendar_get_view($calendar, 'day', $skipnavigation, $skipevents);
+
+ $this->assertEquals($event->id, $data->events[0]->id);
+ }
}