MDL-60125 calendar: Switch to navigation by YMD
authorAndrew Nicols <andrew@nicols.co.uk>
Thu, 14 Sep 2017 03:05:17 +0000 (11:05 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Mon, 18 Sep 2017 03:44:25 +0000 (11:44 +0800)
12 files changed:
calendar/amd/build/repository.min.js
calendar/amd/build/view_manager.min.js
calendar/amd/src/repository.js
calendar/amd/src/view_manager.js
calendar/classes/external/date_exporter.php [new file with mode: 0644]
calendar/classes/external/month_exporter.php
calendar/externallib.php
calendar/templates/calendar_mini.mustache
calendar/templates/month_detailed.mustache
calendar/templates/month_mini.mustache
calendar/templates/month_navigation.mustache
calendar/view.php

index a33feed..b9a5486 100644 (file)
Binary files a/calendar/amd/build/repository.min.js and b/calendar/amd/build/repository.min.js differ
index 301ef42..2258f31 100644 (file)
Binary files a/calendar/amd/build/view_manager.min.js and b/calendar/amd/build/view_manager.min.js differ
index 55c9db9..ef35f4f 100644 (file)
@@ -87,16 +87,18 @@ define(['jquery', 'core/ajax'], function($, Ajax) {
      * Get calendar data for the month view.
      *
      * @method getCalendarMonthData
-     * @param {Number} time Timestamp.
+     * @param {Number} year Year
+     * @param {Number} month Month
      * @param {Number} courseid The course id.
      * @return {promise} Resolved with the month view data.
      */
-    var getCalendarMonthData = function(time, courseid) {
+    var getCalendarMonthData = function(year, month, courseid) {
         var request = {
             methodname: 'core_calendar_get_calendar_monthly_view',
             args: {
-                time: time,
-                courseid: courseid
+                year: year,
+                month: month,
+                courseid: courseid,
             }
         };
 
index f8636ef..a9e3eaf 100644 (file)
@@ -42,7 +42,7 @@ define(['jquery', 'core/templates', 'core/notification', 'core_calendar/reposito
             root.on('click', SELECTORS.CALENDAR_NAV_LINK, function(e) {
                 var courseId = $(root).find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('courseid');
                 var link = $(e.currentTarget);
-                changeMonth(root, link.attr('href'), link.data('time'), courseId);
+                changeMonth(root, link.attr('href'), link.data('year'), link.data('month'), courseId);
 
                 e.preventDefault();
             });
@@ -51,14 +51,16 @@ define(['jquery', 'core/templates', 'core/notification', 'core_calendar/reposito
         /**
          * Refresh the month content.
          *
-         * @param {Number} time The calendar time to be shown
+         * @param {object} root The root element.
+         * @param {Number} year Year
+         * @param {Number} month Month
          * @param {Number} courseid The id of the course whose events are shown
          * @return {promise}
          */
-        var refreshMonthContent = function(root, time, courseid) {
+        var refreshMonthContent = function(root, year, month, courseid) {
             startLoading(root);
 
-            return CalendarRepository.getCalendarMonthData(time, courseid)
+            return CalendarRepository.getCalendarMonthData(year, month, courseid)
                 .then(function(context) {
                     return Templates.render(root.attr('data-template'), context);
                 })
@@ -78,13 +80,15 @@ define(['jquery', 'core/templates', 'core/notification', 'core_calendar/reposito
         /**
          * Handle changes to the current calendar view.
          *
+         * @param {object} root The root element.
          * @param {String} url The calendar url to be shown
-         * @param {Number} time The calendar time to be shown
+         * @param {Number} year Year
+         * @param {Number} month Month
          * @param {Number} courseid The id of the course whose events are shown
          * @return {promise}
          */
-        var changeMonth = function(root, url, time, courseid) {
-            return refreshMonthContent(root, time, courseid)
+        var changeMonth = function(root, url, year, month, courseid) {
+            return refreshMonthContent(root, year, month, courseid)
                 .then(function() {
                     if (url.length && url !== '#') {
                         window.history.pushState({}, '', url);
@@ -92,7 +96,7 @@ define(['jquery', 'core/templates', 'core/notification', 'core_calendar/reposito
                     return arguments;
                 })
                 .then(function() {
-                    $('body').trigger(CalendarEvents.monthChanged, [time, courseid]);
+                    $('body').trigger(CalendarEvents.monthChanged, [year, month, courseid]);
                     return arguments;
                 });
         };
@@ -105,12 +109,13 @@ define(['jquery', 'core/templates', 'core/notification', 'core_calendar/reposito
          * @return {promise}
          */
         var reloadCurrentMonth = function(root, courseId) {
-            var time = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('current-time');
+            var year = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('year');
+            var month = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('month');
 
             if (!courseId) {
                 courseId = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('courseid');
             }
-            return refreshMonthContent(root, time, courseId);
+            return refreshMonthContent(root, year, month, courseId);
         };
 
         /**
diff --git a/calendar/classes/external/date_exporter.php b/calendar/classes/external/date_exporter.php
new file mode 100644 (file)
index 0000000..4615b6c
--- /dev/null
@@ -0,0 +1,92 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Class for normalising the date data.
+ *
+ * @package   core_calendar
+ * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core_calendar\external;
+
+defined('MOODLE_INTERNAL') || die();
+
+use core\external\exporter;
+use renderer_base;
+use moodle_url;
+
+/**
+ * Class for normalising the date data.
+ *
+ * @package   core_calendar
+ * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class date_exporter extends exporter {
+
+    /**
+     * Constructor for date_exporter.
+     *
+     * @param array $data
+     * @param array $related The related information
+     */
+    public function __construct($data, $related = []) {
+        $data['timestamp'] = $data[0];
+        unset($data[0]);
+
+        parent::__construct($data, $related);
+    }
+
+    protected static function define_properties() {
+        return [
+            'seconds' => [
+                'type' => PARAM_INT,
+            ],
+            'minutes' => [
+                'type' => PARAM_INT,
+            ],
+            'hours' => [
+                'type' => PARAM_INT,
+            ],
+            'mday' => [
+                'type' => PARAM_INT,
+            ],
+            'wday' => [
+                'type' => PARAM_INT,
+            ],
+            'mon' => [
+                'type' => PARAM_INT,
+            ],
+            'year' => [
+                'type' => PARAM_INT,
+            ],
+            'yday' => [
+                'type' => PARAM_INT,
+            ],
+            'weekday' => [
+                'type' => PARAM_RAW,
+            ],
+            'month' => [
+                'type' => PARAM_RAW,
+            ],
+            'timestamp' => [
+                'type' => PARAM_INT,
+            ],
+        ];
+    }
+}
index 6d180aa..94298bf 100644 (file)
@@ -104,9 +104,6 @@ class month_exporter extends exporter {
             'filter_selector' => [
                 'type' => PARAM_RAW,
             ],
-            'navigation' => [
-                'type' => PARAM_RAW,
-            ],
             'weeks' => [
                 'type' => week_exporter::read_properties_definition(),
                 'multiple' => true,
@@ -118,8 +115,8 @@ class month_exporter extends exporter {
             'view' => [
                 'type' => PARAM_ALPHA,
             ],
-            'time' => [
-                'type' => PARAM_INT,
+            'date' => [
+                'type' => date_exporter::read_properties_definition(),
             ],
             'periodname' => [
                 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
@@ -127,7 +124,10 @@ class month_exporter extends exporter {
                 'type' => PARAM_RAW,
             ],
             'previousperiod' => [
-                'type' => PARAM_INT,
+                'type' => date_exporter::read_properties_definition(),
+            ],
+            'previousperiodlink' => [
+                'type' => PARAM_URL,
             ],
             'previousperiodname' => [
                 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
@@ -135,13 +135,16 @@ class month_exporter extends exporter {
                 'type' => PARAM_RAW,
             ],
             'nextperiod' => [
-                'type' => PARAM_INT,
+                'type' => date_exporter::read_properties_definition(),
             ],
             'nextperiodname' => [
                 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
                 // calendar format.
                 'type' => PARAM_RAW,
             ],
+            'nextperiodlink' => [
+                'type' => PARAM_URL,
+            ],
             'larrow' => [
                 // The left arrow defined by the theme.
                 'type' => PARAM_RAW,
@@ -160,22 +163,30 @@ class month_exporter extends exporter {
      * @return array Keys are the property names, values are their values.
      */
     protected function get_other_values(renderer_base $output) {
-        $previousperiod = $this->get_previous_month_timestamp();
-        $nextperiod = $this->get_next_month_timestamp();
+        $previousperiod = $this->get_previous_month_data();
+        $nextperiod = $this->get_next_month_data();
+        $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
+
+        $nextperiodlink = new moodle_url($this->url);
+        $nextperiodlink->param('time', $nextperiod[0]);
+
+        $previousperiodlink = new moodle_url($this->url);
+        $previousperiodlink->param('time', $previousperiod[0]);
 
         return [
             'courseid' => $this->calendar->courseid,
             'filter_selector' => $this->get_course_filter_selector($output),
-            'navigation' => $this->get_navigation($output),
             'weeks' => $this->get_weeks($output),
             'daynames' => $this->get_day_names($output),
             'view' => 'month',
-            'time' => $this->calendar->time,
+            'date' => (new date_exporter($date))->export($output),
             'periodname' => userdate($this->calendar->time, get_string('strftimemonthyear')),
-            'previousperiod' => $previousperiod,
-            'previousperiodname' => userdate($previousperiod, get_string('strftimemonthyear')),
-            'nextperiod' => $nextperiod,
-            'nextperiodname' => userdate($nextperiod, get_string('strftimemonthyear')),
+            'previousperiod' => (new date_exporter($previousperiod))->export($output),
+            'previousperiodname' => userdate($previousperiod[0], get_string('strftimemonthyear')),
+            'previousperiodlink' => $previousperiodlink->out(false),
+            'nextperiod' => (new date_exporter($nextperiod))->export($output),
+            'nextperiodname' => userdate($nextperiod[0], get_string('strftimemonthyear')),
+            'nextperiodlink' => $nextperiodlink->out(false),
             'larrow' => $output->larrow(),
             'rarrow' => $output->rarrow(),
         ];
@@ -197,19 +208,6 @@ class month_exporter extends exporter {
         return $content;
     }
 
-    /**
-     * Get the calendar navigation controls.
-     *
-     * @param renderer_base $output
-     * @return string The html code to the calendar top navigation.
-     */
-    protected function get_navigation(renderer_base $output) {
-        return calendar_top_controls('month', [
-            'id' => $this->calendar->courseid,
-            'time' => $this->calendar->time,
-        ]);
-    }
-
     /**
      * Get the list of day names for display, re-ordered from the first day
      * of the week.
@@ -302,17 +300,30 @@ class month_exporter extends exporter {
         ];
     }
 
+    /**
+     * Get the current month timestamp.
+     *
+     * @return int The month timestamp.
+     */
+    protected function get_month_data() {
+        $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
+        $monthtime = $this->related['type']->convert_to_gregorian($date['year'], $date['month'], 1);
+
+        return make_timestamp($monthtime['year'], $monthtime['month']);
+    }
+
     /**
      * Get the previous month timestamp.
      *
      * @return int The previous month timestamp.
      */
-    protected function get_previous_month_timestamp() {
-        $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
-        $month = calendar_sub_month($date['mon'], $date['year']);
-        $monthtime = $this->related['type']->convert_to_gregorian($month[1], $month[0], 1);
+    protected function get_previous_month_data() {
+        $type = $this->related['type'];
+        $date = $type->timestamp_to_date_array($this->calendar->time);
+        list($date['mon'], $date['year']) = $type->get_prev_month($date['year'], $date['mon']);
+        $time = $type->convert_to_timestamp($date['year'], $date['mon'], 1);
 
-        return make_timestamp($monthtime['year'], $monthtime['month'], $monthtime['day'], $monthtime['hour'], $monthtime['minute']);
+        return $type->timestamp_to_date_array($time);
     }
 
     /**
@@ -320,11 +331,12 @@ class month_exporter extends exporter {
      *
      * @return int The next month timestamp.
      */
-    protected function get_next_month_timestamp() {
-        $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
-        $month = calendar_add_month($date['mon'], $date['year']);
-        $monthtime = $this->related['type']->convert_to_gregorian($month[1], $month[0], 1);
+    protected function get_next_month_data() {
+        $type = $this->related['type'];
+        $date = $type->timestamp_to_date_array($this->calendar->time);
+        list($date['mon'], $date['year']) = $type->get_next_month($date['year'], $date['mon']);
+        $time = $type->convert_to_timestamp($date['year'], $date['mon'], 1);
 
-        return make_timestamp($monthtime['year'], $monthtime['month'], $monthtime['day'], $monthtime['hour'], $monthtime['minute']);
+        return $type->timestamp_to_date_array($time);
     }
 }
index afad7dd..8f6d44b 100644 (file)
@@ -879,17 +879,19 @@ class core_calendar_external extends external_api {
     /**
      * Get data for the monthly calendar view.
      *
-     * @param   int     $time The time to be shown
+     * @param   int     $year The year to be shown
+     * @param   int     $month The month to be shown
      * @param   int     $courseid The course to be included
      * @return  array
      */
-    public static function get_calendar_monthly_view($time, $courseid) {
+    public static function get_calendar_monthly_view($year, $month, $courseid) {
         global $CFG, $DB, $USER, $PAGE;
         require_once($CFG->dirroot."/calendar/lib.php");
 
         // Parameter validation.
         $params = self::validate_parameters(self::get_calendar_monthly_view_parameters(), [
-            'time' => $time,
+            'year' => $year,
+            'month' => $month,
             'courseid' => $courseid,
         ]);
 
@@ -906,6 +908,9 @@ class core_calendar_external extends external_api {
         $context = \context_user::instance($USER->id);
         self::validate_context($context);
 
+        $type = \core_calendar\type_factory::get_calendar_instance();
+
+        $time = $type->convert_to_timestamp($year, $month, 1);
         $calendar = new calendar_information(0, 0, 0, $time);
         $calendar->prepare_for_view($course, $courses);
 
@@ -922,7 +927,8 @@ class core_calendar_external extends external_api {
     public static function get_calendar_monthly_view_parameters() {
         return new external_function_parameters(
             [
-                'time' => new external_value(PARAM_INT, 'Time to be viewed', VALUE_REQUIRED, '', NULL_NOT_ALLOWED),
+                'year' => new external_value(PARAM_INT, 'Month to be viewed', VALUE_REQUIRED),
+                'month' => new external_value(PARAM_INT, 'Year to be viewed', VALUE_REQUIRED),
                 'courseid' => new external_value(PARAM_INT, 'Course being viewed', VALUE_DEFAULT, SITEID, NULL_ALLOWED),
             ]
         );
index 6e28cbf..4ae79e4 100644 (file)
     }
 }}
 <div{{!
-  }} id="calendar-month-{{uniqid}}-{{time}}" {{!
+  }} id="calendar-month-{{date.year}}-{{date.month}}-{{uniqid}}" {{!
     }} data-template="core_calendar/month_mini" {{!
-    }} data-includenavigation="{{#includenavigation}}true{{/includenavigation}}{{^includenavigation}}false{{/includenavigation}}"{{!
     }}>
     {{> core_calendar/month_mini}}
 </div>
 {{#js}}
 require(['jquery', 'core_calendar/calendar_mini'], function($, CalendarMini) {
-    CalendarMini.init($("#calendar-month-{{uniqid}}-{{time}}"));
+    CalendarMini.init($("#calendar-month-{{date.year}}-{{date.month}}-{{uniqid}}"));
 });
 {{/js}}
index 463b687..79925c4 100644 (file)
@@ -31,7 +31,7 @@
     {
     }
 }}
-<div class="calendarwrapper" data-courseid="{{courseid}}" data-current-time="{{time}}">
+<div class="calendarwrapper" data-courseid="{{courseid}}" data-month="{{date.mon}}" data-year="{{date.year}}">
     {{> core_calendar/month_header }}
     {{> core_calendar/month_navigation }}
     {{> core/overlay_loading}}
index b14afc9..ae738ba 100644 (file)
     {
     }
 }}
-<div id="month-mini-{{uniqid}}-{{time}}" class="calendarwrapper" data-courseid="{{courseid}}" data-current-time="{{time}}">
+<div{{!
+    }} id="month-mini-{{date.year}}-{{date.month}}-{{uniqid}}"{{!
+    }} class="calendarwrapper"{{!
+    }} data-courseid="{{courseid}}"{{!
+    }} data-month="{{date.mon}}"{{!
+    }} data-year="{{date.year}}"{{!
+    }}>
     {{> core/overlay_loading}}
     <table class="minicalendar calendartable">
         <caption class="calendar-controls">
-            <a href="#" class="arrow_link previous" title="{{#str}}monthprev, calendar{{/str}}" data-time="{{previousperiod}}"><span class="arrow">{{{larrow}}}</span></a>
+            <a{{!
+                }} href="#"{{!
+                }} class="arrow_link previous"{{!
+                }} title="{{#str}}monthprev, calendar{{/str}}"{{!
+                }} data-year="{{previousperiod.year}}"{{!
+                }} data-month="{{previousperiod.mon}}"{{!
+            }}>
+                <span class="arrow">{{{larrow}}}</span>
+            </a>
             <span class="hide"> | </span>
             <span class="current">
-                <a href="{{{url}}}" title="{{#str}}monththis, calendar{{/str}}" data-time="">{{periodname}}</a>
+                <a href="{{{url}}}" title="{{#str}}monththis, calendar{{/str}}">{{periodname}}</a>
             </span>
             <span class="hide"> | </span>
-            <a href="#" class="arrow_link next" title="{{#str}}monthnext, calendar{{/str}}" data-time="{{nextperiod}}"><span class="arrow">{{{rarrow}}}</span></a>
+            <a{{!
+                }} href="#"{{!
+                }} class="arrow_link next"{{!
+                }} title="{{#str}}monthprev, calendar{{/str}}"{{!
+                }} data-year="{{nextperiod.year}}"{{!
+                }} data-month="{{nextperiod.mon}}"{{!
+            }}>
+                <span class="arrow">{{{rarrow}}}</span>
+            </a>
         </caption>
         <thead>
           <tr>
@@ -145,7 +167,7 @@ require([
         M.util.js_pending("month-mini-{{uniqid}}-filterChanged");
         // A filter value has been changed.
         // Find all matching cells in the popover data, and hide them.
-        $("#month-mini-{{uniqid}}-{{time}}")
+        $("#month-mini-{{date.year}}-{{date.month}}-{{uniqid}}")
             .find(CalendarSelectors.popoverType[data.type])
             .toggleClass('hidden', !!data.hidden);
         M.util.js_complete("month-mini-{{uniqid}}-filterChanged");
index f0f67dd..68180c2 100644 (file)
     {
     }
 }}
-{{#navigation}}
 <div id="month-navigation-{{uniqid}}" class="controls" data-view="{{view}}">
-    {{{navigation}}}
+    <div class="calendar-controls">
+        <a{{!
+            }} href="{{previousperiodlink}}"{{!
+            }} class="arrow_link previous"{{!
+            }} title="{{#str}}monthprev, calendar{{/str}}"{{!
+            }} data-year="{{previousperiod.year}}"{{!
+            }} data-month="{{previousperiod.mon}}"{{!
+            }} data-drop-zone="nav-link" {{!
+        }}>
+            <span class="arrow">{{{larrow}}}</span>
+            &nbsp;
+            <span class="arrow_text">{{previousperiodname}}</span>
+        </a>
+        <span class="hide"> | </span>
+        <h2 class="current">{{periodname}}</h2>
+        <span class="hide"> | </span>
+        <a{{!
+            }} href="{{nextperiodlink}}"{{!
+            }} class="arrow_link next"{{!
+            }} title="{{#str}}monthnext, calendar{{/str}}"{{!
+            }} data-year="{{nextperiod.year}}"{{!
+            }} data-month="{{nextperiod.mon}}"{{!
+            }} data-drop-zone="nav-link" {{!
+        }}>
+            <span class="arrow_text">{{nextperiodname}}</span>
+            &nbsp;
+            <span class="arrow">{{{rarrow}}}</span>
+        </a>
+    </div>
 </div>
-{{/navigation}}
 {{#js}}
 require(['jquery', 'core_calendar/month_navigation_drag_drop'], function($, DragDrop) {
     var root = $('#month-navigation-{{uniqid}}');
index 6447c8d..8124af0 100644 (file)
@@ -51,22 +51,10 @@ require_once($CFG->dirroot.'/calendar/lib.php');
 
 $courseid = optional_param('course', SITEID, PARAM_INT);
 $view = optional_param('view', 'upcoming', PARAM_ALPHA);
-$day = optional_param('cal_d', 0, PARAM_INT);
-$mon = optional_param('cal_m', 0, PARAM_INT);
-$year = optional_param('cal_y', 0, PARAM_INT);
 $time = optional_param('time', 0, PARAM_INT);
 
 $url = new moodle_url('/calendar/view.php');
 
-// If a day, month and year were passed then convert it to a timestamp. If these were passed
-// then we can assume the day, month and year are passed as Gregorian, as no where in core
-// should we be passing these values rather than the time. This is done for BC.
-if (!empty($day) && !empty($mon) && !empty($year)) {
-    if (checkdate($mon, $day, $year)) {
-        $time = make_timestamp($year, $mon, $day);
-    }
-}
-
 if (empty($time)) {
     $time = time();
 }