diff --git a/calendar/amd/build/events.min.js b/calendar/amd/build/events.min.js index 99bf77b661a..2387dee0fec 100644 --- a/calendar/amd/build/events.min.js +++ b/calendar/amd/build/events.min.js @@ -1 +1 @@ -define([],function(){return{created:"calendar-events:created",deleted:"calendar-events:deleted",deleteAll:"calendar-events:delete_all",updated:"calendar-events:updated",editEvent:"calendar-events:edit_event",editActionEvent:"calendar-events:edit_action_event",eventMoved:"calendar-events:event_moved",monthChanged:"calendar-events:month_changed",moveEvent:"calendar-events:move_event",filterChanged:"calendar-events:filter_changed",viewUpdated:"calendar-events:view_updated"}}); \ No newline at end of file +define([],function(){return{created:"calendar-events:created",deleted:"calendar-events:deleted",deleteAll:"calendar-events:delete_all",updated:"calendar-events:updated",editEvent:"calendar-events:edit_event",editActionEvent:"calendar-events:edit_action_event",eventMoved:"calendar-events:event_moved",dayChanged:"calendar-events:day_changed",monthChanged:"calendar-events:month_changed",moveEvent:"calendar-events:move_event",filterChanged:"calendar-events:filter_changed",viewUpdated:"calendar-events:view_updated"}}); \ No newline at end of file diff --git a/calendar/amd/build/repository.min.js b/calendar/amd/build/repository.min.js index 2d393c83f1f..388f46e6d95 100644 --- a/calendar/amd/build/repository.min.js +++ b/calendar/amd/build/repository.min.js @@ -1 +1 @@ -define(["jquery","core/ajax"],function(a,b){var c=function(a,c){"undefined"==typeof c&&(c=!1);var d={methodname:"core_calendar_delete_calendar_events",args:{events:[{eventid:a,repeat:c}]}};return b.call([d])[0]},d=function(a){var c={methodname:"core_calendar_get_calendar_event_by_id",args:{eventid:a}};return b.call([c])[0]},e=function(a){var c={methodname:"core_calendar_submit_create_update_form",args:{formdata:a}};return b.call([c])[0]},f=function(a,c,d,e,f){var g={methodname:"core_calendar_get_calendar_monthly_view",args:{year:a,month:c,courseid:d,categoryid:e,includenavigation:f}};return b.call([g])[0]},g=function(a,c){var d={methodname:"core_calendar_update_event_start_day",args:{eventid:a,daytimestamp:c}};return b.call([d])[0]},h=function(a){var c={methodname:"core_calendar_get_calendar_upcoming_view",args:{courseid:a}};return b.call([c])[0]};return{getEventById:d,deleteEvent:c,updateEventStartDay:g,submitCreateUpdateForm:e,getCalendarMonthData:f,getCalendarUpcomingData:h}}); \ No newline at end of file +define(["jquery","core/ajax"],function(a,b){var c=function(a,c){"undefined"==typeof c&&(c=!1);var d={methodname:"core_calendar_delete_calendar_events",args:{events:[{eventid:a,repeat:c}]}};return b.call([d])[0]},d=function(a){var c={methodname:"core_calendar_get_calendar_event_by_id",args:{eventid:a}};return b.call([c])[0]},e=function(a){var c={methodname:"core_calendar_submit_create_update_form",args:{formdata:a}};return b.call([c])[0]},f=function(a,c,d,e,f){var g={methodname:"core_calendar_get_calendar_monthly_view",args:{year:a,month:c,courseid:d,categoryid:e,includenavigation:f}};return b.call([g])[0]},g=function(a,c,d,e,f){var g={methodname:"core_calendar_get_calendar_day_view",args:{year:a,month:c,day:d,courseid:e,categoryid:f}};return b.call([g])[0]},h=function(a,c){var d={methodname:"core_calendar_update_event_start_day",args:{eventid:a,daytimestamp:c}};return b.call([d])[0]},i=function(a){var c={methodname:"core_calendar_get_calendar_upcoming_view",args:{courseid:a}};return b.call([c])[0]};return{getEventById:d,deleteEvent:c,updateEventStartDay:h,submitCreateUpdateForm:e,getCalendarMonthData:f,getCalendarDayData:g,getCalendarUpcomingData:i}}); \ No newline at end of file diff --git a/calendar/amd/build/view_manager.min.js b/calendar/amd/build/view_manager.min.js index a7dbfc28f7b..5d76b1d11fc 100644 --- a/calendar/amd/build/view_manager.min.js +++ b/calendar/amd/build/view_manager.min.js @@ -1 +1 @@ -define(["jquery","core/templates","core/notification","core_calendar/repository","core_calendar/events"],function(a,b,c,d,e){var f={ROOT:"[data-region='calendar']",CALENDAR_NAV_LINK:".calendarwrapper .arrow_link",CALENDAR_MONTH_WRAPPER:".calendarwrapper",LOADING_ICON_CONTAINER:'[data-region="overlay-icon-container"]'},g=function(b){b=a(b),b.on("click",f.CALENDAR_NAV_LINK,function(c){var d=b.find(f.CALENDAR_MONTH_WRAPPER),e=d.data("courseid"),g=d.data("categoryid"),h=a(c.currentTarget);i(b,h.attr("href"),h.data("year"),h.data("month"),e,g),c.preventDefault()})},h=function(g,h,i,j,m,n){k(g),n=n||g.find(f.CALENDAR_MONTH_WRAPPER),M.util.js_pending([g.get("id"),h,i,j].join("-"));var o=g.data("includenavigation");return d.getCalendarMonthData(h,i,j,m,o).then(function(a){return b.render(g.attr("data-template"),a)}).then(function(a,c){return b.replaceNode(n,a,c)}).then(function(){a("body").trigger(e.viewUpdated)}).always(function(){return M.util.js_complete([g.get("id"),h,i,j].join("-")),l(g)}).fail(c.exception)},i=function(b,c,d,f,g,i){return h(b,d,f,g,i).then(function(){return c.length&&"#"!==c&&window.history.pushState({},"",c),arguments}).then(function(){return a("body").trigger(e.monthChanged,[d,f,g,i]),arguments})},j=function(a,b,c){var d=a.find(f.CALENDAR_MONTH_WRAPPER).data("year"),e=a.find(f.CALENDAR_MONTH_WRAPPER).data("month");return"undefined"==typeof b&&(b=a.find(f.CALENDAR_MONTH_WRAPPER).data("courseid")),"undefined"==typeof c&&(c=a.find(f.CALENDAR_MONTH_WRAPPER).data("categoryid")),h(a,d,e,b,c)},k=function(a){var b=a.find(f.LOADING_ICON_CONTAINER);b.removeClass("hidden")},l=function(a){var b=a.find(f.LOADING_ICON_CONTAINER);b.addClass("hidden")},m=function(g,h){k(g);var i=g.find(f.CALENDAR_MONTH_WRAPPER);return h||(h=g.find(f.CALENDAR_MONTH_WRAPPER).data("courseid")),d.getCalendarUpcomingData(h).then(function(a){return b.render(g.attr("data-template"),a)}).then(function(a,c){return window.history.replaceState(null,null,"?view=upcoming&course="+h),b.replaceNode(i,a,c)}).then(function(){a("body").trigger(e.viewUpdated)}).always(function(){return l(g)}).fail(c.exception)};return{init:function(a){g(a)},reloadCurrentMonth:j,changeMonth:i,refreshMonthContent:h,reloadCurrentUpcoming:m}}); \ No newline at end of file +define(["jquery","core/templates","core/notification","core_calendar/repository","core_calendar/events","core_calendar/selectors"],function(a,b,c,d,e,f){var g={CALENDAR_NAV_LINK:".calendarwrapper .arrow_link",LOADING_ICON_CONTAINER:'[data-region="overlay-icon-container"]'},h=function(b){b=a(b),b.on("click",g.CALENDAR_NAV_LINK,function(c){var d=b.find(f.wrapper),e=d.data("view"),g=d.data("courseid"),h=d.data("categoryid"),i=a(c.currentTarget);"month"===e?(j(b,i.attr("href"),i.data("year"),i.data("month"),g,h),c.preventDefault()):"day"===e&&(n(b,i.attr("href"),i.data("year"),i.data("month"),i.data("day"),g,h),c.preventDefault())})},i=function(g,h,i,j,k,l){o(g),l=l||g.find(f.wrapper),M.util.js_pending([g.get("id"),h,i,j].join("-"));var m=g.data("includenavigation");return d.getCalendarMonthData(h,i,j,k,m).then(function(a){return b.render(g.attr("data-template"),a)}).then(function(a,c){return b.replaceNode(l,a,c)}).then(function(){a("body").trigger(e.viewUpdated)}).always(function(){return M.util.js_complete([g.get("id"),h,i,j].join("-")),p(g)}).fail(c.exception)},j=function(b,c,d,f,g,h){return i(b,d,f,g,h).then(function(){return c.length&&"#"!==c&&window.history.pushState({},"",c),arguments}).then(function(){return a("body").trigger(e.monthChanged,[d,f,g,h]),arguments})},k=function(a,b,c){var d=a.find(f.wrapper).data("year"),e=a.find(f.wrapper).data("month");return"undefined"==typeof b&&(b=a.find(f.wrapper).data("courseid")),"undefined"==typeof c&&(c=a.find(f.wrapper).data("categoryid")),i(a,d,e,b,c)},l=function(g,h,i,j,k,l,m){o(g),m=m||g.find(f.wrapper),M.util.js_pending([g.get("id"),h,i,j,k,l].join("-"));var n=g.data("includenavigation");return d.getCalendarDayData(h,i,j,k,l,n).then(function(a){return b.render(g.attr("data-template"),a)}).then(function(a,c){return b.replaceNode(m,a,c)}).then(function(){a("body").trigger(e.viewUpdated)}).always(function(){return M.util.js_complete([g.get("id"),h,i,j,k,l].join("-")),p(g)}).fail(c.exception)},m=function(a,b,c){var d=a.find(f.wrapper),e=d.data("year"),g=d.data("month"),h=d.data("day");return b||(b=a.find(f.wrapper).data("courseid")),"undefined"==typeof c&&(c=a.find(f.wrapper).data("categoryid")),l(a,e,g,h,b,c)},n=function(b,c,d,f,g,h,i){return l(b,d,f,g,h,i).then(function(){return c.length&&"#"!==c&&window.history.pushState({},"",c),arguments}).then(function(){return a("body").trigger(e.dayChanged,[d,f,g,h,i]),arguments})},o=function(a){var b=a.find(g.LOADING_ICON_CONTAINER);b.removeClass("hidden")},p=function(a){var b=a.find(g.LOADING_ICON_CONTAINER);b.addClass("hidden")},q=function(g,h){o(g);var i=g.find(f.wrapper);return h||(h=g.find(f.wrapper).data("courseid")),d.getCalendarUpcomingData(h).then(function(a){return b.render(g.attr("data-template"),a)}).then(function(a,c){return window.history.replaceState(null,null,"?view=upcoming&course="+h),b.replaceNode(i,a,c)}).then(function(){a("body").trigger(e.viewUpdated)}).always(function(){return p(g)}).fail(c.exception)};return{init:function(a){h(a)},reloadCurrentMonth:k,changeMonth:j,refreshMonthContent:i,reloadCurrentDay:m,changeDay:n,refreshDayContent:l,reloadCurrentUpcoming:q}}); \ No newline at end of file diff --git a/calendar/amd/src/events.js b/calendar/amd/src/events.js index 91349446190..a3d0244957c 100644 --- a/calendar/amd/src/events.js +++ b/calendar/amd/src/events.js @@ -31,6 +31,7 @@ define([], function() { editEvent: 'calendar-events:edit_event', editActionEvent: 'calendar-events:edit_action_event', eventMoved: 'calendar-events:event_moved', + dayChanged: 'calendar-events:day_changed', monthChanged: 'calendar-events:month_changed', moveEvent: 'calendar-events:move_event', filterChanged: 'calendar-events:filter_changed', diff --git a/calendar/amd/src/repository.js b/calendar/amd/src/repository.js index a5951e85ab1..1115ba1d94c 100644 --- a/calendar/amd/src/repository.js +++ b/calendar/amd/src/repository.js @@ -112,6 +112,32 @@ define(['jquery', 'core/ajax'], function($, Ajax) { return Ajax.call([request])[0]; }; + /** + * Get calendar data for the day view. + * + * @method getCalendarDayData + * @param {Number} year Year + * @param {Number} month Month + * @param {Number} day Day + * @param {Number} courseid The course id. + * @param {Number} categoryId The id of the category whose events are shown + * @return {promise} Resolved with the day view data. + */ + var getCalendarDayData = function(year, month, day, courseid, categoryId) { + var request = { + methodname: 'core_calendar_get_calendar_day_view', + args: { + year: year, + month: month, + day: day, + courseid: courseid, + categoryid: categoryId, + } + }; + + return Ajax.call([request])[0]; + }; + /** * Change the start day for the given event id. The day timestamp * only has to be any time during the target day because only the @@ -157,6 +183,7 @@ define(['jquery', 'core/ajax'], function($, Ajax) { updateEventStartDay: updateEventStartDay, submitCreateUpdateForm: submitCreateUpdateForm, getCalendarMonthData: getCalendarMonthData, + getCalendarDayData: getCalendarDayData, getCalendarUpcomingData: getCalendarUpcomingData }; }); diff --git a/calendar/amd/src/view_manager.js b/calendar/amd/src/view_manager.js index bf62b76f547..8b3d969e313 100644 --- a/calendar/amd/src/view_manager.js +++ b/calendar/amd/src/view_manager.js @@ -21,13 +21,24 @@ * @copyright 2017 Andrew Nicols * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -define(['jquery', 'core/templates', 'core/notification', 'core_calendar/repository', 'core_calendar/events'], - function($, Templates, Notification, CalendarRepository, CalendarEvents) { +define([ + 'jquery', + 'core/templates', + 'core/notification', + 'core_calendar/repository', + 'core_calendar/events', + 'core_calendar/selectors', +], function( + $, + Templates, + Notification, + CalendarRepository, + CalendarEvents, + CalendarSelectors +) { var SELECTORS = { - ROOT: "[data-region='calendar']", CALENDAR_NAV_LINK: ".calendarwrapper .arrow_link", - CALENDAR_MONTH_WRAPPER: ".calendarwrapper", LOADING_ICON_CONTAINER: '[data-region="overlay-icon-container"]' }; @@ -40,13 +51,21 @@ define(['jquery', 'core/templates', 'core/notification', 'core_calendar/reposito root = $(root); root.on('click', SELECTORS.CALENDAR_NAV_LINK, function(e) { - var wrapper = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER); + var wrapper = root.find(CalendarSelectors.wrapper); + var view = wrapper.data('view'); var courseId = wrapper.data('courseid'); var categoryId = wrapper.data('categoryid'); var link = $(e.currentTarget); - changeMonth(root, link.attr('href'), link.data('year'), link.data('month'), courseId, categoryId); - e.preventDefault(); + if (view === 'month') { + changeMonth(root, link.attr('href'), link.data('year'), link.data('month'), courseId, categoryId); + e.preventDefault(); + } else if (view === 'day') { + changeDay(root, link.attr('href'), link.data('year'), link.data('month'), link.data('day'), + courseId, categoryId); + e.preventDefault(); + } + }); }; @@ -64,7 +83,7 @@ define(['jquery', 'core/templates', 'core/notification', 'core_calendar/reposito var refreshMonthContent = function(root, year, month, courseid, categoryid, target) { startLoading(root); - target = target || root.find(SELECTORS.CALENDAR_MONTH_WRAPPER); + target = target || root.find(CalendarSelectors.wrapper); M.util.js_pending([root.get('id'), year, month, courseid].join('-')); var includenavigation = root.data('includenavigation'); @@ -120,19 +139,107 @@ define(['jquery', 'core/templates', 'core/notification', 'core_calendar/reposito * @return {promise} */ var reloadCurrentMonth = function(root, courseId, categoryId) { - var year = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('year'); - var month = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('month'); + var year = root.find(CalendarSelectors.wrapper).data('year'); + var month = root.find(CalendarSelectors.wrapper).data('month'); if (typeof courseId === 'undefined') { - courseId = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('courseid'); + courseId = root.find(CalendarSelectors.wrapper).data('courseid'); } if (typeof categoryId === 'undefined') { - categoryId = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('categoryid'); + categoryId = root.find(CalendarSelectors.wrapper).data('categoryid'); } return refreshMonthContent(root, year, month, courseId, categoryId); + }; + + /** + * Refresh the day content. + * + * @param {object} root The root element. + * @param {Number} year Year + * @param {Number} month Month + * @param {Number} day Day + * @param {Number} courseid The id of the course whose events are shown + * @param {Number} categoryId The id of the category whose events are shown + * @param {object} target The element being replaced. If not specified, the calendarwrapper is used. + * @return {promise} + */ + var refreshDayContent = function(root, year, month, day, courseid, categoryId, target) { + startLoading(root); + + target = target || root.find(CalendarSelectors.wrapper); + + M.util.js_pending([root.get('id'), year, month, day, courseid, categoryId].join('-')); + var includenavigation = root.data('includenavigation'); + return CalendarRepository.getCalendarDayData(year, month, day, courseid, categoryId, includenavigation) + .then(function(context) { + return Templates.render(root.attr('data-template'), context); + }) + .then(function(html, js) { + return Templates.replaceNode(target, html, js); + }) + .then(function() { + $('body').trigger(CalendarEvents.viewUpdated); + return; + }) + .always(function() { + M.util.js_complete([root.get('id'), year, month, day, courseid, categoryId].join('-')); + return stopLoading(root); + }) + .fail(Notification.exception); + }; + + /** + * Reload the current day view data. + * + * @param {object} root The container element. + * @param {Number} courseId The course id. + * @param {Number} categoryId The id of the category whose events are shown + * @return {promise} + */ + var reloadCurrentDay = function(root, courseId, categoryId) { + var wrapper = root.find(CalendarSelectors.wrapper); + var year = wrapper.data('year'); + var month = wrapper.data('month'); + var day = wrapper.data('day'); + + if (!courseId) { + courseId = root.find(CalendarSelectors.wrapper).data('courseid'); + } + + if (typeof categoryId === 'undefined') { + categoryId = root.find(CalendarSelectors.wrapper).data('categoryid'); + } + + return refreshDayContent(root, year, month, day, courseId, categoryId); + }; + + /** + * Handle changes to the current calendar view. + * + * @param {object} root The root element. + * @param {String} url The calendar url to be shown + * @param {Number} year Year + * @param {Number} month Month + * @param {Number} day Day + * @param {Number} courseId The id of the course whose events are shown + * @param {Number} categoryId The id of the category whose events are shown + * @return {promise} + */ + var changeDay = function(root, url, year, month, day, courseId, categoryId) { + return refreshDayContent(root, year, month, day, courseId, categoryId) + .then(function() { + if (url.length && url !== '#') { + window.history.pushState({}, '', url); + } + return arguments; + }) + .then(function() { + $('body').trigger(CalendarEvents.dayChanged, [year, month, day, courseId, categoryId]); + return arguments; + }); }; /** @@ -169,10 +276,10 @@ define(['jquery', 'core/templates', 'core/notification', 'core_calendar/reposito var reloadCurrentUpcoming = function(root, courseId) { startLoading(root); - var target = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER); + var target = root.find(CalendarSelectors.wrapper); if (!courseId) { - courseId = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('courseid'); + courseId = root.find(CalendarSelectors.wrapper).data('courseid'); } return CalendarRepository.getCalendarUpcomingData(courseId) @@ -200,6 +307,9 @@ define(['jquery', 'core/templates', 'core/notification', 'core_calendar/reposito reloadCurrentMonth: reloadCurrentMonth, changeMonth: changeMonth, refreshMonthContent: refreshMonthContent, + reloadCurrentDay: reloadCurrentDay, + changeDay: changeDay, + refreshDayContent: refreshDayContent, reloadCurrentUpcoming: reloadCurrentUpcoming }; }); diff --git a/calendar/classes/external/calendar_day_exporter.php b/calendar/classes/external/calendar_day_exporter.php new file mode 100644 index 00000000000..a0528740bf4 --- /dev/null +++ b/calendar/classes/external/calendar_day_exporter.php @@ -0,0 +1,281 @@ +. + +/** + * Contains event class for displaying the day view. + * + * @package core_calendar + * @copyright 2017 Simey Lameze + * @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; +use \core_calendar\local\event\container; + +/** + * Class for displaying the day view. + * + * @package core_calendar + * @copyright 2017 Simey Lameze + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class calendar_day_exporter extends exporter { + /** + * @var \calendar_information $calendar The calendar to be rendered. + */ + protected $calendar; + + /** + * @var moodle_url $url The URL for the day view page. + */ + protected $url; + + /** + * Constructor for day exporter. + * + * @param \calendar_information $calendar The calendar being represented. + * @param array $related The related information + */ + public function __construct(\calendar_information $calendar, $related) { + $this->calendar = $calendar; + + parent::__construct([], $related); + } + + /** + * Return the list of additional properties. + * + * @return array + */ + protected static function define_other_properties() { + return [ + 'events' => [ + 'type' => calendar_event_exporter::read_properties_definition(), + 'multiple' => true, + ], + 'defaulteventcontext' => [ + 'type' => PARAM_INT, + 'default' => null, + ], + 'filter_selector' => [ + 'type' => PARAM_RAW, + ], + 'courseid' => [ + 'type' => PARAM_INT, + ], + 'categoryid' => [ + 'type' => PARAM_INT, + 'optional' => true, + 'default' => 0, + ], + 'neweventtimestamp' => [ + '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 + // calendar format. + 'type' => PARAM_RAW, + ], + 'previousperiod' => [ + '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 + // calendar format. + 'type' => PARAM_RAW, + ], + 'nextperiod' => [ + '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, + ], + 'rarrow' => [ + // The right arrow defined by the theme. + 'type' => PARAM_RAW, + ], + ]; + } + + /** + * Get the additional values to inject while exporting. + * + * @param renderer_base $output The renderer. + * @return array Keys are the property names, values are their values. + */ + protected function get_other_values(renderer_base $output) { + $timestamp = $this->calendar->time; + + $cache = $this->related['cache']; + $url = new moodle_url('/calendar/view.php', [ + 'view' => 'day', + 'time' => $timestamp, + ]); + if ($this->calendar->course && SITEID !== $this->calendar->course->id) { + $url->param('course', $this->calendar->course->id); + } else if ($this->calendar->categoryid) { + $url->param('category', $this->calendar->categoryid); + } + $this->url = $url; + $return['events'] = array_map(function($event) use ($cache, $output, $url) { + $context = $cache->get_context($event); + $course = $cache->get_course($event); + $exporter = new calendar_event_exporter($event, [ + 'context' => $context, + 'course' => $course, + 'daylink' => $url, + 'type' => $this->related['type'], + 'today' => $this->calendar->time, + ]); + + $data = $exporter->export($output); + + // We need to override default formatted time because it differs from day view. + // Formatted time for day view adds a link to the day view. + $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event); + $data->formattedtime = calendar_format_event_time($legacyevent, time(), null); + + return $data; + }, $this->related['events']); + + if ($context = $this->get_default_add_context()) { + $return['defaulteventcontext'] = $context->id; + } + + if ($this->calendar->categoryid) { + $return['categoryid'] = $this->calendar->categoryid; + } + + $return['filter_selector'] = $this->get_course_filter_selector($output); + $return['courseid'] = $this->calendar->courseid; + + $previousperiod = $this->get_previous_day_data(); + $nextperiod = $this->get_next_day_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]); + + $days = calendar_get_days(); + $return['date'] = (new date_exporter($date))->export($output); + $return['periodname'] = userdate($this->calendar->time, get_string('strftimedaydate')); + $return['previousperiod'] = (new date_exporter($previousperiod))->export($output); + $return['previousperiodname'] = $days[$previousperiod['wday']]['fullname']; + $return['previousperiodlink'] = $previousperiodlink->out(false); + $return['nextperiod'] = (new date_exporter($nextperiod))->export($output); + $return['nextperiodname'] = $days[$nextperiod['wday']]['fullname']; + $return['nextperiodlink'] = $nextperiodlink->out(false); + $return['larrow'] = $output->larrow(); + $return['rarrow'] = $output->rarrow(); + + // Need to account for user's timezone. + $usernow = usergetdate(time()); + $today = new \DateTimeImmutable(); + $neweventtimestamp = $today->setTimestamp($date[0])->setTime( + $usernow['hours'], + $usernow['minutes'], + $usernow['seconds'] + ); + $return['neweventtimestamp'] = $neweventtimestamp->getTimestamp(); + + return $return; + } + + /** + * Get the default context for use when adding a new event. + * + * @return null|\context + */ + protected function get_default_add_context() { + if (calendar_user_can_add_event($this->calendar->course)) { + return \context_course::instance($this->calendar->course->id); + } + + return null; + } + + /** + * Get the course filter selector. + * + * @param renderer_base $output + * @return string The html code for the course filter selector. + */ + protected function get_course_filter_selector(renderer_base $output) { + $langstr = get_string('upcomingeventsfor', 'calendar'); + return $output->course_filter_selector($this->url, $langstr); + } + + /** + * Returns a list of objects that are related. + * + * @return array + */ + protected static function define_related() { + return [ + 'events' => '\core_calendar\local\event\entities\event_interface[]', + 'cache' => '\core_calendar\external\events_related_objects_cache', + 'type' => '\core_calendar\type_base', + ]; + } + + /** + * Get the previous day timestamp. + * + * @return int The previous day timestamp. + */ + protected function get_previous_day_data() { + $type = $this->related['type']; + $time = $type->get_prev_day($this->calendar->time); + + return $type->timestamp_to_date_array($time); + } + + /** + * Get the next day timestamp. + * + * @return int The next day timestamp. + */ + protected function get_next_day_data() { + $type = $this->related['type']; + $time = $type->get_next_day($this->calendar->time); + + return $type->timestamp_to_date_array($time); + } +} diff --git a/calendar/externallib.php b/calendar/externallib.php index 3436c150795..0c82f8aa223 100644 --- a/calendar/externallib.php +++ b/calendar/externallib.php @@ -972,6 +972,95 @@ class core_calendar_external extends external_api { return \core_calendar\external\month_exporter::get_read_structure(); } + /** + * Get data for the daily calendar view. + * + * @param int $year The year to be shown + * @param int $month The month to be shown + * @param int $day The day to be shown + * @param int $courseid The course to be included + * @return array + */ + public static function get_calendar_day_view($year, $month, $day, $courseid, $categoryid) { + global $CFG, $DB, $USER, $PAGE; + require_once($CFG->dirroot."/calendar/lib.php"); + + // Parameter validation. + $params = self::validate_parameters(self::get_calendar_day_view_parameters(), [ + 'year' => $year, + 'month' => $month, + 'day' => $day, + 'courseid' => $courseid, + 'categoryid' => $categoryid, + ]); + + if ($courseid != SITEID && !empty($courseid)) { + // Course ID must be valid and existing. + $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); + $courses = [$course->id => $course]; + + $coursecat = \coursecat::get($course->category); + $category = $coursecat->get_db_record(); + } else { + $course = get_site(); + $courses = calendar_get_default_courses(); + $category = null; + + if ($categoryid) { + self::validate_context(context_coursecat::instance($categoryid)); + $ids = [$categoryid]; + $category = \coursecat::get($categoryid); + $ids += $category->get_parents(); + $categories = \coursecat::get_many($ids); + $courses = array_filter($courses, function($course) use ($categories) { + return array_search($course->category, $categories) !== false; + }); + $category = $category->get_db_record(); + } + } + + // TODO: Copy what we do in calendar/view.php. + $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, $day); + $calendar = new calendar_information(0, 0, 0, $time); + $calendar->set_sources($course, $courses, $category); + + list($data, $template) = calendar_get_view($calendar, 'day', $params['includenavigation']); + + return $data; + } + + /** + * Returns description of method parameters. + * + * @return external_function_parameters + */ + public static function get_calendar_day_view_parameters() { + return new external_function_parameters( + [ + 'year' => new external_value(PARAM_INT, 'Year to be viewed', VALUE_REQUIRED), + 'month' => new external_value(PARAM_INT, 'Month to be viewed', VALUE_REQUIRED), + 'day' => new external_value(PARAM_INT, 'Day to be viewed', VALUE_REQUIRED), + 'courseid' => new external_value(PARAM_INT, 'Course being viewed', VALUE_DEFAULT, SITEID, NULL_ALLOWED), + 'categoryid' => new external_value(PARAM_INT, 'Category being viewed', VALUE_DEFAULT, null, NULL_ALLOWED), + ] + ); + } + + /** + * Returns description of method result value. + * + * @return external_description + */ + public static function get_calendar_day_view_returns() { + return \core_calendar\external\calendar_day_exporter::get_read_structure(); + } + + /** * Returns description of method parameters. * diff --git a/calendar/lib.php b/calendar/lib.php index a74ddcf189c..48754d18399 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -3231,10 +3231,9 @@ function calendar_get_view(\calendar_information $calendar, $view, $includenavig $month->set_includenavigation($includenavigation); $data = $month->export($renderer); } else if ($view == "day") { - $daydata = $type->timestamp_to_date_array($tstart); - $day = new \core_calendar\external\day_exporter($calendar, $daydata, $related); + $day = new \core_calendar\external\calendar_day_exporter($calendar, $related); $data = $day->export($renderer); - $template = 'core_calendar/day_detailed'; + $template = 'core_calendar/calendar_day'; } else if ($view == "upcoming") { $upcoming = new \core_calendar\external\calendar_upcoming_exporter($calendar, $related); $data = $upcoming->export($renderer); diff --git a/calendar/templates/calendar_day.mustache b/calendar/templates/calendar_day.mustache new file mode 100644 index 00000000000..7d75b5779f6 --- /dev/null +++ b/calendar/templates/calendar_day.mustache @@ -0,0 +1,41 @@ +{{! + 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 . +}} +{{! + @template calendar/calendar_day + + Calendar day view. + + The purpose of this template is to render the calendar day view. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Example context (json): + { + } +}} +
+ {{> core_calendar/day_detailed}} +
+{{#js}} +require(['jquery', 'core_calendar/calendar_view'], function($, CalendarView) { + CalendarView.init($("#calendar-day-{{uniqid}}"), 'day'); +}); +{{/js}} diff --git a/calendar/templates/day_detailed.mustache b/calendar/templates/day_detailed.mustache index cddfae368ca..74d48bb4342 100644 --- a/calendar/templates/day_detailed.mustache +++ b/calendar/templates/day_detailed.mustache @@ -31,9 +31,20 @@ { } }} -
- {{{filter_selector}}} - {{{new_event_button}}} + + {{> core_calendar/header}} + {{> core_calendar/day_navigation }} + {{> core/overlay_loading}} + {{> core_calendar/event_list }}
-{{> core_calendar/day_navigation }} -{{> core_calendar/event_list }} \ No newline at end of file diff --git a/calendar/templates/day_navigation.mustache b/calendar/templates/day_navigation.mustache index fce9dc2a7ec..d2c7ecad8fc 100644 --- a/calendar/templates/day_navigation.mustache +++ b/calendar/templates/day_navigation.mustache @@ -31,9 +31,34 @@ { } }} -{{#navigation}} -
- {{{navigation}}} +
+
+ + {{{larrow}}} +   + {{previousperiodname}} + + | +

{{periodname}}

+ | + + {{nextperiodname}} +   + {{{rarrow}}} + +
-{{/navigation}} - diff --git a/calendar/templates/month_detailed.mustache b/calendar/templates/month_detailed.mustache index a74fb24cbf7..942e459120a 100644 --- a/calendar/templates/month_detailed.mustache +++ b/calendar/templates/month_detailed.mustache @@ -38,6 +38,7 @@ }} data-context-id="{{defaulteventcontext}}"{{! }} data-month="{{date.mon}}"{{! }} data-year="{{date.year}}"{{! + }} data-view="month"{{! }}> {{> core_calendar/header }} {{> core_calendar/month_navigation }} diff --git a/calendar/templates/month_navigation.mustache b/calendar/templates/month_navigation.mustache index 68180c266d7..66eccc0ad0d 100644 --- a/calendar/templates/month_navigation.mustache +++ b/calendar/templates/month_navigation.mustache @@ -31,7 +31,7 @@ { } }} -
+
true, 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), ), + 'core_calendar_get_calendar_day_view' => array( + 'classname' => 'core_calendar_external', + 'methodname' => 'get_calendar_day_view', + 'description' => 'Fetch the day view data for a calendar', + 'classpath' => 'calendar/externallib.php', + 'type' => 'read', + 'capabilities' => '', + 'ajax' => true, + 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), + ), 'core_calendar_get_calendar_upcoming_view' => array( 'classname' => 'core_calendar_external', 'methodname' => 'get_calendar_upcoming_view', diff --git a/version.php b/version.php index c78c05822c0..39c0e2aa411 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2017092900.01; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2017100500.00; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.