MDL-56651 core_role: add styling to submit buttons
[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
1fcf0ca8 31require_once(__DIR__ . '/../../config.php');
bb061951 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();
82ddcad0
RT
199 $tableroleid = $definitiontable->get_role_id();
200 // Trigger event.
201 $event = \core\event\role_capabilities_updated::create(
202 array(
203 'context' => $systemcontext,
ea4f8f9e 204 'objectid' => $tableroleid
82ddcad0
RT
205 )
206 );
207 $event->set_legacy_logdata(array(SITEID, 'role', $action, 'admin/roles/define.php?action=view&roleid=' . $tableroleid,
208 $definitiontable->get_role_name(), '', $USER->id));
209 if (!empty($role)) {
210 $event->add_record_snapshot('role', $role);
211 }
212 $event->trigger();
213
bb061951
PS
214 if ($action === 'add') {
215 redirect(new moodle_url('/admin/roles/define.php', array('action'=>'view', 'roleid'=>$definitiontable->get_role_id())));
bbdb7070 216 } else {
bb061951
PS
217 redirect($returnurl);
218 }
219}
220
221// Print the page header and tabs.
222echo $OUTPUT->header();
223
224$currenttab = 'manage';
225require('managetabs.php');
226
227echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
228
229// Work out some button labels.
230if ($action === 'add') {
231 $submitlabel = get_string('createthisrole', 'core_role');
232} else {
233 $submitlabel = get_string('savechanges');
234}
235
236// On the view page, show some extra controls at the top.
237if ($action === 'view') {
238 echo $OUTPUT->container_start('buttons');
239 $url = new moodle_url('/admin/roles/define.php', array('action'=>'edit', 'roleid'=>$roleid, 'return'=>'define'));
240 echo $OUTPUT->single_button(new moodle_url($url), get_string('edit'));
241 $url = new moodle_url('/admin/roles/define.php', array('action'=>'reset', 'roleid'=>$roleid, 'return'=>'define'));
242 echo $OUTPUT->single_button(new moodle_url($url), get_string('resetrole', 'core_role'));
243 $url = new moodle_url('/admin/roles/define.php', array('action'=>'export', 'roleid'=>$roleid));
244 echo $OUTPUT->single_button(new moodle_url($url), get_string('export', 'core_role'));
245 echo $OUTPUT->single_button($manageurl, get_string('listallroles', 'core_role'));
246 echo $OUTPUT->container_end();
247}
248
249// Start the form.
250echo $OUTPUT->box_start('generalbox');
251if ($action === 'view') {
252 echo '<div class="mform">';
253} else {
bbdb7070 254 ?>
5e72efd4 255<form id="rolesform" class="mform" action="<?php p($baseurl->out(false)); ?>" method="post"><div>
bbdb7070 256<input type="hidden" name="sesskey" value="<?php p(sesskey()) ?>" />
5e72efd4
PS
257<input type="hidden" name="return" value="<?php p($return); ?>" />
258<input type="hidden" name="resettype" value="none" />
0051b085
SL
259<div class="submitbuttons">
260 <input type="submit" name="savechanges" class="btn btn-primary" value="<?php p($submitlabel); ?>" />
261 <input type="submit" name="cancel" class="btn btn-secondary" value="<?php print_string('cancel'); ?>" />
bbdb7070 262</div>
263 <?php
bb061951 264}
bbdb7070 265
bb061951
PS
266// Print the form controls.
267$definitiontable->display();
bbdb7070 268
bb061951
PS
269// Close the stuff we left open above.
270if ($action === 'view') {
271 echo '</div>';
272} else {
273 ?>
0051b085
SL
274<div class="submitbuttons">
275 <input type="submit" name="savechanges" class="btn btn-primary" value="<?php p($submitlabel); ?>" />
276 <input type="submit" name="cancel" class="btn btn-secondary" value="<?php print_string('cancel'); ?>" />
bbdb7070 277</div>
278</div></form>
bb061951
PS
279<?php
280}
281echo $OUTPUT->box_end();
f4acee5d 282
bb061951
PS
283// Print a link back to the all roles list.
284echo '<div class="backlink">';
285echo '<p><a href="' . s($manageurl->out(false)) . '">' . get_string('backtoallroles', 'core_role') . '</a></p>';
286echo '</div>';
bbdb7070 287
bb061951 288echo $OUTPUT->footer();