/** * 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; }
/** * Hook called before we delete a course. * * @param \stdClass $course The course record. */ function tool_recyclebin_pre_course_delete($course) { // Delete all the items in the course recycle bin, regardless if it enabled or not. // It may have been enabled, then disabled later on, so may still have content. $coursebin = new \tool_recyclebin\course_bin($course->id); $coursebin->delete_all_items(); if (\tool_recyclebin\category_bin::is_enabled()) { $categorybin = new \tool_recyclebin\category_bin($course->category); $categorybin->store_item($course); } }
/** * Test the cleanup task. */ public function test_cleanup_task() { global $DB; set_config('coursebinexpiry', WEEKSECS, 'tool_recyclebin'); // Delete the quiz. course_delete_module($this->quiz->cmid); // Now, run the course module deletion adhoc task. phpunit_util::run_all_adhoc_tasks(); // Set deleted date to the distant past. $recyclebin = new \tool_recyclebin\course_bin($this->course->id); foreach ($recyclebin->get_items() as $item) { $item->timecreated = time() - WEEKSECS; $DB->update_record('tool_recyclebin_course', $item); } // Create another module we are going to delete, but not alter the time it was placed in the recycle bin. $book = $this->getDataGenerator()->get_plugin_generator('mod_book')->create_instance(array('course' => $this->course->id)); course_delete_module($book->cmid); // Now, run the course module deletion adhoc task. phpunit_util::run_all_adhoc_tasks(); // Should have 2 items now. $this->assertEquals(2, count($recyclebin->get_items())); // Execute cleanup task. $this->expectOutputRegex("/\\[tool_recyclebin\\] Deleting item '\\d+' from the course recycle bin/"); $task = new \tool_recyclebin\task\cleanup_course_bin(); $task->execute(); // Should only have the book as it was not due to be deleted. $items = $recyclebin->get_items(); $this->assertEquals(1, count($items)); $deletedbook = reset($items); $this->assertEquals($book->name, $deletedbook->name); }
* * @package tool_recyclebin * @copyright 2015 University of Kent * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once dirname(__FILE__) . '/../../../config.php'; require_once $CFG->libdir . '/tablelib.php'; $contextid = required_param('contextid', PARAM_INT); $action = optional_param('action', null, PARAM_ALPHA); $context = context::instance_by_id($contextid, MUST_EXIST); $PAGE->set_context($context); // We could be a course or a category. switch ($context->contextlevel) { 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', '');
/** * Test the course bin item restored event. */ public function test_course_bin_item_restored() { // 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)); 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->restore_item($item); $events = $sink->get_events(); $event = reset($events); // Check that the event contains the expected values. $this->assertInstanceOf('\\tooL_recyclebin\\event\\course_bin_item_restored', $event); $this->assertEquals(context_course::instance($course->id), $event->get_context()); $this->assertEquals($item->id, $event->objectid); $this->assertEventContextNotUsed($event); }
/** * Hook called before we delete a course. * * @param \stdClass $course The course record. */ function tool_recyclebin_pre_course_delete($course) { // It is possible that the course deletion which triggered this hook // was from an in progress course restore. In that case we do not want // it in the recycle bin. if (isset($course->deletesource) && $course->deletesource == 'restore') { return; } // Delete all the items in the course recycle bin, regardless if it enabled or not. // It may have been enabled, then disabled later on, so may still have content. $coursebin = new \tool_recyclebin\course_bin($course->id); $coursebin->delete_all_items(); if (\tool_recyclebin\category_bin::is_enabled()) { $categorybin = new \tool_recyclebin\category_bin($course->category); $categorybin->store_item($course); } }