first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 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.
*/
abstract public function get_item($itemid);
/**
* Returns a list of items in the recycle bin.
*/
abstract public function get_items();
/**
* Store an item in this recycle bin.
*
* @param \stdClass $item Item to store.
*/
abstract public function store_item($item);
/**
* Restore an item from the recycle bin.
*
* @param \stdClass $item The item database record
*/
abstract public function restore_item($item);
/**
* Delete an item from the recycle bin.
*
* @param \stdClass $item The item database record
*/
abstract public 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?
*/
abstract public function can_view();
/**
* Can we restore items in this recycle bin?
*/
abstract public function can_restore();
/**
* Can we delete this?
*/
abstract public function can_delete();
}
@@ -0,0 +1,392 @@
<?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');
// As far as recycle bin is using MODE_AUTOMATED, it observes the backup_auto_storage
// settings (storing backups @ real location and potentially not including files).
// For recycle bin we want to ensure that backup files are always stored in Moodle file
// area and always contain the users' files. In order to achieve that, we hack the
// setting here via $CFG->forced_plugin_settings, so it won't interfere other operations.
// See MDL-65218 and MDL-35773 for more information.
// This hack will be removed once recycle bin switches to use its own backup mode, with
// own preferences and 100% separate from MOODLE_AUTOMATED.
// TODO: Remove this as part of MDL-65228.
$forcedbackupsettings = $CFG->forced_plugin_settings['backup'] ?? [];
$CFG->forced_plugin_settings['backup']['backup_auto_storage'] = 0;
$CFG->forced_plugin_settings['backup']['backup_auto_files'] = 1;
// Backup the course.
$user = get_admin();
$controller = new \backup_controller(
\backup::TYPE_1COURSE,
$course->id,
\backup::FORMAT_MOODLE,
\backup::INTERACTIVE_NO,
\backup::MODE_AUTOMATED,
$user->id
);
$controller->execute_plan();
// We don't need the forced setting anymore, hence restore previous settings.
// TODO: Remove this as part of MDL-65228.
$CFG->forced_plugin_settings['backup'] = $forcedbackupsettings;
// Grab the result.
$result = $controller->get_results();
if (!isset($result['backup_destination'])) {
throw new \moodle_exception('Failed to backup activity prior to deletion.');
}
// Have finished with the controller, let's destroy it, freeing mem and resources.
$controller->destroy();
// 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 backup temp directory name and create it.
$tempdir = \restore_controller::get_tempdir_name($context->id, $user->id);
$fulltempdir = make_backup_temp_directory($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.");
}
// As far as recycle bin is using MODE_AUTOMATED, it observes the General restore settings.
// For recycle bin we want to ensure that backup files are always restore the users and groups information.
// In order to achieve that, we hack the setting here via $CFG->forced_plugin_settings,
// so it won't interfere other operations.
// See MDL-65218 and MDL-35773 for more information.
// This hack will be removed once recycle bin switches to use its own backup mode, with
// own preferences and 100% separate from MOODLE_AUTOMATED.
// TODO: Remove this as part of MDL-65228.
$forcedrestoresettings = $CFG->forced_plugin_settings['restore'] ?? [];
$CFG->forced_plugin_settings['restore']['restore_general_users'] = 1;
$CFG->forced_plugin_settings['restore']['restore_general_groups'] = 1;
// Define the import.
$controller = new \restore_controller(
$tempdir,
$course->id,
\backup::INTERACTIVE_NO,
\backup::MODE_AUTOMATED,
$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();
// We don't need the forced setting anymore, hence restore previous settings.
// TODO: Remove this as part of MDL-65228.
$CFG->forced_plugin_settings['restore'] = $forcedrestoresettings;
// Have finished with the controller, let's destroy it, freeing mem and resources.
$controller->destroy();
// 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, IGNORE_MISSING);
if (!empty($context)) {
// Delete the files.
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'tool_recyclebin', TOOL_RECYCLEBIN_COURSECAT_BIN_FILEAREA, $item->id);
} else {
// Course category has been deleted. Find records using $item->id as this is unique for coursecat recylebin.
$files = $DB->get_recordset('files', [
'component' => 'tool_recyclebin',
'filearea' => TOOL_RECYCLEBIN_COURSECAT_BIN_FILEAREA,
'itemid' => $item->id,
]);
$fs = get_file_storage();
foreach ($files as $filer) {
$file = $fs->get_file_instance($filer);
$file->delete();
}
$files->close();
}
// Delete the record.
$DB->delete_records('tool_recyclebin_category', array(
'id' => $item->id
));
// The coursecat might have been deleted, check we have a context before triggering event.
if (!$context) {
return;
}
// 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,390 @@
<?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;
}
// As far as recycle bin is using MODE_AUTOMATED, it observes the backup_auto_storage
// settings (storing backups @ real location and potentially not including files).
// For recycle bin we want to ensure that backup files are always stored in Moodle file
// area and always contain the users' files. In order to achieve that, we hack the
// setting here via $CFG->forced_plugin_settings, so it won't interfere other operations.
// See MDL-65218 and MDL-35773 for more information.
// This hack will be removed once recycle bin switches to use its own backup mode, with
// own preferences and 100% separate from MOODLE_AUTOMATED.
// TODO: Remove this as part of MDL-65228.
$forcedbackupsettings = $CFG->forced_plugin_settings['backup'] ?? [];
$CFG->forced_plugin_settings['backup']['backup_auto_storage'] = 0;
$CFG->forced_plugin_settings['backup']['backup_auto_files'] = 1;
// Backup the activity.
$user = get_admin();
$controller = new \backup_controller(
\backup::TYPE_1ACTIVITY,
$cm->id,
\backup::FORMAT_MOODLE,
\backup::INTERACTIVE_NO,
\backup::MODE_AUTOMATED,
$user->id
);
// When "backup_auto_activities" setting is disabled, activities can't be restored from recycle bin.
$plan = $controller->get_plan();
$activitiessettings = $plan->get_setting('activities');
$settingsvalue = $activitiessettings->get_value();
if (empty($settingsvalue)) {
$controller->destroy();
return;
}
$controller->execute_plan();
// We don't need the forced setting anymore, hence restore previous settings.
// TODO: Remove this as part of MDL-65228.
$CFG->forced_plugin_settings['backup'] = $forcedbackupsettings;
// Grab the result.
$result = $controller->get_results();
if (!isset($result['backup_destination'])) {
throw new \moodle_exception('Failed to backup activity prior to deletion.');
}
// Have finished with the controller, let's destroy it, freeing mem and resources.
$controller->destroy();
// 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 backup temp directory name and create it.
$tempdir = \restore_controller::get_tempdir_name($context->id, $user->id);
$fulltempdir = make_backup_temp_directory($tempdir);
// Extract the backup to tempdir.
$fb = get_file_packer('application/vnd.moodle.backup');
$fb->extract_to_pathname($file, $fulltempdir);
// As far as recycle bin is using MODE_AUTOMATED, it observes the General restore settings.
// For recycle bin we want to ensure that backup files are always restore the users and groups information.
// In order to achieve that, we hack the setting here via $CFG->forced_plugin_settings,
// so it won't interfere other operations.
// See MDL-65218 and MDL-35773 for more information.
// This hack will be removed once recycle bin switches to use its own backup mode, with
// own preferences and 100% separate from MOODLE_AUTOMATED.
// TODO: Remove this as part of MDL-65228.
$forcedrestoresettings = $CFG->forced_plugin_settings['restore'] ?? null;
$CFG->forced_plugin_settings['restore']['restore_general_users'] = 1;
$CFG->forced_plugin_settings['restore']['restore_general_groups'] = 1;
// Define the import.
$controller = new \restore_controller(
$tempdir,
$this->_courseid,
\backup::INTERACTIVE_NO,
\backup::MODE_AUTOMATED,
$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();
// We don't need the forced setting anymore, hence restore previous settings.
// TODO: Remove this as part of MDL-65228.
$CFG->forced_plugin_settings['restore'] = $forcedrestoresettings;
// Have finished with the controller, let's destroy it, freeing mem and resources.
$controller->destroy();
// 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, IGNORE_MISSING);
if (!empty($context)) {
// Delete the files.
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'tool_recyclebin', TOOL_RECYCLEBIN_COURSE_BIN_FILEAREA, $item->id);
} else {
// Course context has been deleted. Find records using $item->id as this is unique for course bin recyclebin.
$files = $DB->get_recordset('files', [
'component' => 'tool_recyclebin',
'filearea' => TOOL_RECYCLEBIN_COURSE_BIN_FILEAREA,
'itemid' => $item->id,
]);
$fs = get_file_storage();
foreach ($files as $filer) {
$file = $fs->get_file_instance($filer);
$file->delete();
}
$files->close();
}
// 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,46 @@
<?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/>.
/**
* Privacy Subsystem implementation for tool_recyclebin.
*
* @package tool_recyclebin
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_recyclebin\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for tool_recyclebin implementing null_provider.
*
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
@@ -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
)
)
);
+41
View File
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="admin/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="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="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(__DIR__ . '/../../../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(null, false);
$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:
throw new \moodle_exception('invalidcontext', 'tool_recyclebin');
break;
}
if (!$recyclebin::is_enabled()) {
throw new \moodle_exception('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 {
throw new \moodle_exception('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 {
throw new \moodle_exception('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 = $OUTPUT->image_icon('monologo', $modname, $mod->name) . $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 <S.Kelty@kent.ac.uk>
* @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['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';
$string['privacy:metadata'] = 'The Recycle bin plugin does not store any personal data.';
+208
View File
@@ -0,0 +1,208 @@
<?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 <S.Kelty@kent.ac.uk>
* @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 to check whether async course module deletion should be performed or not.
*
* @return true if background deletion is required (is the recyclebin is enabled), false otherwise.
*/
function tool_recyclebin_course_module_background_deletion_recommended() {
if (\tool_recyclebin\course_bin::is_enabled()) {
return true;
}
}
/**
* 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);
}
}
/**
* 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();
}
/**
* Map icons for font-awesome themes.
*/
function tool_recyclebin_get_fontawesome_icon_map() {
return [
'tool_recyclebin:trash' => 'fa-trash'
];
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 247 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:#888;" 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,64 @@
@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 |
And the following "activities" exist:
| activity | course | section | name | intro |
| quiz | C1 | 1 | Quiz 1 | Test quiz description |
And the following "question categories" exist:
| contextlevel | reference | name |
| Course | C1 | Test questions |
And the following "questions" exist:
| questioncategory | qtype | name | questiontext |
| Test questions | truefalse | TF1 | First question |
| Test questions | truefalse | TF2 | Second question |
And quiz "Quiz 1" contains the following questions:
| question | page |
| TF1 | 1 |
| TF2 | 1 |
@javascript
Scenario Outline: Delete and restore a quiz with user data
Given the following config values are set as admin:
| restore_general_users | <include_user> | restore |
And I am on the "Quiz 1" "quiz activity" page logged in as student1
And I press "Attempt quiz"
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" "button" in the "Submit all your answers and finish?" "dialogue"
And I should see "50.00 out of 100.00"
And I log out
And I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
And I delete "Quiz 1" activity
And I run all adhoc tasks
And I navigate to "Recycle bin" in current page administration
And I should see "Quiz 1"
And I click on "Restore" "link" in the "region-main" "region"
When I am on the "Course 1" "grades > User report > View" page logged in as "student1"
Then "Quiz 1" row "Grade" column of "user-grade" table should contain "50"
And "Quiz 1" row "Percentage" column of "user-grade" table should contain "50"
Examples:
| include_user | case_explanation |
| 1 | Checked |
| 1 | Unchecked |
@@ -0,0 +1,152 @@
@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 |
| student2 | Student | 2 | student2@asd.com |
And the following "courses" exist:
| fullname | shortname | initsections |
| Course 1 | C1 | 1 |
| Course 2 | C2 | 0 |
And the following "activities" exist:
| activity | course | section | name | intro |
| assign | C1 | 1 | Test assign 1 | Test 1 |
| assign | C1 | 1 | Test assign 2 | Test 2 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
| student2 | C1 | student |
| teacher1 | C2 | editingteacher |
| student1 | C2 | student |
| student2 | C2 | student |
And the following "groups" exist:
| name | course | idnumber |
| Group A | C2 | G1 |
| Group B | C2 | G2 |
| Group C | C2 | G3 |
And the following "group members" exist:
| user | group |
| teacher1 | G1 |
| teacher1 | G2 |
| student1 | G1 |
| student2 | G1 |
| student2 | G2 |
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 am on "Course 1" course homepage with editing mode on
And I delete "Test assign 1" activity
When I navigate to "Recycle bin" in current page administration
Then I should see "Test assign 1"
And I should see "Contents will be permanently deleted after 7 days"
And I click on "Restore" "link" in the "region-main" "region"
And I should see "'Test assign 1' has been restored"
And I wait to be redirected
And I am on "Course 1" course homepage
And I should see "Test assign 1" in the "Section 1" "section"
@javascript
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 am on course index
And I should see "Course 1"
And I should not see "Course 2"
When I navigate to "Recycle bin" in current page administration
Then I should see "Course 2"
And I should see "Contents will be permanently deleted after 14 days"
And I click on "Restore" "link" in the "region-main" "region"
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"
And I am on the "Course 2" "groups overview" page
And "Student 1" "text" should exist in the "Group A" "table_row"
And "Student 2" "text" should exist in the "Group A" "table_row"
And "Student 2" "text" should exist in the "Group B" "table_row"
@javascript
Scenario: Deleting a single item from the recycle bin
Given I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
And I delete "Test assign 1" activity
And I run all adhoc tasks
And I navigate to "Recycle bin" in current page administration
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 click on "Cancel" "button" in the "Confirmation" "dialogue"
And I should see "Test assign 1"
And I click on "Delete" "link"
And I press "Yes"
And I should see "'Test assign 1' 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 am on "Course 1" course homepage with editing mode on
And I delete "Test assign 1" activity
And I delete "Test assign 2" activity
And I run all adhoc tasks
And I navigate to "Recycle bin" in current page administration
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 click on "Cancel" "button" in the "Confirmation" "dialogue"
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."
@javascript
Scenario: Show recycle bin on category action menu
Given I log in as "admin"
And I navigate to "Courses > Manage courses and categories" in site administration
And I navigate to "Recycle bin" in current page administration
Then I should see "There are no items in the recycle bin."
@javascript
Scenario: Not show recycle bin empty on category action menu whit autohide enable
Given I log in as "admin"
And the following config values are set as admin:
| categorybinenable | 0 | tool_recyclebin |
And I navigate to "Courses > Manage courses and categories" in site administration
And I click on "Actions menu" "link"
Then I should not see "Recycle bin"
@javascript
Scenario: Show recycle bin not empty on category action menu whit autohide enable
Given I log in as "admin"
And the following config values are set as admin:
| autohide | 1 | tool_recyclebin |
And I navigate to "Courses > Manage courses and categories" in site administration
And I click on "Actions menu" "link"
Then I should not see "Recycle bin"
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"
When I click on "Actions menu" "link"
Then I should see "Recycle bin"
@@ -0,0 +1,288 @@
<?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/>.
namespace tool_recyclebin;
/**
* 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 category_bin_test extends \advanced_testcase {
/**
* @var \stdClass $course
*/
protected $course;
/**
* @var \stdClass $coursebeingrestored
*/
protected $coursebeingrestored;
/**
* Setup for each test.
*/
protected function setUp(): void {
$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(): void {
global $DB;
// This simulates a temporary course being cleaned up by a course restore.
$this->coursebeingrestored = $this->getDataGenerator()->create_course();
$this->coursebeingrestored->deletesource = 'restore';
// Should have nothing in the recycle bin.
$this->assertEquals(0, $DB->count_records('tool_recyclebin_category'));
delete_course($this->course, false);
// This should not be added to the recycle bin.
delete_course($this->coursebeingrestored, 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(): void {
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 = \core_course_category::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(): void {
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(): void {
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(): void {
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);
}
/**
* Provider for test_course_restore_with_userdata() and test_course_restore_without_userdata()
*
* Used to verify that recycle bin is immune to various settings. Provides plugin, name, value for
* direct usage with set_config()
*/
public function recycle_bin_settings_provider() {
return [
'backup/backup_auto_storage moodle' => [[
(object)['plugin' => 'backup', 'name' => 'backup_auto_storage', 'value' => 0],
]],
'backup/backup_auto_storage external' => [[
(object)['plugin' => 'backup', 'name' => 'backup_auto_storage', 'value' => 1],
(object)['plugin' => 'backup', 'name' => 'backup_auto_destination', 'value' => true],
]],
'backup/backup_auto_storage mixed' => [[
(object)['plugin' => 'backup', 'name' => 'backup_auto_storage', 'value' => 2],
(object)['plugin' => 'backup', 'name' => 'backup_auto_destination', 'value' => true],
]],
'restore/restore_general_users moodle' => [[
(object)['plugin' => 'restore', 'name' => 'restore_general_users', 'value' => 0],
(object)['plugin' => 'restore', 'name' => 'restore_general_groups', 'value' => 0],
]],
];
}
/**
* Tests that user data is restored when course is restored.
*
* @dataProvider recycle_bin_settings_provider
* @param array $settings array of plugin, name, value stdClass().
*/
public function test_course_restore_with_userdata($settings): void {
global $DB;
// Force configuration changes from provider.
foreach ($settings as $setting) {
// Need to create a directory for backup_auto_destination.
if ($setting->plugin === 'backup' && $setting->name === 'backup_auto_destination' && $setting->value === true) {
$setting->value = make_request_directory();
}
set_config($setting->name, $setting->value, $setting->plugin);
}
// We want user data to be included for this test.
set_config('backup_auto_users', true, 'backup');
$student = $this->getDataGenerator()->create_and_enrol($this->course, 'student');
// Delete course.
delete_course($this->course, false);
$this->assertFalse($DB->record_exists('course', ['id' => $this->course->id]));
// Verify there is now a backup @ cat recycle bin file area.
$recyclebin = new \tool_recyclebin\category_bin($this->course->category);
$this->assertEquals(1, count($recyclebin->get_items()));
// Restore the recycle bin item.
$recyclebin->restore_item(current($recyclebin->get_items()));
// Get the new course.
$newcourse = $DB->get_record('course', ['shortname' => $this->course->shortname], '*', MUST_EXIST);
// Check that it was removed from the recycle bin.
$this->assertEquals(0, count($recyclebin->get_items()));
// Verify that student DOES continue enrolled.
$this->assertTrue(is_enrolled(\context_course::instance($newcourse->id), $student->id));
}
/**
* Tests that user data is not restored when course is restored.
*
* @dataProvider recycle_bin_settings_provider
* @param array $settings array of plugin, name, value stdClass().
*/
public function test_course_restore_without_userdata($settings): void {
global $DB;
// Force configuration changes from provider.
foreach ($settings as $setting) {
// Need to create a directory for backup_auto_destination.
if ($setting->plugin === 'backup' && $setting->name === 'backup_auto_destination' && $setting->value === true) {
$setting->value = make_request_directory();
}
set_config($setting->name, $setting->value, $setting->plugin);
}
// We want user data to be included for this test.
set_config('backup_auto_users', false, 'backup');
$student = $this->getDataGenerator()->create_and_enrol($this->course, 'student');
// Delete course.
delete_course($this->course, false);
$this->assertFalse($DB->record_exists('course', ['id' => $this->course->id]));
// Verify there is now a backup @ cat recycle bin file area.
$recyclebin = new \tool_recyclebin\category_bin($this->course->category);
$this->assertEquals(1, count($recyclebin->get_items()));
// Restore the recycle bin item.
$recyclebin->restore_item(current($recyclebin->get_items()));
// Get the new course.
$newcourse = $DB->get_record('course', ['shortname' => $this->course->shortname], '*', MUST_EXIST);
// Check that it was removed from the recycle bin.
$this->assertEquals(0, count($recyclebin->get_items()));
// Verify that student DOES NOT continue enrolled.
$this->assertFalse(is_enrolled(\context_course::instance($newcourse->id), $student->id));
}
}
@@ -0,0 +1,342 @@
<?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/>.
namespace tool_recyclebin;
use mod_quiz\quiz_attempt;
use stdClass;
/**
* 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 course_bin_test extends \advanced_testcase {
/**
* @var \stdClass $course
*/
protected $course;
/**
* @var stdClass the quiz record
*/
protected $quiz;
/**
* Setup for each test.
*/
protected function setUp(): void {
$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, 'grade' => 100.0, 'sumgrades' => 1
));
}
/**
* Check that our hook is called when an activity is deleted.
*/
public function test_pre_course_module_delete_hook(): void {
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);
// Now, run the course module deletion adhoc task.
\phpunit_util::run_all_adhoc_tasks();
// 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(): void {
global $DB;
$startcount = $DB->count_records('course_modules');
// 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($startcount, $DB->count_records('course_modules'));
$this->assertEquals(0, count($recyclebin->get_items()));
}
/**
* Test that we can delete recycle bin items.
*/
public function test_delete(): void {
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 cleanup task.
*/
public function test_cleanup_task(): void {
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);
}
/**
* Provider for test_coursemodule_restore_with_userdata() and test_coursemodule_restore_without_userdata()
*
* Used to verify that recycle bin is immune to various settings. Provides plugin, name, value for
* direct usage with set_config()
*/
public function recycle_bin_settings_provider() {
return [
'backup/backup_auto_storage moodle' => [[
(object)['plugin' => 'backup', 'name' => 'backup_auto_storage', 'value' => 0],
]],
'backup/backup_auto_storage external' => [[
(object)['plugin' => 'backup', 'name' => 'backup_auto_storage', 'value' => 1],
(object)['plugin' => 'backup', 'name' => 'backup_auto_destination', 'value' => true],
]],
'backup/backup_auto_storage mixed' => [[
(object)['plugin' => 'backup', 'name' => 'backup_auto_storage', 'value' => 2],
(object)['plugin' => 'backup', 'name' => 'backup_auto_destination', 'value' => true],
]],
'restore/restore_general_users moodle' => [[
(object)['plugin' => 'restore', 'name' => 'restore_general_users', 'value' => 0],
(object)['plugin' => 'restore', 'name' => 'restore_general_groups', 'value' => 0],
]],
];
}
/**
* Tests that user data is restored when module is restored.
*
* @dataProvider recycle_bin_settings_provider
* @param array $settings array of plugin, name, value stdClass().
*/
public function test_coursemodule_restore_with_userdata($settings): void {
// Force configuration changes from provider.
foreach ($settings as $setting) {
// Need to create a directory for backup_auto_destination.
if ($setting->plugin === 'backup' && $setting->name === 'backup_auto_destination' && $setting->value === true) {
$setting->value = make_request_directory();
}
set_config($setting->name, $setting->value, $setting->plugin);
}
$student = $this->getDataGenerator()->create_and_enrol($this->course, 'student');
$this->setUser($student);
set_config('backup_auto_users', true, 'backup');
$this->create_quiz_attempt($this->quiz, $student);
// Delete quiz.
$cm = get_coursemodule_from_instance('quiz', $this->quiz->id);
course_delete_module($cm->id);
\phpunit_util::run_all_adhoc_tasks();
$quizzes = get_coursemodules_in_course('quiz', $this->course->id);
$this->assertEquals(0, count($quizzes));
// Restore quiz.
$recyclebin = new \tool_recyclebin\course_bin($this->course->id);
foreach ($recyclebin->get_items() as $item) {
$recyclebin->restore_item($item);
}
$quizzes = get_coursemodules_in_course('quiz', $this->course->id);
$this->assertEquals(1, count($quizzes));
$cm = array_pop($quizzes);
// Check if user quiz attempt data is restored.
$attempts = quiz_get_user_attempts($cm->instance, $student->id);
$this->assertEquals(1, count($attempts));
$attempt = array_pop($attempts);
$attemptobj = quiz_attempt::create($attempt->id);
$this->assertEquals($student->id, $attemptobj->get_userid());
$this->assertEquals(true, $attemptobj->is_finished());
}
/**
* Test that the activity is NOT stored in bin when
* in Automated backup setup settings "backup_auto_activities" is disabled.
*
* @dataProvider recycle_bin_settings_provider
* @covers ::store_item
*/
public function test_coursemodule_restore_with_activity_setting_disabled(): void {
// Set the configuration to not include activities in the automated backup.
set_config('backup_auto_activities', false, 'backup');
// Delete the course module.
course_delete_module($this->quiz->cmid);
// Now, run the course module deletion adhoc task.
\phpunit_util::run_all_adhoc_tasks();
// Check there is no items in the recycle bin.
$recyclebin = new \tool_recyclebin\course_bin($this->course->id);
$this->assertEquals(0, count($recyclebin->get_items()));
}
/**
* Tests that user data is not restored when module is restored.
*
* @dataProvider recycle_bin_settings_provider
* @param array $settings array of plugin, name, value stdClass().
*/
public function test_coursemodule_restore_without_userdata($settings): void {
// Force configuration changes from provider.
foreach ($settings as $setting) {
// Need to create a directory for backup_auto_destination.
if ($setting->plugin === 'backup' && $setting->name === 'backup_auto_destination' && $setting->value === true) {
$setting->value = make_request_directory();
}
set_config($setting->name, $setting->value, $setting->plugin);
}
$student = $this->getDataGenerator()->create_and_enrol($this->course, 'student');
$this->setUser($student);
set_config('backup_auto_users', false, 'backup');
$this->create_quiz_attempt($this->quiz, $student);
// Delete quiz.
$cm = get_coursemodule_from_instance('quiz', $this->quiz->id);
course_delete_module($cm->id);
\phpunit_util::run_all_adhoc_tasks();
$quizzes = get_coursemodules_in_course('quiz', $this->course->id);
$this->assertEquals(0, count($quizzes));
// Restore quiz.
$recyclebin = new \tool_recyclebin\course_bin($this->course->id);
foreach ($recyclebin->get_items() as $item) {
$recyclebin->restore_item($item);
}
$quizzes = get_coursemodules_in_course('quiz', $this->course->id);
$this->assertEquals(1, count($quizzes));
$cm = array_pop($quizzes);
// Check if user quiz attempt data is restored.
$attempts = quiz_get_user_attempts($cm->instance, $student->id);
$this->assertEquals(0, count($attempts));
}
/**
* Add a question to quiz and create a quiz attempt.
* @param \stdClass $quiz Quiz
* @param \stdClass $student User
* @throws coding_exception
* @throws moodle_exception
*/
private function create_quiz_attempt($quiz, $student) {
// Add Question.
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();
$numq = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
quiz_add_quiz_question($numq->id, $quiz);
// Create quiz attempt.
$quizobj = \mod_quiz\quiz_settings::create($quiz->id, $student->id);
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
$timenow = time();
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $student->id);
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
quiz_attempt_save_started($quizobj, $quba, $attempt);
$attemptobj = quiz_attempt::create($attempt->id);
$tosubmit = array(1 => array('answer' => '0'));
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
$attemptobj = quiz_attempt::create($attempt->id);
$attemptobj->process_finish($timenow, false);
}
}
@@ -0,0 +1,233 @@
<?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 <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_recyclebin\event;
/**
* Events tests class.
*
* @package tool_recyclebin
* @category test
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class events_test extends \advanced_testcase {
/**
* Test set up.
*
* This is executed before running any test in this file.
*/
public function setUp(): void {
$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(): void {
// 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);
// Need the second event here, the first is backup created.
$event = next($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(): void {
// 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(): void {
// 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[count($events) - 2];
// 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(): void {
// 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(): void {
// 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(): void {
// 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();
$eventscount = 0;
foreach ($events as $event) {
if ($event instanceof \tooL_recyclebin\event\course_bin_item_restored) {
// Check that the event contains the expected values.
$this->assertEquals(\context_course::instance($course->id), $event->get_context());
$this->assertEquals($item->id, $event->objectid);
$this->assertEventContextNotUsed($event);
$eventscount++;
}
}
// Only one course_bin_item_restored event should be triggered.
$this->assertEquals(1, $eventscount);
}
}
+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 <S.Kelty@kent.ac.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024042200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2024041600; // Requires this Moodle version.
$plugin->component = 'tool_recyclebin'; // Full name of the plugin (used for diagnostics).