From 3fb45f57e4ecd053495c6985a08768e309de30c5 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Mon, 18 Sep 2017 12:47:37 +0800 Subject: [PATCH] MDL-60156 calendar: Replace add event button template --- calendar/classes/external/month_exporter.php | 28 +++++++++++-- calendar/renderer.php | 35 ++++------------ calendar/templates/add_event_button.mustache | 43 ++++++++++++++++++++ calendar/templates/month_detailed.mustache | 8 +++- 4 files changed, 83 insertions(+), 31 deletions(-) create mode 100644 calendar/templates/add_event_button.mustache diff --git a/calendar/classes/external/month_exporter.php b/calendar/classes/external/month_exporter.php index bb599466c94..b71ccc85a3a 100644 --- a/calendar/classes/external/month_exporter.php +++ b/calendar/classes/external/month_exporter.php @@ -162,6 +162,10 @@ class month_exporter extends exporter { // The right arrow defined by the theme. 'type' => PARAM_RAW, ], + 'defaulteventcontext' => [ + 'type' => PARAM_INT, + 'default' => null, + ], ]; } @@ -182,7 +186,7 @@ class month_exporter extends exporter { $previousperiodlink = new moodle_url($this->url); $previousperiodlink->param('time', $previousperiod[0]); - return [ + $return = [ 'courseid' => $this->calendar->courseid, 'filter_selector' => $this->get_course_filter_selector($output), 'weeks' => $this->get_weeks($output), @@ -200,6 +204,12 @@ class month_exporter extends exporter { 'rarrow' => $output->rarrow(), 'includenavigation' => $this->includenavigation, ]; + + if ($context = $this->get_default_add_context()) { + $return['defaulteventcontext'] = $context->id; + } + + return $return; } /** @@ -211,9 +221,6 @@ class month_exporter extends exporter { protected function get_course_filter_selector(renderer_base $output) { $content = ''; $content .= $output->course_filter_selector($this->url, get_string('detailedmonthviewfor', 'calendar')); - if (calendar_user_can_add_event($this->calendar->course)) { - $content .= $output->add_event_button($this->calendar->courseid, 0, 0, 0, $this->calendar->time); - } return $content; } @@ -361,4 +368,17 @@ class month_exporter extends exporter { return $this; } + + /** + * 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; + } } diff --git a/calendar/renderer.php b/calendar/renderer.php index 74b98c6b42b..484265e113b 100644 --- a/calendar/renderer.php +++ b/calendar/renderer.php @@ -114,37 +114,20 @@ class core_calendar_renderer extends plugin_renderer_base { } /** - * Creates a button to add a new event + * Creates a button to add a new event. * * @param int $courseid - * @param int $day - * @param int $month - * @param int $year - * @param int $time the unixtime, used for multiple calendar support. The values $day, - * $month and $year are kept for backwards compatibility. + * @param int $unused1 + * @param int $unused2 + * @param int $unused3 + * @param int $unused4 * @return string */ - public function add_event_button($courseid, $day = null, $month = null, $year = null, $time = null) { - // 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($month) && !empty($year)) { - if (checkdate($month, $day, $year)) { - $time = make_timestamp($year, $month, $day); - } else { - $time = time(); - } - } else if (empty($time)) { - $time = time(); - } - - $coursecontext = \context_course::instance($courseid); - $attributes = [ - 'class' => 'btn btn-secondary pull-xs-right pull-right', - 'data-context-id' => $coursecontext->id, - 'data-action' => 'new-event-button' + public function add_event_button($courseid, $unused1 = null, $unused2 = null, $unused3 = null, $unused4 = null) { + $data = [ + 'contextid' => (\context_course::instance($courseid))->id, ]; - return html_writer::tag('button', get_string('newevent', 'calendar'), $attributes); + return $this->render_from_template('core_calendar/add_event_button', $data); } /** diff --git a/calendar/templates/add_event_button.mustache b/calendar/templates/add_event_button.mustache new file mode 100644 index 00000000000..654a9d995ac --- /dev/null +++ b/calendar/templates/add_event_button.mustache @@ -0,0 +1,43 @@ +{{! + 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/add_event_button + + Button to launch the "Add new event" dialogue. + + The purpose of this template is to render the button used to generate the new event dialogue. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Example context (json): + { + "defaulteventcontext": "2" + } +}} +{{#defaulteventcontext}} + + {{#str}}newevent, core_calendar{{/str}} + +{{/defaulteventcontext}} diff --git a/calendar/templates/month_detailed.mustache b/calendar/templates/month_detailed.mustache index e6ca7776bdb..baa901345f1 100644 --- a/calendar/templates/month_detailed.mustache +++ b/calendar/templates/month_detailed.mustache @@ -31,8 +31,14 @@ { } }} -
+ {{> core_calendar/header }} + {{> core_calendar/add_event_button}} {{> core_calendar/month_navigation }} {{> core/overlay_loading}}