MDL-40044 Roles: Added role_deleted event and replaced related add_to_log
[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
bb061951
PS
31require_once(dirname(__FILE__) . '/../../config.php');
32require_once($CFG->libdir.'/adminlib.php');
f4acee5d 33
bb061951
PS
34$action = required_param('action', PARAM_ALPHA);
35if (!in_array($action, array('add', 'export', 'edit', 'reset', 'view'))) {
36 throw new moodle_exception('invalidaccess');
37}
38if ($action != 'add') {
39 $roleid = required_param('roleid', PARAM_INT);
40} else {
41 $roleid = 0;
42}
43$resettype = optional_param('resettype', '', PARAM_RAW);
44$return = optional_param('return', 'manage', PARAM_ALPHA);
f4acee5d 45
bb061951
PS
46// Get the base URL for this and related pages into a convenient variable.
47$baseurl = new moodle_url('/admin/roles/define.php', array('action'=>$action, 'roleid'=>$roleid));
48$manageurl = new moodle_url('/admin/roles/manage.php');
49if ($return === 'manage') {
50 $returnurl = $manageurl;
51} else {
52 $returnurl = new moodle_url('/admin/roles/define.php', array('action'=>'view', 'roleid'=>$roleid));;
53}
f4acee5d 54
bb061951
PS
55// Check access permissions.
56$systemcontext = context_system::instance();
57require_login();
58require_capability('moodle/role:manage', $systemcontext);
59admin_externalpage_setup('defineroles', '', array('action' => $action, 'roleid' => $roleid), new moodle_url('/admin/roles/define.php'));
f4acee5d 60
bb061951
PS
61// Export role.
62if ($action === 'export') {
63 core_role_preset::send_export_xml($roleid);
64 die;
65}
f4acee5d 66
bb061951
PS
67// Handle the toggle advanced mode button.
68$showadvanced = get_user_preferences('definerole_showadvanced', false);
69if (optional_param('toggleadvanced', false, PARAM_BOOL)) {
70 $showadvanced = !$showadvanced;
71 set_user_preference('definerole_showadvanced', $showadvanced);
72}
5e72efd4 73
bb061951
PS
74// Get some basic data we are going to need.
75$roles = get_all_roles();
76$rolenames = role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL);
77$rolescount = count($roles);
5e72efd4 78
bb061951
PS
79if ($action === 'add') {
80 $title = get_string('addinganewrole', 'core_role');
81} else if ($action == 'view') {
82 $title = get_string('viewingdefinitionofrolex', 'core_role', $rolenames[$roleid]->localname);
83} else if ($action == 'reset') {
84 $title = get_string('resettingrole', 'core_role', $rolenames[$roleid]->localname);
85} else {
86 $title = get_string('editingrolex', 'core_role', $rolenames[$roleid]->localname);
87}
5e72efd4 88
bb061951
PS
89// Decide how to create new role.
90if ($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);
5e72efd4 94
bb061951
PS
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) {
108 $definitiontable = new core_role_define_role_table_advanced($systemcontext, 0);
5e72efd4 109 } else {
bb061951 110 $definitiontable = new core_role_define_role_table_basic($systemcontext, 0);
5e72efd4 111 }
bb061951
PS
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);
5e72efd4 118 }
5e72efd4 119
bb061951
PS
120 if ($xml = $mform->get_file_content('rolepreset')) {
121 $definitiontable->force_preset($xml, $options);
5e72efd4
PS
122 }
123
f4acee5d 124 } else {
bb061951
PS
125 echo $OUTPUT->header();
126 echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
127 $mform->display();
128 echo $OUTPUT->footer();
129 die;
f4acee5d 130 }
5e72efd4 131
bb061951
PS
132} else if ($action === 'reset' and $resettype !== 'none') {
133 if (!$role = $DB->get_record('role', array('id'=>$roleid))) {
134 redirect($manageurl);
f4acee5d 135 }
bb061951
PS
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);
f4acee5d 141
bb061951
PS
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) {
155 $definitiontable = new core_role_define_role_table_advanced($systemcontext, $roleid);
156 } else {
157 $definitiontable = new core_role_define_role_table_basic($systemcontext, $roleid);
158 }
159 if (is_number($resettype)) {
160 // Duplicate the role.
161 $definitiontable->force_duplicate($resettype, $options);
5e72efd4 162 } else {
bb061951
PS
163 // Must be an archetype.
164 $definitiontable->force_archetype($resettype, $options);
5e72efd4 165 }
f4acee5d 166
bb061951
PS
167 if ($xml = $mform->get_file_content('rolepreset')) {
168 $definitiontable->force_preset($xml, $options);
169 }
f4acee5d 170
f4acee5d 171 } else {
bb061951
PS
172 echo $OUTPUT->header();
173 echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
174 $mform->display();
175 echo $OUTPUT->footer();
176 die;
bbdb7070 177 }
f4acee5d 178
bb061951
PS
179} else {
180 // Create the table object.
5e72efd4 181 if ($action === 'view') {
bb061951
PS
182 $definitiontable = new core_role_view_role_definition_table($systemcontext, $roleid);
183 } else if ($showadvanced) {
184 $definitiontable = new core_role_define_role_table_advanced($systemcontext, $roleid);
185 } else {
186 $definitiontable = new core_role_define_role_table_basic($systemcontext, $roleid);
f4acee5d 187 }
bb061951
PS
188 $definitiontable->read_submitted_permissions();
189}
190
191// Handle the cancel button.
192if (optional_param('cancel', false, PARAM_BOOL)) {
193 redirect($returnurl);
194}
f4acee5d 195
bb061951
PS
196// Process submission in necessary.
197if (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);
201 if ($action === 'add') {
202 redirect(new moodle_url('/admin/roles/define.php', array('action'=>'view', 'roleid'=>$definitiontable->get_role_id())));
bbdb7070 203 } else {
bb061951
PS
204 redirect($returnurl);
205 }
206}
207
208// Print the page header and tabs.
209echo $OUTPUT->header();
210
211$currenttab = 'manage';
212require('managetabs.php');
213
214echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
215
216// Work out some button labels.
217if ($action === 'add') {
218 $submitlabel = get_string('createthisrole', 'core_role');
219} else {
220 $submitlabel = get_string('savechanges');
221}
222
223// On the view page, show some extra controls at the top.
224if ($action === 'view') {
225 echo $OUTPUT->container_start('buttons');
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', 'core_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', 'core_role'));
233 echo $OUTPUT->container_end();
234}
235
236// Start the form.
237echo $OUTPUT->box_start('generalbox');
238if ($action === 'view') {
239 echo '<div class="mform">';
240} else {
bbdb7070 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
bb061951 251}
bbdb7070 252
bb061951
PS
253// Print the form controls.
254$definitiontable->display();
bbdb7070 255
bb061951
PS
256// Close the stuff we left open above.
257if ($action === 'view') {
258 echo '</div>';
259} else {
260 ?>
bbdb7070 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>
bb061951
PS
266<?php
267}
268echo $OUTPUT->box_end();
f4acee5d 269
bb061951
PS
270// Print a link back to the all roles list.
271echo '<div class="backlink">';
272echo '<p><a href="' . s($manageurl->out(false)) . '">' . get_string('backtoallroles', 'core_role') . '</a></p>';
273echo '</div>';
bbdb7070 274
bb061951 275echo $OUTPUT->footer();