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.
13//
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/>.
16
17/**
18 * Lets the user define and edit roles.
19 *
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
25 *
26 * For all but the first two of those, you also need a roleid parameter, and
27 * possibly some other data.
28 *
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
32 */
33
34require_once(__DIR__ . '/../../config.php');
35require_once($CFG->libdir.'/adminlib.php');
36require_once($CFG->dirroot . '/' . $CFG->admin . '/roles/lib.php');
37
38$action = optional_param('action', '', PARAM_ALPHA);
39if ($action) {
40    $roleid = required_param('roleid', PARAM_INT);
41} else {
42    $roleid = 0;
43}
44
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';
48
49admin_externalpage_setup('defineroles');
50
51// Check access permissions.
52$systemcontext = context_system::instance();
53require_capability('moodle/role:manage', $systemcontext);
54
55// Get some basic data we are going to need.
56$roles = role_fix_names(get_all_roles(), $systemcontext, ROLENAME_ORIGINAL);
57
58$undeletableroles = array();
59$undeletableroles[$CFG->notloggedinroleid] = 1;
60$undeletableroles[$CFG->guestroleid] = 1;
61$undeletableroles[$CFG->defaultuserroleid] = 1;
62
63// Process submitted data.
64$confirmed = (optional_param('confirm', false, PARAM_BOOL) && data_submitted() && confirm_sesskey());
65switch ($action) {
66    case 'delete':
67        if (isset($undeletableroles[$roleid])) {
68            print_error('cannotdeletethisrole', '', $baseurl);
69        }
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_select('role_assignments',
79                'roleid = ?', array($roleid), 'COUNT(DISTINCT userid)');
80
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();
85            die;
86        }
87        if (!delete_role($roleid)) {
88            // The delete failed.
89            print_error('cannotdeleterolewithid', 'error', $baseurl, $roleid);
90        }
91        // Deleted a role sitewide...
92        redirect($baseurl);
93        break;
94
95    case 'moveup':
96        if (confirm_sesskey()) {
97            $prevrole = null;
98            $thisrole = null;
99            foreach ($roles as $role) {
100                if ($role->id == $roleid) {
101                    $thisrole = $role;
102                    break;
103                } else {
104                    $prevrole = $role;
105                }
106            }
107            if (is_null($thisrole) || is_null($prevrole)) {
108                print_error('cannotmoverolewithid', 'error', '', $roleid);
109            }
110            if (!switch_roles($thisrole, $prevrole)) {
111                print_error('cannotmoverolewithid', 'error', '', $roleid);
112            }
113        }
114
115        redirect($baseurl);
116        break;
117
118    case 'movedown':
119        if (confirm_sesskey()) {
120            $thisrole = null;
121            $nextrole = null;
122            foreach ($roles as $role) {
123                if ($role->id == $roleid) {
124                    $thisrole = $role;
125                } else if (!is_null($thisrole)) {
126                    $nextrole = $role;
127                    break;
128                }
129            }
130            if (is_null($nextrole)) {
131                print_error('cannotmoverolewithid', 'error', '', $roleid);
132            }
133            if (!switch_roles($thisrole, $nextrole)) {
134                print_error('cannotmoverolewithid', 'error', '', $roleid);
135            }
136        }
137
138        redirect($baseurl);
139        break;
140
141}
142
143// Print the page header and tabs.
144echo $OUTPUT->header();
145
146$currenttab = 'manage';
147require('managetabs.php');
148
149// Initialise table.
150$table = new html_table();
151$table->colclasses = array('leftalign', 'leftalign', 'leftalign', 'leftalign');
152$table->id = 'roles';
153$table->attributes['class'] = 'admintable generaltable';
154$table->head = array(
155    get_string('role') . ' ' . $OUTPUT->help_icon('roles', 'core_role'),
156    get_string('description'),
157    get_string('roleshortname', 'core_role'),
158    get_string('edit')
159);
160
161// Get some strings outside the loop.
162$stredit = get_string('edit');
163$strdelete = get_string('delete');
164$strmoveup = get_string('moveup');
165$strmovedown = get_string('movedown');
166
167// Print a list of roles with edit/copy/delete/reorder icons.
168$table->data = array();
169$firstrole = reset($roles);
170$lastrole = end($roles);
171foreach ($roles as $role) {
172    // Basic data.
173    $row = array(
174        '<a href="' . $defineurl . '?action=view&amp;roleid=' . $role->id . '">' . $role->localname . '</a>',
175        role_get_description($role),
176        s($role->shortname),
177        '',
178    );
179
180    // Move up.
181    if ($role->sortorder != $firstrole->sortorder) {
182        $row[3] .= get_action_icon($baseurl . '?action=moveup&amp;roleid=' . $role->id . '&amp;sesskey=' . sesskey(), 'up', $strmoveup, $strmoveup);
183    } else {
184        $row[3] .= get_spacer();
185    }
186    // Move down.
187    if ($role->sortorder != $lastrole->sortorder) {
188        $row[3] .= get_action_icon($baseurl . '?action=movedown&amp;roleid=' . $role->id . '&amp;sesskey=' . sesskey(), 'down', $strmovedown, $strmovedown);
189    } else {
190        $row[3] .= get_spacer();
191    }
192    // Edit.
193    $row[3] .= get_action_icon($defineurl . '?action=edit&amp;roleid=' . $role->id,
194            'edit', $stredit, get_string('editxrole', 'core_role', $role->localname));
195    // Delete.
196    if (isset($undeletableroles[$role->id])) {
197        $row[3] .= get_spacer();
198    } else {
199        $row[3] .= get_action_icon($baseurl . '?action=delete&amp;roleid=' . $role->id,
200              'delete', $strdelete, get_string('deletexrole', 'core_role', $role->localname));
201    }
202
203    $table->data[] = $row;
204}
205echo html_writer::table($table);
206
207echo $OUTPUT->container_start('buttons');
208echo $OUTPUT->single_button(new moodle_url($defineurl, array('action' => 'add')), get_string('addrole', 'core_role'), 'get');
209echo $OUTPUT->container_end();
210
211echo $OUTPUT->footer();
212die;
213
214function get_action_icon($url, $icon, $alt, $tooltip) {
215    global $OUTPUT;
216    return '<a title="' . $tooltip . '" href="'. $url . '">' .
217            $OUTPUT->pix_icon('t/' . $icon, $alt) . '</a> ';
218}
219function get_spacer() {
220    global $OUTPUT;
221    return $OUTPUT->spacer();
222}
223