2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Lets the user edit role definitions.
20 * Responds to actions:
21 * add - add a new role (allows import, duplicate, archetype)
22 * export - save xml role definition
23 * edit - edit the definition of a role
24 * view - view the definition of a role
27 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 require_once(__DIR__
. '/../../config.php');
32 require_once($CFG->libdir
.'/adminlib.php');
34 $action = required_param('action', PARAM_ALPHA
);
35 if (!in_array($action, array('add', 'export', 'edit', 'reset', 'view'))) {
36 throw new moodle_exception('invalidaccess');
38 if ($action != 'add') {
39 $roleid = required_param('roleid', PARAM_INT
);
43 $resettype = optional_param('resettype', '', PARAM_RAW
);
44 $return = optional_param('return', 'manage', PARAM_ALPHA
);
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');
49 if ($return === 'manage') {
50 $returnurl = $manageurl;
52 $returnurl = new moodle_url('/admin/roles/define.php', array('action'=>'view', 'roleid'=>$roleid));;
55 admin_externalpage_setup('defineroles', '', array('action' => $action, 'roleid' => $roleid),
56 new moodle_url('/admin/roles/define.php'));
58 // Check access permissions.
59 $systemcontext = context_system
::instance();
60 require_capability('moodle/role:manage', $systemcontext);
63 if ($action === 'export') {
64 core_role_preset
::send_export_xml($roleid);
68 // Handle the toggle advanced mode button.
69 $showadvanced = get_user_preferences('definerole_showadvanced', false);
70 if (optional_param('toggleadvanced', false, PARAM_BOOL
)) {
71 $showadvanced = !$showadvanced;
72 set_user_preference('definerole_showadvanced', $showadvanced);
75 // Get some basic data we are going to need.
76 $roles = get_all_roles();
77 $rolenames = role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL
);
78 $rolescount = count($roles);
80 if ($action === 'add') {
81 $title = get_string('addinganewrole', 'core_role');
82 } else if ($action == 'view') {
83 $title = get_string('viewingdefinitionofrolex', 'core_role', $rolenames[$roleid]->localname
);
84 } else if ($action == 'reset') {
85 $title = get_string('resettingrole', 'core_role', $rolenames[$roleid]->localname
);
87 $title = get_string('editingrolex', 'core_role', $rolenames[$roleid]->localname
);
90 // Decide how to create new role.
91 if ($action === 'add' and $resettype !== 'none') {
92 $mform = new core_role_preset_form(null, array('action'=>'add', 'roleid'=>0, 'resettype'=>'0', 'return'=>'manage'));
93 if ($mform->is_cancelled()) {
96 } else if ($data = $mform->get_data()) {
97 $resettype = $data->resettype
;
104 'contextlevels' => 1,
106 'allowoverride' => 1,
110 $definitiontable = new core_role_define_role_table_advanced($systemcontext, 0);
112 $definitiontable = new core_role_define_role_table_basic($systemcontext, 0);
114 if (is_number($resettype)) {
115 // Duplicate the role.
116 $definitiontable->force_duplicate($resettype, $options);
118 // Must be an archetype.
119 $definitiontable->force_archetype($resettype, $options);
122 if ($xml = $mform->get_file_content('rolepreset')) {
123 $definitiontable->force_preset($xml, $options);
127 echo $OUTPUT->header();
128 echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
130 echo $OUTPUT->footer();
134 } else if ($action === 'reset' and $resettype !== 'none') {
135 if (!$role = $DB->get_record('role', array('id'=>$roleid))) {
136 redirect($manageurl);
138 $resettype = empty($role->archetype
) ?
'0' : $role->archetype
;
139 $mform = new core_role_preset_form(null,
140 array('action'=>'reset', 'roleid'=>$roleid, 'resettype'=>$resettype , 'permissions'=>1, 'archetype'=>1, 'contextlevels'=>1, 'return'=>$return));
141 if ($mform->is_cancelled()) {
142 redirect($returnurl);
144 } else if ($data = $mform->get_data()) {
145 $resettype = $data->resettype
;
147 'shortname' => $data->shortname
,
148 'name' => $data->name
,
149 'description' => $data->description
,
150 'permissions' => $data->permissions
,
151 'archetype' => $data->archetype
,
152 'contextlevels' => $data->contextlevels
,
153 'allowassign' => $data->allowassign
,
154 'allowoverride' => $data->allowoverride
,
155 'allowswitch' => $data->allowswitch
,
156 'allowview' => $data->allowview
);
158 $definitiontable = new core_role_define_role_table_advanced($systemcontext, $roleid);
160 $definitiontable = new core_role_define_role_table_basic($systemcontext, $roleid);
162 if (is_number($resettype)) {
163 // Duplicate the role.
164 $definitiontable->force_duplicate($resettype, $options);
166 // Must be an archetype.
167 $definitiontable->force_archetype($resettype, $options);
170 if ($xml = $mform->get_file_content('rolepreset')) {
171 $definitiontable->force_preset($xml, $options);
175 echo $OUTPUT->header();
176 echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
178 echo $OUTPUT->footer();
183 // Create the table object.
184 if ($action === 'view') {
185 $definitiontable = new core_role_view_role_definition_table($systemcontext, $roleid);
186 } else if ($showadvanced) {
187 $definitiontable = new core_role_define_role_table_advanced($systemcontext, $roleid);
189 $definitiontable = new core_role_define_role_table_basic($systemcontext, $roleid);
191 $definitiontable->read_submitted_permissions();
194 // Handle the cancel button.
195 if (optional_param('cancel', false, PARAM_BOOL
)) {
196 redirect($returnurl);
199 // Process submission in necessary.
200 if (optional_param('savechanges', false, PARAM_BOOL
) && confirm_sesskey() && $definitiontable->is_submission_valid()) {
201 $definitiontable->save_changes();
202 $tableroleid = $definitiontable->get_role_id();
204 if ($action === 'add') {
205 redirect(new moodle_url('/admin/roles/define.php', array('action'=>'view', 'roleid'=>$definitiontable->get_role_id())));
207 redirect($returnurl);
211 // Print the page header and tabs.
212 echo $OUTPUT->header();
214 $currenttab = 'manage';
215 require('managetabs.php');
217 echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
219 // Work out some button labels.
220 if ($action === 'add') {
221 $submitlabel = get_string('createthisrole', 'core_role');
223 $submitlabel = get_string('savechanges');
226 // On the view page, show some extra controls at the top.
227 if ($action === 'view') {
228 echo $OUTPUT->container_start('buttons');
229 $url = new moodle_url('/admin/roles/define.php', array('action'=>'edit', 'roleid'=>$roleid, 'return'=>'define'));
230 echo $OUTPUT->single_button(new moodle_url($url), get_string('edit'));
231 $url = new moodle_url('/admin/roles/define.php', array('action'=>'reset', 'roleid'=>$roleid, 'return'=>'define'));
232 echo $OUTPUT->single_button(new moodle_url($url), get_string('resetrole', 'core_role'));
233 $url = new moodle_url('/admin/roles/define.php', array('action'=>'export', 'roleid'=>$roleid));
234 echo $OUTPUT->single_button(new moodle_url($url), get_string('export', 'core_role'));
235 echo $OUTPUT->single_button($manageurl, get_string('listallroles', 'core_role'));
236 echo $OUTPUT->container_end();
240 echo $OUTPUT->box_start('generalbox');
241 if ($action === 'view') {
242 echo '<div class="mform">';
245 <form id
="rolesform" class="mform fcontainer" action
="<?php p($baseurl->out(false)); ?>" method
="post"><div
>
246 <input type
="hidden" name
="sesskey" value
="<?php p(sesskey()) ?>" />
247 <input type
="hidden" name
="return" value
="<?php p($return); ?>" />
248 <input type
="hidden" name
="resettype" value
="none" />
249 <div
class="submitbuttons">
250 <input type
="submit" name
="savechanges" class="btn btn-primary" value
="<?php p($submitlabel); ?>" />
251 <input type
="submit" name
="cancel" class="btn btn-secondary" value
="<?php print_string('cancel'); ?>" />
256 // Print the form controls.
257 $definitiontable->display();
259 // Close the stuff we left open above.
260 if ($action === 'view') {
264 <div
class="submitbuttons">
265 <input type
="submit" name
="savechanges" class="btn btn-primary" value
="<?php p($submitlabel); ?>" />
266 <input type
="submit" name
="cancel" class="btn btn-secondary" value
="<?php print_string('cancel'); ?>" />
271 echo $OUTPUT->box_end();
273 // Print a link back to the all roles list.
274 echo '<div class="backlink">';
275 echo '<p><a href="' . s($manageurl->out(false)) . '">' . get_string('backtoallroles', 'core_role') . '</a></p>';
278 echo $OUTPUT->footer();