MDL-57486 tool_recyclebin: Delete items when context already deleted.

This commit is contained in:
Dan Marsden 2018-05-22 18:39:52 +12:00
parent 22744b745b
commit 5efcb522b1
2 changed files with 37 additions and 13 deletions

View File

@ -288,13 +288,22 @@ class category_bin extends base_bin {
global $DB; global $DB;
// Grab the course category context. // Grab the course category context.
$context = \context_coursecat::instance($this->_categoryid); $context = \context_coursecat::instance($this->_categoryid, IGNORE_MISSING);
if (!empty($context)) {
// Delete the files. // Delete the files.
$fs = get_file_storage(); $fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'tool_recyclebin', TOOL_RECYCLEBIN_COURSECAT_BIN_FILEAREA, $item->id); $fs->delete_area_files($context->id, 'tool_recyclebin', TOOL_RECYCLEBIN_COURSECAT_BIN_FILEAREA, $item->id);
foreach ($files as $file) { } else {
$file->delete(); // Course category has been deleted. Find records using $item->id as this is unique for coursecat recylebin.
$files = $DB->get_recordset('files', array('component' => 'tool_recyclebin',
'filearea' => TOOL_RECYCLEBIN_COURSECAT_BIN_FILEAREA,
'itemid' => $item->id));
$fs = get_file_storage();
foreach ($files as $filer) {
$file = $fs->get_file_instance($filer);
$file->delete();
}
$file->close();
} }
// Delete the record. // Delete the record.
@ -302,6 +311,11 @@ class category_bin extends base_bin {
'id' => $item->id 'id' => $item->id
)); ));
// The coursecat might have been deleted, check we have a context before triggering event.
if (!$context) {
return;
}
// Fire event. // Fire event.
$event = \tool_recyclebin\event\category_bin_item_deleted::create(array( $event = \tool_recyclebin\event\category_bin_item_deleted::create(array(
'objectid' => $item->id, 'objectid' => $item->id,

View File

@ -274,13 +274,23 @@ class course_bin extends base_bin {
global $DB; global $DB;
// Grab the course context. // Grab the course context.
$context = \context_course::instance($this->_courseid); $context = \context_course::instance($this->_courseid, IGNORE_MISSING);
// Delete the files. if (!empty($context)) {
$fs = get_file_storage(); // Delete the files.
$files = $fs->get_area_files($context->id, 'tool_recyclebin', TOOL_RECYCLEBIN_COURSE_BIN_FILEAREA, $item->id); $fs = get_file_storage();
foreach ($files as $file) { $fs->delete_area_files($context->id, 'tool_recyclebin', TOOL_RECYCLEBIN_COURSE_BIN_FILEAREA, $item->id);
$file->delete(); } else {
// Course context has been deleted. Find records using $item->id as this is unique for course bin recyclebin.
$files = $DB->get_recordset('files', array('component' => 'tool_recyclebin',
'filearea' => TOOL_RECYCLEBIN_COURSE_BIN_FILEAREA,
'itemid' => $item->id));
$fs = get_file_storage();
foreach ($files as $filer) {
$file = $fs->get_file_instance($filer);
$file->delete();
}
$files->close();
} }
// Delete the record. // Delete the record.