[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/user/filters/ -> globalrole.php (source)

   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 user_filter_globalrole($name, $label, $advanced) {
  42          parent::user_filter_type($name, $label, $advanced);
  43      }
  44  
  45      /**
  46       * Returns an array of available roles
  47       * @return array of availble roles
  48       */
  49      public function get_roles() {
  50          $context = context_system::instance();
  51          $roles = array(0 => get_string('anyrole', 'filters')) + get_assignable_roles($context);
  52          return $roles;
  53      }
  54  
  55      /**
  56       * Adds controls specific to this filter in the form.
  57       * @param object $mform a MoodleForm object to setup
  58       */
  59      public function setupForm(&$mform) {
  60          $obj =& $mform->addElement('select', $this->_name, $this->_label, $this->get_roles());
  61          $mform->setDefault($this->_name, 0);
  62          if ($this->_advanced) {
  63              $mform->setAdvanced($this->_name);
  64          }
  65      }
  66  
  67      /**
  68       * Retrieves data from the form data
  69       * @param object $formdata data submited with the form
  70       * @return mixed array filter data or false when filter not set
  71       */
  72      public function check_data($formdata) {
  73          $field = $this->_name;
  74  
  75          if (array_key_exists($field, $formdata) and !empty($formdata->$field)) {
  76              return array('value' => (int)$formdata->$field);
  77          }
  78          return false;
  79      }
  80  
  81      /**
  82       * Returns the condition to be used with SQL where
  83       * @param array $data filter settings
  84       * @return array sql string and $params
  85       */
  86      public function get_sql_filter($data) {
  87          global $CFG;
  88          $value = (int)$data['value'];
  89  
  90          $timenow = round(time(), 100);
  91  
  92          $sql = "id IN (SELECT userid
  93                           FROM {role_assignments} a
  94                          WHERE a.contextid=".SYSCONTEXTID." AND a.roleid=$value)";
  95          return array($sql, array());
  96      }
  97  
  98      /**
  99       * Returns a human friendly description of the filter used as label.
 100       * @param array $data filter settings
 101       * @return string active filter label
 102       */
 103      public function get_label($data) {
 104          global $DB;
 105  
 106          $role = $DB->get_record('role', array('id' => $data['value']));
 107  
 108          $a = new stdClass();
 109          $a->label = $this->_label;
 110          $a->value = '"'.role_get_name($role).'"';
 111  
 112          return get_string('globalrolelabel', 'filters', $a);
 113      }
 114  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1