/** * Delete all expired items. */ public function execute() { global $DB; // Check if the category bin is disabled or there is no expiry time. $lifetime = get_config('tool_recyclebin', 'categorybinexpiry'); if (!\tool_recyclebin\category_bin::is_enabled() || $lifetime <= 0) { return true; } // Get the items we can delete. $items = $DB->get_recordset_select('tool_recyclebin_category', 'timecreated <= :timecreated', array('timecreated' => time() - $lifetime)); foreach ($items as $item) { mtrace("[tool_recyclebin] Deleting item '{$item->id}' from the category recycle bin ..."); $bin = new \tool_recyclebin\category_bin($item->categoryid); $bin->delete_item($item); } $items->close(); return true; }
/** * Test the category bin item deleted event. */ public function test_category_bin_item_deleted() { // Create a course. $course = $this->getDataGenerator()->create_course(); // Delete the course. delete_course($course, false); // Get the item from the recycle bin. $rb = new \tool_recyclebin\category_bin($course->category); $items = $rb->get_items(); $item = reset($items); // Trigger and capture the event. $sink = $this->redirectEvents(); $rb->delete_item($item); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Check that the event contains the expected values. $this->assertInstanceOf('\\tooL_recyclebin\\event\\category_bin_item_deleted', $event); $this->assertEquals(context_coursecat::instance($course->category), $event->get_context()); $this->assertEquals($item->id, $event->objectid); $this->assertEventContextNotUsed($event); }
/** * Test that we can delete recycle bin items. */ public function test_delete() { global $DB; delete_course($this->course, false); $recyclebin = new \tool_recyclebin\category_bin($this->course->category); foreach ($recyclebin->get_items() as $item) { $recyclebin->delete_item($item); } // Item was deleted, so no course was restored. $this->assertEquals(1, $DB->count_records('course')); // Just the site course. $this->assertEquals(0, count($recyclebin->get_items())); }
} switch ($action) { // Restore it. case 'restore': if ($recyclebin->can_restore()) { $recyclebin->restore_item($item); redirect($PAGE->url, get_string('alertrestored', 'tool_recyclebin', $item), 2); } else { print_error('nopermissions', 'error'); } break; // Delete it. // Delete it. case 'delete': if ($recyclebin->can_delete()) { $recyclebin->delete_item($item); redirect($PAGE->url, get_string('alertdeleted', 'tool_recyclebin', $item), 2); } else { print_error('nopermissions', 'error'); } break; // Empty it. // Empty it. case 'empty': $recyclebin->delete_all_items(); redirect($PAGE->url, get_string('alertemptied', 'tool_recyclebin'), 2); break; } } // Add a "Go Back" button. $goback = html_writer::start_tag('div', array('class' => 'backlink'));