/** * 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 cleanup task. */ public function test_cleanup_task() { global $DB; // Set the expiry to 1 week. set_config('categorybinexpiry', WEEKSECS, 'tool_recyclebin'); delete_course($this->course, false); $recyclebin = new \tool_recyclebin\category_bin($this->course->category); // Set deleted date to the distant past. foreach ($recyclebin->get_items() as $item) { $item->timecreated = time() - WEEKSECS; $DB->update_record('tool_recyclebin_category', $item); } // Create another course to delete. $course = $this->getDataGenerator()->create_course(); delete_course($course, false); // Should now be two courses in the recycle bin. $this->assertEquals(2, count($recyclebin->get_items())); // Execute cleanup task. $this->expectOutputRegex("/\\[tool_recyclebin\\] Deleting item '\\d+' from the category recycle bin/"); $task = new \tool_recyclebin\task\cleanup_category_bin(); $task->execute(); // Task should only have deleted the course where we updated the time. $courses = $recyclebin->get_items(); $this->assertEquals(1, count($courses)); $course = reset($courses); $this->assertEquals('Test course 2', $course->fullname); }
/** * Hook called before we delete a category. * * @param \stdClass $category The category record. */ function tool_recyclebin_pre_course_category_delete($category) { // Delete all the items in the category recycle bin, regardless if it enabled or not. // It may have been enabled, then disabled later on, so may still have content. $categorybin = new \tool_recyclebin\category_bin($category->id); $categorybin->delete_all_items(); }
case CONTEXT_COURSE: require_login($context->instanceid); $recyclebin = new \tool_recyclebin\course_bin($context->instanceid); if (!$recyclebin->can_view()) { throw new required_capability_exception($context, 'tool/recyclebin:viewitems', 'nopermissions', ''); } $PAGE->set_pagelayout('incourse'); // Set the $PAGE heading - this is also the same as the h2 heading. $heading = format_string($COURSE->fullname, true, array('context' => $context)) . ': ' . get_string('pluginname', 'tool_recyclebin'); $PAGE->set_heading($heading); // Get the expiry to use later. $expiry = get_config('tool_recyclebin', 'coursebinexpiry'); break; case CONTEXT_COURSECAT: require_login(); $recyclebin = new \tool_recyclebin\category_bin($context->instanceid); if (!$recyclebin->can_view()) { throw new required_capability_exception($context, 'tool/recyclebin:viewitems', 'nopermissions', ''); } $PAGE->set_pagelayout('admin'); // Set the $PAGE heading. $PAGE->set_heading($COURSE->fullname); // The h2 heading on the page is going to be different than the $PAGE heading. $heading = $context->get_context_name() . ': ' . get_string('pluginname', 'tool_recyclebin'); // Get the expiry to use later. $expiry = get_config('tool_recyclebin', 'categorybinexpiry'); break; default: print_error('invalidcontext', 'tool_recyclebin'); break; }
/** * Test the category bin item restored event. */ public function test_category_bin_item_restored() { // 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->restore_item($item); $events = $sink->get_events(); $event = $events[6]; // Check that the event contains the expected values. $this->assertInstanceOf('\\tooL_recyclebin\\event\\category_bin_item_restored', $event); $this->assertEquals(context_coursecat::instance($course->category), $event->get_context()); $this->assertEquals($item->id, $event->objectid); $this->assertEventContextNotUsed($event); }