Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  
  27  require_once(__DIR__ . '/../../../config.php');
  28  require_once($CFG->libdir.'/adminlib.php');
  29  
  30  $action = optional_param('action', '', PARAM_ALPHANUMEXT);
  31  
  32  
  33  admin_externalpage_setup('toolunsuproles'); // checks permissions specified in settings.php
  34  
  35  if ($action === 'delete') {
  36      $contextlevel = required_param('contextlevel', PARAM_INT);
  37      $roleid       = required_param('roleid', PARAM_INT);
  38      $confirm      = optional_param('confirm', 0, PARAM_BOOL);
  39  
  40      $role = $DB->get_record('role', array('id'=>$roleid), '*', MUST_EXIST);
  41  
  42      if ($confirm and confirm_sesskey()) {
  43          $sql = "SELECT ra.*
  44                    FROM {role_assignments} ra
  45                    JOIN {context} c ON c.id = ra.contextid
  46               LEFT JOIN {role_context_levels} rcl ON (rcl.roleid = ra.roleid AND rcl.contextlevel = c.contextlevel)
  47                   WHERE rcl.id IS NULL AND ra.roleid = :roleid AND c.contextlevel = :contextlevel";
  48          $ras = $DB->get_records_sql($sql, array('roleid'=>$roleid, 'contextlevel'=>$contextlevel));
  49          foreach ($ras as $ra) {
  50              if (!empty($ra->component)) {
  51                  //bad luck, we can not mess with plugin ras!
  52                  //TODO: explain why not possible to remove ras
  53                  continue;
  54              }
  55              role_unassign($ra->roleid, $ra->userid, $ra->contextid);
  56          }
  57          redirect($PAGE->url);
  58      }
  59      //show confirmation
  60      echo $OUTPUT->header();
  61      $yesurl = new moodle_url($PAGE->url, array('roleid'=>$roleid, 'contextlevel'=>$contextlevel, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey()));
  62      $levelname = context_helper::get_level_name($contextlevel);
  63      $rolename = format_string($role->name);
  64      $message = get_string('confirmdelete', 'tool_unsuproles', array('level'=>$levelname, 'role'=>$rolename));
  65      echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
  66      echo $OUTPUT->footer();
  67      die();
  68  }
  69  
  70  
  71  echo $OUTPUT->header();
  72  echo $OUTPUT->heading(get_string('pluginname', 'tool_unsuproles'));
  73  
  74  $sql = "SELECT r.id AS roleid, c.contextlevel, r.sortorder, COUNT(ra.id) AS racount
  75            FROM {role} r
  76            JOIN {role_assignments} ra ON ra.roleid = r.id
  77            JOIN {context} c ON c.id = ra.contextid
  78       LEFT JOIN {role_context_levels} rcl ON (rcl.roleid = r.id AND rcl.contextlevel = c.contextlevel)
  79           WHERE rcl.id IS NULL
  80        GROUP BY r.id, c.contextlevel, r.sortorder
  81        ORDER BY c.contextlevel ASC, r.sortorder ASC";
  82  //print the overview table
  83  
  84  $problems = array();
  85  $rs = $DB->get_recordset_sql($sql);
  86  foreach ($rs as $problem) {
  87      $problems[] = $problem;
  88  }
  89  $rs->close();
  90  
  91  if (!$problems) {
  92      echo $OUTPUT->notification(get_string('noprolbems', 'tool_unsuproles'), 'notifysuccess');
  93  } else {
  94      $roles = get_all_roles();
  95      $data = array();
  96      foreach ($problems as $problem) {
  97          $levelname = context_helper::get_level_name($problem->contextlevel);
  98          $rolename = role_get_name($roles[$problem->roleid]);
  99          //TODO: show list of users if count low
 100          $count = $problem->racount;
 101          $edit = array();
 102          $aurl = new moodle_url('/admin/roles/define.php', array('roleid'=>$problem->roleid, 'action'=>'edit'));
 103          $edit[] = html_writer::link($aurl, $OUTPUT->pix_icon('t/edit', get_string('edit')));
 104          $aurl = new moodle_url($PAGE->url, array('roleid'=>$problem->roleid, 'contextlevel'=>$problem->contextlevel, 'action'=>'delete'));
 105          $edit[] = html_writer::link($aurl, $OUTPUT->pix_icon('t/delete', get_string('delete')));
 106          $data[] = array($levelname, $rolename, $count, implode('&nbsp;', $edit));
 107      }
 108      $table = new html_table();
 109      $table->head  = array(get_string('contextlevel', 'tool_unsuproles'), get_string('role'), get_string('count', 'tool_unsuproles'), get_string('edit'));
 110      $table->size  = array('40%', '40%', '10%', '10%');
 111      $table->align = array('left', 'left', 'center', 'center');
 112      $table->width = '90%';
 113      $table->data  = $data;
 114      echo html_writer::table($table);
 115  }
 116  
 117  echo $OUTPUT->footer();