/** * Delete all expired items. */ public function execute() { global $DB; // Check if the course bin is disabled or there is no expiry time. $lifetime = get_config('tool_recyclebin', 'coursebinexpiry'); if (!\tool_recyclebin\course_bin::is_enabled() || $lifetime <= 0) { return true; } // Get the items we can delete. $items = $DB->get_recordset_select('tool_recyclebin_course', 'timecreated <= :timecreated', array('timecreated' => time() - $lifetime)); foreach ($items as $item) { mtrace("[tool_recyclebin] Deleting item '{$item->id}' from the course recycle bin ..."); $bin = new \tool_recyclebin\course_bin($item->courseid); $bin->delete_item($item); } $items->close(); return true; }
/** * Test that we can delete recycle bin items. */ public function test_delete() { global $DB; $startcount = $DB->count_records('course_modules'); // Delete the course module. course_delete_module($this->quiz->cmid); // Now, run the course module deletion adhoc task. phpunit_util::run_all_adhoc_tasks(); // Try purging. $recyclebin = new \tool_recyclebin\course_bin($this->course->id); foreach ($recyclebin->get_items() as $item) { $recyclebin->delete_item($item); } // Item was deleted, so no course module was restored. $this->assertEquals($startcount - 1, $DB->count_records('course_modules')); $this->assertEquals(0, count($recyclebin->get_items())); }
/** * Test the course bin item deleted event. */ public function test_course_bin_item_deleted() { // Create a course. $course = $this->getDataGenerator()->create_course(); // Create the assignment. $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); $instance = $generator->create_instance(array('course' => $course->id)); // Delete the module. course_delete_module($instance->cmid); // Get the item from the recycle bin. $rb = new \tool_recyclebin\course_bin($course->id); $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\\course_bin_item_deleted', $event); $this->assertEquals(context_course::instance($course->id), $event->get_context()); $this->assertEquals($item->id, $event->objectid); $this->assertEventContextNotUsed($event); }