Merge branch 'MDL-60962-master' of git://github.com/ryanwyllie/moodle
authorJun Pataleta <jun@moodle.com>
Wed, 3 Jan 2018 08:53:49 +0000 (16:53 +0800)
committerJun Pataleta <jun@moodle.com>
Wed, 3 Jan 2018 08:53:49 +0000 (16:53 +0800)
blocks/calendar_month/block_calendar_month.php
calendar/amd/build/calendar_mini.min.js
calendar/amd/src/calendar_mini.js
calendar/classes/external/month_exporter.php
calendar/lib.php
calendar/renderer.php
calendar/templates/calendar_mini.mustache
calendar/tests/lib_test.php

index 6e31b70..00547c6 100644 (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);
index ad5f0df..1c3eecf 100644 (file)
Binary files a/calendar/amd/build/calendar_mini.min.js and b/calendar/amd/build/calendar_mini.min.js differ
index e8031ee..30f0385 100644 (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);
+            }
+
         }
     };
 });
index b735d36..08acf56 100644 (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.
      *
index 0a8408f..beac35f 100644 (file)
@@ -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');
@@ -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,
@@ -3477,6 +3482,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);
index e8dadbe..610ba2d 100644 (file)
@@ -81,15 +81,15 @@ class core_calendar_renderer extends plugin_renderer_base {
 
         // 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);
index 5b91ac6..e7929f8 100644 (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}}
index 081738e..6c6e753 100644 (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);
+    }
 }