Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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   * Global role filter
  19   *
  20   * @package   core_user
  21   * @category  user
  22   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once($CFG->dirroot.'/user/filters/lib.php');
  27  
  28  /**
  29   * User filter based on global roles.
  30   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  31   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class user_filter_globalrole extends user_filter_type {
  34  
  35      /**
  36       * Constructor
  37       * @param string $name the name of the filter instance
  38       * @param string $label the label of the filter instance
  39       * @param boolean $advanced advanced form element flag
  40       */
  41      public function __construct($name, $label, $advanced) {
  42          parent::__construct($name, $label, $advanced);
  43      }
  44  
  45      /**
  46       * Old syntax of class constructor. Deprecated in PHP7.
  47       *
  48       * @deprecated since Moodle 3.1
  49       */
  50      public function user_filter_globalrole($name, $label, $advanced) {
  51          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  52          self::__construct($name, $label, $advanced);
  53      }
  54  
  55      /**
  56       * Returns an array of available roles
  57       * @return array of availble roles
  58       */
  59      public function get_roles() {
  60          $context = context_system::instance();
  61          $roles = array(0 => get_string('anyrole', 'filters')) + get_assignable_roles($context);
  62          return $roles;
  63      }
  64  
  65      /**
  66       * Adds controls specific to this filter in the form.
  67       * @param object $mform a MoodleForm object to setup
  68       */
  69      public function setupForm(&$mform) {
  70          $obj =& $mform->addElement('select', $this->_name, $this->_label, $this->get_roles());
  71          $mform->setDefault($this->_name, 0);
  72          if ($this->_advanced) {
  73              $mform->setAdvanced($this->_name);
  74          }
  75      }
  76  
  77      /**
  78       * Retrieves data from the form data
  79       * @param object $formdata data submited with the form
  80       * @return mixed array filter data or false when filter not set
  81       */
  82      public function check_data($formdata) {
  83          $field = $this->_name;
  84  
  85          if (property_exists($formdata, $field) and !empty($formdata->$field)) {
  86              return array('value' => (int)$formdata->$field);
  87          }
  88          return false;
  89      }
  90  
  91      /**
  92       * Returns the condition to be used with SQL where
  93       * @param array $data filter settings
  94       * @return array sql string and $params
  95       */
  96      public function get_sql_filter($data) {
  97          global $CFG;
  98          $value = (int)$data['value'];
  99  
 100          $timenow = round(time(), 100);
 101  
 102          $sql = "id IN (SELECT userid
 103                           FROM {role_assignments} a
 104                          WHERE a.contextid=".SYSCONTEXTID." AND a.roleid=$value)";
 105          return array($sql, array());
 106      }
 107  
 108      /**
 109       * Returns a human friendly description of the filter used as label.
 110       * @param array $data filter settings
 111       * @return string active filter label
 112       */
 113      public function get_label($data) {
 114          global $DB;
 115  
 116          $role = $DB->get_record('role', array('id' => $data['value']));
 117  
 118          $a = new stdClass();
 119          $a->label = $this->_label;
 120          $a->value = '"'.role_get_name($role).'"';
 121  
 122          return get_string('globalrolelabel', 'filters', $a);
 123      }
 124  }