MDL-8249 localise standard role names and descriptions if empty
[moodle.git] / admin / roles / manage.php
blob85b1c0b240be53dac66de788336d3a9c4a597927
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
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.
31 * @package core
32 * @subpackage role
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);
41 if ($action) {
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 = get_context_instance(CONTEXT_SYSTEM);
51 require_login();
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();
65 switch ($action) {
66 case 'delete':
67 if (isset($undeletableroles[$roleid])) {
68 print_error('cannotdeletethisrole', '', $baseurl);
70 if (!$confirmed) {
71 // show confirmation
72 echo $OUTPUT->header();
73 $optionsyes = array('action'=>'delete', 'roleid'=>$roleid, 'sesskey'=>sesskey(), 'confirm'=>1);
74 $a = new stdClass();
75 $a->id = $roleid;
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();
84 die;
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);
94 redirect($baseurl);
95 break;
97 case 'moveup':
98 if (confirm_sesskey()) {
99 $prevrole = null;
100 $thisrole = null;
101 foreach ($roles as $role) {
102 if ($role->id == $roleid) {
103 $thisrole = $role;
104 break;
105 } else {
106 $prevrole = $role;
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);
117 redirect($baseurl);
118 break;
120 case 'movedown':
121 if (confirm_sesskey()) {
122 $thisrole = null;
123 $nextrole = null;
124 foreach ($roles as $role) {
125 if ($role->id == $roleid) {
126 $thisrole = $role;
127 } else if (!is_null($thisrole)) {
128 $nextrole = $role;
129 break;
132 if (is_null($nextrole)) {
133 print_error('cannotmoverolewithid', 'error', '', $roleid);
135 if (!switch_roles($thisrole, $nextrole)) {
136 print_error('cannotmoverolewithid', 'error', '', $roleid);
140 redirect($baseurl);
141 break;
143 case 'reset':
144 if (!$confirmed) {
145 // show confirmation
146 echo $OUTPUT->header();
147 $optionsyes = array('action'=>'reset', 'roleid'=>$roleid, 'sesskey'=>sesskey(), 'confirm'=>1);
148 $optionsno = array('action'=>'view', 'roleid'=>$roleid);
149 $a = new stdClass();
150 $a->id = $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);
156 } else {
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();
163 die;
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);
178 break;
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'),
197 get_string('edit')
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) {
213 /// Basic data.
214 $row = array(
215 '<a href="' . $defineurl . '?action=view&amp;roleid=' . $role->id . '">' . $role->localname . '</a>',
216 role_get_description($role),
217 s($role->shortname),
221 /// Icons:
222 // move up
223 if ($role->sortorder != $firstrole->sortorder) {
224 $row[3] .= get_action_icon($baseurl . '?action=moveup&amp;roleid=' . $role->id . '&amp;sesskey=' . sesskey(), 'up', $strmoveup, $strmoveup);
225 } else {
226 $row[3] .= get_spacer();
228 // move down
229 if ($role->sortorder != $lastrole->sortorder) {
230 $row[3] .= get_action_icon($baseurl . '?action=movedown&amp;roleid=' . $role->id . '&amp;sesskey=' . sesskey(), 'down', $strmovedown, $strmovedown);
231 } else {
232 $row[3] .= get_spacer();
234 // edit
235 $row[3] .= get_action_icon($defineurl . '?action=edit&amp;roleid=' . $role->id,
236 'edit', $stredit, get_string('editxrole', 'role', $role->localname));
237 // duplicate
238 $row[3] .= get_action_icon($defineurl . '?action=duplicate&amp;roleid=' . $role->id,
239 'copy', $strduplicate, get_string('createrolebycopying', 'role', $role->localname));
240 // delete
241 if (isset($undeletableroles[$role->id])) {
242 $row[3] .= get_spacer();
243 } else {
244 $row[3] .= get_action_icon($baseurl . '?action=delete&amp;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();
257 die;
259 function get_action_icon($url, $icon, $alt, $tooltip) {
260 global $OUTPUT;
261 return '<a title="' . $tooltip . '" href="'. $url . '">' .
262 '<img src="' . $OUTPUT->pix_url('t/' . $icon) . '" class="iconsmall" alt="' . $alt . '" /></a> ';
264 function get_spacer() {
265 global $OUTPUT;
266 return '<img src="' . $OUTPUT->pix_url('spacer') . '" class="iconsmall" alt="" /> ';