MDL-66332 too_capability: add show differences option
[moodle.git] / admin / tool / capability / renderer.php
CommitLineData
9935136f
SH
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 * Capability tool renderer.
19 *
20 * @package tool_capability
21 * @copyright 2013 Sam Hemelryk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25/**
26 * The primary renderer for the capability tool.
27 *
28 * @copyright 2013 Sam Hemelryk
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 */
31class tool_capability_renderer extends plugin_renderer_base {
32
33 /**
34 * Returns an array of permission strings.
35 *
36 * @return lang_string[]
37 */
38 protected function get_permission_strings() {
39 static $strpermissions;
40 if (!$strpermissions) {
41 $strpermissions = array(
46af2845
SH
42 CAP_INHERIT => new lang_string('inherit', 'role'),
43 CAP_ALLOW => new lang_string('allow', 'role'),
44 CAP_PREVENT => new lang_string('prevent', 'role'),
45 CAP_PROHIBIT => new lang_string('prohibit', 'role')
9935136f
SH
46 );
47 }
48 return $strpermissions;
49 }
50
51 /**
52 * Returns an array of permission CSS classes.
53 *
54 * @return string[]
55 */
56 protected function get_permission_classes() {
57 static $permissionclasses;
58 if (!$permissionclasses) {
59 $permissionclasses = array(
60 CAP_INHERIT => 'inherit',
61 CAP_ALLOW => 'allow',
62 CAP_PREVENT => 'prevent',
63 CAP_PROHIBIT => 'prohibit',
64 );
65 }
66 return $permissionclasses;
67 }
68
69 /**
70 * Produces a table to visually compare roles and capabilities.
71 *
72 * @param array $capabilities An array of capabilities to show comparison for.
73 * @param int $contextid The context we are displaying for.
74 * @param array $roles An array of roles to show comparison for.
e96bbb53 75 * @param bool $onlydiff show only different permissions
9935136f
SH
76 * @return string
77 */
e96bbb53 78 public function capability_comparison_table(array $capabilities, $contextid, array $roles, $onlydiff=false) {
9935136f
SH
79
80 $strpermissions = $this->get_permission_strings();
81 $permissionclasses = $this->get_permission_classes();
82
83 if ($contextid === context_system::instance()->id) {
84 $strpermissions[CAP_INHERIT] = new lang_string('notset', 'role');
85 }
86
87 $table = new html_table();
88 $table->attributes['class'] = 'comparisontable';
89 $table->head = array('&nbsp;');
90 foreach ($roles as $role) {
8eb2ea83 91 $url = new moodle_url('/admin/roles/define.php', array('action' => 'view', 'roleid' => $role->id));
9935136f
SH
92 $table->head[] = html_writer::div(html_writer::link($url, $role->localname));
93 }
94 $table->data = array();
95
96 foreach ($capabilities as $capability) {
97 $contexts = tool_capability_calculate_role_data($capability, $roles);
98 $captitle = new html_table_cell(get_capability_string($capability) . html_writer::span($capability));
99 $captitle->header = true;
100
101 $row = new html_table_row(array($captitle));
102
e96bbb53 103 $permissiontypes = array();
9935136f
SH
104 foreach ($roles as $role) {
105 if (isset($contexts[$contextid]->rolecapabilities[$role->id])) {
106 $permission = $contexts[$contextid]->rolecapabilities[$role->id];
107 } else {
108 $permission = CAP_INHERIT;
109 }
e96bbb53 110 if (!in_array($permission, $permissiontypes)) {
111 $permissiontypes[] = $permission;
112 }
9935136f
SH
113 $cell = new html_table_cell($strpermissions[$permission]);
114 $cell->attributes['class'] = $permissionclasses[$permission];
115 $row->cells[] = $cell;
116 }
e96bbb53 117 if (!$onlydiff || count($permissiontypes) > 1) {
118 $table->data[] = $row;
119 }
9935136f
SH
120 }
121
122 // Start the list item, and print the context name as a link to the place to make changes.
580ed1a2
PH
123 $context = context::instance_by_id($contextid);
124
125 if ($context instanceof context_system) {
9935136f 126 $url = new moodle_url('/admin/roles/manage.php');
9935136f 127 } else {
580ed1a2 128 $url = new moodle_url('/admin/roles/permissions.php', ['contextid' => $contextid]);
9935136f 129 }
580ed1a2
PH
130
131 $title = get_string('permissionsincontext', 'core_role', $context->get_context_name());
132
133 $html = $this->output->heading(html_writer::link($url, $title), 3);
e96bbb53 134 if (!empty($table->data)) {
135 $html .= html_writer::table($table);
136 } else {
137 $html .= html_writer::tag('p', get_string('nodifferences', 'tool_capability'));
138 }
9935136f
SH
139 // If there are any child contexts, print them recursively.
140 if (!empty($contexts[$contextid]->children)) {
141 foreach ($contexts[$contextid]->children as $childcontextid) {
e96bbb53 142 $html .= $this->capability_comparison_table($capabilities, $childcontextid, $roles, $onlydiff);
9935136f
SH
143 }
144 }
145 return $html;
146 }
147
580ed1a2 148}