2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Lets you override role definitions in contexts.
21 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require('../../config.php');
27 $contextid = required_param('contextid', PARAM_INT
);
28 $roleid = required_param('roleid', PARAM_INT
);
30 list($context, $course, $cm) = get_context_info_array($contextid);
32 $url = new moodle_url('/admin/roles/override.php', array('contextid' => $contextid, 'roleid' => $roleid));
35 $isfrontpage = ($course->id
== SITEID
);
38 if ($context->contextlevel
== CONTEXT_USER
) {
39 $course = $DB->get_record('course', array('id'=>optional_param('courseid', SITEID
, PARAM_INT
)), '*', MUST_EXIST
);
40 $user = $DB->get_record('user', array('id'=>$context->instanceid
), '*', MUST_EXIST
);
41 $url->param('courseid', $course->id
);
42 $url->param('userid', $user->id
);
49 require_login($course, false, $cm);
50 $safeoverridesonly = false;
51 if (!has_capability('moodle/role:override', $context)) {
52 require_capability('moodle/role:safeoverride', $context);
53 $safeoverridesonly = true;
56 $PAGE->set_pagelayout('admin');
58 if ($context->contextlevel
== CONTEXT_USER
and $USER->id
!= $context->instanceid
) {
59 $PAGE->navigation
->extend_for_user($user);
60 $PAGE->set_context(context_course
::instance($course->id
));
61 navigation_node
::override_active_url(new moodle_url('/admin/roles/permissions.php',
62 array('contextid'=>$context->id
, 'userid'=>$context->instanceid
, 'courseid'=>$course->id
)));
65 $PAGE->set_context($context);
66 navigation_node
::override_active_url(new moodle_url('/admin/roles/permissions.php', array('contextid'=>$context->id
)));
69 $courseid = $course->id
;
71 $returnurl = new moodle_url('/admin/roles/permissions.php', array('contextid' => $context->id
));
73 // Handle the cancel button.
74 if (optional_param('cancel', false, PARAM_BOOL
)) {
78 $role = $DB->get_record('role', array('id'=>$roleid), '*', MUST_EXIST
);
80 // These are needed early.
81 $assignableroles = get_assignable_roles($context, ROLENAME_BOTH
);
82 list($overridableroles, $overridecounts, $nameswithcounts) = get_overridable_roles($context, ROLENAME_BOTH
, true);
84 // Work out an appropriate page title.
85 $contextname = $context->get_context_name();
86 $straction = get_string('overrideroles', 'core_role'); // Used by tabs.php.
87 $a = (object)array('context' => $contextname, 'role' => $overridableroles[$roleid]);
88 $title = get_string('overridepermissionsforrole', 'core_role', $a);
90 $currenttab = 'permissions';
92 $PAGE->set_title($title);
93 $PAGE->navbar
->add($straction);
94 switch ($context->contextlevel
) {
96 print_error('cannotoverridebaserole', 'error');
99 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
100 $PAGE->set_heading($fullname);
103 case CONTEXT_COURSECAT
:
104 $PAGE->set_heading($SITE->fullname
);
108 $PAGE->set_heading(get_string('frontpage', 'admin'));
110 $PAGE->set_heading($course->fullname
);
114 $PAGE->set_heading($context->get_context_name(false));
115 $PAGE->set_cacheable(false);
118 $PAGE->set_heading($PAGE->course
->fullname
);
122 // Make sure this user can override that role.
123 if (empty($overridableroles[$roleid])) {
125 $a->roleid
= $roleid;
126 $a->context
= $contextname;
127 print_error('cannotoverriderolehere', '', $context->get_url(), $a);
130 // If we are actually overriding a role, create the table object, and save changes if appropriate.
131 $overridestable = new core_role_override_permissions_table_advanced($context, $roleid, $safeoverridesonly);
132 $overridestable->read_submitted_permissions();
134 if (optional_param('savechanges', false, PARAM_BOOL
) && confirm_sesskey()) {
135 $overridestable->save_changes();
136 $rolename = $overridableroles[$roleid];
138 $event = \core\event\role_capabilities_updated
::create(
140 'context' => $context,
141 'objectid' => $roleid,
145 $event->set_legacy_logdata(
147 $course->id
, 'role', 'override', 'admin/roles/override.php?contextid=' . $context->id
. '&roleid=' . $roleid,
148 $rolename, '', $USER->id
151 $event->add_record_snapshot('role', $role);
154 redirect($returnurl);
157 // Finally start page output.
158 echo $OUTPUT->header();
159 echo $OUTPUT->heading_with_help($title, 'overridepermissions', 'core_role');
161 // Show UI for overriding roles.
162 if (!empty($capabilities)) {
163 echo $OUTPUT->box(get_string('nocapabilitiesincontext', 'core_role'), 'generalbox boxaligncenter');
166 // Print the capabilities overrideable in this context.
167 echo $OUTPUT->box_start('generalbox capbox');
168 echo html_writer
::start_tag('form', array('id'=>'overrideform', 'action'=>$PAGE->url
->out(), 'method'=>'post'));
169 echo html_writer
::start_tag('div');
170 echo html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
171 echo html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'roleid', 'value'=>$roleid));
172 echo html_writer
::tag('p', get_string('highlightedcellsshowinherit', 'core_role'), array('class'=>'overridenotice'));
174 $overridestable->display();
175 if ($overridestable->has_locked_capabilities()) {
176 echo '<p class="overridenotice">' . get_string('safeoverridenotice', 'core_role') . "</p>\n";
179 echo html_writer
::start_tag('div', array('class'=>'submit_buttons'));
180 $attrs = array('type'=>'submit', 'name'=>'savechanges', 'value'=>get_string('savechanges'), 'class'=>'btn btn-primary');
181 echo html_writer
::empty_tag('input', $attrs);
182 $attrs = array('type'=>'submit', 'name'=>'cancel', 'value'=>get_string('cancel'), 'class' => 'btn btn-secondary');
183 echo html_writer
::empty_tag('input', $attrs);
184 echo html_writer
::end_tag('div');
185 echo html_writer
::end_tag('div');
186 echo html_writer
::end_tag('form');
187 echo $OUTPUT->box_end();
190 // Print a form to swap roles, and a link back to the all roles list.
191 echo html_writer
::start_tag('div', array('class'=>'backlink'));
192 $select = new single_select($PAGE->url
, 'roleid', $nameswithcounts, $roleid, null);
193 $select->label
= get_string('overrideanotherrole', 'core_role');
194 echo $OUTPUT->render($select);
195 echo html_writer
::tag('p', html_writer
::tag('a', get_string('backtoallroles', 'core_role'), array('href'=>$returnurl)));
196 echo html_writer
::end_tag('div');
198 echo $OUTPUT->footer();