Skip to content

Commit

Permalink
MDL-57486 tool_recyclebin: Delete items when context already deleted.
Browse files Browse the repository at this point in the history
  • Loading branch information
danmarsden committed May 29, 2018
1 parent 9cf74e2 commit 92a2087
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
28 changes: 21 additions & 7 deletions admin/tool/recyclebin/classes/category_bin.php
Expand Up @@ -288,20 +288,34 @@ public function delete_item($item) {
global $DB;

// Grab the course category context.
$context = \context_coursecat::instance($this->_categoryid);

// Delete the files.
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'tool_recyclebin', TOOL_RECYCLEBIN_COURSECAT_BIN_FILEAREA, $item->id);
foreach ($files as $file) {
$file->delete();
$context = \context_coursecat::instance($this->_categoryid, IGNORE_MISSING);
if (!empty($context)) {
// Delete the files.
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'tool_recyclebin', TOOL_RECYCLEBIN_COURSECAT_BIN_FILEAREA, $item->id);
} else {
// 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.
$DB->delete_records('tool_recyclebin_category', array(
'id' => $item->id
));

// The coursecat might have been deleted, check we have a context before triggering event.
if (!$context) {
return;
}

// Fire event.
$event = \tool_recyclebin\event\category_bin_item_deleted::create(array(
'objectid' => $item->id,
Expand Down
24 changes: 17 additions & 7 deletions admin/tool/recyclebin/classes/course_bin.php
Expand Up @@ -274,13 +274,23 @@ public function delete_item($item) {
global $DB;

// Grab the course context.
$context = \context_course::instance($this->_courseid);

// Delete the files.
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'tool_recyclebin', TOOL_RECYCLEBIN_COURSE_BIN_FILEAREA, $item->id);
foreach ($files as $file) {
$file->delete();
$context = \context_course::instance($this->_courseid, IGNORE_MISSING);

if (!empty($context)) {
// Delete the files.
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'tool_recyclebin', TOOL_RECYCLEBIN_COURSE_BIN_FILEAREA, $item->id);
} 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.
Expand Down

0 comments on commit 92a2087

Please sign in to comment.