MDL-48012 tool_recyclebin: added recyclebin to core
[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 *
20 * @package local_recyclebin
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 *
30 * @param settings_navigation $nav Nav menu
31 * @param context $context Context of the menu
32 * @return navigation_node A new navigation mode to insert.
33 */
34function local_recyclebin_extend_settings_navigation(settings_navigation $nav, context $context) {
35 global $PAGE;
36
37 $url = null;
38 $bin = null;
39 $settingnode = null;
40
41 // What context are we in?
42 if ($context->contextlevel == \CONTEXT_COURSECAT) {
43 // Check we can view the recycle bin.
44 if (!has_capability('local/recyclebin:view_course', $context) || !\local_recyclebin\category::is_enabled()) {
45 return null;
46 }
47
48 // Add a link to the category recyclebin.
49 $bin = new \local_recyclebin\category($context->instanceid);
50 $url = new moodle_url('/local/recyclebin/index.php', array(
51 'contextid' => $context->id
52 ));
53
54 $settingnode = $nav->find('categorysettings', null);
55 } else {
56 // Only add this settings item on non-site course pages.
57 if (!$PAGE->course || $PAGE->course->id == SITEID || !\local_recyclebin\course::is_enabled()) {
58 return null;
59 }
60
61 // We might be in a mod page, etc.
62 $coursectx = \context_course::instance($PAGE->course->id);
63
64 // Check we can view the recycle bin.
65 if (!has_capability('local/recyclebin:view_item', $coursectx)) {
66 return null;
67 }
68
69 $bin = new \local_recyclebin\course($coursectx->instanceid);
70 $url = new moodle_url('/local/recyclebin/index.php', array(
71 'contextid' => $coursectx->id
72 ));
73
74 $settingnode = $nav->find('courseadmin', navigation_node::TYPE_COURSE);
75 }
76
77 if ($settingnode == null) {
78 return;
79 }
80
81 // If we are set to auto-hide, check the number of items.
82 $autohide = get_config('local_recyclebin', 'autohide');
83 if ($autohide) {
84 $items = $bin->get_items();
85 if (empty($items)) {
86 return null;
87 }
88 }
89
90 // Add the recyclebin link.
91 $pluginname = get_string('pluginname', 'local_recyclebin');
92
93 $node = navigation_node::create(
94 $pluginname,
95 $url,
96 navigation_node::NODETYPE_LEAF,
97 'local_recyclebin',
98 'local_recyclebin',
99 new pix_icon('e/cleanup_messy_code', $pluginname)
100 );
101
102 if ($PAGE->url->compare($url, URL_MATCH_BASE)) {
103 $node->make_active();
104 }
105
106 $settingnode->add_node($node);
107
108 return $node;
109}
110
111/**
112 * For pre-2.9 installations.
113 *
114 * @param settings_navigation $nav
115 * @param context $context
116 */
117function local_recyclebin_extends_settings_navigation(settings_navigation $nav, context $context) {
118 local_recyclebin_extend_settings_navigation($nav, $context);
119}