Merge branch 'MDL-48012_master' of git://github.com/markn86/moodle

This commit is contained in:
David Monllao
2016-03-21 09:41:09 +08:00
32 changed files with 2827 additions and 2 deletions
+105
View File
@@ -0,0 +1,105 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The main interface for recycle bin methods.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_recyclebin;
defined('MOODLE_INTERNAL') || die();
/**
* Represents a recyclebin.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class base_bin {
/**
* Is this recyclebin enabled?
*/
public static function is_enabled() {
return false;
}
/**
* Returns an item from the recycle bin.
*
* @param int $itemid Item ID to retrieve.
*/
public abstract function get_item($itemid);
/**
* Returns a list of items in the recycle bin.
*/
public abstract function get_items();
/**
* Store an item in this recycle bin.
*
* @param \stdClass $item Item to store.
*/
public abstract function store_item($item);
/**
* Restore an item from the recycle bin.
*
* @param \stdClass $item The item database record
*/
public abstract function restore_item($item);
/**
* Delete an item from the recycle bin.
*
* @param \stdClass $item The item database record
*/
public abstract function delete_item($item);
/**
* Empty the recycle bin.
*/
public function delete_all_items() {
// Cleanup all items.
$items = $this->get_items();
foreach ($items as $item) {
if ($this->can_delete()) {
$this->delete_item($item);
}
}
}
/**
* Can we view items in this recycle bin?
*/
public abstract function can_view();
/**
* Can we restore items in this recycle bin?
*/
public abstract function can_restore();
/**
* Can we delete this?
*/
public abstract function can_delete();
}
@@ -0,0 +1,337 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The main interface for recycle bin methods.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_recyclebin;
defined('MOODLE_INTERNAL') || die();
define('TOOL_RECYCLEBIN_COURSECAT_BIN_FILEAREA', 'recyclebin_coursecat');
/**
* Represents a category's recyclebin.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class category_bin extends base_bin {
/**
* @var int The category id.
*/
protected $_categoryid;
/**
* Constructor.
*
* @param int $categoryid The category id.
*/
public function __construct($categoryid) {
$this->_categoryid = $categoryid;
}
/**
* Is this recyclebin enabled?
*
* @return bool true if enabled, false if not.
*/
public static function is_enabled() {
return get_config('tool_recyclebin', 'categorybinenable');
}
/**
* Returns an item from the recycle bin.
*
* @param int $itemid Item ID to retrieve.
* @return \stdClass the item.
*/
public function get_item($itemid) {
global $DB;
$item = $DB->get_record('tool_recyclebin_category', array(
'id' => $itemid
), '*', MUST_EXIST);
$item->name = get_course_display_name_for_list($item);
return $item;
}
/**
* Returns a list of items in the recycle bin for this course.
*
* @return array the list of items.
*/
public function get_items() {
global $DB;
$items = $DB->get_records('tool_recyclebin_category', array(
'categoryid' => $this->_categoryid
));
foreach ($items as $item) {
$item->name = get_course_display_name_for_list($item);
}
return $items;
}
/**
* Store a course in the recycle bin.
*
* @param \stdClass $course Course
* @throws \moodle_exception
*/
public function store_item($course) {
global $CFG, $DB;
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
// Backup the course.
$user = get_admin();
$controller = new \backup_controller(
\backup::TYPE_1COURSE,
$course->id,
\backup::FORMAT_MOODLE,
\backup::INTERACTIVE_NO,
\backup::MODE_GENERAL,
$user->id
);
$controller->execute_plan();
// Grab the result.
$result = $controller->get_results();
if (!isset($result['backup_destination'])) {
throw new \moodle_exception('Failed to backup activity prior to deletion.');
}
// Grab the filename.
$file = $result['backup_destination'];
if (!$file->get_contenthash()) {
throw new \moodle_exception('Failed to backup activity prior to deletion (invalid file).');
}
// Record the activity, get an ID.
$item = new \stdClass();
$item->categoryid = $course->category;
$item->shortname = $course->shortname;
$item->fullname = $course->fullname;
$item->timecreated = time();
$binid = $DB->insert_record('tool_recyclebin_category', $item);
// Create the location we want to copy this file to.
$filerecord = array(
'contextid' => \context_coursecat::instance($course->category)->id,
'component' => 'tool_recyclebin',
'filearea' => TOOL_RECYCLEBIN_COURSECAT_BIN_FILEAREA,
'itemid' => $binid,
'timemodified' => time()
);
// Move the file to our own special little place.
$fs = get_file_storage();
if (!$fs->create_file_from_storedfile($filerecord, $file)) {
// Failed, cleanup first.
$DB->delete_records('tool_recyclebin_category', array(
'id' => $binid
));
throw new \moodle_exception("Failed to copy backup file to recyclebin.");
}
// Delete the old file.
$file->delete();
// Fire event.
$event = \tool_recyclebin\event\category_bin_item_created::create(array(
'objectid' => $binid,
'context' => \context_coursecat::instance($course->category)
));
$event->trigger();
}
/**
* Restore an item from the recycle bin.
*
* @param \stdClass $item The item database record
* @throws \moodle_exception
*/
public function restore_item($item) {
global $CFG, $OUTPUT, $PAGE;
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
require_once($CFG->dirroot . '/course/lib.php');
$user = get_admin();
// Grab the course category context.
$context = \context_coursecat::instance($this->_categoryid);
// Get the backup file.
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'tool_recyclebin', TOOL_RECYCLEBIN_COURSECAT_BIN_FILEAREA, $item->id,
'itemid, filepath, filename', false);
if (empty($files)) {
throw new \moodle_exception('Invalid recycle bin item!');
}
if (count($files) > 1) {
throw new \moodle_exception('Too many files found!');
}
// Get the backup file.
$file = reset($files);
// Get a temp directory name and create it.
$tempdir = \restore_controller::get_tempdir_name($context->id, $user->id);
$fulltempdir = make_temp_directory('/backup/' . $tempdir);
// Extract the backup to tmpdir.
$fb = get_file_packer('application/vnd.moodle.backup');
$fb->extract_to_pathname($file, $fulltempdir);
// Build a course.
$course = new \stdClass();
$course->category = $this->_categoryid;
$course->shortname = $item->shortname;
$course->fullname = $item->fullname;
$course->summary = '';
// Create a new course.
$course = create_course($course);
if (!$course) {
throw new \moodle_exception("Could not create course to restore into.");
}
// Define the import.
$controller = new \restore_controller(
$tempdir,
$course->id,
\backup::INTERACTIVE_NO,
\backup::MODE_GENERAL,
$user->id,
\backup::TARGET_NEW_COURSE
);
// Prechecks.
if (!$controller->execute_precheck()) {
$results = $controller->get_precheck_results();
// Check if errors have been found.
if (!empty($results['errors'])) {
// Delete the temporary file we created.
fulldelete($fulltempdir);
// Delete the course we created.
delete_course($course, false);
echo $OUTPUT->header();
$backuprenderer = $PAGE->get_renderer('core', 'backup');
echo $backuprenderer->precheck_notices($results);
echo $OUTPUT->continue_button(new \moodle_url('/course/index.php', array('categoryid' => $this->_categoryid)));
echo $OUTPUT->footer();
exit();
}
}
// Run the import.
$controller->execute_plan();
// Fire event.
$event = \tool_recyclebin\event\category_bin_item_restored::create(array(
'objectid' => $item->id,
'context' => $context
));
$event->add_record_snapshot('tool_recyclebin_category', $item);
$event->trigger();
// Cleanup.
fulldelete($fulltempdir);
$this->delete_item($item);
}
/**
* Delete an item from the recycle bin.
*
* @param \stdClass $item The item database record
* @throws \coding_exception
*/
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();
}
// Delete the record.
$DB->delete_records('tool_recyclebin_category', array(
'id' => $item->id
));
// Fire event.
$event = \tool_recyclebin\event\category_bin_item_deleted::create(array(
'objectid' => $item->id,
'context' => \context_coursecat::instance($item->categoryid)
));
$event->add_record_snapshot('tool_recyclebin_category', $item);
$event->trigger();
}
/**
* Can we view items in this recycle bin?
*
* @return bool returns true if they can view, false if not
*/
public function can_view() {
$context = \context_coursecat::instance($this->_categoryid);
return has_capability('tool/recyclebin:viewitems', $context);
}
/**
* Can we restore items in this recycle bin?
*
* @return bool returns true if they can restore, false if not
*/
public function can_restore() {
$context = \context_coursecat::instance($this->_categoryid);
return has_capability('tool/recyclebin:restoreitems', $context);
}
/**
* Can we delete items in this recycle bin?
*
* @return bool returns true if they can delete, false if not
*/
public function can_delete() {
$context = \context_coursecat::instance($this->_categoryid);
return has_capability('tool/recyclebin:deleteitems', $context);
}
}
@@ -0,0 +1,329 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The main interface for recycle bin methods.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_recyclebin;
defined('MOODLE_INTERNAL') || die();
define('TOOL_RECYCLEBIN_COURSE_BIN_FILEAREA', 'recyclebin_course');
/**
* Represents a course's recyclebin.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_bin extends base_bin {
/**
* @var int The course id.
*/
protected $_courseid;
/**
* Constructor.
*
* @param int $courseid Course ID.
*/
public function __construct($courseid) {
$this->_courseid = $courseid;
}
/**
* Is this recyclebin enabled?
*
* @return bool true if enabled, false if not.
*/
public static function is_enabled() {
return get_config('tool_recyclebin', 'coursebinenable');
}
/**
* Returns an item from the recycle bin.
*
* @param int $itemid Item ID to retrieve.
* @return \stdClass the item.
*/
public function get_item($itemid) {
global $DB;
return $DB->get_record('tool_recyclebin_course', array(
'id' => $itemid
), '*', MUST_EXIST);
}
/**
* Returns a list of items in the recycle bin for this course.
*
* @return array the list of items.
*/
public function get_items() {
global $DB;
return $DB->get_records('tool_recyclebin_course', array(
'courseid' => $this->_courseid
));
}
/**
* Store a course module in the recycle bin.
*
* @param \stdClass $cm Course module
* @throws \moodle_exception
*/
public function store_item($cm) {
global $CFG, $DB;
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
// Get more information.
$modinfo = get_fast_modinfo($cm->course);
if (!isset($modinfo->cms[$cm->id])) {
return; // Can't continue without the module information.
}
$cminfo = $modinfo->cms[$cm->id];
// Check backup/restore support.
if (!plugin_supports('mod', $cminfo->modname , FEATURE_BACKUP_MOODLE2)) {
return;
}
// Backup the activity.
$user = get_admin();
$controller = new \backup_controller(
\backup::TYPE_1ACTIVITY,
$cm->id,
\backup::FORMAT_MOODLE,
\backup::INTERACTIVE_NO,
\backup::MODE_GENERAL,
$user->id
);
$controller->execute_plan();
// Grab the result.
$result = $controller->get_results();
if (!isset($result['backup_destination'])) {
throw new \moodle_exception('Failed to backup activity prior to deletion.');
}
// Grab the filename.
$file = $result['backup_destination'];
if (!$file->get_contenthash()) {
throw new \moodle_exception('Failed to backup activity prior to deletion (invalid file).');
}
// Record the activity, get an ID.
$activity = new \stdClass();
$activity->courseid = $cm->course;
$activity->section = $cm->section;
$activity->module = $cm->module;
$activity->name = $cminfo->name;
$activity->timecreated = time();
$binid = $DB->insert_record('tool_recyclebin_course', $activity);
// Create the location we want to copy this file to.
$filerecord = array(
'contextid' => \context_course::instance($this->_courseid)->id,
'component' => 'tool_recyclebin',
'filearea' => TOOL_RECYCLEBIN_COURSE_BIN_FILEAREA,
'itemid' => $binid,
'timemodified' => time()
);
// Move the file to our own special little place.
$fs = get_file_storage();
if (!$fs->create_file_from_storedfile($filerecord, $file)) {
// Failed, cleanup first.
$DB->delete_records('tool_recyclebin_course', array(
'id' => $binid
));
throw new \moodle_exception("Failed to copy backup file to recyclebin.");
}
// Delete the old file.
$file->delete();
// Fire event.
$event = \tool_recyclebin\event\course_bin_item_created::create(array(
'objectid' => $binid,
'context' => \context_course::instance($cm->course)
));
$event->trigger();
}
/**
* Restore an item from the recycle bin.
*
* @param \stdClass $item The item database record
* @throws \moodle_exception
*/
public function restore_item($item) {
global $CFG, $OUTPUT, $PAGE;
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
$user = get_admin();
// Grab the course context.
$context = \context_course::instance($this->_courseid);
// Get the files..
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'tool_recyclebin', TOOL_RECYCLEBIN_COURSE_BIN_FILEAREA, $item->id,
'itemid, filepath, filename', false);
if (empty($files)) {
throw new \moodle_exception('Invalid recycle bin item!');
}
if (count($files) > 1) {
throw new \moodle_exception('Too many files found!');
}
// Get the backup file.
$file = reset($files);
// Get a temp directory name and create it.
$tempdir = \restore_controller::get_tempdir_name($context->id, $user->id);
$fulltempdir = make_temp_directory('/backup/' . $tempdir);
// Extract the backup to tempdir.
$fb = get_file_packer('application/vnd.moodle.backup');
$fb->extract_to_pathname($file, $fulltempdir);
// Define the import.
$controller = new \restore_controller(
$tempdir,
$this->_courseid,
\backup::INTERACTIVE_NO,
\backup::MODE_GENERAL,
$user->id,
\backup::TARGET_EXISTING_ADDING
);
// Prechecks.
if (!$controller->execute_precheck()) {
$results = $controller->get_precheck_results();
// If errors are found then delete the file we created.
if (!empty($results['errors'])) {
fulldelete($fulltempdir);
echo $OUTPUT->header();
$backuprenderer = $PAGE->get_renderer('core', 'backup');
echo $backuprenderer->precheck_notices($results);
echo $OUTPUT->continue_button(new \moodle_url('/course/view.php', array('id' => $this->_courseid)));
echo $OUTPUT->footer();
exit();
}
}
// Run the import.
$controller->execute_plan();
// Fire event.
$event = \tool_recyclebin\event\course_bin_item_restored::create(array(
'objectid' => $item->id,
'context' => $context
));
$event->add_record_snapshot('tool_recyclebin_course', $item);
$event->trigger();
// Cleanup.
fulldelete($fulltempdir);
$this->delete_item($item);
}
/**
* Delete an item from the recycle bin.
*
* @param \stdClass $item The item database record
*/
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();
}
// Delete the record.
$DB->delete_records('tool_recyclebin_course', array(
'id' => $item->id
));
// The course might have been deleted, check we have a context.
$context = \context_course::instance($item->courseid, \IGNORE_MISSING);
if (!$context) {
return;
}
// Fire event.
$event = \tool_recyclebin\event\course_bin_item_deleted::create(array(
'objectid' => $item->id,
'context' => $context
));
$event->add_record_snapshot('tool_recyclebin_course', $item);
$event->trigger();
}
/**
* Can we view items in this recycle bin?
*
* @return bool returns true if they can view, false if not
*/
public function can_view() {
$context = \context_course::instance($this->_courseid);
return has_capability('tool/recyclebin:viewitems', $context);
}
/**
* Can we restore items in this recycle bin?
*
* @return bool returns true if they can restore, false if not
*/
public function can_restore() {
$context = \context_course::instance($this->_courseid);
return has_capability('tool/recyclebin:restoreitems', $context);
}
/**
* Can we delete this?
*
* @return bool returns true if they can delete, false if not
*/
public function can_delete() {
$context = \context_course::instance($this->_courseid);
return has_capability('tool/recyclebin:deleteitems', $context);
}
}
@@ -0,0 +1,66 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Recycle bin events.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_recyclebin\event;
defined('MOODLE_INTERNAL') || die();
/**
* Event class.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class category_bin_item_created extends \core\event\base {
/**
* Init method.
*/
protected function init() {
$this->data['objecttable'] = 'tool_recyclebin_category';
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventitemcreated', 'tool_recyclebin');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return get_string('eventitemcreated_desc', 'tool_recyclebin', array(
'objectid' => $this->objectid
));
}
}
@@ -0,0 +1,66 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Recycle bin events.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_recyclebin\event;
defined('MOODLE_INTERNAL') || die();
/**
* Event class.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class category_bin_item_deleted extends \core\event\base {
/**
* Init method.
*/
protected function init() {
$this->data['objecttable'] = 'tool_recyclebin_category';
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventitemdeleted', 'tool_recyclebin');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return get_string('eventitemdeleted_desc', 'tool_recyclebin', array(
'objectid' => $this->objectid
));
}
}
@@ -0,0 +1,66 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Recycle bin events.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_recyclebin\event;
defined('MOODLE_INTERNAL') || die();
/**
* Event Class
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class category_bin_item_restored extends \core\event\base {
/**
* Init method.
*/
protected function init() {
$this->data['objecttable'] = 'tool_recyclebin_category';
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventitemrestored', 'tool_recyclebin');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return get_string('eventitemrestored_desc', 'tool_recyclebin', array(
'objectid' => $this->objectid
));
}
}
@@ -0,0 +1,66 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Recycle bin events.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_recyclebin\event;
defined('MOODLE_INTERNAL') || die();
/**
* Event class.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_bin_item_created extends \core\event\base {
/**
* Init method.
*/
protected function init() {
$this->data['objecttable'] = 'tool_recyclebin_course';
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventitemcreated', 'tool_recyclebin');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return get_string('eventitemcreated_desc', 'tool_recyclebin', array(
'objectid' => $this->objectid
));
}
}
@@ -0,0 +1,66 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Recycle bin events.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_recyclebin\event;
defined('MOODLE_INTERNAL') || die();
/**
* Event class.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_bin_item_deleted extends \core\event\base {
/**
* Init method.
*/
protected function init() {
$this->data['objecttable'] = 'tool_recyclebin_course';
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventitemdeleted', 'tool_recyclebin');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return get_string('eventitemdeleted_desc', 'tool_recyclebin', array(
'objectid' => $this->objectid
));
}
}
@@ -0,0 +1,66 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Recycle bin events.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_recyclebin\event;
defined('MOODLE_INTERNAL') || die();
/**
* Event class.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_bin_item_restored extends \core\event\base {
/**
* Init method.
*/
protected function init() {
$this->data['objecttable'] = 'tool_recyclebin_course';
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventitemrestored', 'tool_recyclebin');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return get_string('eventitemrestored_desc', 'tool_recyclebin', array(
'objectid' => $this->objectid
));
}
}
@@ -0,0 +1,67 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Recycle bin cron task.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_recyclebin\task;
/**
* This task deletes expired category recyclebin items.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cleanup_category_bin extends \core\task\scheduled_task {
/**
* Task name.
*/
public function get_name() {
return get_string('taskcleanupcategorybin', 'tool_recyclebin');
}
/**
* 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;
}
}
@@ -0,0 +1,67 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Recycle bin cron task.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_recyclebin\task;
/**
* This task deletes expired course recyclebin items.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cleanup_course_bin extends \core\task\scheduled_task {
/**
* Task name.
*/
public function get_name() {
return get_string('taskcleanupcoursebin', 'tool_recyclebin');
}
/**
* 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;
}
}
+57
View File
@@ -0,0 +1,57 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Plugin capabilities.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$capabilities = array(
'tool/recyclebin:deleteitems' => array(
'captype' => 'write',
'riskbitmask' => RISK_DATALOSS,
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),
'tool/recyclebin:restoreitems' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),
'tool/recyclebin:viewitems' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
)
);
+43
View File
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="tool/recyclebin/db" VERSION="20160315" COMMENT="XMLDB file for Moodle tool/recyclebin"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="tool_recyclebin_course" COMMENT="A list of items in the course recycle bin">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="section" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="module" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="courseid" TYPE="foreign" FIELDS="courseid" REFTABLE="course" REFFIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="courseid" UNIQUE="false" FIELDS="courseid"/>
<INDEX NAME="timecreated" UNIQUE="false" FIELDS="timecreated"/>
</INDEXES>
</TABLE>
<TABLE NAME="tool_recyclebin_category" COMMENT="A list of items in the category recycle bin">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="categoryid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="shortname" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="fullname" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="categoryid" TYPE="foreign" FIELDS="categoryid" REFTABLE="course_categories" REFFIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="categoryid" UNIQUE="false" FIELDS="categoryid"/>
<INDEX NAME="timecreated" UNIQUE="false" FIELDS="timecreated"/>
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
+44
View File
@@ -0,0 +1,44 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Recycle bin tasks.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$tasks = array(
array(
'classname' => 'tool_recyclebin\task\cleanup_course_bin',
'blocking' => 0,
'minute' => '*/30',
'hour' => '*',
'day' => '*',
'dayofweek' => '*',
'month' => '*'
),
array(
'classname' => 'tool_recyclebin\task\cleanup_category_bin',
'blocking' => 0,
'minute' => '*/30',
'hour' => '*',
'day' => '*',
'dayofweek' => '*',
'month' => '*'
)
);
+254
View File
@@ -0,0 +1,254 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This page shows the contents of a recyclebin for a given course.
*
* @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', '');
}
$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;
}
if (!$recyclebin::is_enabled()) {
print_error('notenabled', 'tool_recyclebin');
}
$PAGE->set_url('/admin/tool/recyclebin/index.php', array(
'contextid' => $contextid
));
$PAGE->set_title(get_string('pluginname', 'tool_recyclebin'));
// If we are doing anything, we need a sesskey!
if (!empty($action)) {
raise_memory_limit(MEMORY_EXTRA);
require_sesskey();
$item = null;
if ($action == 'restore' || $action == 'delete') {
$itemid = required_param('itemid', PARAM_INT);
$item = $recyclebin->get_item($itemid);
}
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.
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.
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'));
$goback .= html_writer::link($context->get_url(), get_string('backto', '', $context->get_context_name()));
$goback .= html_writer::end_tag('div');
// Output header.
echo $OUTPUT->header();
echo $OUTPUT->heading($heading);
// Grab our items, check there is actually something to display.
$items = $recyclebin->get_items();
// Nothing to show? Bail out early.
if (empty($items)) {
echo $OUTPUT->box(get_string('noitemsinbin', 'tool_recyclebin'));
echo $goback;
echo $OUTPUT->footer();
die;
}
// Start with a description.
if ($expiry > 0) {
$expirydisplay = format_time($expiry);
echo '<div class=\'alert\'>' . get_string('deleteexpirywarning', 'tool_recyclebin', $expirydisplay) . '</div>';
}
// Define columns and headers.
$firstcolstr = $context->contextlevel == CONTEXT_COURSE ? 'activity' : 'course';
$columns = array($firstcolstr, 'date', 'restore', 'delete');
$headers = array(
get_string($firstcolstr),
get_string('datedeleted', 'tool_recyclebin'),
get_string('restore'),
get_string('delete')
);
// Define a table.
$table = new flexible_table('recyclebin');
$table->define_columns($columns);
$table->column_style('restore', 'text-align', 'center');
$table->column_style('delete', 'text-align', 'center');
$table->define_headers($headers);
$table->define_baseurl($PAGE->url);
$table->set_attribute('id', 'recycle-bin-table');
$table->setup();
// Cache a list of modules.
$modules = null;
if ($context->contextlevel == CONTEXT_COURSE) {
$modules = $DB->get_records('modules');
}
// Add all the items to the table.
$showempty = false;
$canrestore = $recyclebin->can_restore();
foreach ($items as $item) {
$row = array();
// Build item name.
$name = $item->name;
if ($context->contextlevel == CONTEXT_COURSE) {
if (isset($modules[$item->module])) {
$mod = $modules[$item->module];
$modname = get_string('modulename', $mod->name);
$name = '<img src="' . $OUTPUT->pix_url('icon', $mod->name) . '" class="icon" alt="' . $modname . '" /> ' . $name;
}
}
$row[] = $name;
$row[] = userdate($item->timecreated);
// Build restore link.
if ($canrestore && ($context->contextlevel == CONTEXT_COURSECAT || isset($modules[$item->module]))) {
$restoreurl = new moodle_url($PAGE->url, array(
'contextid' => $contextid,
'itemid' => $item->id,
'action' => 'restore',
'sesskey' => sesskey()
));
$row[] = $OUTPUT->action_icon($restoreurl, new pix_icon('t/restore', get_string('restore'), '', array(
'class' => 'iconsmall'
)));
} else {
// Show padlock.
$row[] = $OUTPUT->pix_icon('t/locked', get_string('locked', 'admin'), '', array('class' => 'iconsmall'));
}
// Build delete link.
if ($recyclebin->can_delete()) {
$showempty = true;
$delete = new moodle_url($PAGE->url, array(
'contextid' => $contextid,
'itemid' => $item->id,
'action' => 'delete',
'sesskey' => sesskey()
));
$deleteaction = new confirm_action(get_string('deleteconfirm', 'tool_recyclebin'));
$delete = $OUTPUT->action_icon($delete, new pix_icon('t/delete', get_string('delete')), $deleteaction);
$row[] = $delete;
} else {
// Show padlock.
$row[] = $OUTPUT->pix_icon('t/locked', get_string('locked', 'admin'), '', array('class' => 'iconsmall'));
}
$table->add_data($row);
}
// Display the table now.
$table->finish_output();
// Empty recyclebin link.
if ($showempty) {
$emptylink = new moodle_url($PAGE->url, array(
'contextid' => $contextid,
'action' => 'empty',
'sesskey' => sesskey()
));
$emptyaction = new confirm_action(get_string('deleteallconfirm', 'tool_recyclebin'));
echo $OUTPUT->action_link($emptylink, get_string('deleteall', 'tool_recyclebin'), $emptyaction);
}
echo $goback;
// Confirmation JS.
$PAGE->requires->strings_for_js(array('deleteallconfirm', 'deleteconfirm'), 'tool_recyclebin');
// Output footer.
echo $OUTPUT->footer();
@@ -0,0 +1,58 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* English strings for tool_recyclebin.
*
* @package tool_recyclebin
* @copyright 2015 Skylar Kelty <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$string['alertdeleted'] = '\'{$a->name}\' has been deleted.';
$string['alertemptied'] = 'Recycle bin has been emptied.';
$string['alertrestored'] = '\'{$a->name}\' has been restored.';
$string['autohide'] = 'Auto hide';
$string['autohide_desc'] = 'Automatically hides the recycle bin link when the bin is empty.';
$string['categorybinenable'] = 'Enable category recycle bin';
$string['categorybinexpiry'] = 'Course lifetime';
$string['categorybinexpiry_desc'] = 'How long should a deleted course remain in the recycle bin?';
$string['coursebinenable'] = 'Enable course recycle bin';
$string['coursebinexpiry'] = 'Item lifetime';
$string['coursebinexpiry_desc'] = 'How long should a deleted item remain in the recycle bin?';
$string['datedeleted'] = 'Date deleted';
$string['deleteall'] = 'Delete all';
$string['deleteallconfirm'] = 'Are you sure you want to delete all items from the recycle bin?';
$string['deleteconfirm'] = 'Are you sure you want to delete the selected item from the recycle bin?';
$string['deleteexpirywarning'] = 'Contents will be permanently deleted after {$a}.';
$string['eventitemcreated'] = 'Item created';
$string['eventitemcreated_desc'] = 'Item created with ID {$a->objectid}.';
$string['eventitemdeleted'] = 'Item deleted';
$string['eventitemdeleted_desc'] = 'Item with ID {$a->objectid} deleted.';
$string['eventitemrestored'] = 'Item restored';
$string['eventitemrestored_desc'] = 'Item with ID {$a->objectid} restored.';
$string['invalidcontext'] = 'Invalid context supplied.';
$string['neverdelete'] = 'Never delete recycled items';
$string['noitemsinbin'] = 'There are no items in the recycle bin.';
$string['notenabled'] = 'Sorry, but the recycle bin has been disabled by the administrator.';
$string['pluginname'] = 'Recycle bin';
$string['taskcleanupcategorybin'] = 'Cleanup category recycle bin';
$string['taskcleanupcoursebin'] = 'Cleanup course recycle bin';
$string['recyclebin:deleteitems'] = 'Delete recycle bin items';
$string['recyclebin:restoreitems'] = 'Restore recycle bin items';
$string['recyclebin:viewitems'] = 'View recycle bin items';
+182
View File
@@ -0,0 +1,182 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Local lib code
*
* @package tool_recyclebin
* @copyright 2015 Skylar Kelty <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* Adds a recycle bin link to the course admin menu.
*
* @param navigation_node $navigation The navigation node to extend
* @param stdClass $course The course to object for the tool
* @param context $context The context of the course
* @return void|null return null if we don't want to display the node.
*/
function tool_recyclebin_extend_navigation_course($navigation, $course, $context) {
global $PAGE;
// Only add this settings item on non-site course pages.
if (!$PAGE->course || $PAGE->course->id == SITEID || !\tool_recyclebin\course_bin::is_enabled()) {
return null;
}
$coursebin = new \tool_recyclebin\course_bin($context->instanceid);
// Check we can view the recycle bin.
if (!$coursebin->can_view()) {
return null;
}
$url = null;
$settingnode = null;
$url = new moodle_url('/admin/tool/recyclebin/index.php', array(
'contextid' => $context->id
));
// If we are set to auto-hide, check the number of items.
$autohide = get_config('tool_recyclebin', 'autohide');
if ($autohide) {
$items = $coursebin->get_items();
if (empty($items)) {
return null;
}
}
// Add the recyclebin link.
$pluginname = get_string('pluginname', 'tool_recyclebin');
$node = navigation_node::create(
$pluginname,
$url,
navigation_node::NODETYPE_LEAF,
'tool_recyclebin',
'tool_recyclebin',
new pix_icon('trash', $pluginname, 'tool_recyclebin')
);
if ($PAGE->url->compare($url, URL_MATCH_BASE)) {
$node->make_active();
}
$navigation->add_node($node);
}
/**
* Adds a recycle bin link to the course admin menu.
*
* @param navigation_node $navigation The navigation node to extend
* @param context $context The context of the course
* @return void|null return null if we don't want to display the node.
*/
function tool_recyclebin_extend_navigation_category_settings($navigation, $context) {
global $PAGE;
// Check if it is enabled.
if (!\tool_recyclebin\category_bin::is_enabled()) {
return null;
}
$categorybin = new \tool_recyclebin\category_bin($context->instanceid);
// Check we can view the recycle bin.
if (!$categorybin->can_view()) {
return null;
}
$url = null;
$settingnode = null;
// Add a link to the category recyclebin.
$url = new moodle_url('/admin/tool/recyclebin/index.php', array(
'contextid' => $context->id
));
// If we are set to auto-hide, check the number of items.
$autohide = get_config('tool_recyclebin', 'autohide');
if ($autohide) {
$items = $categorybin->get_items();
if (empty($items)) {
return null;
}
}
// Add the recyclebin link.
$pluginname = get_string('pluginname', 'tool_recyclebin');
$node = navigation_node::create(
$pluginname,
$url,
navigation_node::NODETYPE_LEAF,
'tool_recyclebin',
'tool_recyclebin',
new pix_icon('trash', $pluginname, 'tool_recyclebin')
);
if ($PAGE->url->compare($url, URL_MATCH_BASE)) {
$node->make_active();
}
$navigation->add_node($node);
}
/**
* Hook called before we delete a course module.
*
* @param \stdClass $cm The course module record.
*/
function tool_recyclebin_pre_course_module_delete($cm) {
if (\tool_recyclebin\course_bin::is_enabled()) {
$coursebin = new \tool_recyclebin\course_bin($cm->course);
$coursebin->store_item($cm);
}
}
/**
* 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);
}
}
/**
* 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();
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 229 B

+16
View File
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 15.1.0, SVG Export Plug-In -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [
<!ENTITY ns_flows "http://ns.adobe.com/Flows/1.0/">
]>
<svg version="1.1"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
x="0px" y="0px" width="16px" height="16px" viewBox="0 0 16 16" style="overflow:visible;enable-background:new 0 0 16 16;"
xml:space="preserve" preserveAspectRatio="xMinYMid meet">
<defs>
</defs>
<path style="fill:#999999;" d="M2,5v9c0,1.1,0.9,2,2,2h8c1.1,0,2-0.9,2-2V5H2z M5,13.5C5,13.8,4.8,14,4.5,14S4,13.8,4,13.5v-6
C4,7.2,4.2,7,4.5,7S5,7.2,5,7.5V13.5z M8.5,13.5C8.5,13.8,8.3,14,8,14s-0.5-0.2-0.5-0.5v-6C7.5,7.2,7.7,7,8,7s0.5,0.2,0.5,0.5V13.5z
M12,13.5c0,0.3-0.2,0.5-0.5,0.5S11,13.8,11,13.5v-6C11,7.2,11.2,7,11.5,7S12,7.2,12,7.5V13.5z M0,4c0-1.1,0.9-2,2-2h4v0
c0-1.1,0.9-2,2-2s2,0.9,2,2v0h4c1.1,0,2,0.9,2,2H0z"/>
</svg>

After

Width:  |  Height:  |  Size: 1.0 KiB

+67
View File
@@ -0,0 +1,67 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Recycle bin settings.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $PAGE;
if ($hassiteconfig) {
$settings = new admin_settingpage('tool_recyclebin', get_string('pluginname', 'tool_recyclebin'));
$ADMIN->add('tools', $settings);
$settings->add(new admin_setting_configcheckbox(
'tool_recyclebin/coursebinenable',
new lang_string('coursebinenable', 'tool_recyclebin'),
'',
1
));
$settings->add(new admin_setting_configduration(
'tool_recyclebin/coursebinexpiry',
new lang_string('coursebinexpiry', 'tool_recyclebin'),
new lang_string('coursebinexpiry_desc', 'tool_recyclebin'),
WEEKSECS
));
$settings->add(new admin_setting_configcheckbox(
'tool_recyclebin/categorybinenable',
new lang_string('categorybinenable', 'tool_recyclebin'),
'',
1
));
$settings->add(new admin_setting_configduration(
'tool_recyclebin/categorybinexpiry',
new lang_string('categorybinexpiry', 'tool_recyclebin'),
new lang_string('categorybinexpiry_desc', 'tool_recyclebin'),
WEEKSECS
));
$settings->add(new admin_setting_configcheckbox(
'tool_recyclebin/autohide',
new lang_string('autohide', 'tool_recyclebin'),
new lang_string('autohide_desc', 'tool_recyclebin'),
1
));
}
@@ -0,0 +1,69 @@
@tool @tool_recyclebin
Feature: Backup user data
As a teacher
I want user data to be backed up when I delete a course module
So that I can recover student content
Background: Course with teacher and student exist.
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher@asd.com |
| student1 | Student | 1 | student@asd.com |
And the following "courses" exist:
| fullname | shortname |
| Course 1 | C1 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following config values are set as admin:
| coursebinenable | 1 | tool_recyclebin |
| autohide | 0 | tool_recyclebin |
@javascript
Scenario: Delete and restore a quiz with user data
Given I log in as "teacher1"
And I follow "Course 1"
And I turn editing mode on
And I add a "Quiz" to section "1" and I fill the form with:
| Name | Quiz 1 |
| Description | Test quiz description |
And I add a "True/False" question to the "Quiz 1" quiz with:
| Question name | TF1 |
| Question text | First question |
| General feedback | Thank you, this is the general feedback |
| Correct answer | False |
| Feedback for the response 'True'. | So you think it is true |
| Feedback for the response 'False'. | So you think it is false |
And I add a "True/False" question to the "Quiz 1" quiz with:
| Question name | TF2 |
| Question text | Second question |
| General feedback | Thank you, this is the general feedback |
| Correct answer | False |
| Feedback for the response 'True'. | So you think it is true |
| Feedback for the response 'False'. | So you think it is false |
And I log out
And I log in as "student1"
And I follow "Course 1"
And I follow "Quiz 1"
And I press "Attempt quiz now"
And I click on "True" "radio" in the "First question" "question"
And I click on "False" "radio" in the "Second question" "question"
And I press "Finish attempt"
And I press "Submit all and finish"
And I click on "Submit all and finish" "button" in the "Confirmation" "dialogue"
And I should see "5.00 out of 10.00"
And I log out
And I log in as "teacher1"
And I follow "Course 1"
And I turn editing mode on
And I delete "Quiz 1" activity
And I follow "Recycle bin"
And I should see "Quiz 1"
And I follow "Restore"
And I log out
And I log in as "student1"
And I follow "Course 1"
When I navigate to "Grades" node in "Course administration"
Then "Quiz 1" row "Grade" column of "user-grade" table should contain "5"
And "Quiz 1" row "Percentage" column of "user-grade" table should contain "50"
@@ -0,0 +1,106 @@
@tool @tool_recyclebin
Feature: Basic recycle bin functionality
As a teacher
I want be able to recover deleted content and manage the recycle bin content
So that I can fix an accidental deletion and clean the recycle bin
Background: Course with teacher exists.
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher@asd.com |
| student1 | Student | 1 | student@asd.com |
And the following "courses" exist:
| fullname | shortname |
| Course 1 | C1 |
| Course 2 | C2 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
And the following config values are set as admin:
| coursebinenable | 1 | tool_recyclebin |
| categorybinenable | 1 | tool_recyclebin |
| coursebinexpiry | 604800 | tool_recyclebin |
| categorybinexpiry | 1209600 | tool_recyclebin |
| autohide | 0 | tool_recyclebin |
Scenario: Restore a deleted assignment
Given I log in as "teacher1"
And I follow "Course 1"
And I turn editing mode on
And I add a "Assignment" to section "1" and I fill the form with:
| Assignment name | Test assign |
| Description | Test |
And I delete "Test assign" activity
When I follow "Recycle bin"
Then I should see "Test assign"
And I should see "Contents will be permanently deleted after 7 days"
And I follow "Restore"
And I should see "'Test assign' has been restored"
And I wait to be redirected
And I am on homepage
And I follow "Course 1"
And I should see "Test assign" in the "Topic 1" "section"
Scenario: Restore a deleted course
Given I log in as "admin"
And I go to the courses management page
And I click on "delete" action for "Course 2" in management course listing
And I press "Delete"
And I should see "Deleting C2"
And I should see "C2 has been completely deleted"
And I press "Continue"
And I go to the courses management page
And I should not see "Course 2" in the "#course-listing" "css_element"
When I follow "Recycle bin"
Then I should see "Course 2"
And I should see "Contents will be permanently deleted after 14 days"
And I follow "Restore"
And I should see "'Course 2' has been restored"
And I wait to be redirected
And I go to the courses management page
And I should see "Course 2" in the "#course-listing" "css_element"
@javascript
Scenario: Deleting a single item from the recycle bin
Given I log in as "teacher1"
And I follow "Course 1"
And I turn editing mode on
And I add a "Assignment" to section "1" and I fill the form with:
| Assignment name | Test assign |
| Description | Test |
And I delete "Test assign" activity
And I follow "Recycle bin"
When I click on "Delete" "link"
Then I should see "Are you sure you want to delete the selected item from the recycle bin?"
And I press "Cancel"
And I should see "Test assign"
And I click on "Delete" "link"
And I press "Yes"
And I should see "'Test assign' has been deleted"
And I should see "There are no items in the recycle bin."
@javascript
Scenario: Deleting all the items from the recycle bin
Given I log in as "teacher1"
And I follow "Course 1"
And I turn editing mode on
And I add a "Assignment" to section "1" and I fill the form with:
| Assignment name | Test assign 1 |
| Description | Test 1 |
And I add a "Assignment" to section "1" and I fill the form with:
| Assignment name | Test assign 2 |
| Description | Test 2 |
And I delete "Test assign 1" activity
And I delete "Test assign 2" activity
And I follow "Recycle bin"
And I should see "Test assign 1"
And I should see "Test assign 2"
When I click on "Delete all" "link"
Then I should see "Are you sure you want to delete all items from the recycle bin?"
And I press "Cancel"
And I should see "Test assign 1"
And I should see "Test assign 2"
And I click on "Delete all" "link"
And I press "Yes"
And I should see "Recycle bin has been emptied"
And I should see "There are no items in the recycle bin."
@@ -0,0 +1,168 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Recycle bin tests.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Recycle bin category tests.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_recyclebin_category_bin_tests extends advanced_testcase {
/**
* @var stdClass $course
*/
protected $course;
/**
* Setup for each test.
*/
protected function setUp() {
$this->resetAfterTest();
$this->setAdminUser();
// We want the category bin to be enabled.
set_config('categorybinenable', 1, 'tool_recyclebin');
$this->course = $this->getDataGenerator()->create_course();
}
/**
* Check that our hook is called when a course is deleted.
*/
public function test_pre_course_delete_hook() {
global $DB;
// Should have nothing in the recycle bin.
$this->assertEquals(0, $DB->count_records('tool_recyclebin_category'));
delete_course($this->course, false);
// Check the course is now in the recycle bin.
$this->assertEquals(1, $DB->count_records('tool_recyclebin_category'));
// Try with the API.
$recyclebin = new \tool_recyclebin\category_bin($this->course->category);
$this->assertEquals(1, count($recyclebin->get_items()));
}
/**
* Check that our hook is called when a course is deleted.
*/
public function test_pre_course_category_delete_hook() {
global $DB;
// Should have nothing in the recycle bin.
$this->assertEquals(0, $DB->count_records('tool_recyclebin_category'));
delete_course($this->course, false);
// Check the course is now in the recycle bin.
$this->assertEquals(1, $DB->count_records('tool_recyclebin_category'));
// Now let's delete the course category.
$category = coursecat::get($this->course->category);
$category->delete_full(false);
// Check that the course was deleted from the category recycle bin.
$this->assertEquals(0, $DB->count_records('tool_recyclebin_category'));
}
/**
* Test that we can restore recycle bin items.
*/
public function test_restore() {
global $DB;
delete_course($this->course, false);
$recyclebin = new \tool_recyclebin\category_bin($this->course->category);
foreach ($recyclebin->get_items() as $item) {
$recyclebin->restore_item($item);
}
// Check that it was restored and removed from the recycle bin.
$this->assertEquals(2, $DB->count_records('course')); // Site course and the course we restored.
$this->assertEquals(0, count($recyclebin->get_items()));
}
/**
* 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()));
}
/**
* 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);
}
}
@@ -0,0 +1,160 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Recycle bin tests.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Recycle bin course tests.
*
* @package tool_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_recyclebin_course_bin_tests extends advanced_testcase {
/**
* @var stdClass $course
*/
protected $course;
/**
* @var stdClass the quiz record
*/
protected $quiz;
/**
* Setup for each test.
*/
protected function setUp() {
$this->resetAfterTest(true);
$this->setAdminUser();
// We want the course bin to be enabled.
set_config('coursebinenable', 1, 'tool_recyclebin');
$this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->getDataGenerator()->get_plugin_generator('mod_quiz')->create_instance(array(
'course' => $this->course->id
));
}
/**
* Check that our hook is called when an activity is deleted.
*/
public function test_pre_course_module_delete_hook() {
global $DB;
// Should have nothing in the recycle bin.
$this->assertEquals(0, $DB->count_records('tool_recyclebin_course'));
// Delete the course module.
course_delete_module($this->quiz->cmid);
// Check the course module is now in the recycle bin.
$this->assertEquals(1, $DB->count_records('tool_recyclebin_course'));
// Try with the API.
$recyclebin = new \tool_recyclebin\course_bin($this->course->id);
$this->assertEquals(1, count($recyclebin->get_items()));
}
/**
* Test that we can restore recycle bin items.
*/
public function test_restore() {
global $DB;
// Delete the course module.
course_delete_module($this->quiz->cmid);
// Try restoring.
$recyclebin = new \tool_recyclebin\course_bin($this->course->id);
foreach ($recyclebin->get_items() as $item) {
$recyclebin->restore_item($item);
}
// Check that it was restored and removed from the recycle bin.
$this->assertEquals(1, $DB->count_records('course_modules'));
$this->assertEquals(0, count($recyclebin->get_items()));
}
/**
* Test that we can delete recycle bin items.
*/
public function test_delete() {
global $DB;
// Delete the course module.
course_delete_module($this->quiz->cmid);
// 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(0, $DB->count_records('course_modules'));
$this->assertEquals(0, count($recyclebin->get_items()));
}
/**
* 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);
// 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);
// 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);
}
}
+225
View File
@@ -0,0 +1,225 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Events tests.
*
* @package tool_recyclebin
* @category test
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Events tests class.
*
* @package tool_recyclebin
* @category test
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_recyclebin_events_testcase extends advanced_testcase {
/**
* Test set up.
*
* This is executed before running any test in this file.
*/
public function setUp() {
$this->resetAfterTest();
// We want the category and course bin to be enabled.
set_config('categorybinenable', 1, 'tool_recyclebin');
set_config('coursebinenable', 1, 'tool_recyclebin');
}
/**
* Test the category bin item created event.
*/
public function test_category_bin_item_created() {
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Trigger and capture the event.
$sink = $this->redirectEvents();
delete_course($course, false);
$events = $sink->get_events();
$event = reset($events);
// Get the item from the recycle bin.
$rb = new \tool_recyclebin\category_bin($course->category);
$items = $rb->get_items();
$item = reset($items);
// Check that the event contains the expected values.
$this->assertInstanceOf('\tooL_recyclebin\event\category_bin_item_created', $event);
$this->assertEquals(context_coursecat::instance($course->category), $event->get_context());
$this->assertEquals($item->id, $event->objectid);
$this->assertEventContextNotUsed($event);
}
/**
* 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 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);
}
/**
* Test the course bin item created event.
*/
public function test_course_bin_item_created() {
// 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));
// Trigger and capture the event.
$sink = $this->redirectEvents();
course_delete_module($instance->cmid);
$events = $sink->get_events();
$event = reset($events);
// Get the item from the recycle bin.
$rb = new \tool_recyclebin\course_bin($course->id);
$items = $rb->get_items();
$item = reset($items);
// Check that the event contains the expected values.
$this->assertInstanceOf('\tooL_recyclebin\event\course_bin_item_created', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals($item->id, $event->objectid);
$this->assertEventContextNotUsed($event);
}
/**
* 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);
}
/**
* 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);
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines the version.
*
* @package tool_recyclebin
* @copyright 2016 Skylar Kelty <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2016030200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2015111000; // Requires this Moodle version.
$plugin->component = 'tool_recyclebin'; // Full name of the plugin (used for diagnostics).
+9
View File
@@ -1696,6 +1696,15 @@ function course_delete_module($cmid) {
"Cannot delete this module as the function {$modulename}_delete_instance is missing in mod/$modulename/lib.php.");
}
// Allow plugins to use this course module before we completely delete it.
if ($pluginsfunction = get_plugins_with_function('pre_course_module_delete')) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$pluginfunction($cm);
}
}
}
// Delete activity context questions and question categories.
question_delete_activity($cm);
+9
View File
@@ -2041,6 +2041,15 @@ function blocks_name_allowed_in_format($name, $pageformat) {
function blocks_delete_instance($instance, $nolongerused = false, $skipblockstables = false) {
global $DB;
// Allow plugins to use this block before we completely delete it.
if ($pluginsfunction = get_plugins_with_function('pre_block_delete')) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$pluginfunction($instance);
}
}
}
if ($block = block_instance($instance->blockname, $instance)) {
$block->instance_delete();
}
+1 -1
View File
@@ -1901,7 +1901,7 @@ class core_plugin_manager {
'assignmentupgrade', 'availabilityconditions', 'behat', 'capability', 'customlang',
'dbtransfer', 'filetypes', 'generator', 'health', 'innodb', 'installaddon',
'langimport', 'log', 'messageinbound', 'multilangupgrade', 'monitor', 'phpunit', 'profiling',
'replace', 'spamcleaner', 'task', 'templatelibrary',
'recyclebin', 'replace', 'spamcleaner', 'task', 'templatelibrary',
'unittest', 'uploadcourse', 'uploaduser', 'unsuproles', 'xmldb'
),
+10
View File
@@ -1670,6 +1670,16 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate {
// Make sure we won't timeout when deleting a lot of courses.
$settimeout = core_php_time_limit::raise();
// Allow plugins to use this category before we completely delete it.
if ($pluginsfunction = get_plugins_with_function('pre_course_category_delete')) {
$category = $this->get_db_record();
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$pluginfunction($category);
}
}
}
$deletedcourses = array();
// Get children. Note, we don't want to use cache here because it would be rebuilt too often.
+18
View File
@@ -3928,6 +3928,15 @@ function delete_user(stdClass $user) {
return false;
}
// Allow plugins to use this user object before we completely delete it.
if ($pluginsfunction = get_plugins_with_function('pre_user_delete')) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$pluginfunction($user);
}
}
}
// Keep user record before updating it, as we have to pass this to user_deleted event.
$olduser = clone $user;
@@ -4677,6 +4686,15 @@ function delete_course($courseorid, $showfeedback = true) {
return false;
}
// Allow plugins to use this course before we completely delete it.
if ($pluginsfunction = get_plugins_with_function('pre_course_delete')) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$pluginfunction($course);
}
}
}
// Make the course completely empty.
remove_course_contents($courseid, $showfeedback);
+1 -1
View File
@@ -69,7 +69,7 @@ class core_event_grade_deleted_testcase extends advanced_testcase {
$sink = $this->redirectEvents();
course_delete_module($quiz->cmid);
$events = $sink->get_events();
$event = reset($events);
$event = $events[1];
$sink->close();
// Check the event details are correct.