source: moodle/trunk/fuentes/admin/roles/override.php @ 1331

Last change on this file since 1331 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 7.4 KB
Line 
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.
13//
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/>.
16
17/**
18 * Lets you override role definitions in contexts.
19 *
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
23 */
24
25require('../../config.php');
26
27$contextid = required_param('contextid', PARAM_INT);
28$roleid    = required_param('roleid', PARAM_INT);
29
30list($context, $course, $cm) = get_context_info_array($contextid);
31
32$url = new moodle_url('/admin/roles/override.php', array('contextid' => $contextid, 'roleid' => $roleid));
33
34if ($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;
45    }
46}
47
48// Security first.
49require_login($course, false, $cm);
50$safeoverridesonly = false;
51if (!has_capability('moodle/role:override', $context)) {
52    require_capability('moodle/role:safeoverride', $context);
53    $safeoverridesonly = true;
54}
55$PAGE->set_url($url);
56$PAGE->set_pagelayout('admin');
57
58if ($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)));
63
64} else {
65    $PAGE->set_context($context);
66    navigation_node::override_active_url(new moodle_url('/admin/roles/permissions.php', array('contextid'=>$context->id)));
67}
68
69$courseid = $course->id;
70
71$returnurl = new moodle_url('/admin/roles/permissions.php', array('contextid' => $context->id));
72
73// Handle the cancel button.
74if (optional_param('cancel', false, PARAM_BOOL)) {
75    redirect($returnurl);
76}
77
78$role = $DB->get_record('role', array('id'=>$roleid), '*', MUST_EXIST);
79
80// These are needed early.
81$assignableroles  = get_assignable_roles($context, ROLENAME_BOTH);
82list($overridableroles, $overridecounts, $nameswithcounts) = get_overridable_roles($context, ROLENAME_BOTH, true);
83
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);
89
90$currenttab = 'permissions';
91
92$PAGE->set_title($title);
93$PAGE->navbar->add($straction);
94switch ($context->contextlevel) {
95    case CONTEXT_SYSTEM:
96        print_error('cannotoverridebaserole', 'error');
97        break;
98    case CONTEXT_USER:
99        $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
100        $PAGE->set_heading($fullname);
101        $showroles = 1;
102        break;
103    case CONTEXT_COURSECAT:
104        $PAGE->set_heading($SITE->fullname);
105        break;
106    case CONTEXT_COURSE:
107        if ($isfrontpage) {
108            $PAGE->set_heading(get_string('frontpage', 'admin'));
109        } else {
110            $PAGE->set_heading($course->fullname);
111        }
112        break;
113    case CONTEXT_MODULE:
114        $PAGE->set_heading($context->get_context_name(false));
115        $PAGE->set_cacheable(false);
116        break;
117    case CONTEXT_BLOCK:
118        $PAGE->set_heading($PAGE->course->fullname);
119        break;
120}
121
122// Make sure this user can override that role.
123if (empty($overridableroles[$roleid])) {
124    $a = new stdClass;
125    $a->roleid = $roleid;
126    $a->context = $contextname;
127    print_error('cannotoverriderolehere', '', $context->get_url(), $a);
128}
129
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();
133
134if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
135    $overridestable->save_changes();
136    $rolename = $overridableroles[$roleid];
137    // Trigger event.
138    $event = \core\event\role_capabilities_updated::create(
139        array(
140            'context' => $context,
141            'objectid' => $roleid,
142        )
143    );
144
145    $event->set_legacy_logdata(
146        array(
147            $course->id, 'role', 'override', 'admin/roles/override.php?contextid=' . $context->id . '&roleid=' . $roleid,
148            $rolename, '', $USER->id
149        )
150    );
151    $event->add_record_snapshot('role', $role);
152    $event->trigger();
153
154    redirect($returnurl);
155}
156
157// Finally start page output.
158echo $OUTPUT->header();
159echo $OUTPUT->heading_with_help($title, 'overridepermissions', 'core_role');
160
161// Show UI for overriding roles.
162if (!empty($capabilities)) {
163    echo $OUTPUT->box(get_string('nocapabilitiesincontext', 'core_role'), 'generalbox boxaligncenter');
164
165} else {
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'));
173
174    $overridestable->display();
175    if ($overridestable->has_locked_capabilities()) {
176        echo '<p class="overridenotice">' . get_string('safeoverridenotice', 'core_role') . "</p>\n";
177    }
178
179    echo html_writer::start_tag('div', array('class'=>'submit_buttons'));
180    echo html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'savechanges', 'value'=>get_string('savechanges')));
181    echo html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'cancel', 'value'=>get_string('cancel')));
182    echo html_writer::end_tag('div');
183    echo html_writer::end_tag('div');
184    echo html_writer::end_tag('form');
185    echo $OUTPUT->box_end();
186}
187
188// Print a form to swap roles, and a link back to the all roles list.
189echo html_writer::start_tag('div', array('class'=>'backlink'));
190$select = new single_select($PAGE->url, 'roleid', $nameswithcounts, $roleid, null);
191$select->label = get_string('overrideanotherrole', 'core_role');
192echo $OUTPUT->render($select);
193echo html_writer::tag('p', html_writer::tag('a', get_string('backtoallroles', 'core_role'), array('href'=>$returnurl)));
194echo html_writer::end_tag('div');
195
196echo $OUTPUT->footer();
Note: See TracBrowser for help on using the repository browser.