Skip to content

Commit

Permalink
MDL-71817 calendar: Fix duplicate IDs for multiple calendar blocks
Browse files Browse the repository at this point in the history
Done by:
* Designating instance IDs for each month_exporter instances and
assigning these instance IDs in templates.
* Adding the instance ID as an optional parameter for the
core_calendar_renderer::course_filter_selector() to generate
course filters with unique element IDs.
  • Loading branch information
junpataleta authored and lameze committed Jul 22, 2021
1 parent f0897dc commit 73a14b1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
17 changes: 16 additions & 1 deletion calendar/classes/external/month_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
*/
class month_exporter extends exporter {

/** @var int Number of calendar instances displayed. */
protected static $calendarinstances = 0;

/** @var int This calendar instance's ID. */
protected $calendarinstanceid = 0;

/**
* @var \calendar_information $calendar The calendar to be rendered.
*/
Expand Down Expand Up @@ -77,6 +83,10 @@ class month_exporter extends exporter {
* @param array $related The related information
*/
public function __construct(\calendar_information $calendar, \core_calendar\type_base $type, $related) {
// Increment the calendar instances count on initialisation.
self::$calendarinstances++;
// Assign this instance an ID based on the latest calendar instances count.
$this->calendarinstanceid = self::$calendarinstances;
$this->calendar = $calendar;
$this->firstdayofweek = $type->get_starting_weekday();

Expand Down Expand Up @@ -190,6 +200,10 @@ protected static function define_other_properties() {
'type' => PARAM_INT,
'default' => 0,
],
'calendarinstanceid' => [
'type' => PARAM_INT,
'default' => 0,
],
];
}

Expand Down Expand Up @@ -227,6 +241,7 @@ protected function get_other_values(renderer_base $output) {
'rarrow' => $output->rarrow(),
'includenavigation' => $this->includenavigation,
'initialeventsloaded' => $this->initialeventsloaded,
'calendarinstanceid' => $this->calendarinstanceid,
];

if ($this->showcoursefilter) {
Expand All @@ -252,7 +267,7 @@ protected function get_other_values(renderer_base $output) {
*/
protected function get_course_filter_selector(renderer_base $output) {
$content = '';
$content .= $output->course_filter_selector($this->url, '', $this->calendar->course->id);
$content .= $output->course_filter_selector($this->url, '', $this->calendar->course->id, $this->calendarinstanceid);

return $content;
}
Expand Down
11 changes: 8 additions & 3 deletions calendar/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,10 @@ public function event(calendar_event $event, $showactions=true) {
* @param moodle_url $returnurl The URL that the user should be taken too upon selecting a course.
* @param string $label The label to use for the course select.
* @param int $courseid The id of the course to be selected.
* @param int|null $calendarinstanceid The instance ID of the calendar we're generating this course filter for.
* @return string
*/
public function course_filter_selector(moodle_url $returnurl, $label = null, $courseid = null) {
public function course_filter_selector(moodle_url $returnurl, $label = null, $courseid = null, int $calendarinstanceid = null) {
global $CFG, $DB;

if (!isloggedin() or isguestuser()) {
Expand Down Expand Up @@ -301,9 +302,13 @@ public function course_filter_selector(moodle_url $returnurl, $label = null, $co
$labelattributes['class'] = 'sr-only';
}

$select = html_writer::label($label, 'course', false, $labelattributes);
$filterid = 'calendar-course-filter';
if ($calendarinstanceid) {
$filterid .= "-$calendarinstanceid";
}
$select = html_writer::label($label, $filterid, false, $labelattributes);
$select .= html_writer::select($courseoptions, 'course', $selected, false,
['class' => 'cal_courses_flt ml-1 mr-auto', 'id' => 'course']);
['class' => 'cal_courses_flt ml-1 mr-auto', 'id' => $filterid]);

return $select;
}
Expand Down
4 changes: 2 additions & 2 deletions calendar/templates/calendar_month.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
{
}
}}
<div id="calendar-month-{{uniqid}}" data-template="core_calendar/month_detailed">
<div id="calendar-month-{{uniqid}}-{{calendarinstanceid}}" data-template="core_calendar/month_detailed">
{{> core_calendar/header}}
{{> core_calendar/month_detailed}}
</div>
{{#js}}
require(['jquery', 'core_calendar/calendar'], function($, Calendar) {
Calendar.init($("#calendar-month-{{uniqid}}"));
Calendar.init($("#calendar-month-{{uniqid}}-{{calendarinstanceid}}"));
});
{{/js}}
4 changes: 2 additions & 2 deletions calendar/templates/month_detailed.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
}}>
{{> core_calendar/month_navigation }}
{{> core/overlay_loading}}
<table id="month-detailed-{{uniqid}}" class="calendarmonth calendartable mb-0">
<table id="month-detailed-{{uniqid}}-{{calendarinstanceid}}" class="calendarmonth calendartable mb-0">
<thead>
<tr>
{{# daynames }}
Expand Down Expand Up @@ -159,7 +159,7 @@ require([
CalendarSelectors,
CalendarEvents
) {
var root = $('#month-detailed-{{uniqid}}');
var root = $('#month-detailed-{{uniqid}}-{{calendarinstanceid}}');
DragDrop.init(root);
$('body').on(CalendarEvents.filterChanged, function(e, data) {
Expand Down
4 changes: 2 additions & 2 deletions calendar/templates/month_navigation.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
{
}
}}
<div id="month-navigation-{{uniqid}}" class="controls">
<div id="month-navigation-{{uniqid}}-{{calendarinstanceid}}" class="controls">
<div class="calendar-controls">
<a{{!
}} href="{{previousperiodlink}}"{{!
Expand Down Expand Up @@ -64,7 +64,7 @@
</div>
{{#js}}
require(['jquery', 'core_calendar/month_navigation_drag_drop'], function($, DragDrop) {
var root = $('#month-navigation-{{uniqid}}');
var root = $('#month-navigation-{{uniqid}}-{{calendarinstanceid}}');
DragDrop.init(root);
});
{{/js}}

0 comments on commit 73a14b1

Please sign in to comment.