forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.php
224 lines (200 loc) · 7.79 KB
/
manage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Lets the user define and edit roles.
*
* Responds to actions:
* [blank] - list roles.
* delete - delete a role (with are-you-sure)
* moveup - change the sort order
* movedown - change the sort order
*
* For all but the first two of those, you also need a roleid parameter, and
* possibly some other data.
*
* @package core_role
* @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot . '/' . $CFG->admin . '/roles/lib.php');
$action = optional_param('action', '', PARAM_ALPHA);
if ($action) {
$roleid = required_param('roleid', PARAM_INT);
} else {
$roleid = 0;
}
// Get the base URL for this and related pages into a convenient variable.
$baseurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/manage.php';
$defineurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/define.php';
// Check access permissions.
$systemcontext = context_system::instance();
require_login();
require_capability('moodle/role:manage', $systemcontext);
admin_externalpage_setup('defineroles');
// Get some basic data we are going to need.
$roles = role_fix_names(get_all_roles(), $systemcontext, ROLENAME_ORIGINAL);
$undeletableroles = array();
$undeletableroles[$CFG->notloggedinroleid] = 1;
$undeletableroles[$CFG->guestroleid] = 1;
$undeletableroles[$CFG->defaultuserroleid] = 1;
// Process submitted data.
$confirmed = (optional_param('confirm', false, PARAM_BOOL) && data_submitted() && confirm_sesskey());
switch ($action) {
case 'delete':
if (isset($undeletableroles[$roleid])) {
print_error('cannotdeletethisrole', '', $baseurl);
}
if (!$confirmed) {
// Show confirmation.
echo $OUTPUT->header();
$optionsyes = array('action'=>'delete', 'roleid'=>$roleid, 'sesskey'=>sesskey(), 'confirm'=>1);
$a = new stdClass();
$a->id = $roleid;
$a->name = $roles[$roleid]->name;
$a->shortname = $roles[$roleid]->shortname;
$a->count = $DB->count_records_select('role_assignments',
'roleid = ?', array($roleid), 'COUNT(DISTINCT userid)');
$formcontinue = new single_button(new moodle_url($baseurl, $optionsyes), get_string('yes'));
$formcancel = new single_button(new moodle_url($baseurl), get_string('no'), 'get');
echo $OUTPUT->confirm(get_string('deleterolesure', 'core_role', $a), $formcontinue, $formcancel);
echo $OUTPUT->footer();
die;
}
if (!delete_role($roleid)) {
// The delete failed, but mark the context dirty in case.
$systemcontext->mark_dirty();
print_error('cannotdeleterolewithid', 'error', $baseurl, $roleid);
}
// Deleted a role sitewide...
$systemcontext->mark_dirty();
redirect($baseurl);
break;
case 'moveup':
if (confirm_sesskey()) {
$prevrole = null;
$thisrole = null;
foreach ($roles as $role) {
if ($role->id == $roleid) {
$thisrole = $role;
break;
} else {
$prevrole = $role;
}
}
if (is_null($thisrole) || is_null($prevrole)) {
print_error('cannotmoverolewithid', 'error', '', $roleid);
}
if (!switch_roles($thisrole, $prevrole)) {
print_error('cannotmoverolewithid', 'error', '', $roleid);
}
}
redirect($baseurl);
break;
case 'movedown':
if (confirm_sesskey()) {
$thisrole = null;
$nextrole = null;
foreach ($roles as $role) {
if ($role->id == $roleid) {
$thisrole = $role;
} else if (!is_null($thisrole)) {
$nextrole = $role;
break;
}
}
if (is_null($nextrole)) {
print_error('cannotmoverolewithid', 'error', '', $roleid);
}
if (!switch_roles($thisrole, $nextrole)) {
print_error('cannotmoverolewithid', 'error', '', $roleid);
}
}
redirect($baseurl);
break;
}
// Print the page header and tabs.
echo $OUTPUT->header();
$currenttab = 'manage';
require('managetabs.php');
// Initialise table.
$table = new html_table();
$table->colclasses = array('leftalign', 'leftalign', 'leftalign', 'leftalign');
$table->id = 'roles';
$table->attributes['class'] = 'admintable generaltable';
$table->head = array(
get_string('role') . ' ' . $OUTPUT->help_icon('roles', 'core_role'),
get_string('description'),
get_string('roleshortname', 'core_role'),
get_string('edit')
);
// Get some strings outside the loop.
$stredit = get_string('edit');
$strdelete = get_string('delete');
$strmoveup = get_string('moveup');
$strmovedown = get_string('movedown');
// Print a list of roles with edit/copy/delete/reorder icons.
$table->data = array();
$firstrole = reset($roles);
$lastrole = end($roles);
foreach ($roles as $role) {
// Basic data.
$row = array(
'<a href="' . $defineurl . '?action=view&roleid=' . $role->id . '">' . $role->localname . '</a>',
role_get_description($role),
s($role->shortname),
'',
);
// Move up.
if ($role->sortorder != $firstrole->sortorder) {
$row[3] .= get_action_icon($baseurl . '?action=moveup&roleid=' . $role->id . '&sesskey=' . sesskey(), 'up', $strmoveup, $strmoveup);
} else {
$row[3] .= get_spacer();
}
// Move down.
if ($role->sortorder != $lastrole->sortorder) {
$row[3] .= get_action_icon($baseurl . '?action=movedown&roleid=' . $role->id . '&sesskey=' . sesskey(), 'down', $strmovedown, $strmovedown);
} else {
$row[3] .= get_spacer();
}
// Edit.
$row[3] .= get_action_icon($defineurl . '?action=edit&roleid=' . $role->id,
'edit', $stredit, get_string('editxrole', 'core_role', $role->localname));
// Delete.
if (isset($undeletableroles[$role->id])) {
$row[3] .= get_spacer();
} else {
$row[3] .= get_action_icon($baseurl . '?action=delete&roleid=' . $role->id,
'delete', $strdelete, get_string('deletexrole', 'core_role', $role->localname));
}
$table->data[] = $row;
}
echo html_writer::table($table);
echo $OUTPUT->container_start('buttons');
echo $OUTPUT->single_button(new moodle_url($defineurl, array('action' => 'add')), get_string('addrole', 'core_role'), 'get');
echo $OUTPUT->container_end();
echo $OUTPUT->footer();
die;
function get_action_icon($url, $icon, $alt, $tooltip) {
global $OUTPUT;
return '<a title="' . $tooltip . '" href="'. $url . '">' .
'<img src="' . $OUTPUT->pix_url('t/' . $icon) . '" class="iconsmall" alt="' . $alt . '" /></a> ';
}
function get_spacer() {
global $OUTPUT;
return '<img src="' . $OUTPUT->pix_url('spacer') . '" class="iconsmall" alt="" /> ';
}