From a66de3ca7098f6cd7d2b201f935748f949e75f5d Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Wed, 25 Oct 2017 15:42:14 +0800 Subject: [PATCH] MDL-59909 calendar: Try hard not to fail the adhoc task. Triggering a fatal error in an adhoc task is bad. It will be retried indefinitely. Even though we are not sure how to get a module instance without a course module record, it is possible and should not kill the Moodle site. --- completion/classes/api.php | 5 ++++- completion/tests/api_test.php | 7 +++++++ course/lib.php | 9 ++++++--- lib/classes/task/refresh_mod_calendar_events_task.php | 4 ++++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/completion/classes/api.php b/completion/classes/api.php index 3bc469b1ae2a8..f8040c5864cc7 100644 --- a/completion/classes/api.php +++ b/completion/classes/api.php @@ -59,7 +59,10 @@ public static function update_completion_date_event($cmid, $modulename, $instanc if (is_object($instanceorid)) { $instance = $instanceorid; } else { - $instance = $DB->get_record($modulename, array('id' => $instanceorid), '*', MUST_EXIST); + $instance = $DB->get_record($modulename, array('id' => $instanceorid), '*', IGNORE_MISSING); + } + if (!$instance) { + return false; } $course = get_course($instance->course); diff --git a/completion/tests/api_test.php b/completion/tests/api_test.php index 489d0d03694d1..af24c079a8416 100644 --- a/completion/tests/api_test.php +++ b/completion/tests/api_test.php @@ -72,6 +72,13 @@ public function test_update_completion_date_event() { $this->assertEquals(\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED, $event->eventtype); $this->assertEquals($time, $event->timestart); $this->assertEquals($time, $event->timesort); + + require_once($CFG->dirroot . '/course/lib.php'); + // Delete the module. + course_delete_module($assign->cmid); + + // Check we don't get a failure when called on a deleted module. + \core_completion\api::update_completion_date_event($assign->cmid, 'assign', null, $time); } public function test_update_completion_date_event_update() { diff --git a/course/lib.php b/course/lib.php index c68879115134d..d0ad4f0660a49 100644 --- a/course/lib.php +++ b/course/lib.php @@ -1404,7 +1404,9 @@ function course_module_update_calendar_events($modulename, $instance = null, $cm if (!isset($cm)) { $cm = get_coursemodule_from_instance($modulename, $instance->id, $instance->course); } - course_module_calendar_event_update_process($instance, $cm); + if (!empty($cm)) { + course_module_calendar_event_update_process($instance, $cm); + } return true; } return false; @@ -1433,8 +1435,9 @@ function course_module_bulk_update_calendar_events($modulename, $courseid = 0) { } foreach ($instances as $instance) { - $cm = get_coursemodule_from_instance($modulename, $instance->id, $instance->course); - course_module_calendar_event_update_process($instance, $cm); + if ($cm = get_coursemodule_from_instance($modulename, $instance->id, $instance->course)) { + course_module_calendar_event_update_process($instance, $cm); + } } return true; } diff --git a/lib/classes/task/refresh_mod_calendar_events_task.php b/lib/classes/task/refresh_mod_calendar_events_task.php index 2e967545f18f3..3e2641aea06cd 100644 --- a/lib/classes/task/refresh_mod_calendar_events_task.php +++ b/lib/classes/task/refresh_mod_calendar_events_task.php @@ -44,6 +44,10 @@ class refresh_mod_calendar_events_task extends adhoc_task { * Run the task to refresh calendar events. */ public function execute() { + global $CFG; + + require_once($CFG->dirroot . '/course/lib.php'); + // Specific list of plugins that need to be refreshed. If not set, then all mod plugins will be refreshed. $pluginstorefresh = null; if (isset($this->get_custom_data()->plugins)) {