MDL-65978 blog: Fix removing associations
[moodle.git] / admin / roles / override.php
blobf9d97a3fcbf224fbbf7f37c3794b38890c6d25c9
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Lets you override role definitions in contexts.
20 * @package core_role
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));
34 if ($course) {
35 $isfrontpage = ($course->id == SITEID);
36 } else {
37 $isfrontpage = false;
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);
43 } else {
44 $course = $SITE;
48 // Security first.
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;
55 $PAGE->set_url($url);
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)));
64 } else {
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)) {
75 redirect($returnurl);
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->activityheader->disable();
94 $PAGE->navbar->add($straction);
95 switch ($context->contextlevel) {
96 case CONTEXT_SYSTEM:
97 throw new \moodle_exception('cannotoverridebaserole', 'error');
98 break;
99 case CONTEXT_USER:
100 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
101 $PAGE->set_heading($fullname);
102 $showroles = 1;
103 break;
104 case CONTEXT_COURSECAT:
105 $PAGE->set_heading($SITE->fullname);
106 break;
107 case CONTEXT_COURSE:
108 if ($isfrontpage) {
109 $PAGE->set_heading(get_string('frontpage', 'admin'));
110 } else {
111 $PAGE->set_heading($course->fullname);
113 break;
114 case CONTEXT_MODULE:
115 $PAGE->set_heading($context->get_context_name(false));
116 $PAGE->set_cacheable(false);
117 break;
118 case CONTEXT_BLOCK:
119 $PAGE->set_heading($PAGE->course->fullname);
120 break;
123 // Make sure this user can override that role.
124 if (empty($overridableroles[$roleid])) {
125 $a = new stdClass;
126 $a->roleid = $roleid;
127 $a->context = $contextname;
128 throw new \moodle_exception('cannotoverriderolehere', '', $context->get_url(), $a);
131 // If we are actually overriding a role, create the table object, and save changes if appropriate.
132 $overridestable = new core_role_override_permissions_table_advanced($context, $roleid, $safeoverridesonly);
133 $overridestable->read_submitted_permissions();
135 if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
136 $overridestable->save_changes();
137 $rolename = $overridableroles[$roleid];
139 redirect($returnurl);
142 // Finally start page output.
143 echo $OUTPUT->header();
144 echo $OUTPUT->heading_with_help($title, 'overridepermissions', 'core_role');
146 // Show UI for overriding roles.
147 if (!empty($capabilities)) {
148 echo $OUTPUT->box(get_string('nocapabilitiesincontext', 'core_role'), 'generalbox boxaligncenter');
150 } else {
151 // Print the capabilities overrideable in this context.
152 echo $OUTPUT->box_start('generalbox capbox');
153 echo html_writer::start_tag('form', array('id'=>'overrideform', 'action'=>$PAGE->url->out(), 'method'=>'post'));
154 echo html_writer::start_tag('div');
155 echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
156 echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'roleid', 'value'=>$roleid));
157 echo html_writer::tag('p', get_string('highlightedcellsshowinherit', 'core_role'), array('class'=>'overridenotice'));
159 $overridestable->display();
160 if ($overridestable->has_locked_capabilities()) {
161 echo '<p class="overridenotice">' . get_string('safeoverridenotice', 'core_role') . "</p>\n";
164 echo html_writer::start_tag('div', array('class'=>'submit_buttons'));
165 $attrs = array('type'=>'submit', 'name'=>'savechanges', 'value'=>get_string('savechanges'), 'class'=>'btn btn-primary');
166 echo html_writer::empty_tag('input', $attrs);
167 $attrs = array('type' => 'submit', 'name' => 'cancel', 'value' => get_string('cancel'),
168 'class' => 'btn btn-secondary ml-1');
169 echo html_writer::empty_tag('input', $attrs);
170 echo html_writer::end_tag('div');
171 echo html_writer::end_tag('div');
172 echo html_writer::end_tag('form');
173 echo $OUTPUT->box_end();
176 // Print a form to swap roles, and a link back to the all roles list.
177 echo html_writer::start_tag('div', array('class'=>'backlink'));
178 $select = new single_select($PAGE->url, 'roleid', $nameswithcounts, $roleid, null);
179 $select->label = get_string('overrideanotherrole', 'core_role');
180 echo $OUTPUT->render($select);
181 echo html_writer::tag('p', html_writer::tag('a', get_string('backtoallroles', 'core_role'), array('href'=>$returnurl)));
182 echo html_writer::end_tag('div');
184 echo $OUTPUT->footer();