3 * For a given capability, show what permission it has for every role, and
4 * everywhere that it is overridden.
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 require_once(dirname(__FILE__).'/../../../config.php');
12 require_once($CFG->libdir.'/adminlib.php');
16 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
17 require_capability('moodle/role:manage', $systemcontext);
19 // Get URL parameters.
20 $capability = optional_param('capability', '', PARAM_CAPABILITY);
21 $roleids = optional_param_array('roles', array('0'), PARAM_INTEGER);
23 // Clean the passed in list of role ids. If 'All' selected as an option, or
24 // if none were selected, do all roles.
25 $allroles = get_all_roles();
26 $cleanedroleids = array();
27 foreach ($roleids as $roleid) {
29 $cleanedroleids = array_keys($allroles);
32 if (array_key_exists($roleid, $allroles)) {
33 $cleanedroleids[] = $roleid;
36 if (empty($cleanedroleids)) {
37 $cleanedroleids = array_keys($allroles);
40 // Include the required JavaScript.
41 $PAGE->requires->js_init_call('M.report_capability.init', array(get_string('search')));
44 add_to_log(SITEID, "admin", "report capability", "report/capability/index.php?capability=$capability", $capability);
47 admin_externalpage_setup('reportcapability');
48 echo $OUTPUT->header();
50 // Prepare the list of capabilities to choose from
51 $allcapabilities = fetch_context_capabilities($systemcontext);
52 $capabilitychoices = array();
53 foreach ($allcapabilities as $cap) {
54 $capabilitychoices[$cap->name] = $cap->name . ': ' . get_capability_string($cap->name);
57 // Prepare the list of roles to choose from
58 $rolechoices = array('0' => get_string('all'));
59 foreach ($allroles as $role) {
60 $rolechoices[$role->id] = $role->name;
62 if (count($cleanedroleids) == count($allroles)) {
63 // Select 'All', rather than each role individually.
64 $selectedroleids = array('0');
66 $selectedroleids = $cleanedroleids;
69 // Print the settings form.
70 echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
71 echo '<form method="get" action="." id="settingsform"><div>';
72 echo $OUTPUT->heading(get_string('reportsettings', 'report_capability'));
73 echo '<p id="intro">', get_string('intro', 'report_capability') , '</p>';
74 echo '<p><label for="menucapability"> ' . get_string('capabilitylabel', 'report_capability') . '</label></p>';
75 echo html_writer::select($capabilitychoices, 'capability', $capability, array(''=>'choose'), array('size'=>10));
76 echo '<p><label for="menuroles"> ' . get_string('roleslabel', 'report_capability') . '</label></p>';
77 echo html_writer::select($rolechoices, 'roles[]', $selectedroleids, false, array('size'=>10, 'multiple'=>'multiple'));
78 echo '<p><input type="submit" id="settingssubmit" value="' . get_string('getreport', 'report_capability') . '" /></p>';
80 echo $OUTPUT->box_end();
82 // If we have a capability, generate the report.
85 // Work out the bits needed for the SQL WHERE clauses.
86 $params = array($capability);
88 if (count($cleanedroleids) != count($allroles)) {
89 list($sqlroletest, $roleparams) = $DB->get_in_or_equal($cleanedroleids);
90 $params = array_merge($params, $roleparams);
91 $sqlroletest = 'AND roleid ' . $sqlroletest;
94 // Get all the role_capabilities rows for this capability - that is, all
95 // role definitions, and all role overrides.
96 $rolecaps = $DB->get_records_sql("
97 SELECT id, roleid, contextid, permission
98 FROM {role_capabilities}
99 WHERE capability = ? $sqlroletest", $params);
101 // In order to display a nice tree of contexts, we need to get all the
102 // ancestors of all the contexts in the query we just did.
103 $relevantpaths = $DB->get_records_sql_menu("
104 SELECT DISTINCT con.path, 1
105 FROM {context} con JOIN {role_capabilities} rc ON rc.contextid = con.id
106 WHERE capability = ? $sqlroletest", $params);
107 $requiredcontexts = array($systemcontext->id);
108 foreach ($relevantpaths as $path => $notused) {
109 $requiredcontexts = array_merge($requiredcontexts, explode('/', trim($path, '/')));
111 $requiredcontexts = array_unique($requiredcontexts);
113 // Now load those contexts.
114 list($sqlcontexttest, $contextparams) = $DB->get_in_or_equal($requiredcontexts);
115 $contexts = get_sorted_contexts('ctx.id ' . $sqlcontexttest, $contextparams);
117 // Prepare some empty arrays to hold the data we are about to compute.
118 foreach ($contexts as $conid => $con) {
119 $contexts[$conid]->children = array();
120 $contexts[$conid]->rolecapabilities = array();
123 // Put the contexts into a tree structure.
124 foreach ($contexts as $conid => $con) {
125 $parentcontextid = get_parent_contextid($con);
126 if ($parentcontextid) {
127 $contexts[$parentcontextid]->children[] = $conid;
131 // Put the role capabilities into the context tree.
132 foreach ($rolecaps as $rolecap) {
133 $contexts[$rolecap->contextid]->rolecapabilities[$rolecap->roleid] = $rolecap->permission;
136 // Fill in any missing rolecaps for the system context.
137 foreach ($cleanedroleids as $roleid) {
138 if (!isset($contexts[$systemcontext->id]->rolecapabilities[$roleid])) {
139 $contexts[$systemcontext->id]->rolecapabilities[$roleid] = CAP_INHERIT;
143 // Print the report heading.
144 echo $OUTPUT->heading(get_string('reportforcapability', 'report_capability', get_capability_string($capability)), 2, 'main', 'report');
145 if (count($cleanedroleids) != count($allroles)) {
146 $rolenames = array();
147 foreach ($cleanedroleids as $roleid) {
148 $rolenames[] = $allroles[$roleid]->name;
150 echo '<p>', get_string('forroles', 'report_capability', implode(', ', $rolenames)), '</p>';
153 // Now, recursively print the contexts, and the role information.
154 print_report_tree($systemcontext->id, $contexts, $allroles);
158 echo $OUTPUT->footer();
160 function print_report_tree($contextid, $contexts, $allroles) {
163 // Array for holding lang strings.
164 static $strpermissions = null;
165 if (is_null($strpermissions)) {
166 $strpermissions = array(
167 CAP_INHERIT => get_string('notset','role'),
168 CAP_ALLOW => get_string('allow','role'),
169 CAP_PREVENT => get_string('prevent','role'),
170 CAP_PROHIBIT => get_string('prohibit','role')
174 // Start the list item, and print the context name as a link to the place to
176 if ($contextid == get_system_context()->id) {
177 $url = "$CFG->wwwroot/$CFG->admin/roles/manage.php";
178 $title = get_string('changeroles', 'report_capability');
180 $url = "$CFG->wwwroot/$CFG->admin/roles/override.php?contextid=$contextid";
181 $title = get_string('changeoverrides', 'report_capability');
183 echo '<h3><a href="' . $url . '" title="' . $title . '">', print_context_name($contexts[$contextid]), '</a></h3>';
185 // If there are any role overrides here, print them.
186 if (!empty($contexts[$contextid]->rolecapabilities)) {
188 echo '<table class="generaltable rolecaps"><tbody>';
189 foreach ($allroles as $role) {
190 if (isset($contexts[$contextid]->rolecapabilities[$role->id])) {
191 $permission = $contexts[$contextid]->rolecapabilities[$role->id];
192 echo '<tr class="r' . ($rowcounter % 2) . '"><th class="cell">', $role->name,
193 '</th><td class="cell">' . $strpermissions[$permission] . '</td></tr>';
197 echo '</tbody></table>';
200 // After we have done the site context, change the string for CAP_INHERIT
201 // from 'notset' to 'inherit'.
202 $strpermissions[CAP_INHERIT] = get_string('inherit','role');
204 // If there are any child contexts, print them recursively.
205 if (!empty($contexts[$contextid]->children)) {
207 foreach ($contexts[$contextid]->children as $childcontextid) {
209 print_report_tree($childcontextid, $contexts, $allroles);