MDL-24761 we can safely reset all roles now
[moodle.git] / admin / roles / manage.php
CommitLineData
11b749ca 1<?php
9584965f 2
01a2ce80
PS
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
1e8e4687 17
18/**
19 * Lets the user define and edit roles.
20 *
bbdb7070 21 * Responds to actions:
22 * [blank] - list roles.
23 * delete - delete a role (with are-you-sure)
24 * moveup - change the sort order
25 * movedown - change the sort order
26 * reset - set a role's permissions back to the default for that legacy role type.
27 *
28 * For all but the first two of those, you also need a roleid parameter, and
29 * possibly some other data.
30 *
5d354ded 31 * @package core
01a2ce80
PS
32 * @subpackage role
33 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
1e8e4687 36
37 require_once(dirname(__FILE__) . '/../../config.php');
38 require_once($CFG->dirroot . '/' . $CFG->admin . '/roles/lib.php');
bbbf2d40 39
bbdb7070 40 $action = optional_param('action', '', PARAM_ALPHA);
41 if ($action) {
42 $roleid = required_param('roleid', PARAM_INT);
e9871557 43 }
9584965f 44
bbdb7070 45/// Get the base URL for this and related pages into a convenient variable.
46 $baseurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/manage.php';
47 $defineurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/define.php';
bbbf2d40 48
bbdb7070 49/// Check access permissions.
50 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
51 require_login();
52 require_capability('moodle/role:manage', $systemcontext);
53 admin_externalpage_setup('defineroles');
54
55/// Get some basic data we are going to need.
ece4945b 56 $roles = get_all_roles();
bbdb7070 57 role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL);
b5959f30 58
bbdb7070 59 $undeletableroles = array();
60 $undeletableroles[$CFG->notloggedinroleid] = 1;
61 $undeletableroles[$CFG->guestroleid] = 1;
62 $undeletableroles[$CFG->defaultuserroleid] = 1;
68c52526 63
bbdb7070 64///.Process submitted data.
65 $confirmed = optional_param('confirm', false, PARAM_BOOL) && data_submitted() && confirm_sesskey();
b5959f30 66 switch ($action) {
b5959f30 67 case 'delete':
bbdb7070 68 if (isset($undeletableroles[$roleid])) {
69 print_error('cannotdeletethisrole', '', $baseurl);
f4d6704d 70 }
bbdb7070 71 if (!$confirmed) {
b5959f30 72 // show confirmation
61ef8f9f 73 echo $OUTPUT->header();
38d2d43b 74 $optionsyes = array('action'=>'delete', 'roleid'=>$roleid, 'sesskey'=>sesskey(), 'confirm'=>1);
a226a972 75 $a = new stdClass();
b5959f30 76 $a->id = $roleid;
77 $a->name = $roles[$roleid]->name;
78 $a->shortname = $roles[$roleid]->shortname;
9bec5274 79 $a->count = $DB->count_records('role_assignments', array('roleid'=>$roleid));
0df0df23 80
dc6896ef 81 $formcontinue = new single_button(new moodle_url($baseurl, $optionsyes), get_string('yes'));
bc5048f6 82 $formcancel = new single_button(new moodle_url($baseurl), get_string('no'), 'get');
414a4a91 83 echo $OUTPUT->confirm(get_string('deleterolesure', 'role', $a), $formcontinue, $formcancel);
73d6f52f 84 echo $OUTPUT->footer();
b5959f30 85 die;
86 }
bbdb7070 87 if (!delete_role($roleid)) {
88 // The delete failed, but mark the context dirty in case.
89 mark_context_dirty($systemcontext->path);
90 print_error('cannotdeleterolewithid', 'error', $baseurl, $roleid);
91 }
92 // Deleted a role sitewide...
93 mark_context_dirty($systemcontext->path);
94 add_to_log(SITEID, 'role', 'delete', 'admin/roles/manage.php', $roles[$roleid]->localname, '', $USER->id);
95 redirect($baseurl);
b5959f30 96 break;
97
98 case 'moveup':
bbdb7070 99 if (confirm_sesskey()) {
100 $prevrole = null;
101 $thisrole = null;
102 foreach ($roles as $role) {
103 if ($role->id == $roleid) {
104 $thisrole = $role;
105 break;
106 } else {
107 $prevrole = $role;
b5959f30 108 }
5ec8a4f0 109 }
bbdb7070 110 if (is_null($thisrole) || is_null($prevrole)) {
111 print_error('cannotmoverolewithid', 'error', '', $roleid);
112 }
113 if (!switch_roles($thisrole, $prevrole)) {
114 print_error('cannotmoverolewithid', 'error', '', $roleid);
115 }
b5959f30 116 }
117
bbdb7070 118 redirect($baseurl);
b5959f30 119 break;
120
121 case 'movedown':
bbdb7070 122 if (confirm_sesskey()) {
123 $thisrole = null;
124 $nextrole = null;
125 foreach ($roles as $role) {
126 if ($role->id == $roleid) {
127 $thisrole = $role;
128 } else if (!is_null($thisrole)) {
129 $nextrole = $role;
130 break;
b5959f30 131 }
132 }
bbdb7070 133 if (is_null($nextrole)) {
134 print_error('cannotmoverolewithid', 'error', '', $roleid);
135 }
136 if (!switch_roles($thisrole, $nextrole)) {
137 print_error('cannotmoverolewithid', 'error', '', $roleid);
d471721c 138 }
a83addc5 139 }
bbdb7070 140
141 redirect($baseurl);
d471721c 142 break;
a83addc5 143
144 case 'reset':
bbdb7070 145 if (!$confirmed) {
a83addc5 146 // show confirmation
61ef8f9f 147 echo $OUTPUT->header();
a83addc5 148 $optionsyes = array('action'=>'reset', 'roleid'=>$roleid, 'sesskey'=>sesskey(), 'confirm'=>1);
149 $optionsno = array('action'=>'view', 'roleid'=>$roleid);
a226a972 150 $a = new stdClass();
a83addc5 151 $a->id = $roleid;
152 $a->name = $roles[$roleid]->name;
153 $a->shortname = $roles[$roleid]->shortname;
4f0c2d00 154 $a->legacytype = $roles[$roleid]->archetype;
b357ed13 155 if (empty($a->legacytype)) {
156 $warning = get_string('resetrolesurenolegacy', 'role', $a);
157 } else {
158 $warning = get_string('resetrolesure', 'role', $a);
159 }
dc6896ef
PS
160 $formcontinue = new single_button(new moodle_url('manage.php', $optionsyes), get_string('yes'));
161 $formcancel = new single_button(new moodle_url('manage.php', $optionsno), get_string('no'), 'get');
4f0c2d00 162 echo $OUTPUT->confirm($warning, $formcontinue, $formcancel);
73d6f52f 163 echo $OUTPUT->footer();
a83addc5 164 die;
165 }
166
155ef38b 167 // Reset context levels for standard archetypes
4f0c2d00
PS
168 if ($roles[$roleid]->archetype) {
169 set_role_contextlevels($roleid, get_default_contextlevels($roles[$roleid]->archetype));
bbdb7070 170 }
155ef38b
PS
171
172 //reset or delete the capabilities
bbdb7070 173 reset_role_capabilities($roleid);
a83addc5 174
bbdb7070 175 // Mark context dirty, log and redirect.
176 mark_context_dirty($systemcontext->path);
177 add_to_log(SITEID, 'role', 'reset', 'admin/roles/manage.php?action=reset&roleid=' . $roleid, $roles[$roleid]->localname, '', $USER->id);
4f0c2d00 178 redirect($defineurl . '?action=view&roleid=' . $roleid);
b5959f30 179 break;
5ec8a4f0 180 }
bbbf2d40 181
bbdb7070 182/// Print the page header and tabs.
01a2ce80 183 echo $OUTPUT->header();
bbbf2d40 184
b5959f30 185 $currenttab = 'manage';
186 include_once('managetabs.php');
9584965f 187
bbdb7070 188/// Initialise table.
414a4a91 189 $table = new html_table();
bbdb7070 190 $table->tablealign = 'center';
191 $table->align = array('left', 'left', 'left', 'left');
192 $table->wrap = array('nowrap', '', 'nowrap','nowrap');
193 $table->width = '90%';
194 $table->head = array(
849c94be 195 get_string('role') . ' ' . $OUTPUT->help_icon('roles', 'role'),
bbdb7070 196 get_string('description'),
197 get_string('shortname'),
198 get_string('edit')
199 );
b5959f30 200
bbdb7070 201/// Get some strings outside the loop.
202 $stredit = get_string('edit');
203 $strduplicate = get_string('duplicate');
204 $strdelete = get_string('delete');
205 $strmoveup = get_string('moveup');
206 $strmovedown = get_string('movedown');
207
208/// Print a list of roles with edit/copy/delete/reorder icons.
209 $table->data = array();
994cc719 210 $firstrole = reset($roles);
bbdb7070 211 $lastrole = end($roles);
212 foreach ($roles as $role) {
213
214 /// Basic data.
215 $row = array(
216 '<a href="' . $defineurl . '?action=view&amp;roleid=' . $role->id . '">' . $role->localname . '</a>',
217 format_text($role->description, FORMAT_HTML),
218 s($role->shortname),
219 '',
220 );
221
222 /// Icons:
223 // move up
994cc719 224 if ($role->sortorder != $firstrole->sortorder) {
bbdb7070 225 $row[3] .= get_action_icon($baseurl . '?action=moveup&amp;roleid=' . $role->id . '&amp;sesskey=' . sesskey(), 'up', $strmoveup, $strmoveup);
eef868d1 226 } else {
bbdb7070 227 $row[3] .= get_spacer();
9e38e940 228 }
bbdb7070 229 // move down
230 if ($role->sortorder != $lastrole->sortorder) {
231 $row[3] .= get_action_icon($baseurl . '?action=movedown&amp;roleid=' . $role->id . '&amp;sesskey=' . sesskey(), 'down', $strmovedown, $strmovedown);
01a80f51 232 } else {
bbdb7070 233 $row[3] .= get_spacer();
01a80f51 234 }
bbdb7070 235 // edit
236 $row[3] .= get_action_icon($defineurl . '?action=edit&amp;roleid=' . $role->id,
237 'edit', $stredit, get_string('editxrole', 'role', $role->localname));
238 // duplicate
239 $row[3] .= get_action_icon($defineurl . '?action=duplicate&amp;roleid=' . $role->id,
240 'copy', $strduplicate, get_string('createrolebycopying', 'role', $role->localname));
241 // delete
242 if (isset($undeletableroles[$role->id])) {
243 $row[3] .= get_spacer();
244 } else {
0df0df23 245 $row[3] .= get_action_icon($baseurl . '?action=delete&amp;roleid=' . $role->id,
bbdb7070 246 'delete', $strdelete, get_string('deletexrole', 'role', $role->localname));
eef868d1 247 }
9584965f 248
bbdb7070 249 $table->data[] = $row;
5ec8a4f0 250 }
16be8974 251 echo html_writer::table($table);
bbdb7070 252
414a4a91 253 echo $OUTPUT->container_start('buttons');
5c2ed7e2 254 echo $OUTPUT->single_button(new moodle_url($defineurl, array('action' => 'add')), get_string('addrole', 'role'), 'get');
414a4a91 255 echo $OUTPUT->container_end();
bbbf2d40 256
73d6f52f 257 echo $OUTPUT->footer();
ece4945b 258 die;
259
bbdb7070 260function get_action_icon($url, $icon, $alt, $tooltip) {
5d3b9994 261 global $OUTPUT;
bbdb7070 262 return '<a title="' . $tooltip . '" href="'. $url . '">' .
b5d0cafc 263 '<img src="' . $OUTPUT->pix_url('t/' . $icon) . '" class="iconsmall" alt="' . $alt . '" /></a> ';
bbdb7070 264}
265function get_spacer() {
5d3b9994 266 global $OUTPUT;
b5d0cafc 267 return '<img src="' . $OUTPUT->pix_url('spacer') . '" class="iconsmall" alt="" /> ';
bbdb7070 268}