From 8dada7e334af76f2a9dc529ec3e07c55e47a86d2 Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Fri, 5 May 2017 16:44:52 +0800 Subject: [PATCH] MDL-58777 mod_lesson: Explicitly sort records and set event priority The lesson update events code depends on the "old" events in the DB being returned in the same order as they were originally made, however there was no guarantee that this would be the case. There were also situations where the priority would not be explicitly set (e.g., when creating the "original" event). --- mod/lesson/lib.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mod/lesson/lib.php b/mod/lesson/lib.php index 7a6ab2500e8..e838f5d3be3 100644 --- a/mod/lesson/lib.php +++ b/mod/lesson/lib.php @@ -121,14 +121,17 @@ function lesson_update_events($lesson, $override = null) { $conds['groupid'] = $override->groupid; } } - $oldevents = $DB->get_records('event', $conds); + $oldevents = $DB->get_records('event', $conds, 'id ASC'); // Now make a to-do list of all that needs to be updated. if (empty($override)) { // We are updating the primary settings for the lesson, so we need to add all the overrides. - $overrides = $DB->get_records('lesson_overrides', array('lessonid' => $lesson->id)); - // As well as the original lesson (empty override). - $overrides[] = new stdClass(); + $overrides = $DB->get_records('lesson_overrides', array('lessonid' => $lesson->id), 'id ASC'); + // It is necessary to add an empty stdClass to the beginning of the array as the $oldevents + // list contains the original (non-override) event for the module. If this is not included + // the logic below will end up updating the wrong row when we try to reconcile this $overrides + // list against the $oldevents list. + array_unshift($overrides, new stdClass()); } else { // Just do the one override. $overrides = array($override); @@ -167,6 +170,7 @@ function lesson_update_events($lesson, $override = null) { $event->timesort = $available; $event->visible = instance_is_visible('lesson', $lesson); $event->eventtype = LESSON_EVENT_TYPE_OPEN; + $event->priority = null; // Determine the event name and priority. if ($groupid) {