15
15
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18
* For a given capability, show what permission it has for every role, and
19
* everywhere that it is overridden.
18
* For a given capability, show what permission it has for every role, and everywhere that it is overridden.
22
* @subpackage capability
20
* @package tool_capability
23
21
* @copyright 2008 Tim Hunt
24
22
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
25
require_once(dirname(__FILE__) . '/../../../config.php');
26
require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/capability/locallib.php');
28
27
require_once($CFG->libdir.'/adminlib.php');
29
// Get URL parameters.
30
$systemcontext = context_system::instance();
31
$contextid = optional_param('context', $systemcontext->id, PARAM_INT);
30
33
// Check permissions.
32
$systemcontext = context_system::instance();
33
require_capability('moodle/role:manage', $systemcontext);
35
// Get URL parameters.
36
$capability = optional_param('capability', '', PARAM_CAPABILITY);
37
$roleids = optional_param_array('roles', array('0'), PARAM_INT);
39
// Clean the passed in list of role ids. If 'All' selected as an option, or
40
// if none were selected, do all roles.
41
$allroles = role_fix_names(get_all_roles());
42
$cleanedroleids = array();
43
foreach ($roleids as $roleid) {
45
$cleanedroleids = array_keys($allroles);
48
if (array_key_exists($roleid, $allroles)) {
49
$cleanedroleids[] = $roleid;
52
if (empty($cleanedroleids)) {
53
$cleanedroleids = array_keys($allroles);
56
// Include the required JavaScript.
57
$PAGE->requires->js_init_call('M.tool_capability.init', array(get_string('search')));
60
add_to_log(SITEID, "admin", "tool capability", "tool/capability/index.php?capability=$capability", $capability);
34
list($context, $course, $cm) = get_context_info_array($contextid);
35
require_login($course, false, $cm);
36
require_capability('moodle/role:manage', $context);
62
38
// Print the header.
63
39
admin_externalpage_setup('toolcapability');
64
echo $OUTPUT->header();
66
// Prepare the list of capabilities to choose from
67
$allcapabilities = fetch_context_capabilities($systemcontext);
41
// Prepare the list of capabilities to choose from.
68
42
$capabilitychoices = array();
69
foreach ($allcapabilities as $cap) {
43
foreach ($context->get_capabilities() as $cap) {
70
44
$capabilitychoices[$cap->name] = $cap->name . ': ' . get_capability_string($cap->name);
73
// Prepare the list of roles to choose from
47
$allroles = role_fix_names(get_all_roles($context));
48
// Prepare the list of roles to choose from.
74
49
$rolechoices = array('0' => get_string('all'));
75
50
foreach ($allroles as $role) {
76
51
$rolechoices[$role->id] = $role->localname;
78
if (count($cleanedroleids) == count($allroles)) {
79
// Select 'All', rather than each role individually.
80
$selectedroleids = array('0');
82
$selectedroleids = $cleanedroleids;
54
$form = new tool_capability_settings_form(null, array(
55
'capabilities' => $capabilitychoices,
56
'roles' => $rolechoices
58
$PAGE->requires->yui_module(
59
'moodle-tool_capability-search',
60
'M.tool_capability.init_capability_search',
61
array(array('strsearch' => get_string('search')))
65
$capabilities = array();
66
$rolestoshow = array();
67
$roleids = array('0');
68
$cleanedroleids = array();
69
if ($data = $form->get_data()) {
72
if (!empty($data->roles)) {
73
$roleids = $data->roles;
76
$capabilities = array();
77
if (!empty($data->capability)) {
78
$capabilities = $data->capability;
81
if (in_array('0', $roleids)) {
82
$rolestoshow = $allroles;
84
$cleanedroleids = array_intersect(array_keys($allroles), $roleids);
85
if (count($cleanedroleids) === 0) {
86
$rolestoshow = $allroles;
88
foreach ($cleanedroleids as $id) {
89
$rolestoshow[$id] = $allroles[$id];
85
// Print the settings form.
86
echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
87
echo '<form method="get" action="" id="settingsform"><div>';
88
echo $OUTPUT->heading(get_string('reportsettings', 'tool_capability'));
89
echo '<p id="intro">', get_string('intro', 'tool_capability') , '</p>';
90
echo '<p><label for="menucapability"> ' . get_string('capabilitylabel', 'tool_capability') . '</label></p>';
91
echo html_writer::select($capabilitychoices, 'capability', $capability, array(''=>'choose'), array('size'=>10));
92
echo '<p><label for="menuroles"> ' . get_string('roleslabel', 'tool_capability') . '</label></p>';
93
echo html_writer::select($rolechoices, 'roles[]', $selectedroleids, false, array('size'=>10, 'multiple'=>'multiple'));
94
echo '<p><input type="submit" id="settingssubmit" value="' . get_string('getreport', 'tool_capability') . '" /></p>';
96
echo $OUTPUT->box_end();
95
add_to_log(SITEID, "admin", "tool capability", "tool/capability/index.php", count($capabilities));
97
$renderer = $PAGE->get_renderer('tool_capability');
99
echo $OUTPUT->header();
98
103
// If we have a capability, generate the report.
101
// Work out the bits needed for the SQL WHERE clauses.
102
$params = array($capability);
104
if (count($cleanedroleids) != count($allroles)) {
105
list($sqlroletest, $roleparams) = $DB->get_in_or_equal($cleanedroleids);
106
$params = array_merge($params, $roleparams);
107
$sqlroletest = 'AND roleid ' . $sqlroletest;
110
// Get all the role_capabilities rows for this capability - that is, all
111
// role definitions, and all role overrides.
112
$rolecaps = $DB->get_records_sql("
113
SELECT id, roleid, contextid, permission
114
FROM {role_capabilities}
115
WHERE capability = ? $sqlroletest", $params);
117
// In order to display a nice tree of contexts, we need to get all the
118
// ancestors of all the contexts in the query we just did.
119
$relevantpaths = $DB->get_records_sql_menu("
120
SELECT DISTINCT con.path, 1
121
FROM {context} con JOIN {role_capabilities} rc ON rc.contextid = con.id
122
WHERE capability = ? $sqlroletest", $params);
123
$requiredcontexts = array($systemcontext->id);
124
foreach ($relevantpaths as $path => $notused) {
125
$requiredcontexts = array_merge($requiredcontexts, explode('/', trim($path, '/')));
127
$requiredcontexts = array_unique($requiredcontexts);
129
// Now load those contexts.
130
list($sqlcontexttest, $contextparams) = $DB->get_in_or_equal($requiredcontexts);
131
$contexts = get_sorted_contexts('ctx.id ' . $sqlcontexttest, $contextparams);
133
// Prepare some empty arrays to hold the data we are about to compute.
134
foreach ($contexts as $conid => $con) {
135
$contexts[$conid]->children = array();
136
$contexts[$conid]->rolecapabilities = array();
139
// Put the contexts into a tree structure.
140
foreach ($contexts as $conid => $con) {
141
$context = context::instance_by_id($conid);
143
$parentcontext = $context->get_parent_context();
144
if ($parentcontext) { // Will be false if $context is the system context.
145
$contexts[$parentcontext->id]->children[] = $conid;
147
} catch (dml_missing_record_exception $e) {
148
// Ignore corrupt context tree structure here. Don't let it break
149
// showing the rest of the report.
154
// Put the role capabilities into the context tree.
155
foreach ($rolecaps as $rolecap) {
156
$contexts[$rolecap->contextid]->rolecapabilities[$rolecap->roleid] = $rolecap->permission;
159
// Fill in any missing rolecaps for the system context.
160
foreach ($cleanedroleids as $roleid) {
161
if (!isset($contexts[$systemcontext->id]->rolecapabilities[$roleid])) {
162
$contexts[$systemcontext->id]->rolecapabilities[$roleid] = CAP_INHERIT;
166
// Print the report heading.
167
echo $OUTPUT->heading(get_string('reportforcapability', 'tool_capability', get_capability_string($capability)), 2, 'main', 'report');
168
if (count($cleanedroleids) != count($allroles)) {
169
$rolenames = array();
170
foreach ($cleanedroleids as $roleid) {
171
$rolenames[] = $allroles[$roleid]->localname;
173
echo '<p>', get_string('forroles', 'tool_capability', implode(', ', $rolenames)), '</p>';
176
// Now, recursively print the contexts, and the role information.
177
print_report_tree($systemcontext->id, $contexts, $allroles);
104
if (count($capabilities) && count($rolestoshow)) {
105
/* @var tool_capability_renderer $renderer */
106
echo $renderer->capability_comparison_table($capabilities, $context->id, $rolestoshow);