MDL-54751 core_course: Create adhoc task for course module deletion
[moodle.git] / admin / tool / recyclebin / lib.php
CommitLineData
60444e53
SK
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Local lib code
19 *
e78cd9b7 20 * @package tool_recyclebin
60444e53
SK
21 * @copyright 2015 Skylar Kelty <S.Kelty@kent.ac.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die;
26
27/**
28 * Adds a recycle bin link to the course admin menu.
29 *
e78cd9b7 30 * @param navigation_node $navigation The navigation node to extend
93af6a61
MN
31 * @param stdClass $course The course to object for the tool
32 * @param context $context The context of the course
33 * @return void|null return null if we don't want to display the node.
60444e53 34 */
e78cd9b7 35function tool_recyclebin_extend_navigation_course($navigation, $course, $context) {
60444e53
SK
36 global $PAGE;
37
e78cd9b7 38 // Only add this settings item on non-site course pages.
93af6a61 39 if (!$PAGE->course || $PAGE->course->id == SITEID || !\tool_recyclebin\course_bin::is_enabled()) {
e78cd9b7
MN
40 return null;
41 }
60444e53 42
93af6a61
MN
43 $coursebin = new \tool_recyclebin\course_bin($context->instanceid);
44
e78cd9b7 45 // Check we can view the recycle bin.
93af6a61 46 if (!$coursebin->can_view()) {
e78cd9b7
MN
47 return null;
48 }
60444e53 49
93af6a61
MN
50 $url = null;
51 $settingnode = null;
52
e78cd9b7
MN
53 $url = new moodle_url('/admin/tool/recyclebin/index.php', array(
54 'contextid' => $context->id
55 ));
60444e53 56
e78cd9b7
MN
57 // If we are set to auto-hide, check the number of items.
58 $autohide = get_config('tool_recyclebin', 'autohide');
59 if ($autohide) {
93af6a61 60 $items = $coursebin->get_items();
e78cd9b7 61 if (empty($items)) {
60444e53
SK
62 return null;
63 }
e78cd9b7
MN
64 }
65
66 // Add the recyclebin link.
67 $pluginname = get_string('pluginname', 'tool_recyclebin');
60444e53 68
e78cd9b7
MN
69 $node = navigation_node::create(
70 $pluginname,
71 $url,
72 navigation_node::NODETYPE_LEAF,
73 'tool_recyclebin',
74 'tool_recyclebin',
93af6a61 75 new pix_icon('trash', $pluginname, 'tool_recyclebin')
e78cd9b7 76 );
60444e53 77
e78cd9b7
MN
78 if ($PAGE->url->compare($url, URL_MATCH_BASE)) {
79 $node->make_active();
60444e53
SK
80 }
81
e78cd9b7
MN
82 $navigation->add_node($node);
83}
84
85/**
86 * Adds a recycle bin link to the course admin menu.
87 *
88 * @param navigation_node $navigation The navigation node to extend
93af6a61
MN
89 * @param context $context The context of the course
90 * @return void|null return null if we don't want to display the node.
e78cd9b7
MN
91 */
92function tool_recyclebin_extend_navigation_category_settings($navigation, $context) {
93 global $PAGE;
94
93af6a61
MN
95 // Check if it is enabled.
96 if (!\tool_recyclebin\category_bin::is_enabled()) {
97 return null;
98 }
99
100 $categorybin = new \tool_recyclebin\category_bin($context->instanceid);
e78cd9b7
MN
101
102 // Check we can view the recycle bin.
93af6a61 103 if (!$categorybin->can_view()) {
e78cd9b7 104 return null;
60444e53
SK
105 }
106
93af6a61
MN
107 $url = null;
108 $settingnode = null;
109
e78cd9b7 110 // Add a link to the category recyclebin.
e78cd9b7
MN
111 $url = new moodle_url('/admin/tool/recyclebin/index.php', array(
112 'contextid' => $context->id
113 ));
114
60444e53 115 // If we are set to auto-hide, check the number of items.
e78cd9b7 116 $autohide = get_config('tool_recyclebin', 'autohide');
60444e53 117 if ($autohide) {
93af6a61 118 $items = $categorybin->get_items();
60444e53
SK
119 if (empty($items)) {
120 return null;
121 }
122 }
123
124 // Add the recyclebin link.
e78cd9b7 125 $pluginname = get_string('pluginname', 'tool_recyclebin');
60444e53
SK
126
127 $node = navigation_node::create(
128 $pluginname,
129 $url,
130 navigation_node::NODETYPE_LEAF,
e78cd9b7
MN
131 'tool_recyclebin',
132 'tool_recyclebin',
93af6a61 133 new pix_icon('trash', $pluginname, 'tool_recyclebin')
60444e53
SK
134 );
135
136 if ($PAGE->url->compare($url, URL_MATCH_BASE)) {
137 $node->make_active();
138 }
139
e78cd9b7 140 $navigation->add_node($node);
c27596c5
MN
141}
142
143/**
144 * Hook called before we delete a course module.
145 *
146 * @param \stdClass $cm The course module record.
147 */
148function tool_recyclebin_pre_course_module_delete($cm) {
93af6a61
MN
149 if (\tool_recyclebin\course_bin::is_enabled()) {
150 $coursebin = new \tool_recyclebin\course_bin($cm->course);
151 $coursebin->store_item($cm);
c27596c5
MN
152 }
153}
154
155/**
156 * Hook called before we delete a course.
157 *
158 * @param \stdClass $course The course record.
159 */
160function tool_recyclebin_pre_course_delete($course) {
aa60ab01
CB
161 // It is possible that the course deletion which triggered this hook
162 // was from an in progress course restore. In that case we do not want
163 // it in the recycle bin.
164 if (isset($course->deletesource) && $course->deletesource == 'restore') {
165 return;
166 }
93af6a61
MN
167 // Delete all the items in the course recycle bin, regardless if it enabled or not.
168 // It may have been enabled, then disabled later on, so may still have content.
169 $coursebin = new \tool_recyclebin\course_bin($course->id);
170 $coursebin->delete_all_items();
171
172 if (\tool_recyclebin\category_bin::is_enabled()) {
173 $categorybin = new \tool_recyclebin\category_bin($course->category);
174 $categorybin->store_item($course);
c27596c5
MN
175 }
176}
93af6a61
MN
177
178/**
179 * Hook called before we delete a category.
180 *
181 * @param \stdClass $category The category record.
182 */
183function tool_recyclebin_pre_course_category_delete($category) {
184 // Delete all the items in the category recycle bin, regardless if it enabled or not.
185 // It may have been enabled, then disabled later on, so may still have content.
186 $categorybin = new \tool_recyclebin\category_bin($category->id);
187 $categorybin->delete_all_items();
188}