diff --git a/admin/tool/recyclebin/.travis.yml b/admin/tool/recyclebin/.travis.yml
new file mode 100644
index 00000000000..48586350136
--- /dev/null
+++ b/admin/tool/recyclebin/.travis.yml
@@ -0,0 +1,51 @@
+language: php
+
+sudo: false
+
+cache:
+ directories:
+ - $HOME/.composer/cache
+
+php:
+ - 5.4
+ - 5.5
+# - 5.6
+# - 7.0
+
+matrix:
+ fast_finish: true
+ allow_failures:
+ - php: 7.0
+
+env:
+ matrix:
+ - DB=pgsql MOODLE_BRANCH=MOODLE_27_STABLE
+ - DB=pgsql MOODLE_BRANCH=MOODLE_28_STABLE
+ - DB=pgsql MOODLE_BRANCH=MOODLE_29_STABLE
+ - DB=pgsql MOODLE_BRANCH=MOODLE_30_STABLE
+ - DB=mysqli MOODLE_BRANCH=MOODLE_27_STABLE
+ - DB=mysqli MOODLE_BRANCH=MOODLE_28_STABLE
+ - DB=mysqli MOODLE_BRANCH=MOODLE_29_STABLE
+ - DB=mysqli MOODLE_BRANCH=MOODLE_30_STABLE
+
+before_install:
+ - cd ../..
+ - composer selfupdate
+ - composer create-project -n --no-dev moodlerooms/moodle-plugin-ci ci ^1
+ - export PATH="$(cd ci/bin; pwd):$(cd ci/vendor/bin; pwd):$PATH"
+
+install:
+ - moodle-plugin-ci install
+ - sed -i.bak "s/\/\/ Get the module context./\\\\local_recyclebin\\\\observer::pre_cm_delete(\$cm);/g" /home/travis/build/moodle/course/lib.php
+ - sed -i.bak "s/\/\/ Make the course completely empty./\\\\local_recyclebin\\\\observer::pre_course_delete(\$course);/g" /home/travis/build/moodle/lib/moodlelib.php
+
+script:
+ - moodle-plugin-ci phplint
+ - moodle-plugin-ci phpcpd
+# - moodle-plugin-ci phpmd
+ - moodle-plugin-ci codechecker
+# - moodle-plugin-ci csslint
+# - moodle-plugin-ci shifter
+# - moodle-plugin-ci jshint
+ - moodle-plugin-ci phpunit
+# - moodle-plugin-ci behat
diff --git a/admin/tool/recyclebin/LICENSE b/admin/tool/recyclebin/LICENSE
new file mode 100644
index 00000000000..8cdb8451d9b
--- /dev/null
+++ b/admin/tool/recyclebin/LICENSE
@@ -0,0 +1,340 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users. This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it. (Some other Free Software Foundation software is covered by
+the GNU Lesser General Public License instead.) You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+ To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have. You must make sure that they, too, receive or can get the
+source code. And you must show them these terms so they know their
+rights.
+
+ We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+ Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software. If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+ Finally, any free program is threatened constantly by software
+patents. We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary. To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ GNU GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License. The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language. (Hereinafter, translation is included without limitation in
+the term "modification".) Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+ 1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+ 2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) You must cause the modified files to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ b) You must cause any work that you distribute or publish, that in
+ whole or in part contains or is derived from the Program or any
+ part thereof, to be licensed as a whole at no charge to all third
+ parties under the terms of this License.
+
+ c) If the modified program normally reads commands interactively
+ when run, you must cause it, when started running for such
+ interactive use in the most ordinary way, to print or display an
+ announcement including an appropriate copyright notice and a
+ notice that there is no warranty (or else, saying that you provide
+ a warranty) and that users may redistribute the program under
+ these conditions, and telling the user how to view a copy of this
+ License. (Exception: if the Program itself is interactive but
+ does not normally print such an announcement, your work based on
+ the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+ a) Accompany it with the complete corresponding machine-readable
+ source code, which must be distributed under the terms of Sections
+ 1 and 2 above on a medium customarily used for software interchange; or,
+
+ b) Accompany it with a written offer, valid for at least three
+ years, to give any third party, for a charge no more than your
+ cost of physically performing source distribution, a complete
+ machine-readable copy of the corresponding source code, to be
+ distributed under the terms of Sections 1 and 2 above on a medium
+ customarily used for software interchange; or,
+
+ c) Accompany it with the information you received as to the offer
+ to distribute corresponding source code. (This alternative is
+ allowed only for noncommercial distribution and only if you
+ received the program in object code or executable form with such
+ an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it. For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable. However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License. Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+ 5. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Program or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+ 6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+ 7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all. For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded. In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+ 9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation. If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+ 10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission. For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this. Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+ NO WARRANTY
+
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ {description}
+ Copyright (C) {year} {fullname}
+
+ This program 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 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+ Gnomovision version 69, Copyright (C) year name of author
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+ {signature of Ty Coon}, 1 April 1989
+ Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs. If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library. If this is what you want to do, use the GNU Lesser General
+Public License instead of this License.
+
diff --git a/admin/tool/recyclebin/README.md b/admin/tool/recyclebin/README.md
new file mode 100644
index 00000000000..cac3fede6e0
--- /dev/null
+++ b/admin/tool/recyclebin/README.md
@@ -0,0 +1,45 @@
+# Moodle Recycle Bin [](https://travis-ci.org/unikent/moodle-local_recyclebin)
+This plugin adds a "recycle bin" for course modules to Moodle.
+It requires a core hack.
+
+See plugin pages here: https://moodle.org/plugins/view/local_recyclebin
+
+See documentation here: https://docs.moodle.org/29/en/local/Recycle_bin
+
+## Installation - course recyclebin
+As there is no "pre-cm-deleted" event, you will need to add a line to '/course/lib.php' (function course_delete_module), right after the first "if()".
+```
+diff --git a/course/lib.php b/course/lib.php
+index e49bdf1..5f8d6e6 100644
+--- a/course/lib.php
++++ b/course/lib.php
+@@ -1654,6 +1654,9 @@ function course_delete_module($cmid) {
+ return true;
+ }
+
++ // Notify the recycle bin plugin.
++ \local_recyclebin\observer::pre_cm_delete($cm);
++
+ // Get the module context.
+ $modcontext = context_module::instance($cm->id);
+
+```
+
+## Installation - category recyclebin
+As there is no "pre-course-deleted" event, you will need to add a line to '/lib/moodlelib.php' (function delete_course), right after the second "if()".
+```
+diff --git a/lib/moodlelib.php b/lib/moodlelib.php
+index 456d0f1..aeb0853 100644
+--- a/lib/moodlelib.php
++++ b/lib/moodlelib.php
+@@ -4683,6 +4683,9 @@ function delete_course($courseorid, $showfeedback = true) {
+ return false;
+ }
+
++ // Notify the recycle bin plugin.
++ \local_recyclebin\observer::pre_course_delete($course);
++
+ // Make the course completely empty.
+ remove_course_contents($courseid, $showfeedback);
+
+```
diff --git a/admin/tool/recyclebin/classes/category.php b/admin/tool/recyclebin/classes/category.php
new file mode 100644
index 00000000000..df408897e1f
--- /dev/null
+++ b/admin/tool/recyclebin/classes/category.php
@@ -0,0 +1,309 @@
+.
+
+/**
+ * The main interface for recycle bin methods.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace local_recyclebin;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Represents a category's recyclebin.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class category extends recyclebin
+{
+ private $_categoryid;
+
+ /**
+ * Constructor.
+ *
+ * @param int $categoryid Category ID.
+ */
+ public function __construct($categoryid) {
+ $this->_categoryid = $categoryid;
+ }
+
+ /**
+ * Is this recyclebin enabled?
+ */
+ public static function is_enabled() {
+ return get_config('local_recyclebin', 'enablecategory');
+ }
+
+ /**
+ * Returns an item from the recycle bin.
+ *
+ * @param $item int Item ID to retrieve.
+ */
+ public function get_item($itemid) {
+ global $DB;
+
+ $item = $DB->get_record('local_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.
+ */
+ public function get_items() {
+ global $DB;
+
+ $items = $DB->get_records('local_recyclebin_category', array(
+ 'category' => $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 $course stdClass Course
+ * @throws \coding_exception
+ * @throws \invalid_dataroot_permissions
+ * @throws \moodle_exception
+ */
+ public function store_item($course) {
+ global $CFG, $DB;
+
+ require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
+
+ // Backup the course.
+ $user = get_admin();
+ $controller = new \backup_controller(
+ \backup::TYPE_1COURSE,
+ $course->id,
+ \backup::FORMAT_MOODLE,
+ \backup::INTERACTIVE_NO,
+ \backup::MODE_GENERAL,
+ $user->id
+ );
+ $controller->execute_plan();
+
+ // Grab the result.
+ $result = $controller->get_results();
+ if (!isset($result['backup_destination'])) {
+ throw new \moodle_exception('Failed to backup activity prior to deletion.');
+ }
+
+ // Grab the filename.
+ $file = $result['backup_destination'];
+ if (!$file->get_contenthash()) {
+ throw new \moodle_exception('Failed to backup activity prior to deletion (invalid file).');
+ }
+
+ // Make sure our backup dir exists.
+ $bindir = $CFG->dataroot . '/recyclebin';
+ if (!file_exists($bindir)) {
+ make_writable_directory($bindir);
+ }
+
+ // Record the activity, get an ID.
+ $binid = $DB->insert_record('local_recyclebin_category', array(
+ 'category' => $course->category,
+ 'shortname' => $course->shortname,
+ 'fullname' => $course->fullname,
+ 'deleted' => time()
+ ));
+
+ // Move the file to our own special little place.
+ if (!$file->copy_content_to($bindir . '/course-' . $binid)) {
+ // Failed, cleanup first.
+ $DB->delete_records('local_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 = \local_recyclebin\event\course_stored::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 \Exception
+ * @throws \coding_exception
+ * @throws \moodle_exception
+ * @throws \restore_controller_exception
+ */
+ public function restore_item($item) {
+ global $CFG;
+
+ require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
+ require_once($CFG->dirroot . '/course/lib.php');
+
+ $user = get_admin();
+
+ // Build a course.
+ $course = new \stdClass();
+ $course->category = $this->_categoryid;
+ $course->shortname = $item->shortname;
+ $course->fullname = $item->fullname;
+ $course->summary = '';
+
+ // TODO - Maybe handle non-unique shortnames, missing categories, etc?
+
+ // Create a new course.
+ $course = create_course($course);
+ if (!$course) {
+ throw new \moodle_exception("Could not create course to restore into.");
+ }
+
+ // Get the pathname.
+ $source = $CFG->dataroot . '/recyclebin/course-' . $item->id;
+ if (!file_exists($source)) {
+ throw new \moodle_exception('Invalid recycle bin item!');
+ }
+
+ // Grab the course context.
+ $context = \context_coursecat::instance($this->_categoryid);
+
+ // Grab a tmpdir.
+ $tmpdir = \restore_controller::get_tempdir_name($context->id, $user->id);
+
+ // Extract the backup to tmpdir.
+ $fb = get_file_packer('application/vnd.moodle.backup');
+ $fb->extract_to_pathname($source, $CFG->tempdir . '/backup/' . $tmpdir . '/');
+
+ // Define the import.
+ $controller = new \restore_controller(
+ $tmpdir,
+ $course->id,
+ \backup::INTERACTIVE_NO,
+ \backup::MODE_GENERAL,
+ $user->id,
+ \backup::TARGET_NEW_COURSE
+ );
+
+ // Prechecks.
+ if (!$controller->execute_precheck()) {
+ $results = $controller->get_precheck_results();
+
+ if (isset($results['errors'])) {
+ debugging(var_export($results, true));
+ throw new \moodle_exception("Restore failed.");
+ }
+
+ if (isset($results['warnings'])) {
+ debugging(var_export($results['warnings'], true));
+ }
+ }
+
+ // Run the import.
+ $controller->execute_plan();
+
+ // Fire event.
+ $event = \local_recyclebin\event\course_restored::create(array(
+ 'objectid' => $item->id,
+ 'context' => $context
+ ));
+ $event->add_record_snapshot('local_recyclebin_category', $item);
+ $event->trigger();
+
+ // Cleanup.
+ $this->delete_item($item, true);
+ }
+
+ /**
+ * Delete an item from the recycle bin.
+ *
+ * @param stdClass $item The item database record
+ * @param boolean $noevent Whether or not to fire a purged event.
+ * @throws \coding_exception
+ */
+ public function delete_item($item, $noevent = false) {
+ global $CFG, $DB;
+
+ // Delete the file.
+ unlink($CFG->dataroot . '/recyclebin/course-' . $item->id);
+
+ // Delete the record.
+ $DB->delete_records('local_recyclebin_category', array(
+ 'id' => $item->id
+ ));
+
+ if ($noevent) {
+ return;
+ }
+
+ // Fire event.
+ $event = \local_recyclebin\event\course_purged::create(array(
+ 'objectid' => $item->id,
+ 'context' => \context_coursecat::instance($item->category)
+ ));
+ $event->add_record_snapshot('local_recyclebin_category', $item);
+ $event->trigger();
+ }
+
+ /**
+ * Can we view this item?
+ *
+ * @param stdClass $item The item database record
+ */
+ public function can_view($item) {
+ $context = \context_coursecat::instance($item->category);
+ return has_capability('local/recyclebin:view_course', $context);
+ }
+
+ /**
+ * Can we restore this?
+ *
+ * @param stdClass $item The item database record
+ */
+ public function can_restore($item) {
+ $context = \context_coursecat::instance($item->category);
+ return has_capability('local/recyclebin:restore_course', $context);
+ }
+
+ /**
+ * Can we delete this?
+ *
+ * @param stdClass $item The item database record
+ */
+ public function can_delete($item) {
+ $context = \context_coursecat::instance($item->category);
+ return has_capability('local/recyclebin:delete_course', $context);
+ }
+}
diff --git a/admin/tool/recyclebin/classes/course.php b/admin/tool/recyclebin/classes/course.php
new file mode 100644
index 00000000000..8896f049f03
--- /dev/null
+++ b/admin/tool/recyclebin/classes/course.php
@@ -0,0 +1,314 @@
+.
+
+/**
+ * The main interface for recycle bin methods.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace local_recyclebin;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Represents a course's recyclebin.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class course extends recyclebin
+{
+ private $_courseid;
+
+ /**
+ * Constructor.
+ *
+ * @param int $courseid Course ID.
+ */
+ public function __construct($courseid) {
+ $this->_courseid = $courseid;
+ }
+
+ /**
+ * Is this recyclebin enabled?
+ */
+ public static function is_enabled() {
+ return get_config('local_recyclebin', 'enablecourse');
+ }
+
+ /**
+ * Returns an item from the recycle bin.
+ *
+ * @param $item int Item ID to retrieve.
+ */
+ public function get_item($itemid) {
+ global $DB;
+
+ return $DB->get_record('local_recyclebin_course', array(
+ 'id' => $itemid
+ ), '*', MUST_EXIST);
+ }
+
+ /**
+ * Returns a list of items in the recycle bin for this course.
+ */
+ public function get_items() {
+ global $DB;
+
+ return $DB->get_records('local_recyclebin_course', array(
+ 'course' => $this->_courseid
+ ));
+ }
+
+ /**
+ * Store a course module in the recycle bin.
+ *
+ * @param $cm stdClass Course module
+ * @throws \coding_exception
+ * @throws \invalid_dataroot_permissions
+ * @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);
+ $cminfo = $modinfo->cms[$cm->id];
+
+ // Check backup/restore support.
+ if (!plugin_supports('mod', $cminfo->modname , FEATURE_BACKUP_MOODLE2)) {
+ return;
+ }
+
+ // Backup the activity.
+ $user = get_admin();
+ $controller = new \backup_controller(
+ \backup::TYPE_1ACTIVITY,
+ $cm->id,
+ \backup::FORMAT_MOODLE,
+ \backup::INTERACTIVE_NO,
+ \backup::MODE_GENERAL,
+ $user->id
+ );
+ $controller->execute_plan();
+
+ // Grab the result.
+ $result = $controller->get_results();
+ if (!isset($result['backup_destination'])) {
+ throw new \moodle_exception('Failed to backup activity prior to deletion.');
+ }
+
+ // Grab the filename.
+ $file = $result['backup_destination'];
+ if (!$file->get_contenthash()) {
+ throw new \moodle_exception('Failed to backup activity prior to deletion (invalid file).');
+ }
+
+ // Make sure our backup dir exists.
+ $bindir = $CFG->dataroot . '/recyclebin';
+ if (!file_exists($bindir)) {
+ make_writable_directory($bindir);
+ }
+
+ // Record the activity, get an ID.
+ $binid = $DB->insert_record('local_recyclebin_course', array(
+ 'course' => $cm->course,
+ 'section' => $cm->section,
+ 'module' => $cm->module,
+ 'name' => $cminfo->name,
+ 'deleted' => time()
+ ));
+
+ // Move the file to our own special little place.
+ if (!$file->copy_content_to($bindir . '/' . $binid)) {
+ // Failed, cleanup first.
+ $DB->delete_records('local_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 = \local_recyclebin\event\item_stored::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 \Exception
+ * @throws \coding_exception
+ * @throws \moodle_exception
+ * @throws \restore_controller_exception
+ */
+ public function restore_item($item) {
+ global $CFG;
+
+ require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
+
+ $user = get_admin();
+
+ // Get the pathname.
+ $source = $CFG->dataroot . '/recyclebin/' . $item->id;
+ if (!file_exists($source)) {
+ throw new \moodle_exception('Invalid recycle bin item!');
+ }
+
+ // Grab the course context.
+ $context = \context_course::instance($this->_courseid);
+
+ // Grab a tmpdir.
+ $tmpdir = \restore_controller::get_tempdir_name($context->id, $user->id);
+
+ // Extract the backup to tmpdir.
+ $fb = get_file_packer('application/vnd.moodle.backup');
+ $fb->extract_to_pathname($source, $CFG->tempdir . '/backup/' . $tmpdir . '/');
+
+ // Define the import.
+ $controller = new \restore_controller(
+ $tmpdir,
+ $this->_courseid,
+ \backup::INTERACTIVE_NO,
+ \backup::MODE_GENERAL,
+ $user->id,
+ \backup::TARGET_EXISTING_ADDING
+ );
+
+ // Prechecks.
+ if (!$controller->execute_precheck()) {
+ $results = $controller->get_precheck_results();
+
+ if (isset($results['errors'])) {
+ debugging(var_export($results, true));
+ throw new \moodle_exception("Restore failed.");
+ }
+
+ if (isset($results['warnings'])) {
+ debugging(var_export($results['warnings'], true));
+ }
+ }
+
+ // Run the import.
+ $controller->execute_plan();
+
+ // Fire event.
+ $event = \local_recyclebin\event\item_restored::create(array(
+ 'objectid' => $item->id,
+ 'context' => $context
+ ));
+ $event->add_record_snapshot('local_recyclebin_course', $item);
+ $event->trigger();
+
+ // Cleanup.
+ $this->delete_item($item, true);
+ }
+
+ /**
+ * Delete an item from the recycle bin.
+ *
+ * @param stdClass $item The item database record
+ * @param boolean $noevent Whether or not to fire a purged event.
+ * @throws \coding_exception
+ */
+ public function delete_item($item, $noevent = false) {
+ global $CFG, $DB;
+
+ // Delete the file.
+ unlink($CFG->dataroot . '/recyclebin/' . $item->id);
+
+ // Delete the record.
+ $DB->delete_records('local_recyclebin_course', array(
+ 'id' => $item->id
+ ));
+
+ // Return now if we don't need an event.
+ if ($noevent) {
+ return;
+ }
+
+ // The course might have been deleted, check we have a context.
+ $context = \context_course::instance($item->course, \IGNORE_MISSING);
+ if (!$context) {
+ return;
+ }
+
+ // Fire event.
+ $event = \local_recyclebin\event\item_purged::create(array(
+ 'objectid' => $item->id,
+ 'context' => $context
+ ));
+ $event->add_record_snapshot('local_recyclebin_course', $item);
+ $event->trigger();
+ }
+
+ /**
+ * Can we view this item?
+ *
+ * @param stdClass $item The item database record
+ */
+ public function can_view($item) {
+ $context = \context_course::instance($item->course);
+ return has_capability('local/recyclebin:view_item', $context);
+ }
+
+ /**
+ * Can we restore this?
+ *
+ * @param stdClass $item The item database record
+ */
+ public function can_restore($item) {
+ $context = \context_course::instance($item->course);
+ return has_capability('local/recyclebin:restore_item', $context);
+ }
+
+ /**
+ * Can we delete this?
+ *
+ * @param stdClass $item The item database record
+ */
+ public function can_delete($item) {
+ $context = \context_course::instance($item->course);
+
+ // Basic check - do we have the first require capability?
+ if (!has_capability('local/recyclebin:delete_item', $context)) {
+ return false;
+ }
+
+ // Are we a protected item?
+ $protected = get_config('local_recyclebin', 'protectedmods');
+ $protected = explode(',', $protected);
+ if (!in_array($item->module, $protected)) {
+ return true;
+ }
+
+ // Yes! Can we delete protected items?
+ return has_capability('local/recyclebin:delete_protected_item', $context);
+ }
+}
diff --git a/admin/tool/recyclebin/classes/event/course_purged.php b/admin/tool/recyclebin/classes/event/course_purged.php
new file mode 100644
index 00000000000..55f9567dd4b
--- /dev/null
+++ b/admin/tool/recyclebin/classes/event/course_purged.php
@@ -0,0 +1,66 @@
+.
+
+/**
+ * Recycle bin events.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace local_recyclebin\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event Class
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class course_purged extends \core\event\base
+{
+ /**
+ * Init method.
+ */
+ protected function init() {
+ $this->data['objecttable'] = 'local_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('event_purged_name', 'local_recyclebin');
+ }
+
+ /**
+ * Returns description of what happened.
+ *
+ * @return string
+ */
+ public function get_description() {
+ return get_string('event_purged_description', 'local_recyclebin', array(
+ 'objectid' => $this->objectid
+ ));
+ }
+}
diff --git a/admin/tool/recyclebin/classes/event/course_restored.php b/admin/tool/recyclebin/classes/event/course_restored.php
new file mode 100644
index 00000000000..f50139b9905
--- /dev/null
+++ b/admin/tool/recyclebin/classes/event/course_restored.php
@@ -0,0 +1,66 @@
+.
+
+/**
+ * Recycle bin events.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace local_recyclebin\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event Class
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class course_restored extends \core\event\base
+{
+ /**
+ * Init method.
+ */
+ protected function init() {
+ $this->data['objecttable'] = 'local_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('event_restored_name', 'local_recyclebin');
+ }
+
+ /**
+ * Returns description of what happened.
+ *
+ * @return string
+ */
+ public function get_description() {
+ return get_string('event_restored_description', 'local_recyclebin', array(
+ 'objectid' => $this->objectid
+ ));
+ }
+}
diff --git a/admin/tool/recyclebin/classes/event/course_stored.php b/admin/tool/recyclebin/classes/event/course_stored.php
new file mode 100644
index 00000000000..818154a4e33
--- /dev/null
+++ b/admin/tool/recyclebin/classes/event/course_stored.php
@@ -0,0 +1,66 @@
+.
+
+/**
+ * Recycle bin events.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace local_recyclebin\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event Class
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class course_stored extends \core\event\base
+{
+ /**
+ * Init method.
+ */
+ protected function init() {
+ $this->data['objecttable'] = 'local_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('event_stored_name', 'local_recyclebin');
+ }
+
+ /**
+ * Returns description of what happened.
+ *
+ * @return string
+ */
+ public function get_description() {
+ return get_string('event_stored_description', 'local_recyclebin', array(
+ 'objectid' => $this->objectid
+ ));
+ }
+}
diff --git a/admin/tool/recyclebin/classes/event/item_purged.php b/admin/tool/recyclebin/classes/event/item_purged.php
new file mode 100644
index 00000000000..db85e482f86
--- /dev/null
+++ b/admin/tool/recyclebin/classes/event/item_purged.php
@@ -0,0 +1,66 @@
+.
+
+/**
+ * Recycle bin events.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace local_recyclebin\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event Class
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class item_purged extends \core\event\base
+{
+ /**
+ * Init method.
+ */
+ protected function init() {
+ $this->data['objecttable'] = 'local_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('event_purged_name', 'local_recyclebin');
+ }
+
+ /**
+ * Returns description of what happened.
+ *
+ * @return string
+ */
+ public function get_description() {
+ return get_string('event_purged_description', 'local_recyclebin', array(
+ 'objectid' => $this->objectid
+ ));
+ }
+}
diff --git a/admin/tool/recyclebin/classes/event/item_restored.php b/admin/tool/recyclebin/classes/event/item_restored.php
new file mode 100644
index 00000000000..856f1a5872a
--- /dev/null
+++ b/admin/tool/recyclebin/classes/event/item_restored.php
@@ -0,0 +1,66 @@
+.
+
+/**
+ * Recycle bin events.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace local_recyclebin\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event Class
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class item_restored extends \core\event\base
+{
+ /**
+ * Init method.
+ */
+ protected function init() {
+ $this->data['objecttable'] = 'local_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('event_restored_name', 'local_recyclebin');
+ }
+
+ /**
+ * Returns description of what happened.
+ *
+ * @return string
+ */
+ public function get_description() {
+ return get_string('event_restored_description', 'local_recyclebin', array(
+ 'objectid' => $this->objectid
+ ));
+ }
+}
diff --git a/admin/tool/recyclebin/classes/event/item_stored.php b/admin/tool/recyclebin/classes/event/item_stored.php
new file mode 100644
index 00000000000..b96d85ed80d
--- /dev/null
+++ b/admin/tool/recyclebin/classes/event/item_stored.php
@@ -0,0 +1,66 @@
+.
+
+/**
+ * Recycle bin events.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace local_recyclebin\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event Class
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class item_stored extends \core\event\base
+{
+ /**
+ * Init method.
+ */
+ protected function init() {
+ $this->data['objecttable'] = 'local_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('event_stored_name', 'local_recyclebin');
+ }
+
+ /**
+ * Returns description of what happened.
+ *
+ * @return string
+ */
+ public function get_description() {
+ return get_string('event_stored_description', 'local_recyclebin', array(
+ 'objectid' => $this->objectid
+ ));
+ }
+}
diff --git a/admin/tool/recyclebin/classes/observer.php b/admin/tool/recyclebin/classes/observer.php
new file mode 100644
index 00000000000..805426b7972
--- /dev/null
+++ b/admin/tool/recyclebin/classes/observer.php
@@ -0,0 +1,65 @@
+.
+
+/**
+ * Recycle bin observers.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace local_recyclebin;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Main class for the recycle bin.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class observer
+{
+ /**
+ * Course hook.
+ * Note: This is not actually a typical observer.
+ * There is no pre-course delete event, see README.
+ *
+ * @param \stdClass $course The course record.
+ */
+ public static function pre_course_delete($course) {
+ if (\local_recyclebin\category::is_enabled()) {
+ $recyclebin = new \local_recyclebin\category($course->category);
+ $recyclebin->store_item($course);
+ }
+ }
+
+ /**
+ * Course module hook.
+ * Note: This is not actually a typical observer.
+ * There is no pre-cm event, see README.
+ *
+ * @param \stdClass $cm The course module record.
+ */
+ public static function pre_cm_delete($cm) {
+ if (\local_recyclebin\course::is_enabled()) {
+ $recyclebin = new \local_recyclebin\course($cm->course);
+ $recyclebin->store_item($cm);
+ }
+ }
+}
diff --git a/admin/tool/recyclebin/classes/recyclebin.php b/admin/tool/recyclebin/classes/recyclebin.php
new file mode 100644
index 00000000000..f8169d2867e
--- /dev/null
+++ b/admin/tool/recyclebin/classes/recyclebin.php
@@ -0,0 +1,120 @@
+.
+
+/**
+ * The main interface for recycle bin methods.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace local_recyclebin;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Represents a recyclebin.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+abstract class recyclebin
+{
+ /**
+ * Is this recyclebin enabled?
+ */
+ public static function is_enabled() {
+ return false;
+ }
+
+ /**
+ * Returns an item from the recycle bin.
+ *
+ * @param $item int Item ID to retrieve.
+ */
+ public abstract function get_item($itemid);
+
+ /**
+ * Returns a list of items in the recycle bin.
+ */
+ public abstract function get_items();
+
+ /**
+ * Store an item in this recycle bin.
+ *
+ * @param $item stdClass Item to store.
+ * @throws \coding_exception
+ * @throws \invalid_dataroot_permissions
+ * @throws \moodle_exception
+ */
+ public abstract function store_item($item);
+
+ /**
+ * Restore an item from the recycle bin.
+ *
+ * @param stdClass $item The item database record
+ * @throws \Exception
+ * @throws \coding_exception
+ * @throws \moodle_exception
+ * @throws \restore_controller_exception
+ */
+ public abstract function restore_item($item);
+
+ /**
+ * Delete an item from the recycle bin.
+ *
+ * @param stdClass $item The item database record
+ * @param boolean $noevent Whether or not to fire a purged event.
+ * @throws \coding_exception
+ */
+ public abstract function delete_item($item, $noevent = false);
+
+ /**
+ * Empty the recycle bin.
+ */
+ public function delete_all_items() {
+ // Cleanup all items.
+ $items = $this->get_items();
+ foreach ($items as $item) {
+ if ($this->can_delete($item)) {
+ $this->delete_item($item);
+ }
+ }
+ }
+
+ /**
+ * Can we view this?
+ *
+ * @param stdClass $item The item database record
+ */
+ public abstract function can_view($item);
+
+ /**
+ * Can we restore this?
+ *
+ * @param stdClass $item The item database record
+ */
+ public abstract function can_restore($item);
+
+ /**
+ * Can we delete this?
+ *
+ * @param stdClass $item The item database record
+ */
+ public abstract function can_delete($item);
+}
diff --git a/admin/tool/recyclebin/classes/task/cleanup_activities.php b/admin/tool/recyclebin/classes/task/cleanup_activities.php
new file mode 100644
index 00000000000..15af3949455
--- /dev/null
+++ b/admin/tool/recyclebin/classes/task/cleanup_activities.php
@@ -0,0 +1,82 @@
+.
+
+/**
+ * Recycle bin cron task.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace local_recyclebin\task;
+
+/**
+ * This task deletes expired course recyclebin items.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class cleanup_activities extends \core\task\scheduled_task {
+ /**
+ * Task name.
+ */
+ public function get_name() {
+ return get_string('cleancourserecyclebin', 'local_recyclebin');
+ }
+
+ /**
+ * Delete all expired items.
+ */
+ public function execute() {
+ global $DB;
+
+ // Delete mods.
+ $lifetime = get_config('local_recyclebin', 'expiry');
+ if (!\local_recyclebin\course::is_enabled() || $lifetime <= 0) {
+ return true;
+ }
+
+ // Start building SQL.
+ $sql = '';
+ $params = array();
+
+ // Protected mods are exempt.
+ $protected = get_config('local_recyclebin', 'protectedmods');
+ if (!empty($protected)) {
+ $protected = explode(',', $protected);
+ list($sql, $params) = $DB->get_in_or_equal($protected, SQL_PARAMS_NAMED, 'm', false);
+ $sql = " AND module {$sql}";
+ }
+
+ // Add deleted param.
+ $params = is_array($params) ? $params : array();
+ $params['deleted'] = time() - (86400 * $lifetime);
+
+ // Delete items.
+ $items = $DB->get_recordset_select('local_recyclebin_course', 'deleted < :deleted' . $sql, $params);
+ foreach ($items as $item) {
+ mtrace("[RecycleBin] Deleting item {$item->id}...");
+
+ $bin = new \local_recyclebin\course($item->course);
+ $bin->delete_item($item);
+ }
+ $items->close();
+
+ return true;
+ }
+}
diff --git a/admin/tool/recyclebin/classes/task/cleanup_courses.php b/admin/tool/recyclebin/classes/task/cleanup_courses.php
new file mode 100644
index 00000000000..9933e5d39f3
--- /dev/null
+++ b/admin/tool/recyclebin/classes/task/cleanup_courses.php
@@ -0,0 +1,65 @@
+.
+
+/**
+ * Recycle bin cron task.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace local_recyclebin\task;
+
+/**
+ * This task deletes expired category recyclebin items.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class cleanup_courses extends \core\task\scheduled_task {
+ /**
+ * Task name.
+ */
+ public function get_name() {
+ return get_string('cleancategoryrecyclebin', 'local_recyclebin');
+ }
+
+ /**
+ * Delete all expired items.
+ */
+ public function execute() {
+ global $DB;
+
+ // Delete courses.
+ $lifetime = get_config('local_recyclebin', 'course_expiry');
+ if (!\local_recyclebin\category::is_enabled() || $lifetime <= 0) {
+ return true;
+ }
+
+ $items = $DB->get_recordset_select('local_recyclebin_category', 'deleted < ?', array(time() - (86400 * $lifetime)));
+ foreach ($items as $item) {
+ mtrace("[RecycleBin] Deleting course {$item->id}...");
+
+ $bin = new \local_recyclebin\category($item->category);
+ $bin->delete_item($item);
+ }
+ $items->close();
+
+ return true;
+ }
+}
diff --git a/admin/tool/recyclebin/db/access.php b/admin/tool/recyclebin/db/access.php
new file mode 100644
index 00000000000..265e17976c4
--- /dev/null
+++ b/admin/tool/recyclebin/db/access.php
@@ -0,0 +1,90 @@
+.
+
+/**
+ * Plugin capabilities.
+ *
+ * @package local_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(
+ 'local/recyclebin:view_item' => array(
+ 'captype' => 'read',
+ 'contextlevel' => CONTEXT_COURSE,
+ 'archetypes' => array(
+ 'teacher' => CAP_ALLOW,
+ 'editingteacher' => CAP_ALLOW,
+ 'manager' => CAP_ALLOW
+ )
+ ),
+
+ 'local/recyclebin:restore_item' => array(
+ 'captype' => 'write',
+ 'contextlevel' => CONTEXT_COURSE,
+ 'archetypes' => array(
+ 'editingteacher' => CAP_ALLOW,
+ 'manager' => CAP_ALLOW
+ )
+ ),
+
+ 'local/recyclebin:delete_item' => array(
+ 'captype' => 'write',
+ 'riskbitmask' => RISK_DATALOSS,
+ 'contextlevel' => CONTEXT_COURSE,
+ 'archetypes' => array(
+ 'editingteacher' => CAP_ALLOW,
+ 'manager' => CAP_ALLOW
+ )
+ ),
+
+ 'local/recyclebin:view_course' => array(
+ 'captype' => 'read',
+ 'contextlevel' => CONTEXT_COURSECAT,
+ 'archetypes' => array(
+ 'manager' => CAP_ALLOW
+ )
+ ),
+
+ 'local/recyclebin:restore_course' => array(
+ 'captype' => 'write',
+ 'contextlevel' => CONTEXT_COURSECAT,
+ 'archetypes' => array(
+ 'manager' => CAP_ALLOW
+ )
+ ),
+
+ 'local/recyclebin:delete_course' => array(
+ 'captype' => 'write',
+ 'riskbitmask' => RISK_DATALOSS,
+ 'contextlevel' => CONTEXT_COURSECAT,
+ 'archetypes' => array(
+ 'manager' => CAP_ALLOW
+ )
+ ),
+
+ 'local/recyclebin:delete_protected_item' => array(
+ 'captype' => 'write',
+ 'riskbitmask' => RISK_DATALOSS,
+ 'contextlevel' => CONTEXT_COURSE,
+ 'archetypes' => array(
+ 'manager' => CAP_ALLOW
+ )
+ )
+);
diff --git a/admin/tool/recyclebin/db/install.xml b/admin/tool/recyclebin/db/install.xml
new file mode 100644
index 00000000000..f06a6dd598a
--- /dev/null
+++ b/admin/tool/recyclebin/db/install.xml
@@ -0,0 +1,40 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/admin/tool/recyclebin/db/tasks.php b/admin/tool/recyclebin/db/tasks.php
new file mode 100644
index 00000000000..5408bf9d75d
--- /dev/null
+++ b/admin/tool/recyclebin/db/tasks.php
@@ -0,0 +1,44 @@
+.
+
+/**
+ * Recycle bin tasks.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+$tasks = array(
+ array(
+ 'classname' => 'local_recyclebin\task\cleanup_activities',
+ 'blocking' => 0,
+ 'minute' => '*/30',
+ 'hour' => '*',
+ 'day' => '*',
+ 'dayofweek' => '*',
+ 'month' => '*'
+ ),
+ array(
+ 'classname' => 'local_recyclebin\task\cleanup_courses',
+ 'blocking' => 0,
+ 'minute' => '*/30',
+ 'hour' => '*',
+ 'day' => '*',
+ 'dayofweek' => '*',
+ 'month' => '*'
+ )
+);
\ No newline at end of file
diff --git a/admin/tool/recyclebin/db/upgrade.php b/admin/tool/recyclebin/db/upgrade.php
new file mode 100644
index 00000000000..fe87e936a0f
--- /dev/null
+++ b/admin/tool/recyclebin/db/upgrade.php
@@ -0,0 +1,99 @@
+.
+
+/**
+ * Upgrade code.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 Skylar Kelty
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die;
+
+/**
+ * Recycle bin upgrade task
+ *
+ * @param int $oldversion the version we are upgrading from
+ * @return bool always true
+ */
+function xmldb_local_recyclebin_upgrade($oldversion) {
+ global $DB;
+
+ $dbman = $DB->get_manager();
+
+ if ($oldversion < 2015060400) {
+ // Define field deleted to be added to local_recyclebin.
+ $table = new xmldb_table('local_recyclebin');
+ if ($dbman->table_exists($table)) {
+ $field = new xmldb_field('deleted', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'name');
+ // Conditionally launch add field deleted.
+ if (!$dbman->field_exists($table, $field)) {
+ $dbman->add_field($table, $field);
+ }
+
+ // Define index i_deleted (not unique) to be added to local_recyclebin.
+ $index = new xmldb_index('i_deleted', XMLDB_INDEX_NOTUNIQUE, array('deleted'));
+ // Conditionally launch add index i_deleted.
+ if (!$dbman->index_exists($table, $index)) {
+ $dbman->add_index($table, $index);
+ }
+ }
+
+ upgrade_plugin_savepoint(true, 2015060400, 'local', 'recyclebin');
+ }
+
+ if ($oldversion < 2015082700) {
+ // Define table local_recyclebin to be renamed to local_recyclebin_course.
+ $table = new xmldb_table('local_recyclebin');
+
+ // Launch rename table for local_recyclebin.
+ if ($dbman->table_exists($table)) {
+ $dbman->rename_table($table, 'local_recyclebin_course');
+ }
+
+ // Recyclebin savepoint reached.
+ upgrade_plugin_savepoint(true, 2015082700, 'local', 'recyclebin');
+ }
+
+ if ($oldversion < 2015082701) {
+ // Define table local_recyclebin_category to be created.
+ $table = new xmldb_table('local_recyclebin_category');
+
+ // Adding fields to table local_recyclebin_category.
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('category', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('fullname', XMLDB_TYPE_CHAR, '254', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('deleted', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+
+ // Adding keys to table local_recyclebin_category.
+ $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
+
+ // Adding indexes to table local_recyclebin_category.
+ $table->add_index('i_category', XMLDB_INDEX_NOTUNIQUE, array('category'));
+
+ // Conditionally launch create table for local_recyclebin_category.
+ if (!$dbman->table_exists($table)) {
+ $dbman->create_table($table);
+ }
+
+ // Recyclebin savepoint reached.
+ upgrade_plugin_savepoint(true, 2015082701, 'local', 'recyclebin');
+ }
+
+ return true;
+}
diff --git a/admin/tool/recyclebin/index.php b/admin/tool/recyclebin/index.php
new file mode 100644
index 00000000000..b3aa61ea577
--- /dev/null
+++ b/admin/tool/recyclebin/index.php
@@ -0,0 +1,238 @@
+.
+
+/**
+ * This page shows the contents of a recyclebin for a given course.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require_once(dirname(__FILE__) . '/../../config.php');
+require_once($CFG->libdir . '/tablelib.php');
+
+$contextid = required_param('contextid', PARAM_INT);
+$action = optional_param('action', null, PARAM_ALPHA);
+
+$context = context::instance_by_id($contextid, MUST_EXIST);
+$PAGE->set_context($context);
+
+$description = '';
+
+// We could be a course or a category.
+switch ($context->contextlevel) {
+ case \CONTEXT_COURSE:
+ require_login($context->instanceid);
+ require_capability('local/recyclebin:view_item', $context);
+
+ $recyclebin = new \local_recyclebin\course($context->instanceid);
+ $description = get_string('description_course', 'local_recyclebin');
+ break;
+
+ case \CONTEXT_COURSECAT:
+ require_login();
+ require_capability('local/recyclebin:view_course', $context);
+
+ $recyclebin = new \local_recyclebin\category($context->instanceid);
+ $description = get_string('description_coursecat', 'local_recyclebin');
+ break;
+
+ default:
+ print_error('invalidcontext', 'local_recyclebin');
+ break;
+}
+
+if (!$recyclebin::is_enabled()) {
+ print_error('notenabled', 'local_recyclebin');
+}
+
+$PAGE->set_url('/local/recyclebin/index.php', array(
+ 'contextid' => $contextid
+));
+$PAGE->set_title(get_string('pluginname', 'local_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($item)) {
+ $recyclebin->restore_item($item);
+ redirect($PAGE->url, get_string('alertrestored', 'local_recyclebin', $item), 2);
+ } else {
+ print_error('nopermissions', 'error');
+ }
+ break;
+
+ // Delete it.
+ case 'delete':
+ if ($recyclebin->can_delete($item)) {
+ $recyclebin->delete_item($item);
+ redirect($PAGE->url, get_string('alertdeleted', 'local_recyclebin', $item), 2);
+ } else {
+ print_error('nopermissions', 'error');
+ }
+ break;
+
+ // Empty it.
+ case 'empty':
+ $recyclebin->delete_all_items();
+ redirect($PAGE->url, get_string('alertemptied', 'local_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($PAGE->title);
+
+// 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('emptybin', 'local_recyclebin'));
+ echo $goback;
+ echo $OUTPUT->footer();
+ die;
+}
+
+// Start with a description.
+$expiry = get_config('local_recyclebin', 'expiry');
+if ($expiry > 0) {
+ $description .= ' ' . get_string('descriptionexpiry', 'local_recyclebin', $expiry);
+}
+echo $OUTPUT->box($description, 'generalbox descriptionbox');
+
+// Define columns and headers.
+$firstcolstr = $context->contextlevel == \CONTEXT_COURSE ? 'activity' : 'course';
+$columns = array($firstcolstr, 'date', 'restore', 'delete');
+$headers = array(
+ get_string($firstcolstr),
+ get_string('deleted', 'local_recyclebin'),
+ get_string('restore'),
+ get_string('delete')
+);
+
+// Define a table.
+$table = new flexible_table('recyclebin');
+$table->define_columns($columns);
+$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;
+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 = '
' . $name;
+ }
+ }
+
+ $row[] = $name;
+ $row[] = userdate($item->deleted);
+
+ // Build restore link.
+ if ($recyclebin->can_restore($item) && ($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($item)) {
+ $showempty = true;
+ $delete = new \moodle_url($PAGE->url, array(
+ 'contextid' => $contextid,
+ 'itemid' => $item->id,
+ 'action' => 'delete',
+ 'sesskey' => sesskey()
+ ));
+ $delete = $OUTPUT->action_icon($delete, new pix_icon('t/delete',
+ get_string('delete'), '', array('class' => 'iconsmall')), null,
+ array('class' => 'action-icon recycle-bin-delete'));
+
+ $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) {
+ $empty = new \moodle_url($PAGE->url, array(
+ 'contextid' => $contextid,
+ 'action' => 'empty',
+ 'sesskey' => sesskey()
+ ));
+
+ echo $OUTPUT->single_button($empty, get_string('empty', 'local_recyclebin'), 'post', array(
+ 'class' => 'singlebutton recycle-bin-delete-all'
+ ));
+}
+
+echo $goback;
+
+// Confirmation JS.
+$PAGE->requires->strings_for_js(array('emptyconfirm', 'deleteconfirm'), 'local_recyclebin');
+$PAGE->requires->js_init_call('M.local_recyclebin.init');
+
+// Output footer.
+echo $OUTPUT->footer();
diff --git a/admin/tool/recyclebin/lang/en/local_recyclebin.php b/admin/tool/recyclebin/lang/en/local_recyclebin.php
new file mode 100644
index 00000000000..11507bceabf
--- /dev/null
+++ b/admin/tool/recyclebin/lang/en/local_recyclebin.php
@@ -0,0 +1,85 @@
+.
+
+/**
+ * English strings for local_recyclebin.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 Skylar Kelty
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+$string['pluginname'] = 'Recycle bin';
+
+$string['cleancourserecyclebin'] = 'Clean course recycle bins';
+$string['cleancategoryrecyclebin'] = 'Clean category recycle bins';
+
+$string['enablecourse'] = 'Enable course recycle bin';
+$string['enablecourse_desc'] = 'Enables or disables the course recycle bin';
+
+$string['expiry'] = 'Activity lifetime';
+$string['expiry_desc'] = 'How long should a deleted mod remain in the recycle bin?';
+
+$string['enablecategory'] = 'Enable category recycle bin';
+$string['enablecategory_desc'] = 'Enables or disables the category recycle bin';
+
+$string['course_expiry'] = 'Course lifetime';
+$string['course_expiry_desc'] = 'How long should a deleted course remain in the recycle bin?';
+
+$string['autohide'] = 'Auto hide?';
+$string['autohide_desc'] = 'Automatically hides the recycle bin link when the bin is empty.';
+
+$string['protectedmods'] = 'Protected modules';
+$string['protectedmods_desc'] = 'Protected modules will not be automatically deleted.';
+
+$string['neverdelete'] = 'Never delete recycled items';
+$string['deleted'] = 'Date deleted';
+$string['empty'] = 'Delete all';
+
+$string['recyclebin:view_item'] = 'View course recycle bin items';
+$string['recyclebin:restore_item'] = 'Restore course recycle bin items';
+$string['recyclebin:delete_item'] = 'Delete course recycle bin items';
+$string['recyclebin:delete_protected_item'] = 'Delete protected course recycle bin items';
+
+$string['recyclebin:view_course'] = 'View category recycle bin items';
+$string['recyclebin:restore_course'] = 'Restore category recycle bin items';
+$string['recyclebin:delete_course'] = 'Delete category recycle bin items';
+
+$string['alertrestored'] = '{$a->name} has been restored';
+$string['alertdeleted'] = '{$a->name} has been deleted';
+$string['alertemptied'] = 'Recycle bin has been emptied';
+
+$string['event_stored_name'] = 'Item stored';
+$string['event_restored_name'] = 'Item restored';
+$string['event_purged_name'] = 'Item purged';
+
+$string['event_stored_description'] = 'Item stored with ID {$a->objectid}.';
+$string['event_restored_description'] = 'Item with ID {$a->objectid} restored.';
+$string['event_purged_description'] = 'Item with ID {$a->objectid} purged.';
+
+$string['description_course'] = 'Items that have been deleted from a course can be restored if they are still in the recycle bin, and will appear at the bottom of the section from which they were deleted.';
+$string['description_coursecat'] = 'Items that have been deleted from a category can be restored if they are still in the recycle bin.';
+$string['descriptionexpiry'] = 'Contents will be permanently deleted after {$a} days.';
+
+$string['emptybin'] = 'There are no items in the recycle bin.';
+$string['emptyconfirm'] = 'Are you sure you want to delete all items in the recycle bin?';
+$string['deleteconfirm'] = 'Are you sure you want to delete the selected item from the recycle bin?';
+
+$string['invalidcontext'] = 'Invalid context supplied.';
+
+$string['notenabled'] = 'Sorry, but the recycle bin has been disabled by the administrator.';
diff --git a/admin/tool/recyclebin/lib.php b/admin/tool/recyclebin/lib.php
new file mode 100644
index 00000000000..f97013bc4db
--- /dev/null
+++ b/admin/tool/recyclebin/lib.php
@@ -0,0 +1,119 @@
+.
+
+/**
+ * Local lib code
+ *
+ * @package local_recyclebin
+ * @copyright 2015 Skylar Kelty
+ * @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 settings_navigation $nav Nav menu
+ * @param context $context Context of the menu
+ * @return navigation_node A new navigation mode to insert.
+ */
+function local_recyclebin_extend_settings_navigation(settings_navigation $nav, context $context) {
+ global $PAGE;
+
+ $url = null;
+ $bin = null;
+ $settingnode = null;
+
+ // What context are we in?
+ if ($context->contextlevel == \CONTEXT_COURSECAT) {
+ // Check we can view the recycle bin.
+ if (!has_capability('local/recyclebin:view_course', $context) || !\local_recyclebin\category::is_enabled()) {
+ return null;
+ }
+
+ // Add a link to the category recyclebin.
+ $bin = new \local_recyclebin\category($context->instanceid);
+ $url = new moodle_url('/local/recyclebin/index.php', array(
+ 'contextid' => $context->id
+ ));
+
+ $settingnode = $nav->find('categorysettings', null);
+ } else {
+ // Only add this settings item on non-site course pages.
+ if (!$PAGE->course || $PAGE->course->id == SITEID || !\local_recyclebin\course::is_enabled()) {
+ return null;
+ }
+
+ // We might be in a mod page, etc.
+ $coursectx = \context_course::instance($PAGE->course->id);
+
+ // Check we can view the recycle bin.
+ if (!has_capability('local/recyclebin:view_item', $coursectx)) {
+ return null;
+ }
+
+ $bin = new \local_recyclebin\course($coursectx->instanceid);
+ $url = new moodle_url('/local/recyclebin/index.php', array(
+ 'contextid' => $coursectx->id
+ ));
+
+ $settingnode = $nav->find('courseadmin', navigation_node::TYPE_COURSE);
+ }
+
+ if ($settingnode == null) {
+ return;
+ }
+
+ // If we are set to auto-hide, check the number of items.
+ $autohide = get_config('local_recyclebin', 'autohide');
+ if ($autohide) {
+ $items = $bin->get_items();
+ if (empty($items)) {
+ return null;
+ }
+ }
+
+ // Add the recyclebin link.
+ $pluginname = get_string('pluginname', 'local_recyclebin');
+
+ $node = navigation_node::create(
+ $pluginname,
+ $url,
+ navigation_node::NODETYPE_LEAF,
+ 'local_recyclebin',
+ 'local_recyclebin',
+ new pix_icon('e/cleanup_messy_code', $pluginname)
+ );
+
+ if ($PAGE->url->compare($url, URL_MATCH_BASE)) {
+ $node->make_active();
+ }
+
+ $settingnode->add_node($node);
+
+ return $node;
+}
+
+/**
+ * For pre-2.9 installations.
+ *
+ * @param settings_navigation $nav
+ * @param context $context
+ */
+function local_recyclebin_extends_settings_navigation(settings_navigation $nav, context $context) {
+ local_recyclebin_extend_settings_navigation($nav, $context);
+}
diff --git a/admin/tool/recyclebin/module.js b/admin/tool/recyclebin/module.js
new file mode 100644
index 00000000000..9b864b621ca
--- /dev/null
+++ b/admin/tool/recyclebin/module.js
@@ -0,0 +1,53 @@
+// Added a confirmation dialogue when the empty recycle bin
+// button has been selected.
+M.local_recyclebin = {
+ init: function (Y) {
+ // Confirmation dialogue function.
+ function confirmDialogue(e, str, callback) {
+ // Prevent the button from immediately performing its action.
+ e.preventDefault();
+
+ // Add a confirm dialogue box.
+ YUI().use('moodle-core-notification-confirm', function(Y) {
+ var confirm = new M.core.confirm({
+ question: str,
+ center: true,
+ modal: true,
+ });
+
+ // Perform the button's action if "Yes" is selected.
+ confirm.on('complete-yes', callback, this);
+
+ // Render the confirm dialogue.
+ confirm.render().show();
+ });
+ }
+
+ // Perform this action when any "Delete" button/link is clicked.
+ Y.all('.recycle-bin-delete').each(function(node) {
+ node.on('click', function(e) {
+ // Get some strings from the Recycle bin lang file.
+ var str = M.util.get_string('deleteconfirm', 'local_recyclebin');
+
+ // Get the URL that leads to emptying the recycle bin.
+ var urldelete = this.get('href');
+
+ // Show the dialogue.
+ confirmDialogue(e, str, function() {
+ window.location = urldelete;
+ });
+ });
+ });
+
+ // Find the "Delete All" button and perform an action when it is clicked.
+ Y.one('.recycle-bin-delete-all input').on('click', function(e) {
+ // Get some strings from the Recycle bin lang file.
+ var str = M.util.get_string('emptyconfirm', 'local_recyclebin');
+
+ // Show the dialogue.
+ confirmDialogue(e, str, function() {
+ Y.one('.recycle-bin-delete-all form').submit();
+ });
+ });
+ }
+};
diff --git a/admin/tool/recyclebin/settings.php b/admin/tool/recyclebin/settings.php
new file mode 100644
index 00000000000..d9d429b6b8e
--- /dev/null
+++ b/admin/tool/recyclebin/settings.php
@@ -0,0 +1,97 @@
+.
+
+/**
+ * Recycle bin settings.
+ *
+ * @package local_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('local_recyclebin', get_string('pluginname', 'local_recyclebin'));
+ $ADMIN->add('localplugins', $settings);
+
+ $lifetimes = array(
+ 0 => new lang_string('neverdelete', 'local_recyclebin'),
+ 1000 => new lang_string('numdays', '', 1000),
+ 365 => new lang_string('numdays', '', 365),
+ 180 => new lang_string('numdays', '', 180),
+ 150 => new lang_string('numdays', '', 150),
+ 120 => new lang_string('numdays', '', 120),
+ 90 => new lang_string('numdays', '', 90),
+ 60 => new lang_string('numdays', '', 60),
+ 35 => new lang_string('numdays', '', 35),
+ 21 => new lang_string('numdays', '', 21),
+ 14 => new lang_string('numdays', '', 14),
+ 10 => new lang_string('numdays', '', 10),
+ 5 => new lang_string('numdays', '', 5),
+ 2 => new lang_string('numdays', '', 2)
+ );
+
+ $settings->add(new admin_setting_configcheckbox(
+ 'local_recyclebin/enablecourse',
+ new lang_string('enablecourse', 'local_recyclebin'),
+ new lang_string('enablecourse_desc', 'local_recyclebin'),
+ 1
+ ));
+
+ $settings->add(new admin_setting_configselect(
+ 'local_recyclebin/expiry',
+ new lang_string('expiry', 'local_recyclebin'),
+ new lang_string('expiry_desc', 'local_recyclebin'),
+ 0,
+ $lifetimes
+ ));
+
+
+ $settings->add(new admin_setting_configcheckbox(
+ 'local_recyclebin/enablecategory',
+ new lang_string('enablecategory', 'local_recyclebin'),
+ new lang_string('enablecategory_desc', 'local_recyclebin'),
+ 1
+ ));
+
+ $settings->add(new admin_setting_configselect(
+ 'local_recyclebin/course_expiry',
+ new lang_string('course_expiry', 'local_recyclebin'),
+ new lang_string('course_expiry_desc', 'local_recyclebin'),
+ 0,
+ $lifetimes
+ ));
+
+ unset($lifetimes);
+
+ $settings->add(new admin_setting_configcheckbox(
+ 'local_recyclebin/autohide',
+ new lang_string('autohide', 'local_recyclebin'),
+ new lang_string('autohide_desc', 'local_recyclebin'),
+ 0
+ ));
+
+ $settings->add(new admin_setting_configmultiselect(
+ 'local_recyclebin/protectedmods',
+ new lang_string('protectedmods', 'local_recyclebin'),
+ new lang_string('protectedmods_desc', 'local_recyclebin'),
+ array(),
+ $DB->get_records_menu('modules', array('visible' => 1), 'name ASC', 'id, name')
+ ));
+}
diff --git a/admin/tool/recyclebin/styles.css b/admin/tool/recyclebin/styles.css
new file mode 100644
index 00000000000..28ca97a3901
--- /dev/null
+++ b/admin/tool/recyclebin/styles.css
@@ -0,0 +1,5 @@
+/* Make the icons center. */
+table#recycle-bin-table .c2,
+table#recycle-bin-table .c3 {
+ text-align: center;
+}
\ No newline at end of file
diff --git a/admin/tool/recyclebin/tests/behat/backup_user_data.feature b/admin/tool/recyclebin/tests/behat/backup_user_data.feature
new file mode 100644
index 00000000000..010a9a23a20
--- /dev/null
+++ b/admin/tool/recyclebin/tests/behat/backup_user_data.feature
@@ -0,0 +1,65 @@
+@local_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 |
+
+ @javascript
+ Scenario: Delete and restore a quiz.
+ Given I log in as "teacher1"
+ And I follow "Course 1"
+ And I turn editing mode on
+ And I add a "Quiz" to section "1" and I fill the form with:
+ | Name | Quiz 1 |
+ | Description | Test quiz description |
+ And I add a "True/False" question to the "Quiz 1" quiz with:
+ | Question name | TF1 |
+ | Question text | First question |
+ | General feedback | Thank you, this is the general feedback |
+ | Correct answer | False |
+ | Feedback for the response 'True'. | So you think it is true |
+ | Feedback for the response 'False'. | So you think it is false |
+ And I add a "True/False" question to the "Quiz 1" quiz with:
+ | Question name | TF2 |
+ | Question text | Second question |
+ | General feedback | Thank you, this is the general feedback |
+ | Correct answer | False |
+ | Feedback for the response 'True'. | So you think it is true |
+ | Feedback for the response 'False'. | So you think it is false |
+ And I log out
+ And I log in as "student1"
+ And I follow "Course 1"
+ And I follow "Quiz 1"
+ And I press "Attempt quiz now"
+ And I click on "True" "radio" in the "First question" "question"
+ And I click on "False" "radio" in the "Second question" "question"
+ And I press "Next"
+ And I press "Submit all and finish"
+ And I click on "Submit all and finish" "button" in the "Confirmation" "dialogue"
+ Then I should see "5.00 out of 10.00"
+ Given I log out
+ And I log in as "teacher1"
+ And I follow "Course 1"
+ And I turn editing mode on
+ And I delete "Quiz 1" activity
+ And I follow "Recycle bin"
+ And I follow "Restore"
+ And I log out
+ And I log in as "student1"
+ And I follow "Course 1"
+ And I follow "Grades"
+ Then "Quiz 1" row "Grade" column of "user-grade" table should contain "5"
+ And "Quiz 1" row "Percentage" column of "user-grade" table should contain "50"
diff --git a/admin/tool/recyclebin/tests/behat/basic_functionality.feature b/admin/tool/recyclebin/tests/behat/basic_functionality.feature
new file mode 100644
index 00000000000..f162ccd2bcc
--- /dev/null
+++ b/admin/tool/recyclebin/tests/behat/basic_functionality.feature
@@ -0,0 +1,44 @@
+@local_recyclebin
+Feature: Basic recycle bin functionality
+ As a teacher
+ I want be able to recover deleted content
+ So that I can fix a mistake or accidently deletion
+
+ Background: Course with teacher exists.
+ Given the following "users" exist:
+ | username | firstname | lastname | email |
+ | teacher1 | Teacher | 1 | teacher@asd.com |
+ | student1 | Student | 1 | student@asd.com |
+ And the following "courses" exist:
+ | fullname | shortname |
+ | Course 1 | C1 |
+ And the following "course enrolments" exist:
+ | user | course | role |
+ | teacher1 | C1 | editingteacher |
+
+ Scenario: Restore and delete an assingnment.
+ Given I log in as "teacher1"
+ And I follow "Course 1"
+ And I turn editing mode on
+ And I add a "Page" to section "1" and I fill the form with:
+ | Name | Test page |
+ | Description | Test |
+ | Page content | Test |
+ When I delete "Test page" activity
+ And I follow "Recycle bin"
+ Then I should see "Test page"
+ When I follow "Restore"
+ Then I should see "Test page has been restored"
+ When I wait to be redirected
+ And I am on homepage
+ And I follow "Course 1"
+ Then I should see "Test page" in the "Topic 1" "section"
+ When I delete "Test page" activity
+ And I follow "Recycle bin"
+ Then I should see "Test page"
+ When I follow "Delete"
+ Then I should see "Test page has been deleted"
+ When I wait to be redirected
+ And I am on homepage
+ And I follow "Course 1"
+ Then I should not see "Test page" in the "Topic 1" "section"
diff --git a/admin/tool/recyclebin/tests/behat/delete_confirmation.feature b/admin/tool/recyclebin/tests/behat/delete_confirmation.feature
new file mode 100644
index 00000000000..7983dbcab9b
--- /dev/null
+++ b/admin/tool/recyclebin/tests/behat/delete_confirmation.feature
@@ -0,0 +1,45 @@
+@local_recyclebin @javascript
+Feature: Delete confirmation
+ As a teacher
+ I want to be prompted before I permanently delete something
+ So that I do not make a mistake again
+
+Background:
+ Given the following "users" exist:
+ | username | firstname | lastname | email |
+ | teacher1 | Teacher | 1 | teacher@asd.com |
+ Given the following "courses" exist:
+ | fullname | shortname |
+ | Course 1 | C1 |
+ And the following "course enrolments" exist:
+ | user | course | role |
+ | teacher1 | C1 | editingteacher |
+ And I log in as "teacher1"
+ And I follow "Course 1"
+ And I turn editing mode on
+ And I add a "Page" to section "1" and I fill the form with:
+ | Name | Test page |
+ | Description | Test |
+ | Page content | Test |
+ And I delete "Test page" activity
+ And I follow "Recycle bin"
+
+Scenario: Confirm single delete
+ When I click on "Delete" "link"
+ Then I should see "Are you sure you want to delete the selected item in the recycle bin?"
+ And I press "No"
+ And I should see "Test page"
+ When I click on "Delete" "link"
+ And I press "Yes"
+ And I wait to be redirected
+ Then I should see "There are no items in the recycle bin."
+
+Scenario: Confirm empty bin
+ When I press "Empty recycle bin"
+ Then I should see "Are you sure you want to delete all items in the recycle bin?"
+ And I press "No"
+ And I should see "Test page"
+ When I press "Empty recycle bin"
+ And I press "Yes"
+ And I wait to be redirected
+ Then I should see "There are no items in the recycle bin."
diff --git a/admin/tool/recyclebin/tests/behat/description.feature b/admin/tool/recyclebin/tests/behat/description.feature
new file mode 100644
index 00000000000..e5f49cf8a32
--- /dev/null
+++ b/admin/tool/recyclebin/tests/behat/description.feature
@@ -0,0 +1,33 @@
+@local_recyclebin
+Feature: Description of recycle bin and expiry
+ As a teacher
+ I want to know what the recycle bin will do and how long contents last in the bin
+ So that I can better understand the tool
+
+Scenario: Description should show when the recycle bin will clean up files.
+ Given the following "users" exist:
+ | username | firstname | lastname | email |
+ | teacher1 | Teacher | 1 | teacher@asd.com |
+ Given the following "courses" exist:
+ | fullname | shortname |
+ | Course 1 | C1 |
+ And the following "course enrolments" exist:
+ | user | course | role |
+ | teacher1 | C1 | editingteacher |
+ And I log in as "teacher1"
+ And I follow "Course 1"
+ And I turn editing mode on
+ And I add a "Page" to section "1" and I fill the form with:
+ | Name | Test page |
+ | Description | Test |
+ | Page content | Test |
+ When I delete "Test page" activity
+ And I follow "Recycle bin"
+ # Default expiry is 0 (never).
+ Then I should not see "Contents will be permanently deleted"
+ # Test changing expiry to something else.
+ When the following config values are set as admin:
+ | expiry | 10 | local_recyclebin |
+ # Step "I reload the page" doesn't work outside of javascript.
+ And I follow "Recycle bin"
+ Then I should see "Contents will be permanently deleted after 10 days"
\ No newline at end of file
diff --git a/admin/tool/recyclebin/tests/behat/logs_test.feature b/admin/tool/recyclebin/tests/behat/logs_test.feature
new file mode 100644
index 00000000000..3956805737e
--- /dev/null
+++ b/admin/tool/recyclebin/tests/behat/logs_test.feature
@@ -0,0 +1,36 @@
+@local_recyclebin
+Feature: Recycle bin refinements
+ As a teacher
+ I want the log to reflect the recycle bin's actions
+
+ Background: Course with teacher exists.
+ Given the following "users" exist:
+ | username | firstname | lastname | email |
+ | teacher1 | Teacher | 1 | teacher@asd.com |
+ And the following "courses" exist:
+ | fullname | shortname |
+ | Course 1 | C1 |
+ And the following "course enrolments" exist:
+ | user | course | role |
+ | teacher1 | C1 | editingteacher |
+
+ Scenario: Delete an assignment and check if it got logged.
+ Given I log in as "teacher1"
+ And I follow "Course 1"
+ And I turn editing mode on
+ And I add a "Page" to section "1" and I fill the form with:
+ | Name | Test page |
+ | Description | Test |
+ | Page content | Test |
+ And I delete "Test page" activity
+ And I follow "Recycle bin"
+ When I click on "Delete" "link"
+ # Uncomment if running with javascript.
+ #And I press "Yes"
+ And I wait to be redirected
+ And I follow "C1"
+ And I expand "Reports" node
+ And I follow "Logs"
+ And I click on "Get these logs" "link_or_button"
+ Then I should see "Item stored"
+ And I should see "Item purged"
diff --git a/admin/tool/recyclebin/tests/category_test.php b/admin/tool/recyclebin/tests/category_test.php
new file mode 100644
index 00000000000..c3d0ae25f9b
--- /dev/null
+++ b/admin/tool/recyclebin/tests/category_test.php
@@ -0,0 +1,124 @@
+.
+
+/**
+ * Recycle bin tests.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Recycle bin category tests.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class local_recyclebin_category_tests extends \advanced_testcase
+{
+ /**
+ * Setup for each test.
+ */
+ protected function setUp() {
+ global $DB;
+
+ $this->resetAfterTest(true);
+ $this->setAdminUser();
+
+ $this->before = $DB->count_records('course');
+ $this->course = $this->getDataGenerator()->create_course();
+ }
+
+ /**
+ * Run a bunch of tests to make sure we capture courses.
+ */
+ public function test_observer() {
+ global $DB;
+
+ $this->assertEquals($this->before + 1, $DB->count_records('course'));
+ delete_course($this->course, false);
+ $this->assertEquals($this->before, $DB->count_records('course'));
+
+ // Try with the API.
+ $recyclebin = new \local_recyclebin\category($this->course->category);
+ $this->assertEquals(1, count($recyclebin->get_items()));
+ }
+
+ /**
+ * Run a bunch of tests to make sure we can restore courses.
+ */
+ public function test_restore() {
+ global $DB;
+
+ delete_course($this->course, false);
+ $this->assertEquals($this->before, $DB->count_records('course'));
+
+ $recyclebin = new \local_recyclebin\category($this->course->category);
+ foreach ($recyclebin->get_items() as $item) {
+ $recyclebin->restore_item($item);
+ }
+
+ $this->assertEquals($this->before + 1, $DB->count_records('course'));
+ $this->assertEquals(0, count($recyclebin->get_items()));
+ }
+
+ /**
+ * Run a bunch of tests to make sure we can purge courses.
+ */
+ public function test_purge() {
+ global $DB;
+
+ delete_course($this->course, false);
+ $this->assertEquals($this->before, $DB->count_records('course'));
+
+ $recyclebin = new \local_recyclebin\category($this->course->category);
+ foreach ($recyclebin->get_items() as $item) {
+ $recyclebin->delete_item($item);
+ }
+
+ $this->assertEquals($this->before, $DB->count_records('course'));
+ $this->assertEquals(0, count($recyclebin->get_items()));
+ }
+
+ /**
+ * Test the cleanup/purge task.
+ */
+ public function test_purge_task() {
+ global $DB;
+
+ set_config('course_expiry', 1, 'local_recyclebin');
+
+ delete_course($this->course, false);
+ $this->assertEquals($this->before, $DB->count_records('course'));
+
+ // Set deleted date to the distant past.
+ $recyclebin = new \local_recyclebin\category($this->course->category);
+ foreach ($recyclebin->get_items() as $item) {
+ $item->deleted = 1;
+ $DB->update_record('local_recyclebin_category', $item);
+ }
+ // Execute cleanup task.
+ $task = new local_recyclebin\task\cleanup_courses();
+ $task->execute();
+
+ $this->assertEquals($this->before, $DB->count_records('course'));
+ $this->assertEquals(0, count($recyclebin->get_items()));
+ }
+}
diff --git a/admin/tool/recyclebin/tests/course_test.php b/admin/tool/recyclebin/tests/course_test.php
new file mode 100644
index 00000000000..83b88675030
--- /dev/null
+++ b/admin/tool/recyclebin/tests/course_test.php
@@ -0,0 +1,138 @@
+.
+
+/**
+ * Recycle bin tests.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Recycle bin course tests.
+ *
+ * @package local_recyclebin
+ * @copyright 2015 University of Kent
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class local_recyclebin_course_tests extends \advanced_testcase
+{
+ /**
+ * Setup for each test.
+ */
+ protected function setUp() {
+ global $DB;
+
+ $this->resetAfterTest(true);
+ $this->setAdminUser();
+
+ $this->course = $this->getDataGenerator()->create_course();
+ $this->before = $DB->count_records('course_modules');
+ $generator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
+ $this->instance = $generator->create_instance(array(
+ 'course' => $this->course->id
+ ));
+ }
+
+ /**
+ * Run a bunch of tests to make sure we capture mods.
+ */
+ public function test_observer() {
+ global $DB;
+
+ $this->assertEquals($this->before + 1, $DB->count_records('course_modules'));
+ $this->assertEquals(0, $DB->count_records('local_recyclebin_course'));
+
+ // Delete the CM.
+ course_delete_module($this->instance->cmid);
+
+ $this->assertEquals($this->before, $DB->count_records('course_modules'));
+ $this->assertEquals(1, $DB->count_records('local_recyclebin_course'));
+
+ // Try with the API.
+ $recyclebin = new \local_recyclebin\course($this->course->id);
+ $this->assertEquals(1, count($recyclebin->get_items()));
+ }
+
+ /**
+ * Run a bunch of tests to make sure we can restore mods.
+ */
+ public function test_restore() {
+ global $DB;
+
+ // Delete the CM.
+ course_delete_module($this->instance->cmid);
+
+ // Try restoring.
+ $recyclebin = new \local_recyclebin\course($this->course->id);
+ foreach ($recyclebin->get_items() as $item) {
+ $recyclebin->restore_item($item);
+ }
+
+ $this->assertEquals($this->before + 1, $DB->count_records('course_modules'));
+ $this->assertEquals(0, $DB->count_records('local_recyclebin_course'));
+ $this->assertEquals(0, count($recyclebin->get_items()));
+ }
+
+ /**
+ * Run a bunch of tests to make sure we can purge mods.
+ */
+ public function test_purge() {
+ global $DB;
+
+ // Delete the CM.
+ course_delete_module($this->instance->cmid);
+
+ // Try purging.
+ $recyclebin = new \local_recyclebin\course($this->course->id);
+ foreach ($recyclebin->get_items() as $item) {
+ $recyclebin->delete_item($item);
+ }
+
+ $this->assertEquals($this->before, $DB->count_records('course_modules'));
+ $this->assertEquals(0, $DB->count_records('local_recyclebin_course'));
+ $this->assertEquals(0, count($recyclebin->get_items()));
+ }
+
+ /**
+ * Test the cleanup/purge task.
+ */
+ public function test_purge_task() {
+ global $DB;
+
+ set_config('expiry', 1, 'local_recyclebin');
+
+ // Delete the CM.
+ course_delete_module($this->instance->cmid);
+
+ // Set deleted date to the distant past.
+ $recyclebin = new \local_recyclebin\course($this->course->id);
+ foreach ($recyclebin->get_items() as $item) {
+ $item->deleted = 1;
+ $DB->update_record('local_recyclebin_course', $item);
+ }
+ // Execute cleanup task.
+ $task = new local_recyclebin\task\cleanup_activities();
+ $task->execute();
+
+ $this->assertEquals($this->before, $DB->count_records('course_modules'));
+ $this->assertEquals(0, $DB->count_records('local_recyclebin_course'));
+ $this->assertEquals(0, count($recyclebin->get_items()));
+ }
+}
diff --git a/admin/tool/recyclebin/upgrade.txt b/admin/tool/recyclebin/upgrade.txt
new file mode 100644
index 00000000000..e65a4684c87
--- /dev/null
+++ b/admin/tool/recyclebin/upgrade.txt
@@ -0,0 +1,4 @@
+3.0
+===
+
+ * local_recyclebin\Observer has been changed to local_recyclebin\observer (lower case). Please update the core hack.
\ No newline at end of file
diff --git a/admin/tool/recyclebin/version.php b/admin/tool/recyclebin/version.php
new file mode 100644
index 00000000000..e22b308dc75
--- /dev/null
+++ b/admin/tool/recyclebin/version.php
@@ -0,0 +1,31 @@
+.
+
+/**
+ * Defines the version.
+ *
+ * @package local_recyclebin
+ * @copyright 2016 Skylar Kelty
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+$plugin->version = 2016022200;
+$plugin->component = 'local_recyclebin';
+$plugin->requires = 2014051200;
+$plugin->maturity = MATURITY_STABLE;
+$plugin->release = '3.3 (Build: 2016022200)';