MDL-36983 fix incorrect test method name
[moodle.git] / admin / roles / override.php
blobff6848d803bbce7e39296f29cc185da8473c7bff
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Lets you override role definitions in contexts.
21 * @package core
22 * @subpackage role
23 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require('../../config.php');
28 require_once("$CFG->dirroot/$CFG->admin/roles/lib.php");
30 $contextid = required_param('contextid', PARAM_INT); // context id
31 $roleid = required_param('roleid', PARAM_INT); // requested role id
33 list($context, $course, $cm) = get_context_info_array($contextid);
35 $url = new moodle_url('/admin/roles/override.php', array('contextid' => $contextid, 'roleid' => $roleid));
37 if ($course) {
38 $isfrontpage = ($course->id == SITEID);
39 } else {
40 $isfrontpage = false;
41 if ($context->contextlevel == CONTEXT_USER) {
42 $course = $DB->get_record('course', array('id'=>optional_param('courseid', SITEID, PARAM_INT)), '*', MUST_EXIST);
43 $user = $DB->get_record('user', array('id'=>$context->instanceid), '*', MUST_EXIST);
44 $url->param('courseid', $course->id);
45 $url->param('userid', $user->id);
46 } else {
47 $course = $SITE;
51 // security first
52 require_login($course, false, $cm);
53 $safeoverridesonly = false;
54 if (!has_capability('moodle/role:override', $context)) {
55 require_capability('moodle/role:safeoverride', $context);
56 $safeoverridesonly = true;
58 $PAGE->set_url($url);
59 $PAGE->set_context($context);
60 $PAGE->set_pagelayout('admin');
62 $courseid = $course->id;
64 $returnurl = new moodle_url('/admin/roles/permissions.php', array('contextid' => $context->id));
66 // Handle the cancel button.
67 if (optional_param('cancel', false, PARAM_BOOL)) {
68 redirect($returnurl);
71 $role = $DB->get_record('role', array('id'=>$roleid), '*', MUST_EXIST);
73 // These are needed early
74 $assignableroles = get_assignable_roles($context, ROLENAME_BOTH);
75 list($overridableroles, $overridecounts, $nameswithcounts) = get_overridable_roles($context, ROLENAME_BOTH, true);
77 // Work out an appropriate page title.
78 $contextname = print_context_name($context);
79 $straction = get_string('overrideroles', 'role'); // Used by tabs.php
80 $a = (object)array('context' => $contextname, 'role' => $overridableroles[$roleid]);
81 $title = get_string('overridepermissionsforrole', 'role', $a);
83 $currenttab = 'permissions';
85 $PAGE->set_title($title);
86 $PAGE->navbar->add($straction);
87 switch ($context->contextlevel) {
88 case CONTEXT_SYSTEM:
89 print_error('cannotoverridebaserole', 'error');
90 break;
91 case CONTEXT_USER:
92 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
93 $PAGE->set_heading($fullname);
94 $showroles = 1;
95 break;
96 case CONTEXT_COURSECAT:
97 $PAGE->set_heading("$SITE->fullname: ".get_string("categories"));
98 break;
99 case CONTEXT_COURSE:
100 if ($isfrontpage) {
101 admin_externalpage_setup('frontpageroles', '', array(), $PAGE->url);
102 } else {
103 $PAGE->set_heading($course->fullname);
105 break;
106 case CONTEXT_MODULE:
107 $PAGE->set_heading(print_context_name($context, false));
108 $PAGE->set_cacheable(false);
109 break;
110 case CONTEXT_BLOCK:
111 $PAGE->set_heading($PAGE->course->fullname);
112 break;
115 // Make sure this user can override that role
116 if (empty($overridableroles[$roleid])) {
117 $a = new stdClass;
118 $a->roleid = $roleid;
119 $a->context = $contextname;
120 print_error('cannotoverriderolehere', '', get_context_url($context), $a);
123 // If we are actually overriding a role, create the table object, and save changes if appropriate.
124 $overridestable = new override_permissions_table_advanced($context, $roleid, $safeoverridesonly);
125 $overridestable->read_submitted_permissions();
127 if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
128 $overridestable->save_changes();
129 $rolename = $overridableroles[$roleid];
130 add_to_log($course->id, 'role', 'override', 'admin/roles/override.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id);
131 redirect($returnurl);
134 // Finally start page output
135 echo $OUTPUT->header();
136 echo $OUTPUT->heading_with_help($title, 'overridepermissions', 'role');
138 // Show UI for overriding roles.
139 if (!empty($capabilities)) {
140 echo $OUTPUT->box(get_string('nocapabilitiesincontext', 'role'), 'generalbox boxaligncenter');
142 } else {
143 // Print the capabilities overrideable in this context
144 echo $OUTPUT->box_start('generalbox capbox');
145 echo html_writer::start_tag('form', array('id'=>'overrideform', 'action'=>$PAGE->url->out(), 'method'=>'post'));
146 echo html_writer::start_tag('div');
147 echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
148 echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'roleid', 'value'=>$roleid));
149 echo html_writer::tag('p', get_string('highlightedcellsshowinherit', 'role'), array('class'=>'overridenotice'));
151 $overridestable->display();
152 if ($overridestable->has_locked_capabilities()) {
153 echo '<p class="overridenotice">' . get_string('safeoverridenotice', 'role') . "</p>\n";
156 echo html_writer::start_tag('div', array('class'=>'submit_buttons'));
157 echo html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'savechanges', 'value'=>get_string('savechanges')));
158 echo html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'cancel', 'value'=>get_string('cancel')));
159 echo html_writer::end_tag('div');
160 echo html_writer::end_tag('div');
161 echo html_writer::end_tag('form');
162 echo $OUTPUT->box_end();
165 // Print a form to swap roles, and a link back to the all roles list.
166 echo html_writer::start_tag('div', array('class'=>'backlink'));
167 $select = new single_select($PAGE->url, 'roleid', $nameswithcounts, $roleid, null);
168 $select->label = get_string('overrideanotherrole', 'role');
169 echo $OUTPUT->render($select);
170 echo html_writer::tag('p', html_writer::tag('a', get_string('backtoallroles', 'role'), array('href'=>$returnurl)));
171 echo html_writer::end_tag('div');
173 echo $OUTPUT->footer();