3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Lets you override role definitions in contexts.
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 $PAGE->set_url('/admin/roles/override.php', array('contextid' => $contextid, 'roleid' => $roleid));
36 $PAGE->set_context($context);
42 $isfrontpage = ($context->contextlevel == CONTEXT_COURSE and $context->instanceid == SITEID);
46 if ($context->contextlevel == CONTEXT_USER) {
47 $courseid = optional_param('courseid', SITEID, PARAM_INT); // needed for user/tabs.php
48 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
49 $PAGE->url->param('courseid', $courseid);
50 $userid = $context->instanceid;
57 require_login($course, false, $cm);
58 $safeoverridesonly = !has_capability('moodle/role:override', $context);
59 if ($safeoverridesonly) {
60 require_capability('moodle/role:safeoverride', $context);
63 $courseid = $course->id;
65 $baseurl = $PAGE->url->out();
66 $returnurl = new moodle_url('/admin/roles/permissions.php', array('contextid' => $context->id));
68 $role = $DB->get_record('role', array('id'=>$roleid), '*', MUST_EXIST);
70 // These are needed early because of tabs.php
71 $assignableroles = get_assignable_roles($context, ROLENAME_BOTH);
72 list($overridableroles, $overridecounts, $nameswithcounts) = get_overridable_roles($context, ROLENAME_BOTH, true);
74 // Work out an appropriate page title.
75 $contextname = print_context_name($context);
76 $straction = get_string('overrideroles', 'role'); // Used by tabs.php
77 $a = (object)array('context' => $contextname, 'role' => $overridableroles[$roleid]);
78 $title = get_string('overridepermissionsforrole', 'role', $a);
80 // Print the header and tabs
81 if ($context->contextlevel == CONTEXT_SYSTEM) {
82 print_error('cannotoverridebaserole', 'error');
84 } else if ($context->contextlevel == CONTEXT_USER) {
85 // NOTE: this is not linked from UI for now
86 $userid = $context->instanceid;
87 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
88 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
92 $PAGE->set_heading($course->fullname);
94 if (has_capability('moodle/course:viewparticipants', get_context_instance(CONTEXT_COURSE, $courseid))) {
95 $PAGE->navbar->add(get_string('participants'), new moodle_url('/user/index.php', array('id'=>$courseid)));
97 $PAGE->set_heading($fullname);
99 $PAGE->navbar->add($fullname, new moodle_url("$CFG->wwwroot/user/view.php", array('id'=>$userid,'course'=>$courseid)));
100 $PAGE->navbar->add($straction);
103 $currenttab = 'permissions';
104 $tabfile = $CFG->dirroot.'/user/tabs.php';
106 } else if ($isfrontpage) {
107 admin_externalpage_setup('frontpageroles', '', array(), $PAGE->url);
108 $currenttab = 'permissions';
109 $tabfile = 'tabs.php';
112 $currenttab = 'permissions';
113 $tabfile = 'tabs.php';
117 // Handle the cancel button.
118 if (optional_param('cancel', false, PARAM_BOOL)) {
119 redirect($returnurl);
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', '', get_context_url($context), $a);
130 // If we are actually overriding a role, create the table object, and save changes if appropriate.
131 $overridestable = new 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];
137 add_to_log($course->id, 'role', 'override', 'admin/roles/override.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id);
138 redirect($returnurl);
141 // Finally start page output
142 echo $OUTPUT->header();
146 echo $OUTPUT->heading_with_help($title, 'overrides');
148 // Show UI for overriding roles.
149 if (!empty($capabilities)) {
150 echo $OUTPUT->box(get_string('nocapabilitiesincontext', 'role'), 'generalbox boxaligncenter');
153 // Print the capabilities overrideable in this context
154 echo $OUTPUT->box_start('generalbox capbox');
157 <form id="overrideform" action="<?php echo $baseurl ?>" method="post"><div>
158 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
159 <input type="hidden" name="roleid" value="<?php p($roleid); ?>" />
162 echo '<p class="overridenotice">' . get_string('highlightedcellsshowinherit', 'role') . ' </p>';
163 $overridestable->display();
165 if ($overridestable->has_locked_capabiltites()) {
166 echo '<p class="overridenotice">' . get_string('safeoverridenotice', 'role') . "</p>\n";
170 <div class="submit buttons">
171 <input type="submit" name="savechanges" value="<?php print_string('savechanges') ?>" />
172 <input type="submit" name="cancel" value="<?php print_string('cancel') ?>" />
176 echo $OUTPUT->box_end();
180 // Print a form to swap roles, and a link back to the all roles list.
181 echo '<div class="backlink">';
182 $select = new single_select(new moodle_url($baseurl), 'roleid', $nameswithcounts, $roleid, null);
183 $select->label = get_string('overrideanotherrole', 'role');
184 echo $OUTPUT->render($select);
185 echo '<p><a href="' . $returnurl . '">' . get_string('backtoallroles', 'role') . '</a></p>';
188 echo $OUTPUT->footer();