MDL-48012 tool_recyclebin: converted to an admin tool
[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
MN
30 * @param navigation_node $navigation The navigation node to extend
31 * @param stdClass $course The course to object for the tool
32 * @param context $context The context of the course
60444e53 33 */
e78cd9b7 34function tool_recyclebin_extend_navigation_course($navigation, $course, $context) {
60444e53
SK
35 global $PAGE;
36
37 $url = null;
38 $bin = null;
39 $settingnode = null;
40
e78cd9b7
MN
41 // Only add this settings item on non-site course pages.
42 if (!$PAGE->course || $PAGE->course->id == SITEID || !\tool_recyclebin\course::is_enabled()) {
43 return null;
44 }
60444e53 45
e78cd9b7
MN
46 // Check we can view the recycle bin.
47 if (!has_capability('tool/recyclebin:view_item', $context)) {
48 return null;
49 }
60444e53 50
e78cd9b7
MN
51 $bin = new \tool_recyclebin\course($context->instanceid);
52 $url = new moodle_url('/admin/tool/recyclebin/index.php', array(
53 'contextid' => $context->id
54 ));
60444e53 55
e78cd9b7
MN
56 // If we are set to auto-hide, check the number of items.
57 $autohide = get_config('tool_recyclebin', 'autohide');
58 if ($autohide) {
59 $items = $bin->get_items();
60 if (empty($items)) {
60444e53
SK
61 return null;
62 }
e78cd9b7
MN
63 }
64
65 // Add the recyclebin link.
66 $pluginname = get_string('pluginname', 'tool_recyclebin');
60444e53 67
e78cd9b7
MN
68 $node = navigation_node::create(
69 $pluginname,
70 $url,
71 navigation_node::NODETYPE_LEAF,
72 'tool_recyclebin',
73 'tool_recyclebin',
74 new pix_icon('e/cleanup_messy_code', $pluginname)
75 );
60444e53 76
e78cd9b7
MN
77 if ($PAGE->url->compare($url, URL_MATCH_BASE)) {
78 $node->make_active();
60444e53
SK
79 }
80
e78cd9b7
MN
81 $navigation->add_node($node);
82}
83
84/**
85 * Adds a recycle bin link to the course admin menu.
86 *
87 * @param navigation_node $navigation The navigation node to extend
88 * @param context $context The context of the course
89 */
90function tool_recyclebin_extend_navigation_category_settings($navigation, $context) {
91 global $PAGE;
92
93 $url = null;
94 $bin = null;
95 $settingnode = null;
96
97 // Check we can view the recycle bin.
98 if (!has_capability('tool/recyclebin:view_course', $context) || !\tool_recyclebin\category::is_enabled()) {
99 return null;
60444e53
SK
100 }
101
e78cd9b7
MN
102 // Add a link to the category recyclebin.
103 $bin = new \tool_recyclebin\category($context->instanceid);
104 $url = new moodle_url('/admin/tool/recyclebin/index.php', array(
105 'contextid' => $context->id
106 ));
107
60444e53 108 // If we are set to auto-hide, check the number of items.
e78cd9b7 109 $autohide = get_config('tool_recyclebin', 'autohide');
60444e53
SK
110 if ($autohide) {
111 $items = $bin->get_items();
112 if (empty($items)) {
113 return null;
114 }
115 }
116
117 // Add the recyclebin link.
e78cd9b7 118 $pluginname = get_string('pluginname', 'tool_recyclebin');
60444e53
SK
119
120 $node = navigation_node::create(
121 $pluginname,
122 $url,
123 navigation_node::NODETYPE_LEAF,
e78cd9b7
MN
124 'tool_recyclebin',
125 'tool_recyclebin',
60444e53
SK
126 new pix_icon('e/cleanup_messy_code', $pluginname)
127 );
128
129 if ($PAGE->url->compare($url, URL_MATCH_BASE)) {
130 $node->make_active();
131 }
132
e78cd9b7
MN
133 $navigation->add_node($node);
134}