3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Lets the user define and edit roles.
21 * Responds to actions:
22 * [blank] - list roles.
23 * delete - delete a role (with are-you-sure)
24 * moveup - change the sort order
25 * movedown - change the sort order
26 * reset - set a role's permissions back to the default for that legacy role type.
28 * For all but the first two of those, you also need a roleid parameter, and
29 * possibly some other data.
33 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 require_once(dirname(__FILE__
) . '/../../config.php');
38 require_once($CFG->dirroot
. '/' . $CFG->admin
. '/roles/lib.php');
40 $action = optional_param('action', '', PARAM_ALPHA
);
42 $roleid = required_param('roleid', PARAM_INT
);
45 /// Get the base URL for this and related pages into a convenient variable.
46 $baseurl = $CFG->wwwroot
. '/' . $CFG->admin
. '/roles/manage.php';
47 $defineurl = $CFG->wwwroot
. '/' . $CFG->admin
. '/roles/define.php';
49 /// Check access permissions.
50 $systemcontext = context_system
::instance();
52 require_capability('moodle/role:manage', $systemcontext);
53 admin_externalpage_setup('defineroles');
55 /// Get some basic data we are going to need.
56 $roles = role_fix_names(get_all_roles(), $systemcontext, ROLENAME_ORIGINAL
);
58 $undeletableroles = array();
59 $undeletableroles[$CFG->notloggedinroleid
] = 1;
60 $undeletableroles[$CFG->guestroleid
] = 1;
61 $undeletableroles[$CFG->defaultuserroleid
] = 1;
63 ///.Process submitted data.
64 $confirmed = optional_param('confirm', false, PARAM_BOOL
) && data_submitted() && confirm_sesskey();
67 if (isset($undeletableroles[$roleid])) {
68 print_error('cannotdeletethisrole', '', $baseurl);
72 echo $OUTPUT->header();
73 $optionsyes = array('action'=>'delete', 'roleid'=>$roleid, 'sesskey'=>sesskey(), 'confirm'=>1);
76 $a->name
= $roles[$roleid]->name
;
77 $a->shortname
= $roles[$roleid]->shortname
;
78 $a->count
= $DB->count_records('role_assignments', array('roleid'=>$roleid));
80 $formcontinue = new single_button(new moodle_url($baseurl, $optionsyes), get_string('yes'));
81 $formcancel = new single_button(new moodle_url($baseurl), get_string('no'), 'get');
82 echo $OUTPUT->confirm(get_string('deleterolesure', 'role', $a), $formcontinue, $formcancel);
83 echo $OUTPUT->footer();
86 if (!delete_role($roleid)) {
87 // The delete failed, but mark the context dirty in case.
88 mark_context_dirty($systemcontext->path
);
89 print_error('cannotdeleterolewithid', 'error', $baseurl, $roleid);
91 // Deleted a role sitewide...
92 mark_context_dirty($systemcontext->path
);
93 add_to_log(SITEID
, 'role', 'delete', 'admin/roles/manage.php', $roles[$roleid]->localname
, '', $USER->id
);
98 if (confirm_sesskey()) {
101 foreach ($roles as $role) {
102 if ($role->id
== $roleid) {
109 if (is_null($thisrole) ||
is_null($prevrole)) {
110 print_error('cannotmoverolewithid', 'error', '', $roleid);
112 if (!switch_roles($thisrole, $prevrole)) {
113 print_error('cannotmoverolewithid', 'error', '', $roleid);
121 if (confirm_sesskey()) {
124 foreach ($roles as $role) {
125 if ($role->id
== $roleid) {
127 } else if (!is_null($thisrole)) {
132 if (is_null($nextrole)) {
133 print_error('cannotmoverolewithid', 'error', '', $roleid);
135 if (!switch_roles($thisrole, $nextrole)) {
136 print_error('cannotmoverolewithid', 'error', '', $roleid);
146 echo $OUTPUT->header();
147 $optionsyes = array('action'=>'reset', 'roleid'=>$roleid, 'sesskey'=>sesskey(), 'confirm'=>1);
148 $optionsno = array('action'=>'view', 'roleid'=>$roleid);
151 $a->name
= $roles[$roleid]->name
;
152 $a->shortname
= $roles[$roleid]->shortname
;
153 $a->legacytype
= $roles[$roleid]->archetype
;
154 if (empty($a->legacytype
)) {
155 $warning = get_string('resetrolesurenolegacy', 'role', $a);
157 $warning = get_string('resetrolesure', 'role', $a);
159 $formcontinue = new single_button(new moodle_url('manage.php', $optionsyes), get_string('yes'));
160 $formcancel = new single_button(new moodle_url('manage.php', $optionsno), get_string('no'), 'get');
161 echo $OUTPUT->confirm($warning, $formcontinue, $formcancel);
162 echo $OUTPUT->footer();
166 // Reset context levels for standard archetypes
167 if ($roles[$roleid]->archetype
) {
168 set_role_contextlevels($roleid, get_default_contextlevels($roles[$roleid]->archetype
));
171 //reset or delete the capabilities
172 reset_role_capabilities($roleid);
174 // Mark context dirty, log and redirect.
175 mark_context_dirty($systemcontext->path
);
176 add_to_log(SITEID
, 'role', 'reset', 'admin/roles/manage.php?action=reset&roleid=' . $roleid, $roles[$roleid]->localname
, '', $USER->id
);
177 redirect($defineurl . '?action=view&roleid=' . $roleid);
181 /// Print the page header and tabs.
182 echo $OUTPUT->header();
184 $currenttab = 'manage';
185 include_once('managetabs.php');
187 /// Initialise table.
188 $table = new html_table();
189 $table->tablealign
= 'center';
190 $table->align
= array('left', 'left', 'left', 'left');
191 $table->wrap
= array('nowrap', '', 'nowrap','nowrap');
192 $table->width
= '90%';
193 $table->head
= array(
194 get_string('role') . ' ' . $OUTPUT->help_icon('roles', 'role'),
195 get_string('description'),
196 get_string('roleshortname', 'role'),
200 /// Get some strings outside the loop.
201 $stredit = get_string('edit');
202 $strduplicate = get_string('duplicate');
203 $strdelete = get_string('delete');
204 $strmoveup = get_string('moveup');
205 $strmovedown = get_string('movedown');
207 /// Print a list of roles with edit/copy/delete/reorder icons.
208 $table->data
= array();
209 $firstrole = reset($roles);
210 $lastrole = end($roles);
211 foreach ($roles as $role) {
215 '<a href="' . $defineurl . '?action=view&roleid=' . $role->id
. '">' . $role->localname
. '</a>',
216 role_get_description($role),
223 if ($role->sortorder
!= $firstrole->sortorder
) {
224 $row[3] .= get_action_icon($baseurl . '?action=moveup&roleid=' . $role->id
. '&sesskey=' . sesskey(), 'up', $strmoveup, $strmoveup);
226 $row[3] .= get_spacer();
229 if ($role->sortorder
!= $lastrole->sortorder
) {
230 $row[3] .= get_action_icon($baseurl . '?action=movedown&roleid=' . $role->id
. '&sesskey=' . sesskey(), 'down', $strmovedown, $strmovedown);
232 $row[3] .= get_spacer();
235 $row[3] .= get_action_icon($defineurl . '?action=edit&roleid=' . $role->id
,
236 'edit', $stredit, get_string('editxrole', 'role', $role->localname
));
238 $row[3] .= get_action_icon($defineurl . '?action=duplicate&roleid=' . $role->id
,
239 'copy', $strduplicate, get_string('createrolebycopying', 'role', $role->localname
));
241 if (isset($undeletableroles[$role->id
])) {
242 $row[3] .= get_spacer();
244 $row[3] .= get_action_icon($baseurl . '?action=delete&roleid=' . $role->id
,
245 'delete', $strdelete, get_string('deletexrole', 'role', $role->localname
));
248 $table->data
[] = $row;
250 echo html_writer
::table($table);
252 echo $OUTPUT->container_start('buttons');
253 echo $OUTPUT->single_button(new moodle_url($defineurl, array('action' => 'add')), get_string('addrole', 'role'), 'get');
254 echo $OUTPUT->container_end();
256 echo $OUTPUT->footer();
259 function get_action_icon($url, $icon, $alt, $tooltip) {
261 return '<a title="' . $tooltip . '" href="'. $url . '">' .
262 '<img src="' . $OUTPUT->pix_url('t/' . $icon) . '" class="iconsmall" alt="' . $alt . '" /></a> ';
264 function get_spacer() {
266 return '<img src="' . $OUTPUT->pix_url('spacer') . '" class="iconsmall" alt="" /> ';