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 $PAGE->set_secondary_active_tab('users');
91 $PAGE->set_primary_active_tab('siteadminnode');
92 $PAGE->navbar
->add($title, $baseurl);
94 // Decide how to create new role.
95 if ($action === 'add' and $resettype !== 'none') {
96 $mform = new core_role_preset_form(null, array('action'=>'add', 'roleid'=>0, 'resettype'=>'0', 'return'=>'manage'));
97 if ($mform->is_cancelled()) {
100 } else if ($data = $mform->get_data()) {
101 $resettype = $data->resettype
;
108 'contextlevels' => 1,
110 'allowoverride' => 1,
114 $definitiontable = new core_role_define_role_table_advanced($systemcontext, 0);
116 $definitiontable = new core_role_define_role_table_basic($systemcontext, 0);
118 if (is_number($resettype)) {
119 // Duplicate the role.
120 $definitiontable->force_duplicate($resettype, $options);
122 // Must be an archetype.
123 $definitiontable->force_archetype($resettype, $options);
126 if ($xml = $mform->get_file_content('rolepreset')) {
127 $definitiontable->force_preset($xml, $options);
131 echo $OUTPUT->header();
132 echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
134 echo $OUTPUT->footer();
138 } else if ($action === 'reset' and $resettype !== 'none') {
139 if (!$role = $DB->get_record('role', array('id'=>$roleid))) {
140 redirect($manageurl);
142 $resettype = empty($role->archetype
) ?
'0' : $role->archetype
;
143 $mform = new core_role_preset_form(null,
144 array('action'=>'reset', 'roleid'=>$roleid, 'resettype'=>$resettype , 'permissions'=>1, 'archetype'=>1, 'contextlevels'=>1, 'return'=>$return));
145 if ($mform->is_cancelled()) {
146 redirect($returnurl);
148 } else if ($data = $mform->get_data()) {
149 $resettype = $data->resettype
;
151 'shortname' => $data->shortname
,
152 'name' => $data->name
,
153 'description' => $data->description
,
154 'permissions' => $data->permissions
,
155 'archetype' => $data->archetype
,
156 'contextlevels' => $data->contextlevels
,
157 'allowassign' => $data->allowassign
,
158 'allowoverride' => $data->allowoverride
,
159 'allowswitch' => $data->allowswitch
,
160 'allowview' => $data->allowview
);
162 $definitiontable = new core_role_define_role_table_advanced($systemcontext, $roleid);
164 $definitiontable = new core_role_define_role_table_basic($systemcontext, $roleid);
166 if (is_number($resettype)) {
167 // Duplicate the role.
168 $definitiontable->force_duplicate($resettype, $options);
170 // Must be an archetype.
171 $definitiontable->force_archetype($resettype, $options);
174 if ($xml = $mform->get_file_content('rolepreset')) {
175 $definitiontable->force_preset($xml, $options);
179 echo $OUTPUT->header();
180 echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
182 echo $OUTPUT->footer();
187 // Create the table object.
188 if ($action === 'view') {
189 $definitiontable = new core_role_view_role_definition_table($systemcontext, $roleid);
190 } else if ($showadvanced) {
191 $definitiontable = new core_role_define_role_table_advanced($systemcontext, $roleid);
193 $definitiontable = new core_role_define_role_table_basic($systemcontext, $roleid);
195 $definitiontable->read_submitted_permissions();
198 // Handle the cancel button.
199 if (optional_param('cancel', false, PARAM_BOOL
)) {
200 redirect($returnurl);
203 // Process submission in necessary.
204 if (optional_param('savechanges', false, PARAM_BOOL
) && confirm_sesskey() && $definitiontable->is_submission_valid()) {
205 $definitiontable->save_changes();
206 $tableroleid = $definitiontable->get_role_id();
208 if ($action === 'add') {
209 redirect(new moodle_url('/admin/roles/define.php', array('action'=>'view', 'roleid'=>$definitiontable->get_role_id())));
211 redirect($returnurl);
215 // Print the page header and tabs.
216 echo $OUTPUT->header();
218 $currenttab = 'manage';
219 require('managetabs.php');
221 echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
223 // Work out some button labels.
224 if ($action === 'add') {
225 $submitlabel = get_string('createthisrole', 'core_role');
227 $submitlabel = get_string('savechanges');
230 // On the view page, show some extra controls at the top.
231 if ($action === 'view') {
232 echo $OUTPUT->container_start('buttons');
233 $url = new moodle_url('/admin/roles/define.php', array('action'=>'edit', 'roleid'=>$roleid, 'return'=>'define'));
234 echo $OUTPUT->single_button(new moodle_url($url), get_string('edit'));
235 $url = new moodle_url('/admin/roles/define.php', array('action'=>'reset', 'roleid'=>$roleid, 'return'=>'define'));
236 echo $OUTPUT->single_button(new moodle_url($url), get_string('resetrole', 'core_role'));
237 $url = new moodle_url('/admin/roles/define.php', array('action'=>'export', 'roleid'=>$roleid));
238 echo $OUTPUT->single_button(new moodle_url($url), get_string('export', 'core_role'));
239 echo $OUTPUT->single_button($manageurl, get_string('listallroles', 'core_role'));
240 echo $OUTPUT->container_end();
244 echo $OUTPUT->box_start('generalbox');
245 if ($action === 'view') {
246 echo '<div class="mform">';
249 <form id
="rolesform" class="mform fcontainer" action
="<?php p($baseurl->out(false)); ?>" method
="post"><div
>
250 <input type
="hidden" name
="sesskey" value
="<?php p(sesskey()) ?>" />
251 <input type
="hidden" name
="return" value
="<?php p($return); ?>" />
252 <input type
="hidden" name
="resettype" value
="none" />
253 <div
class="submitbuttons">
254 <input type
="submit" name
="savechanges" class="btn btn-primary" value
="<?php p($submitlabel); ?>" />
255 <input type
="submit" name
="cancel" class="btn btn-secondary" value
="<?php print_string('cancel'); ?>" />
260 // Print the form controls.
261 $definitiontable->display();
263 // Close the stuff we left open above.
264 if ($action === 'view') {
268 <div
class="submitbuttons">
269 <input type
="submit" name
="savechanges" class="btn btn-primary" value
="<?php p($submitlabel); ?>" />
270 <input type
="submit" name
="cancel" class="btn btn-secondary" value
="<?php print_string('cancel'); ?>" />
275 echo $OUTPUT->box_end();
277 // Print a link back to the all roles list.
278 echo '<div class="backlink">';
279 echo '<p><a href="' . s($manageurl->out(false)) . '">' . get_string('backtoallroles', 'core_role') . '</a></p>';
282 echo $OUTPUT->footer();