MDL-66332 too_capability: add show differences option
[moodle.git] / admin / tool / capability / index.php
CommitLineData
244bb396 1<?php
4211dfa4
PS
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
1ea118e4 17/**
46af2845 18 * For a given capability, show what permission it has for every role, and everywhere that it is overridden.
1ea118e4 19 *
9935136f 20 * @package tool_capability
4211dfa4
PS
21 * @copyright 2008 Tim Hunt
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1ea118e4 23 */
24
1fcf0ca8 25require_once(__DIR__ . '/../../../config.php');
9935136f 26require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/capability/locallib.php');
1ea118e4 27require_once($CFG->libdir.'/adminlib.php');
28
1ea118e4 29// Get URL parameters.
9935136f
SH
30$systemcontext = context_system::instance();
31$contextid = optional_param('context', $systemcontext->id, PARAM_INT);
1ea118e4 32
9935136f
SH
33// Check permissions.
34list($context, $course, $cm) = get_context_info_array($contextid);
35require_login($course, false, $cm);
36require_capability('moodle/role:manage', $context);
1ea118e4 37
38// Print the header.
4211dfa4 39admin_externalpage_setup('toolcapability');
1ea118e4 40
9935136f 41// Prepare the list of capabilities to choose from.
1ea118e4 42$capabilitychoices = array();
9935136f 43foreach ($context->get_capabilities() as $cap) {
1ea118e4 44 $capabilitychoices[$cap->name] = $cap->name . ': ' . get_capability_string($cap->name);
45}
46
9935136f
SH
47$allroles = role_fix_names(get_all_roles($context));
48// Prepare the list of roles to choose from.
1ea118e4 49$rolechoices = array('0' => get_string('all'));
50foreach ($allroles as $role) {
c52551dc 51 $rolechoices[$role->id] = $role->localname;
1ea118e4 52}
1ea118e4 53
9935136f
SH
54$form = new tool_capability_settings_form(null, array(
55 'capabilities' => $capabilitychoices,
56 'roles' => $rolechoices
57));
46af2845
SH
58$PAGE->requires->yui_module(
59 'moodle-tool_capability-search',
60 'M.tool_capability.init_capability_search',
61 array(array('strsearch' => get_string('search')))
62);
1ea118e4 63
9935136f
SH
64// Log.
65$capabilities = array();
66$rolestoshow = array();
67$roleids = array('0');
68$cleanedroleids = array();
e96bbb53 69$onlydiff = false;
9935136f 70if ($data = $form->get_data()) {
1ea118e4 71
46af2845
SH
72 $roleids = array();
73 if (!empty($data->roles)) {
74 $roleids = $data->roles;
1ea118e4 75 }
1ea118e4 76
46af2845
SH
77 $capabilities = array();
78 if (!empty($data->capability)) {
79 $capabilities = $data->capability;
1ea118e4 80 }
81
9935136f
SH
82 if (in_array('0', $roleids)) {
83 $rolestoshow = $allroles;
84 } else {
46af2845 85 $cleanedroleids = array_intersect(array_keys($allroles), $roleids);
9935136f
SH
86 if (count($cleanedroleids) === 0) {
87 $rolestoshow = $allroles;
88 } else {
89 foreach ($cleanedroleids as $id) {
90 $rolestoshow[$id] = $allroles[$id];
91 }
1ea118e4 92 }
93 }
e96bbb53 94
95 if (isset($data->onlydiff)) {
96 $onlydiff = $data->onlydiff;
97 }
9935136f 98}
1ea118e4 99
db473c0d 100\tool_capability\event\report_viewed::create()->trigger();
1ea118e4 101
9935136f 102$renderer = $PAGE->get_renderer('tool_capability');
1ea118e4 103
9935136f
SH
104echo $OUTPUT->header();
105
106$form->display();
1ea118e4 107
9935136f
SH
108// If we have a capability, generate the report.
109if (count($capabilities) && count($rolestoshow)) {
46af2845 110 /* @var tool_capability_renderer $renderer */
e96bbb53 111 echo $renderer->capability_comparison_table($capabilities, $context->id, $rolestoshow, $onlydiff);
1ea118e4 112}
113
114// Footer.
73d6f52f 115echo $OUTPUT->footer();
1ea118e4 116
117function print_report_tree($contextid, $contexts, $allroles) {
118 global $CFG;
119
120 // Array for holding lang strings.
121 static $strpermissions = null;
122 if (is_null($strpermissions)) {
123 $strpermissions = array(
124 CAP_INHERIT => get_string('notset','role'),
125 CAP_ALLOW => get_string('allow','role'),
126 CAP_PREVENT => get_string('prevent','role'),
127 CAP_PROHIBIT => get_string('prohibit','role')
128 );
129 }
130
131 // Start the list item, and print the context name as a link to the place to
132 // make changes.
0601e0ee 133 if ($contextid == context_system::instance()->id) {
d1aa1e48 134 $url = "$CFG->wwwroot/$CFG->admin/roles/manage.php";
4211dfa4 135 $title = get_string('changeroles', 'tool_capability');
1ea118e4 136 } else {
d1aa1e48 137 $url = "$CFG->wwwroot/$CFG->admin/roles/override.php?contextid=$contextid";
4211dfa4 138 $title = get_string('changeoverrides', 'tool_capability');
1ea118e4 139 }
e922fe23
PS
140 $context = context::instance_by_id($contextid);
141 echo '<h3><a href="' . $url . '" title="' . $title . '">', $context->get_context_name(), '</a></h3>';
1ea118e4 142
143 // If there are any role overrides here, print them.
eab8ed9f 144 if (!empty($contexts[$contextid]->rolecapabilities)) {
1ea118e4 145 $rowcounter = 0;
146 echo '<table class="generaltable rolecaps"><tbody>';
147 foreach ($allroles as $role) {
eab8ed9f
PS
148 if (isset($contexts[$contextid]->rolecapabilities[$role->id])) {
149 $permission = $contexts[$contextid]->rolecapabilities[$role->id];
c52551dc 150 echo '<tr class="r' . ($rowcounter % 2) . '"><th class="cell">', $role->localname,
1ea118e4 151 '</th><td class="cell">' . $strpermissions[$permission] . '</td></tr>';
152 $rowcounter++;
153 }
154 }
155 echo '</tbody></table>';
156 }
157
158 // After we have done the site context, change the string for CAP_INHERIT
159 // from 'notset' to 'inherit'.
160 $strpermissions[CAP_INHERIT] = get_string('inherit','role');
161
162 // If there are any child contexts, print them recursively.
163 if (!empty($contexts[$contextid]->children)) {
164 echo '<ul>';
165 foreach ($contexts[$contextid]->children as $childcontextid) {
166 echo '<li>';
167 print_report_tree($childcontextid, $contexts, $allroles);
168 echo '</li>';
169 }
170 echo '</ul>';
171 }
172}