MDL-80930 core: Course delete modules task handle non deletable cms

This commit is contained in:
raortegar
2024-03-01 13:19:06 +01:00
parent 994a21d4ff
commit ab1fcc6db4
2 changed files with 38 additions and 9 deletions
+20 -3
View File
@@ -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));
}
}
}