MDL-40337 cleanup role assign pages
[moodle.git] / admin / roles / define.php
CommitLineData
9e492db0 1<?php
01a2ce80
PS
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/>.
f4acee5d 16
17/**
18 * Lets the user edit role definitions.
19 *
20 * Responds to actions:
5e72efd4
PS
21 * add - add a new role (allows import, duplicate, archetype)
22 * export - save xml role definition
bbdb7070 23 * edit - edit the definition of a role
24 * view - view the definition of a role
f4acee5d 25 *
5e72efd4 26 * @package core_role
01a2ce80
PS
27 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 */
f4acee5d 30
31 require_once(dirname(__FILE__) . '/../../config.php');
bc7b53fb 32 require_once($CFG->libdir.'/adminlib.php');
f4acee5d 33
34 $action = required_param('action', PARAM_ALPHA);
5e72efd4 35 if (!in_array($action, array('add', 'export', 'edit', 'reset', 'view'))) {
bbdb7070 36 throw new moodle_exception('invalidaccess');
37 }
f4acee5d 38 if ($action != 'add') {
1e12c120 39 $roleid = required_param('roleid', PARAM_INT);
f4acee5d 40 } else {
41 $roleid = 0;
42 }
5e72efd4
PS
43 $resettype = optional_param('resettype', '', PARAM_RAW);
44 $return = optional_param('return', 'manage', PARAM_ALPHA);
f4acee5d 45
46/// Get the base URL for this and related pages into a convenient variable.
5e72efd4
PS
47 $baseurl = new moodle_url('/admin/roles/define.php', array('action'=>$action, 'roleid'=>$roleid));
48 $manageurl = new moodle_url('/admin/roles/manage.php');
49 if ($return === 'manage') {
50 $returnurl = $manageurl;
bbdb7070 51 } else {
5e72efd4 52 $returnurl = new moodle_url('/admin/roles/define.php', array('action'=>'view', 'roleid'=>$roleid));;
bbdb7070 53 }
f4acee5d 54
55/// Check access permissions.
bf006d2c 56 $systemcontext = context_system::instance();
f4acee5d 57 require_login();
58 require_capability('moodle/role:manage', $systemcontext);
5e72efd4 59 admin_externalpage_setup('defineroles', '', array('action' => $action, 'roleid' => $roleid), new moodle_url('/admin/roles/define.php'));
f4acee5d 60
5e72efd4
PS
61/// Export role.
62 if ($action === 'export') {
63 core_role_preset::send_export_xml($roleid);
64 die;
f4acee5d 65 }
66
67/// Handle the toggle advanced mode button.
68 $showadvanced = get_user_preferences('definerole_showadvanced', false);
69 if (optional_param('toggleadvanced', false, PARAM_BOOL)) {
70 $showadvanced = !$showadvanced;
71 set_user_preference('definerole_showadvanced', $showadvanced);
72 }
73
74/// Get some basic data we are going to need.
75 $roles = get_all_roles();
bbdb7070 76 $rolenames = role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL);
f4acee5d 77 $rolescount = count($roles);
78
5e72efd4
PS
79 if ($action == 'add') {
80 $title = get_string('addinganewrole', 'role');
81 } else if ($action == 'view') {
82 $title = get_string('viewingdefinitionofrolex', 'role', $rolenames[$roleid]->localname);
83 } else if ($action == 'reset') {
84 $title = get_string('resettingrole', 'role', $rolenames[$roleid]->localname);
85 } else {
86 $title = get_string('editingrolex', 'role', $rolenames[$roleid]->localname);
87 }
88
89/// Decide how to create new role.
90 if ($action === 'add' and $resettype !== 'none') {
91 $mform = new core_role_preset_form(null, array('action'=>'add', 'roleid'=>0, 'resettype'=>'0', 'return'=>'manage'));
92 if ($mform->is_cancelled()) {
93 redirect($manageurl);
94
95 } else if ($data = $mform->get_data()) {
96 $resettype = $data->resettype;
97 $options = array(
98 'shortname' => 1,
99 'name' => 1,
100 'description' => 1,
101 'permissions' => 1,
102 'archetype' => 1,
103 'contextlevels' => 1,
104 'allowassign' => 1,
105 'allowoverride' => 1,
106 'allowswitch' => 1);
107 if ($showadvanced) {
bc7b53fb 108 $definitiontable = new core_role_define_role_table_advanced($systemcontext, 0);
5e72efd4 109 } else {
bc7b53fb 110 $definitiontable = new core_role_define_role_table_basic($systemcontext, 0);
5e72efd4
PS
111 }
112 if (is_number($resettype)) {
113 // Duplicate the role.
114 $definitiontable->force_duplicate($resettype, $options);
115 } else {
116 // Must be an archetype.
117 $definitiontable->force_archetype($resettype, $options);
118 }
119
120 if ($xml = $mform->get_file_content('rolepreset')) {
121 $definitiontable->force_preset($xml, $options);
122 }
123
124 } else {
125 echo $OUTPUT->header();
126 echo $OUTPUT->heading_with_help($title, 'roles', 'role');
127 $mform->display();
128 echo $OUTPUT->footer();
129 die;
130 }
131
132 } else if ($action === 'reset' and $resettype !== 'none') {
133 if (!$role = $DB->get_record('role', array('id'=>$roleid))) {
134 redirect($manageurl);
135 }
136 $resettype = empty($role->archetype) ? '0' : $role->archetype;
137 $mform = new core_role_preset_form(null,
138 array('action'=>'reset', 'roleid'=>$roleid, 'resettype'=>$resettype , 'permissions'=>1, 'archetype'=>1, 'contextlevels'=>1, 'return'=>$return));
139 if ($mform->is_cancelled()) {
140 redirect($returnurl);
141
142 } else if ($data = $mform->get_data()) {
143 $resettype = $data->resettype;
144 $options = array(
145 'shortname' => $data->shortname,
146 'name' => $data->name,
147 'description' => $data->description,
148 'permissions' => $data->permissions,
149 'archetype' => $data->archetype,
150 'contextlevels' => $data->contextlevels,
151 'allowassign' => $data->allowassign,
152 'allowoverride' => $data->allowoverride,
153 'allowswitch' => $data->allowswitch);
154 if ($showadvanced) {
bc7b53fb 155 $definitiontable = new core_role_define_role_table_advanced($systemcontext, $roleid);
5e72efd4 156 } else {
bc7b53fb 157 $definitiontable = new core_role_define_role_table_basic($systemcontext, $roleid);
5e72efd4
PS
158 }
159 if (is_number($resettype)) {
160 // Duplicate the role.
161 $definitiontable->force_duplicate($resettype, $options);
162 } else {
163 // Must be an archetype.
164 $definitiontable->force_archetype($resettype, $options);
165 }
166
167 if ($xml = $mform->get_file_content('rolepreset')) {
168 $definitiontable->force_preset($xml, $options);
169 }
170
171 } else {
172 echo $OUTPUT->header();
173 echo $OUTPUT->heading_with_help($title, 'roles', 'role');
174 $mform->display();
175 echo $OUTPUT->footer();
176 die;
177 }
178
f4acee5d 179 } else {
5e72efd4
PS
180 /// Create the table object.
181 if ($action == 'view') {
bc7b53fb 182 $definitiontable = new core_role_view_role_definition_table($systemcontext, $roleid);
5e72efd4 183 } else if ($showadvanced) {
bc7b53fb 184 $definitiontable = new core_role_define_role_table_advanced($systemcontext, $roleid);
5e72efd4 185 } else {
bc7b53fb 186 $definitiontable = new core_role_define_role_table_basic($systemcontext, $roleid);
5e72efd4
PS
187 }
188 $definitiontable->read_submitted_permissions();
f4acee5d 189 }
5e72efd4
PS
190
191/// Handle the cancel button.
192 if (optional_param('cancel', false, PARAM_BOOL)) {
193 redirect($returnurl);
f4acee5d 194 }
195
bbdb7070 196/// Process submission in necessary.
197 if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey() && $definitiontable->is_submission_valid()) {
198 $definitiontable->save_changes();
199 add_to_log(SITEID, 'role', $action, 'admin/roles/define.php?action=view&roleid=' .
200 $definitiontable->get_role_id(), $definitiontable->get_role_name(), '', $USER->id);
5e72efd4
PS
201 if ($action === 'add') {
202 redirect(new moodle_url('/admin/roles/define.php', array('action'=>'view', 'roleid'=>$definitiontable->get_role_id())));
203 } else {
204 redirect($returnurl);
205 }
bbdb7070 206 }
f4acee5d 207
208/// Print the page header and tabs.
61ef8f9f 209 echo $OUTPUT->header();
f4acee5d 210
211 $currenttab = 'manage';
4f0c2d00 212 include('managetabs.php');
f4acee5d 213
5435c9dc 214 echo $OUTPUT->heading_with_help($title, 'roles', 'role');
f4acee5d 215
bbdb7070 216/// Work out some button labels.
5e72efd4 217 if ($action === 'add') {
bed9cec8 218 $submitlabel = get_string('createthisrole', 'role');
f4acee5d 219 } else {
bbdb7070 220 $submitlabel = get_string('savechanges');
221 }
f4acee5d 222
bbdb7070 223/// On the view page, show some extra controls at the top.
5e72efd4 224 if ($action === 'view') {
414a4a91 225 echo $OUTPUT->container_start('buttons');
5e72efd4
PS
226 $url = new moodle_url('/admin/roles/define.php', array('action'=>'edit', 'roleid'=>$roleid, 'return'=>'define'));
227 echo $OUTPUT->single_button(new moodle_url($url), get_string('edit'));
228 $url = new moodle_url('/admin/roles/define.php', array('action'=>'reset', 'roleid'=>$roleid, 'return'=>'define'));
229 echo $OUTPUT->single_button(new moodle_url($url), get_string('resetrole', 'role'));
230 $url = new moodle_url('/admin/roles/define.php', array('action'=>'export', 'roleid'=>$roleid));
231 echo $OUTPUT->single_button(new moodle_url($url), get_string('export', 'core_role'));
232 echo $OUTPUT->single_button($manageurl, get_string('listallroles', 'role'));
414a4a91 233 echo $OUTPUT->container_end();
f4acee5d 234 }
235
bbdb7070 236 // Start the form.
20486a5a 237 echo $OUTPUT->box_start('generalbox');
bbdb7070 238 if ($action == 'view') {
239 echo '<div class="mform">';
240 } else {
241 ?>
5e72efd4 242<form id="rolesform" class="mform" action="<?php p($baseurl->out(false)); ?>" method="post"><div>
bbdb7070 243<input type="hidden" name="sesskey" value="<?php p(sesskey()) ?>" />
5e72efd4
PS
244<input type="hidden" name="return" value="<?php p($return); ?>" />
245<input type="hidden" name="resettype" value="none" />
bbdb7070 246<div class="submit buttons">
5e72efd4 247 <input type="submit" name="savechanges" value="<?php p($submitlabel); ?>" />
bbdb7070 248 <input type="submit" name="cancel" value="<?php print_string('cancel'); ?>" />
249</div>
250 <?php
251 }
252
253 // Print the form controls.
f4acee5d 254 $definitiontable->display();
bbdb7070 255
256/// Close the stuff we left open above.
257 if ($action == 'view') {
258 echo '</div>';
259 } else {
260 ?>
261<div class="submit buttons">
5e72efd4 262 <input type="submit" name="savechanges" value="<?php p($submitlabel); ?>" />
bbdb7070 263 <input type="submit" name="cancel" value="<?php print_string('cancel'); ?>" />
264</div>
265</div></form>
266 <?php
267 }
20486a5a 268 echo $OUTPUT->box_end();
f4acee5d 269
bbdb7070 270/// Print a link back to the all roles list.
271 echo '<div class="backlink">';
5e72efd4 272 echo '<p><a href="' . s($manageurl->out(false)) . '">' . get_string('backtoallroles', 'role') . '</a></p>';
bbdb7070 273 echo '</div>';
274
73d6f52f 275 echo $OUTPUT->footer();