MDL-80930 core: Course delete modules task handle non deletable cms
This commit is contained in:
@@ -61,13 +61,30 @@ class course_delete_modules extends \core\task\adhoc_task {
|
||||
}
|
||||
|
||||
$cms = $this->get_custom_data()->cms;
|
||||
foreach ($cms as $cm) {
|
||||
$exceptions = [];
|
||||
$cmsfailed = [];
|
||||
foreach ($cms as $key => $cm) {
|
||||
try {
|
||||
course_delete_module($cm->id);
|
||||
} catch (\Exception $e) {
|
||||
throw new \coding_exception("The course module {$cm->id} could not be deleted. "
|
||||
. "{$e->getMessage()}: {$e->getFile()}({$e->getLine()}) {$e->getTraceAsString()}");
|
||||
// Keep the information instead of throw an exception and continue with next cms.
|
||||
$exceptions[] = ("The course module {$cm->id} could not be deleted. "
|
||||
. "{$e->getMessage()}: {$e->getFile()}({$e->getLine()}) {$e->getTraceAsString()}");
|
||||
// Save the cms that has failed to set the data only with this values.
|
||||
$cmsfailed[$key] = $cm;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Throw the existing exceptions if there is any.
|
||||
if (!empty($exceptions)) {
|
||||
// Save the failed CMS.
|
||||
$customdata = $this->get_custom_data();
|
||||
$customdata->cms = $cmsfailed;
|
||||
$this->set_custom_data($customdata);
|
||||
|
||||
throw new \coding_exception("The following course modules could not be deleted:\n " .
|
||||
implode('\n', $exceptions));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user