Skip to content

Commit

Permalink
Merge branch 'MDL-60962-master' of git://github.com/ryanwyllie/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Jan 3, 2018
2 parents 80d7aa1 + 0ef41ee commit 6023665
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 35 deletions.
2 changes: 1 addition & 1 deletion blocks/calendar_month/block_calendar_month.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function get_content() {
$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);
Expand Down
2 changes: 1 addition & 1 deletion calendar/amd/build/calendar_mini.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion calendar/amd/src/calendar_mini.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
};
});
25 changes: 25 additions & 0 deletions calendar/classes/external/month_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -139,6 +144,12 @@ protected static function define_other_properties() {
'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(),
],
Expand Down Expand Up @@ -210,6 +221,7 @@ protected function get_other_values(renderer_base $output) {
'larrow' => $output->larrow(),
'rarrow' => $output->rarrow(),
'includenavigation' => $this->includenavigation,
'initialeventsloaded' => $this->initialeventsloaded,
];

if ($context = $this->get_default_add_context()) {
Expand Down Expand Up @@ -380,6 +392,19 @@ public function set_includenavigation($include) {
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.
*
Expand Down
62 changes: 34 additions & 28 deletions calendar/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3361,9 +3361,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');
Expand Down Expand Up @@ -3436,36 +3437,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();
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,
Expand All @@ -3477,6 +3482,7 @@ function ($event) {
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);
Expand Down
6 changes: 3 additions & 3 deletions calendar/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ public function fake_block_threemonths(calendar_information $calendar) {

// 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);
Expand Down
2 changes: 1 addition & 1 deletion calendar/templates/calendar_mini.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
29 changes: 29 additions & 0 deletions calendar/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,4 +728,33 @@ public function test_calendar_get_default_courses() {
$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);
}
}

0 comments on commit 6023665

Please sign in to comment.