MDL-12531, make the new member value available to all the affected plugins, thanks...
[moodle-linuxchix.git] / admin / roles / override.php
blob27a6c8518dfd3f93849d685b5e8bc5e904b9c7bc
1 <?php //$Id$
3 require_once('../../config.php');
5 $contextid = required_param('contextid', PARAM_INT); // context id
6 $roleid = optional_param('roleid', 0, PARAM_INT); // requested role id
7 $userid = optional_param('userid', 0, PARAM_INT); // needed for user tabs
8 $courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tabs
9 $cancel = optional_param('cancel', 0, PARAM_BOOL);
11 if (!$context = get_record('context', 'id', $contextid)) {
12 error('Bad context ID');
15 if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM)) {
16 error('No site ID');
19 if ($context->id == $sitecontext->id) {
20 error('Can not override base role capabilities');
23 if (!has_capability('moodle/role:override', $context)) {
24 error('You do not have permission to change overrides in this context!');
27 if ($courseid) {
28 if (!$course = get_record('course', 'id', $courseid)) {
29 error('Bad course ID');
32 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
34 } else {
35 $course = clone($SITE);
36 $courseid = SITEID;
37 $coursecontext = $context;
40 require_login($course);
42 $baseurl = 'override.php?contextid='.$context->id;
43 if (!empty($userid)) {
44 $baseurl .= '&amp;userid='.$userid;
46 if ($courseid != SITEID) {
47 $baseurl .= '&amp;courseid='.$courseid;
50 if ($cancel) {
51 redirect($baseurl);
54 /// needed for tabs.php
55 $overridableroles = get_overridable_roles($context);
56 $assignableroles = get_assignable_roles($context);
58 /// Get some language strings
60 $strroletooverride = get_string('roletooverride', 'role');
61 $straction = get_string('overrideroles', 'role');
62 $strcurrentrole = get_string('currentrole', 'role');
63 $strparticipants = get_string('participants');
65 /// Make sure this user can override that role
67 if ($roleid) {
68 if (!user_can_override($context, $roleid)) {
69 error ('you can not override this role in this context');
73 if ($userid) {
74 $user = get_record('user', 'id', $userid);
75 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
78 /// get all cababilities
79 $capabilities = fetch_context_capabilities($context);
81 /// Process incoming role override
82 if ($data = data_submitted() and $roleid and confirm_sesskey()) {
83 $allowed_values = array(CAP_INHERIT, CAP_ALLOW, CAP_PREVENT, CAP_PROHIBIT);
85 $localoverrides = get_records_select('role_capabilities', "roleid = $roleid AND contextid = $context->id",
86 '', 'capability, permission, id');
88 foreach ($capabilities as $cap) {
90 if (!isset($data->{$cap->name})) {
91 //cap not specified in form
92 continue;
95 if (islegacy($data->{$cap->name})) {
96 continue;
99 $capname = $cap->name;
100 $value = clean_param($data->{$cap->name}, PARAM_INT);
101 if (!in_array($value, $allowed_values)) {
102 continue;
105 if (isset($localoverrides[$capname])) {
106 // Something exists, so update it
107 assign_capability($capname, $value, $roleid, $context->id, true);
108 } else { // insert a record
109 if ($value != CAP_INHERIT) { // Ignore inherits
110 assign_capability($capname, $value, $roleid, $context->id);
115 // force accessinfo refresh for users visiting this context...
116 mark_context_dirty($context->path);
117 $rolename = get_field('role', 'name', 'id', $roleid);
118 add_to_log($course->id, 'role', 'override', 'admin/roles/override.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id);
119 redirect($baseurl);
123 /// Print the header and tabs
125 if ($context->contextlevel == CONTEXT_USER) {
126 $navlinks = array();
127 /// course header
128 if ($course->id != SITEID) {
129 if (has_capability('moodle/course:viewparticipants', get_context_instance(CONTEXT_COURSE, $course->id))) {
130 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$course->id", 'type' => 'misc');
132 $navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$userid&amp;course=$courseid", 'type' => 'misc');
133 $navlinks[] = array('name' => $straction, 'link' => null, 'type' => 'misc');
134 $navigation = build_navigation($navlinks);
135 print_header("$fullname", "$fullname", $navigation, "", "", true, "&nbsp;", navmenu($course));
137 /// site header
138 } else {
139 $navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$userid&amp;course=$courseid", 'type' => 'misc');
140 $navlinks[] = array('name' => $straction, 'link' => null, 'type' => 'misc');
141 $navigation = build_navigation($navlinks);
142 print_header("$course->fullname: $fullname", $course->fullname, $navigation, "", "", true, "&nbsp;", navmenu($course));
144 $showroles = 1;
145 $currenttab = 'override';
146 include_once($CFG->dirroot.'/user/tabs.php');
147 } else if ($context->contextlevel==CONTEXT_COURSE and $context->instanceid == SITEID) {
148 require_once($CFG->libdir.'/adminlib.php');
149 admin_externalpage_setup('frontpageroles');
150 admin_externalpage_print_header();
151 $currenttab = 'override';
152 include_once('tabs.php');
153 } else {
154 $currenttab = 'override';
155 include_once('tabs.php');
158 /// Rename some of the role names if needed
159 if (isset($coursecontext)) {
160 if ($aliasnames = get_records('role_names', 'contextid', $coursecontext->id)) {
161 foreach ($aliasnames as $alias) {
162 if (isset($overridableroles[$alias->roleid])) {
163 $overridableroles[$alias->roleid] = $alias->name.' ('.$overridableroles[$alias->roleid].')';
170 print_heading_with_help(get_string('overridepermissionsin', 'role', print_context_name($context)), 'overrides');
172 if ($roleid) {
173 /// prints a form to swap roles
174 echo '<div class="selector">';
175 $overridableroles = array('0'=>get_string('listallroles', 'role').'...') + $overridableroles;
176 popup_form("$CFG->wwwroot/$CFG->admin/roles/override.php?userid=$userid&amp;courseid=$courseid&amp;contextid=$contextid&amp;roleid=",
177 $overridableroles, 'switchrole', $roleid, '', '', '', false, 'self', $strroletooverride);
178 echo '</div>';
180 $parentcontexts = get_parent_contexts($context);
181 if (!empty($parentcontexts)) {
182 $parentcontext = array_shift($parentcontexts);
183 $parentcontext = get_context_instance_by_id($parentcontext);
184 } else {
185 $parentcontext = $context; // site level in override??
188 $r_caps = role_context_capabilities($roleid, $parentcontext);
190 $localoverrides = get_records_select('role_capabilities', "roleid = $roleid AND contextid = $context->id",
191 '', 'capability, permission, id');
193 $lang = str_replace('_utf8', '', current_language());
195 if (!empty($capabilities)) {
196 // Print the capabilities overrideable in this context
197 print_simple_box_start('center');
198 include_once('override.html');
199 print_simple_box_end();
201 } else {
202 notice(get_string('nocapabilitiesincontext', 'role'),
203 $CFG->wwwroot.'/'.$CFG->admin.'/roles/'.$baseurl);
206 } else { // Print overview table
208 $table->tablealign = 'center';
209 $table->cellpadding = 5;
210 $table->cellspacing = 0;
211 $table->width = '60%';
212 $table->head = array(get_string('roles', 'role'), get_string('description'), get_string('overrides', 'role'));
213 $table->wrap = array('nowrap', '', 'nowrap');
214 $table->align = array('right', 'left', 'center');
216 foreach ($overridableroles as $roleid => $rolename) {
217 $countusers = 0;
218 $overridecount = count_records_select('role_capabilities', "roleid = $roleid AND contextid = $context->id");
219 $description = format_string(get_field('role', 'description', 'id', $roleid));
220 $table->data[] = array('<a href="'.$baseurl.'&amp;roleid='.$roleid.'">'.$rolename.'</a>', $description, $overridecount);
223 print_table($table);
226 print_footer($course);