Merge branch 'install_master' of https://git.in.moodle.com/amosbot/moodle-install
[moodle.git] / admin / roles / manage.php
blob9da57eb5a147b37dd1d559be18ca63db137c64f9
1 <?php
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.
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/>.
17 /**
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.
29 * @package core_role
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(__DIR__ . '/../../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);
39 if ($action) {
40 $roleid = required_param('roleid', PARAM_INT);
41 } else {
42 $roleid = 0;
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();
51 require_capability('moodle/role:manage', $systemcontext);
52 admin_externalpage_setup('defineroles');
54 // Get some basic data we are going to need.
55 $roles = role_fix_names(get_all_roles(), $systemcontext, ROLENAME_ORIGINAL);
57 $undeletableroles = array();
58 $undeletableroles[$CFG->notloggedinroleid] = 1;
59 $undeletableroles[$CFG->guestroleid] = 1;
60 $undeletableroles[$CFG->defaultuserroleid] = 1;
62 // Process submitted data.
63 $confirmed = (optional_param('confirm', false, PARAM_BOOL) && data_submitted() && confirm_sesskey());
64 switch ($action) {
65 case 'delete':
66 if (isset($undeletableroles[$roleid])) {
67 print_error('cannotdeletethisrole', '', $baseurl);
69 if (!$confirmed) {
70 // Show confirmation.
71 echo $OUTPUT->header();
72 $optionsyes = array('action'=>'delete', 'roleid'=>$roleid, 'sesskey'=>sesskey(), 'confirm'=>1);
73 $a = new stdClass();
74 $a->id = $roleid;
75 $a->name = $roles[$roleid]->name;
76 $a->shortname = $roles[$roleid]->shortname;
77 $a->count = $DB->count_records_select('role_assignments',
78 'roleid = ?', array($roleid), 'COUNT(DISTINCT userid)');
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', 'core_role', $a), $formcontinue, $formcancel);
83 echo $OUTPUT->footer();
84 die;
86 if (!delete_role($roleid)) {
87 // The delete failed.
88 print_error('cannotdeleterolewithid', 'error', $baseurl, $roleid);
90 // Deleted a role sitewide...
91 redirect($baseurl);
92 break;
94 case 'moveup':
95 if (confirm_sesskey()) {
96 $prevrole = null;
97 $thisrole = null;
98 foreach ($roles as $role) {
99 if ($role->id == $roleid) {
100 $thisrole = $role;
101 break;
102 } else {
103 $prevrole = $role;
106 if (is_null($thisrole) || is_null($prevrole)) {
107 print_error('cannotmoverolewithid', 'error', '', $roleid);
109 if (!switch_roles($thisrole, $prevrole)) {
110 print_error('cannotmoverolewithid', 'error', '', $roleid);
114 redirect($baseurl);
115 break;
117 case 'movedown':
118 if (confirm_sesskey()) {
119 $thisrole = null;
120 $nextrole = null;
121 foreach ($roles as $role) {
122 if ($role->id == $roleid) {
123 $thisrole = $role;
124 } else if (!is_null($thisrole)) {
125 $nextrole = $role;
126 break;
129 if (is_null($nextrole)) {
130 print_error('cannotmoverolewithid', 'error', '', $roleid);
132 if (!switch_roles($thisrole, $nextrole)) {
133 print_error('cannotmoverolewithid', 'error', '', $roleid);
137 redirect($baseurl);
138 break;
142 // Print the page header and tabs.
143 echo $OUTPUT->header();
145 $currenttab = 'manage';
146 require('managetabs.php');
148 // Initialise table.
149 $table = new html_table();
150 $table->colclasses = array('leftalign', 'leftalign', 'leftalign', 'leftalign');
151 $table->id = 'roles';
152 $table->attributes['class'] = 'admintable generaltable';
153 $table->head = array(
154 get_string('role') . ' ' . $OUTPUT->help_icon('roles', 'core_role'),
155 get_string('description'),
156 get_string('roleshortname', 'core_role'),
157 get_string('edit')
160 // Get some strings outside the loop.
161 $stredit = get_string('edit');
162 $strdelete = get_string('delete');
163 $strmoveup = get_string('moveup');
164 $strmovedown = get_string('movedown');
166 // Print a list of roles with edit/copy/delete/reorder icons.
167 $table->data = array();
168 $firstrole = reset($roles);
169 $lastrole = end($roles);
170 foreach ($roles as $role) {
171 // Basic data.
172 $row = array(
173 '<a href="' . $defineurl . '?action=view&amp;roleid=' . $role->id . '">' . $role->localname . '</a>',
174 role_get_description($role),
175 s($role->shortname),
179 // Move up.
180 if ($role->sortorder != $firstrole->sortorder) {
181 $row[3] .= get_action_icon($baseurl . '?action=moveup&amp;roleid=' . $role->id . '&amp;sesskey=' . sesskey(), 'up', $strmoveup, $strmoveup);
182 } else {
183 $row[3] .= get_spacer();
185 // Move down.
186 if ($role->sortorder != $lastrole->sortorder) {
187 $row[3] .= get_action_icon($baseurl . '?action=movedown&amp;roleid=' . $role->id . '&amp;sesskey=' . sesskey(), 'down', $strmovedown, $strmovedown);
188 } else {
189 $row[3] .= get_spacer();
191 // Edit.
192 $row[3] .= get_action_icon($defineurl . '?action=edit&amp;roleid=' . $role->id,
193 'edit', $stredit, get_string('editxrole', 'core_role', $role->localname));
194 // Delete.
195 if (isset($undeletableroles[$role->id])) {
196 $row[3] .= get_spacer();
197 } else {
198 $row[3] .= get_action_icon($baseurl . '?action=delete&amp;roleid=' . $role->id,
199 'delete', $strdelete, get_string('deletexrole', 'core_role', $role->localname));
202 $table->data[] = $row;
204 echo html_writer::table($table);
206 echo $OUTPUT->container_start('buttons');
207 echo $OUTPUT->single_button(new moodle_url($defineurl, array('action' => 'add')), get_string('addrole', 'core_role'), 'get');
208 echo $OUTPUT->container_end();
210 echo $OUTPUT->footer();
211 die;
213 function get_action_icon($url, $icon, $alt, $tooltip) {
214 global $OUTPUT;
215 return '<a title="' . $tooltip . '" href="'. $url . '">' .
216 $OUTPUT->pix_icon('t/' . $icon, $alt) . '</a> ';
218 function get_spacer() {
219 global $OUTPUT;
220 return $OUTPUT->spacer();