~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to admin/tool/capability/index.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
17
/**
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.
20
19
 *
21
 
 * @package    tool
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
25
23
 */
26
24
 
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
28
 
 
29
// Get URL parameters.
 
30
$systemcontext = context_system::instance();
 
31
$contextid = optional_param('context', $systemcontext->id, PARAM_INT);
 
32
 
30
33
// Check permissions.
31
 
require_login();
32
 
$systemcontext = context_system::instance();
33
 
require_capability('moodle/role:manage', $systemcontext);
34
 
 
35
 
// Get URL parameters.
36
 
$capability = optional_param('capability', '', PARAM_CAPABILITY);
37
 
$roleids = optional_param_array('roles', array('0'), PARAM_INT);
38
 
 
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) {
44
 
    if ($roleid == 0) {
45
 
        $cleanedroleids = array_keys($allroles);
46
 
        break;
47
 
    }
48
 
    if (array_key_exists($roleid, $allroles)) {
49
 
        $cleanedroleids[] = $roleid;
50
 
    }
51
 
}
52
 
if (empty($cleanedroleids)) {
53
 
    $cleanedroleids = array_keys($allroles);
54
 
}
55
 
 
56
 
// Include the required JavaScript.
57
 
$PAGE->requires->js_init_call('M.tool_capability.init', array(get_string('search')));
58
 
 
59
 
// Log.
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);
61
37
 
62
38
// Print the header.
63
39
admin_externalpage_setup('toolcapability');
64
 
echo $OUTPUT->header();
65
40
 
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);
71
45
}
72
46
 
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;
77
52
}
78
 
if (count($cleanedroleids) == count($allroles)) {
79
 
    // Select 'All', rather than each role individually.
80
 
    $selectedroleids = array('0');
81
 
} else {
82
 
    $selectedroleids = $cleanedroleids;
 
53
 
 
54
$form = new tool_capability_settings_form(null, array(
 
55
    'capabilities' => $capabilitychoices,
 
56
    'roles' => $rolechoices
 
57
));
 
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
);
 
63
 
 
64
// Log.
 
65
$capabilities = array();
 
66
$rolestoshow = array();
 
67
$roleids = array('0');
 
68
$cleanedroleids = array();
 
69
if ($data = $form->get_data()) {
 
70
 
 
71
    $roleids = array();
 
72
    if (!empty($data->roles)) {
 
73
        $roleids = $data->roles;
 
74
    }
 
75
 
 
76
    $capabilities = array();
 
77
    if (!empty($data->capability)) {
 
78
        $capabilities = $data->capability;
 
79
    }
 
80
 
 
81
    if (in_array('0', $roleids)) {
 
82
        $rolestoshow = $allroles;
 
83
    } else {
 
84
        $cleanedroleids = array_intersect(array_keys($allroles), $roleids);
 
85
        if (count($cleanedroleids) === 0) {
 
86
            $rolestoshow = $allroles;
 
87
        } else {
 
88
            foreach ($cleanedroleids as $id) {
 
89
                $rolestoshow[$id] = $allroles[$id];
 
90
            }
 
91
        }
 
92
    }
83
93
}
84
94
 
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>';
95
 
echo '</div></form>';
96
 
echo $OUTPUT->box_end();
 
95
add_to_log(SITEID, "admin", "tool capability", "tool/capability/index.php", count($capabilities));
 
96
 
 
97
$renderer = $PAGE->get_renderer('tool_capability');
 
98
 
 
99
echo $OUTPUT->header();
 
100
 
 
101
$form->display();
97
102
 
98
103
// If we have a capability, generate the report.
99
 
if ($capability) {
100
 
 
101
 
    // Work out the bits needed for the SQL WHERE clauses.
102
 
    $params = array($capability);
103
 
    $sqlroletest = '';
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;
108
 
    }
109
 
 
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);
116
 
 
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, '/')));
126
 
    }
127
 
    $requiredcontexts = array_unique($requiredcontexts);
128
 
 
129
 
    // Now load those contexts.
130
 
    list($sqlcontexttest, $contextparams) = $DB->get_in_or_equal($requiredcontexts);
131
 
    $contexts = get_sorted_contexts('ctx.id ' . $sqlcontexttest, $contextparams);
132
 
 
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();
137
 
    }
138
 
 
139
 
    // Put the contexts into a tree structure.
140
 
    foreach ($contexts as $conid => $con) {
141
 
        $context = context::instance_by_id($conid);
142
 
        try {
143
 
            $parentcontext = $context->get_parent_context();
144
 
            if ($parentcontext) { // Will be false if $context is the system context.
145
 
                $contexts[$parentcontext->id]->children[] = $conid;
146
 
            }
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.
150
 
            continue;
151
 
        }
152
 
    }
153
 
 
154
 
    // Put the role capabilities into the context tree.
155
 
    foreach ($rolecaps as $rolecap) {
156
 
        $contexts[$rolecap->contextid]->rolecapabilities[$rolecap->roleid] = $rolecap->permission;
157
 
    }
158
 
 
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;
163
 
        }
164
 
    }
165
 
 
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;
172
 
        }
173
 
        echo '<p>', get_string('forroles', 'tool_capability', implode(', ', $rolenames)), '</p>';
174
 
    }
175
 
 
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);
178
107
}
179
108
 
180
109
// Footer.
196
125
 
197
126
    // Start the list item, and print the context name as a link to the place to
198
127
    // make changes.
199
 
    if ($contextid == get_system_context()->id) {
 
128
    if ($contextid == context_system::instance()->id) {
200
129
        $url = "$CFG->wwwroot/$CFG->admin/roles/manage.php";
201
130
        $title = get_string('changeroles', 'tool_capability');
202
131
    } else {