source: moodle/trunk/fuentes/admin/tool/unsuproles/index.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 5.1 KB
Line 
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 * Report of unsupported role assignments,
19 * unsupported role assignments can be dropped from here.
20 *
21 * @package    tool
22 * @subpackage unsuproles
23 * @copyright  2010 Petr Skoda {@link http://skodak.org}
24 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require_once(dirname(__FILE__) . '/../../../config.php');
28require_once($CFG->libdir.'/adminlib.php');
29
30$action = optional_param('action', '', PARAM_ALPHANUMEXT);
31
32$syscontext = context_system::instance();
33
34require_login();
35admin_externalpage_setup('toolunsuproles'); // checks permissions specified in settings.php
36
37if ($action === 'delete') {
38    $contextlevel = required_param('contextlevel', PARAM_INT);
39    $roleid       = required_param('roleid', PARAM_INT);
40    $confirm      = optional_param('confirm', 0, PARAM_BOOL);
41
42    $role = $DB->get_record('role', array('id'=>$roleid), '*', MUST_EXIST);
43
44    if ($confirm and confirm_sesskey()) {
45        $sql = "SELECT ra.*
46                  FROM {role_assignments} ra
47                  JOIN {context} c ON c.id = ra.contextid
48             LEFT JOIN {role_context_levels} rcl ON (rcl.roleid = ra.roleid AND rcl.contextlevel = c.contextlevel)
49                 WHERE rcl.id IS NULL AND ra.roleid = :roleid AND c.contextlevel = :contextlevel";
50        $ras = $DB->get_records_sql($sql, array('roleid'=>$roleid, 'contextlevel'=>$contextlevel));
51        foreach ($ras as $ra) {
52            if (!empty($ra->component)) {
53                //bad luck, we can not mess with plugin ras!
54                //TODO: explain why not possible to remove ras
55                continue;
56            }
57            role_unassign($ra->roleid, $ra->userid, $ra->contextid);
58        }
59        redirect($PAGE->url);
60    }
61    //show confirmation
62    echo $OUTPUT->header();
63    $yesurl = new moodle_url($PAGE->url, array('roleid'=>$roleid, 'contextlevel'=>$contextlevel, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey()));
64    $levelname = context_helper::get_level_name($contextlevel);
65    $rolename = format_string($role->name);
66    $message = get_string('confirmdelete', 'tool_unsuproles', array('level'=>$levelname, 'role'=>$rolename));
67    echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
68    echo $OUTPUT->footer();
69    die();
70}
71
72
73echo $OUTPUT->header();
74echo $OUTPUT->heading(get_string('pluginname', 'tool_unsuproles'));
75
76$sql = "SELECT r.id AS roleid, c.contextlevel, r.sortorder, COUNT(ra.id) AS racount
77          FROM {role} r
78          JOIN {role_assignments} ra ON ra.roleid = r.id
79          JOIN {context} c ON c.id = ra.contextid
80     LEFT JOIN {role_context_levels} rcl ON (rcl.roleid = r.id AND rcl.contextlevel = c.contextlevel)
81         WHERE rcl.id IS NULL
82      GROUP BY r.id, c.contextlevel, r.sortorder
83      ORDER BY c.contextlevel ASC, r.sortorder ASC";
84//print the overview table
85
86$problems = array();
87$rs = $DB->get_recordset_sql($sql);
88foreach ($rs as $problem) {
89    $problems[] = $problem;
90}
91$rs->close();
92
93if (!$problems) {
94    echo $OUTPUT->notification(get_string('noprolbems', 'tool_unsuproles'), 'notifysuccess');
95} else {
96    $roles = get_all_roles();
97    $data = array();
98    foreach ($problems as $problem) {
99        $levelname = context_helper::get_level_name($problem->contextlevel);
100        $rolename = role_get_name($roles[$problem->roleid]);
101        //TODO: show list of users if count low
102        $count = $problem->racount;
103        $edit = array();
104        $aurl = new moodle_url('/admin/roles/define.php', array('roleid'=>$problem->roleid, 'action'=>'edit'));
105        $edit[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/edit'), 'alt'=>get_string('edit'), 'class'=>'smallicon')));
106        $aurl = new moodle_url($PAGE->url, array('roleid'=>$problem->roleid, 'contextlevel'=>$problem->contextlevel, 'action'=>'delete'));
107        $edit[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/delete'), 'alt'=>get_string('delete'), 'class'=>'smallicon')));
108        $data[] = array($levelname, $rolename, $count, implode('&nbsp;', $edit));
109    }
110    $table = new html_table();
111    $table->head  = array(get_string('contextlevel', 'tool_unsuproles'), get_string('role'), get_string('count', 'tool_unsuproles'), get_string('edit'));
112    $table->size  = array('40%', '40%', '10%', '10%');
113    $table->align = array('left', 'left', 'center', 'center');
114    $table->width = '90%';
115    $table->data  = $data;
116    echo html_writer::table($table);
117}
118
119echo $OUTPUT->footer();
Note: See TracBrowser for help on using the repository browser.