MDL-60962 block_calendar_month: load content after page render

This commit is contained in:
Ryan Wyllie 2017-11-10 03:41:06 +00:00
parent 5f54a8760f
commit 7884ecf24f
7 changed files with 101 additions and 34 deletions

View File

@ -51,7 +51,7 @@ class block_calendar_month extends block_base {
$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);

View File

@ -1 +1 @@
define(["jquery","core_calendar/selectors","core_calendar/events","core_calendar/view_manager"],function(a,b,c,d){var e=function(b){var d=a("body"),e="."+b.attr("id");d.on(c.created+e,b,f),d.on(c.deleted+e,b,f),d.on(c.updated+e,b,f),d.on(c.eventMoved+e,b,f)},f=function(b){var e=b.data,f=a("body"),g="."+e.attr("id");e.is(":visible")?d.reloadCurrentMonth(e):(f.off(c.created+g),f.off(c.deleted+g),f.off(c.updated+g),f.off(c.eventMoved+g))},g=function(e){a("body").on(c.filterChanged,function(a,c){var d=e.find(b.eventType[c.type]);d.toggleClass("calendar_event_"+c.type,!c.hidden)});var f="."+e.attr("id");a("body").on("change"+f,b.elements.courseSelector,function(){if(e.is(":visible")){var b=a(this),c=b.val(),g=null;d.reloadCurrentMonth(e,c,g)}else a("body").off("change"+f)})};return{init:function(b){b=a(b),d.init(b),g(b),e(b)}}});
define(["jquery","core_calendar/selectors","core_calendar/events","core_calendar/view_manager"],function(a,b,c,d){var e=function(b){var d=a("body"),e="."+b.attr("id");d.on(c.created+e,b,f),d.on(c.deleted+e,b,f),d.on(c.updated+e,b,f),d.on(c.eventMoved+e,b,f)},f=function(b){var e=b.data,f=a("body"),g="."+e.attr("id");e.is(":visible")?d.reloadCurrentMonth(e):(f.off(c.created+g),f.off(c.deleted+g),f.off(c.updated+g),f.off(c.eventMoved+g))},g=function(e){a("body").on(c.filterChanged,function(a,c){var d=e.find(b.eventType[c.type]);d.toggleClass("calendar_event_"+c.type,!c.hidden)});var f="."+e.attr("id");a("body").on("change"+f,b.elements.courseSelector,function(){if(e.is(":visible")){var b=a(this),c=b.val(),g=null;d.reloadCurrentMonth(e,c,g)}else a("body").off("change"+f)})};return{init:function(b,c){b=a(b),d.init(b),g(b),e(b),c&&d.reloadCurrentMonth(b)}}});

View File

@ -99,12 +99,19 @@ function(
};
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);
}
}
};
});

View File

@ -59,6 +59,11 @@ class month_exporter extends exporter {
*/
protected $includenavigation = true;
/**
* @var bool $initialeventsloaded Whether the events have been loaded for this month.
*/
protected $initialeventsloaded = true;
/**
* Constructor for month_exporter.
*
@ -139,6 +144,12 @@ class month_exporter extends 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(),
],
@ -210,6 +221,7 @@ class month_exporter extends exporter {
'larrow' => $output->larrow(),
'rarrow' => $output->rarrow(),
'includenavigation' => $this->includenavigation,
'initialeventsloaded' => $this->initialeventsloaded,
];
if ($context = $this->get_default_add_context()) {
@ -380,6 +392,19 @@ class month_exporter extends exporter {
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.
*

View File

@ -3347,9 +3347,10 @@ function calendar_get_legacy_events($tstart, $tend, $users, $groups, $courses,
* @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');
@ -3422,36 +3423,40 @@ function calendar_get_view(\calendar_information $calendar, $view, $includenavig
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();
return $category->is_uservisible();
}
return true;
}
if ($proxy = $event->get_category()) {
$category = $proxy->get_proxied_instance();
return $category->is_uservisible();
}
return true;
}
);
);
}
$related = [
'events' => $events,
@ -3463,6 +3468,7 @@ function calendar_get_view(\calendar_information $calendar, $view, $includenavig
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);

View File

@ -40,6 +40,6 @@
</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}}

View File

@ -728,4 +728,33 @@ class core_calendar_lib_testcase extends advanced_testcase {
$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);
}
}