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/>.
21 * @copyright 2009 Petr Skoda {@link http://skodak.org}
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
);
29 $roleid = optional_param('roleid', 0, PARAM_INT
);
30 $capability = optional_param('capability', false, PARAM_CAPABILITY
);
31 $confirm = optional_param('confirm', 0, PARAM_BOOL
);
32 $prevent = optional_param('prevent', 0, PARAM_BOOL
);
33 $allow = optional_param('allow', 0, PARAM_BOOL
);
34 $unprohibit = optional_param('unprohibit', 0, PARAM_BOOL
);
35 $prohibit = optional_param('prohibit', 0, PARAM_BOOL
);
36 $returnurl = optional_param('returnurl', null, PARAM_LOCALURL
);
38 list($context, $course, $cm) = get_context_info_array($contextid);
40 $url = new moodle_url('/admin/roles/permissions.php', array('contextid' => $contextid));
43 $isfrontpage = ($course->id
== SITEID
);
46 if ($context->contextlevel
== CONTEXT_USER
) {
47 $course = $DB->get_record('course', array('id'=>optional_param('courseid', SITEID
, PARAM_INT
)), '*', MUST_EXIST
);
48 $user = $DB->get_record('user', array('id'=>$context->instanceid
), '*', MUST_EXIST
);
49 $url->param('courseid', $course->id
);
50 $url->param('userid', $user->id
);
57 require_login($course, false, $cm);
58 require_capability('moodle/role:review', $context);
60 navigation_node
::override_active_url($url);
61 $pageurl = new moodle_url($url);
63 $pageurl->param('returnurl', $returnurl);
65 $PAGE->set_url($pageurl);
67 if ($context->contextlevel
== CONTEXT_USER
and $USER->id
!= $context->instanceid
) {
68 $PAGE->navbar
->includesettingsbase
= true;
69 $PAGE->navigation
->extend_for_user($user);
70 $PAGE->set_context(context_user
::instance($user->id
));
72 $PAGE->set_context($context);
75 $courseid = $course->id
;
78 // These are needed early because of tabs.php.
79 $assignableroles = get_assignable_roles($context, ROLENAME_BOTH
);
80 list($overridableroles, $overridecounts, $nameswithcounts) = get_overridable_roles($context, ROLENAME_BOTH
, true);
82 $capability = $DB->get_record('capabilities', array('name'=>$capability), '*', MUST_EXIST
);
85 $allowoverrides = has_capability('moodle/role:override', $context);
86 $allowsafeoverrides = has_capability('moodle/role:safeoverride', $context);
88 $contextname = $context->get_context_name();
89 $title = get_string('permissionsincontext', 'core_role', $contextname);
90 $straction = get_string('permissions', 'core_role'); // Used by tabs.php.
91 $currenttab = 'permissions';
93 $PAGE->set_pagelayout('admin');
94 if ($context->contextlevel
== CONTEXT_BLOCK
) {
95 // Do not show blocks when changing block's settings, it is confusing.
96 $PAGE->blocks
->show_only_fake_blocks(true);
99 $PAGE->set_title($title);
100 switch ($context->contextlevel
) {
102 print_error('cannotoverridebaserole', 'error');
105 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
106 $PAGE->set_heading($fullname);
109 case CONTEXT_COURSECAT
:
110 $PAGE->set_heading($SITE->fullname
);
114 $PAGE->set_heading(get_string('frontpage', 'admin'));
116 $PAGE->set_heading($course->fullname
);
120 $PAGE->set_heading($context->get_context_name(false));
121 $PAGE->set_cacheable(false);
124 $PAGE->set_heading($PAGE->course
->fullname
);
128 // Handle confirmations and actions.
129 // We have a capability and overrides are allowed or safe overrides are allowed and this is safe.
130 if ($capability && ($allowoverrides ||
($allowsafeoverrides && is_safe_capability($capability)))) {
131 // If we already know the the role ID, it is overrideable, and we are setting prevent or unprohibit.
132 if (isset($overridableroles[$roleid]) && ($prevent ||
$unprohibit)) {
133 // We are preventing.
135 if ($confirm && data_submitted() && confirm_sesskey()) {
136 role_change_permission($roleid, $context, $capability->name
, CAP_PREVENT
);
137 redirect($PAGE->url
);
140 $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'role'=>$overridableroles[$roleid], 'context'=>$contextname);
141 $message = get_string('confirmroleprevent', 'core_role', $a);
142 $continueurl = new moodle_url($PAGE->url
,
143 array('contextid'=>$context->id
, 'roleid'=>$roleid, 'capability'=>$capability->name
, 'prevent'=>1, 'sesskey'=>sesskey(), 'confirm'=>1));
146 // We are unprohibiting.
148 if ($confirm && data_submitted() && confirm_sesskey()) {
149 role_change_permission($roleid, $context, $capability->name
, CAP_INHERIT
);
150 redirect($PAGE->url
);
152 $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'role'=>$overridableroles[$roleid], 'context'=>$contextname);
153 $message = get_string('confirmroleunprohibit', 'core_role', $a);
154 $continueurl = new moodle_url($PAGE->url
,
155 array('contextid'=>$context->id
, 'roleid'=>$roleid, 'capability'=>$capability->name
, 'unprohibit'=>1, 'sesskey'=>sesskey(), 'confirm'=>1));
158 // Display and print.
159 echo $OUTPUT->header();
160 echo $OUTPUT->heading($title);
161 echo $OUTPUT->confirm($message, $continueurl, $PAGE->url
);
162 echo $OUTPUT->footer();
166 if ($allow ||
$prohibit) {
168 $mform = new core_role_permission_allow_form(null, array($context, $capability, $overridableroles));
169 if ($mform->is_cancelled()) {
170 redirect($PAGE->url
);
171 } else if ($data = $mform->get_data() and !empty($data->roleid
)) {
172 $roleid = $data->roleid
;
173 if (isset($overridableroles[$roleid])) {
174 role_change_permission($roleid, $context, $capability->name
, CAP_ALLOW
);
176 redirect($PAGE->url
);
178 $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'context'=>$contextname);
179 $message = get_string('roleallowinfo', 'core_role', $a);
183 $mform = new core_role_permission_prohibit_form(null, array($context, $capability, $overridableroles));
184 if ($mform->is_cancelled()) {
185 redirect($PAGE->url
);
186 } else if ($data = $mform->get_data() and !empty($data->roleid
)) {
187 $roleid = $data->roleid
;
188 if (isset($overridableroles[$roleid])) {
189 role_change_permission($roleid, $context, $capability->name
, CAP_PROHIBIT
);
191 redirect($PAGE->url
);
193 $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'context'=>$contextname);
194 $message = get_string('roleprohibitinfo', 'core_role', $a);
197 echo $OUTPUT->header();
198 echo $OUTPUT->heading($title);
199 echo $OUTPUT->box($message);
201 echo $OUTPUT->footer();
206 echo $OUTPUT->header();
207 echo $OUTPUT->heading($title);
209 $adminurl = new moodle_url('/admin/');
210 $arguments = array('contextid' => $contextid,
211 'contextname' => $contextname,
212 'adminurl' => $adminurl->out());
213 $PAGE->requires
->strings_for_js(
214 array('roleprohibitinfo', 'roleprohibitheader', 'roleallowinfo', 'roleallowheader',
215 'confirmunassigntitle', 'confirmroleunprohibit', 'confirmroleprevent', 'confirmunassignyes',
216 'confirmunassignno', 'deletexrole'), 'core_role');
217 $PAGE->requires
->js_call_amd('core/permissionmanager', 'initialize', array($arguments));
218 $table = new core_role_permissions_table($context, $contextname, $allowoverrides, $allowsafeoverrides, $overridableroles);
219 echo $OUTPUT->box_start('generalbox capbox');
220 // Print link to advanced override page.
221 if ($overridableroles) {
222 $overrideurl = new moodle_url('/admin/roles/override.php', array('contextid' => $context->id
));
223 $select = new single_select($overrideurl, 'roleid', $nameswithcounts);
224 $select->label
= get_string('advancedoverride', 'core_role');
225 echo html_writer
::tag('div', $OUTPUT->render($select), array('class'=>'advancedoverride'));
228 echo $OUTPUT->box_end();
231 if ($context->contextlevel
> CONTEXT_USER
) {
234 $url = new moodle_url($returnurl);
236 $url = $context->get_url();
239 echo html_writer
::start_tag('div', array('class'=>'backlink'));
240 echo html_writer
::tag('a', get_string('backto', '', $contextname), array('href' => $url));
241 echo html_writer
::end_tag('div');
244 echo $OUTPUT->footer($course);