forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
136 lines (122 loc) · 5.11 KB
/
renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Capability tool renderer.
*
* @package tool_capability
* @copyright 2013 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* The primary renderer for the capability tool.
*
* @copyright 2013 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_capability_renderer extends plugin_renderer_base {
/**
* Returns an array of permission strings.
*
* @return lang_string[]
*/
protected function get_permission_strings() {
static $strpermissions;
if (!$strpermissions) {
$strpermissions = array(
CAP_INHERIT => new lang_string('inherit', 'role'),
CAP_ALLOW => new lang_string('allow', 'role'),
CAP_PREVENT => new lang_string('prevent', 'role'),
CAP_PROHIBIT => new lang_string('prohibit', 'role')
);
}
return $strpermissions;
}
/**
* Returns an array of permission CSS classes.
*
* @return string[]
*/
protected function get_permission_classes() {
static $permissionclasses;
if (!$permissionclasses) {
$permissionclasses = array(
CAP_INHERIT => 'inherit',
CAP_ALLOW => 'allow',
CAP_PREVENT => 'prevent',
CAP_PROHIBIT => 'prohibit',
);
}
return $permissionclasses;
}
/**
* Produces a table to visually compare roles and capabilities.
*
* @param array $capabilities An array of capabilities to show comparison for.
* @param int $contextid The context we are displaying for.
* @param array $roles An array of roles to show comparison for.
* @return string
*/
public function capability_comparison_table(array $capabilities, $contextid, array $roles) {
$strpermissions = $this->get_permission_strings();
$permissionclasses = $this->get_permission_classes();
if ($contextid === context_system::instance()->id) {
$strpermissions[CAP_INHERIT] = new lang_string('notset', 'role');
}
$table = new html_table();
$table->attributes['class'] = 'comparisontable';
$table->head = array(' ');
foreach ($roles as $role) {
$url = new moodle_url('/admin/roles/define.php', array('action' => 'view', 'roleid' => $role->id));
$table->head[] = html_writer::div(html_writer::link($url, $role->localname));
}
$table->data = array();
foreach ($capabilities as $capability) {
$contexts = tool_capability_calculate_role_data($capability, $roles);
$captitle = new html_table_cell(get_capability_string($capability) . html_writer::span($capability));
$captitle->header = true;
$row = new html_table_row(array($captitle));
foreach ($roles as $role) {
if (isset($contexts[$contextid]->rolecapabilities[$role->id])) {
$permission = $contexts[$contextid]->rolecapabilities[$role->id];
} else {
$permission = CAP_INHERIT;
}
$cell = new html_table_cell($strpermissions[$permission]);
$cell->attributes['class'] = $permissionclasses[$permission];
$row->cells[] = $cell;
}
$table->data[] = $row;
}
// Start the list item, and print the context name as a link to the place to make changes.
if ($contextid == context_system::instance()->id) {
$url = new moodle_url('/admin/roles/manage.php');
$title = get_string('changeroles', 'tool_capability');
} else {
$url = new moodle_url('/admin/roles/override.php', array('contextid' => $contextid));
$title = get_string('changeoverrides', 'tool_capability');
}
$context = context::instance_by_id($contextid);
$html = $this->output->heading(html_writer::link($url, $context->get_context_name(), array('title' => $title)), 3);
$html .= html_writer::table($table);
// If there are any child contexts, print them recursively.
if (!empty($contexts[$contextid]->children)) {
foreach ($contexts[$contextid]->children as $childcontextid) {
$html .= $this->capability_comparison_table($capabilities, $childcontextid, $roles, true);
}
}
return $html;
}
}