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 define and edit roles.
20 * Responds to actions:
21 * [blank] - list roles.
22 * delete - delete a role (with are-you-sure)
23 * moveup - change the sort order
24 * movedown - change the sort order
26 * For all but the first two of those, you also need a roleid parameter, and
27 * possibly some other data.
30 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 require_once(dirname(__FILE__
) . '/../../config.php');
35 require_once($CFG->libdir
.'/adminlib.php');
36 require_once($CFG->dirroot
. '/' . $CFG->admin
. '/roles/lib.php');
38 $action = optional_param('action', '', PARAM_ALPHA
);
40 $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_select('role_assignments',
79 'roleid = ?', array($roleid), 'COUNT(DISTINCT userid)');
81 $formcontinue = new single_button(new moodle_url($baseurl, $optionsyes), get_string('yes'));
82 $formcancel = new single_button(new moodle_url($baseurl), get_string('no'), 'get');
83 echo $OUTPUT->confirm(get_string('deleterolesure', 'core_role', $a), $formcontinue, $formcancel);
84 echo $OUTPUT->footer();
87 if (!delete_role($roleid)) {
88 // The delete failed, but mark the context dirty in case.
89 $systemcontext->mark_dirty();
90 print_error('cannotdeleterolewithid', 'error', $baseurl, $roleid);
92 // Deleted a role sitewide...
93 $systemcontext->mark_dirty();
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);
145 // Print the page header and tabs.
146 echo $OUTPUT->header();
148 $currenttab = 'manage';
149 require('managetabs.php');
152 $table = new html_table();
153 $table->colclasses
= array('leftalign', 'leftalign', 'leftalign', 'leftalign');
154 $table->id
= 'roles';
155 $table->attributes
['class'] = 'admintable generaltable';
156 $table->head
= array(
157 get_string('role') . ' ' . $OUTPUT->help_icon('roles', 'core_role'),
158 get_string('description'),
159 get_string('roleshortname', 'core_role'),
163 // Get some strings outside the loop.
164 $stredit = get_string('edit');
165 $strdelete = get_string('delete');
166 $strmoveup = get_string('moveup');
167 $strmovedown = get_string('movedown');
169 // Print a list of roles with edit/copy/delete/reorder icons.
170 $table->data
= array();
171 $firstrole = reset($roles);
172 $lastrole = end($roles);
173 foreach ($roles as $role) {
176 '<a href="' . $defineurl . '?action=view&roleid=' . $role->id
. '">' . $role->localname
. '</a>',
177 role_get_description($role),
183 if ($role->sortorder
!= $firstrole->sortorder
) {
184 $row[3] .= get_action_icon($baseurl . '?action=moveup&roleid=' . $role->id
. '&sesskey=' . sesskey(), 'up', $strmoveup, $strmoveup);
186 $row[3] .= get_spacer();
189 if ($role->sortorder
!= $lastrole->sortorder
) {
190 $row[3] .= get_action_icon($baseurl . '?action=movedown&roleid=' . $role->id
. '&sesskey=' . sesskey(), 'down', $strmovedown, $strmovedown);
192 $row[3] .= get_spacer();
195 $row[3] .= get_action_icon($defineurl . '?action=edit&roleid=' . $role->id
,
196 'edit', $stredit, get_string('editxrole', 'core_role', $role->localname
));
198 if (isset($undeletableroles[$role->id
])) {
199 $row[3] .= get_spacer();
201 $row[3] .= get_action_icon($baseurl . '?action=delete&roleid=' . $role->id
,
202 'delete', $strdelete, get_string('deletexrole', 'core_role', $role->localname
));
205 $table->data
[] = $row;
207 echo html_writer
::table($table);
209 echo $OUTPUT->container_start('buttons');
210 echo $OUTPUT->single_button(new moodle_url($defineurl, array('action' => 'add')), get_string('addrole', 'core_role'), 'get');
211 echo $OUTPUT->container_end();
213 echo $OUTPUT->footer();
216 function get_action_icon($url, $icon, $alt, $tooltip) {
218 return '<a title="' . $tooltip . '" href="'. $url . '">' .
219 '<img src="' . $OUTPUT->pix_url('t/' . $icon) . '" class="iconsmall" alt="' . $alt . '" /></a> ';
221 function get_spacer() {
223 return '<img src="' . $OUTPUT->pix_url('spacer') . '" class="iconsmall" alt="" /> ';