[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/user/filters/ -> select.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   * Value select 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   * Generic filter based on a list of values.
  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_select extends user_filter_type {
  34      /**
  35       * options for the list values
  36       * @var array
  37       */
  38      public $_options;
  39  
  40      /** @var string */
  41      public $_field;
  42  
  43      /** @var mixed|null */
  44      public $_default;
  45  
  46      /**
  47       * Constructor
  48       * @param string $name the name of the filter instance
  49       * @param string $label the label of the filter instance
  50       * @param boolean $advanced advanced form element flag
  51       * @param string $field user table filed name
  52       * @param array $options select options
  53       * @param mixed $default option
  54       */
  55      public function user_filter_select($name, $label, $advanced, $field, $options, $default=null) {
  56          parent::user_filter_type($name, $label, $advanced);
  57          $this->_field   = $field;
  58          $this->_options = $options;
  59          $this->_default = $default;
  60      }
  61  
  62      /**
  63       * Returns an array of comparison operators
  64       * @return array of comparison operators
  65       */
  66      public function get_operators() {
  67          return array(0 => get_string('isanyvalue', 'filters'),
  68                       1 => get_string('isequalto', 'filters'),
  69                       2 => get_string('isnotequalto', 'filters'));
  70      }
  71  
  72      /**
  73       * Adds controls specific to this filter in the form.
  74       * @param moodleform $mform a MoodleForm object to setup
  75       */
  76      public function setupForm(&$mform) {
  77          $objs = array();
  78          $objs[] = $mform->createElement('select', $this->_name.'_op', null, $this->get_operators());
  79          $objs[] = $mform->createElement('select', $this->_name, null, $this->_options);
  80          $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
  81          $mform->disabledIf($this->_name, $this->_name.'_op', 'eq', 0);
  82          if (!is_null($this->_default)) {
  83              $mform->setDefault($this->_name, $this->_default);
  84          }
  85          if ($this->_advanced) {
  86              $mform->setAdvanced($this->_name.'_grp');
  87          }
  88      }
  89  
  90      /**
  91       * Retrieves data from the form data
  92       * @param stdClass $formdata data submited with the form
  93       * @return mixed array filter data or false when filter not set
  94       */
  95      public function check_data($formdata) {
  96          $field    = $this->_name;
  97          $operator = $field.'_op';
  98  
  99          if (array_key_exists($field, $formdata) and !empty($formdata->$operator)) {
 100              return array('operator' => (int)$formdata->$operator,
 101                           'value'    => (string)$formdata->$field);
 102          }
 103  
 104          return false;
 105      }
 106  
 107      /**
 108       * Returns the condition to be used with SQL where
 109       * @param array $data filter settings
 110       * @return array sql string and $params
 111       */
 112      public function get_sql_filter($data) {
 113          static $counter = 0;
 114          $name = 'ex_select'.$counter++;
 115  
 116          $operator = $data['operator'];
 117          $value    = $data['value'];
 118          $field    = $this->_field;
 119  
 120          $params = array();
 121  
 122          switch($operator) {
 123              case 1: // Equal to.
 124                  $res = "=:$name";
 125                  $params[$name] = $value;
 126                  break;
 127              case 2: // Not equal to.
 128                  $res = "<>:$name";
 129                  $params[$name] = $value;
 130                   break;
 131              default:
 132                  return array('', array());
 133          }
 134          return array($field.$res, $params);
 135      }
 136  
 137      /**
 138       * Returns a human friendly description of the filter used as label.
 139       * @param array $data filter settings
 140       * @return string active filter label
 141       */
 142      public function get_label($data) {
 143          $operators = $this->get_operators();
 144          $operator  = $data['operator'];
 145          $value     = $data['value'];
 146  
 147          if (empty($operator)) {
 148              return '';
 149          }
 150  
 151          $a = new stdClass();
 152          $a->label    = $this->_label;
 153          $a->value    = '"'.s($this->_options[$value]).'"';
 154          $a->operator = $operators[$operator];
 155  
 156          return get_string('selectlabel', 'filters', $a);
 157      }
 158  }
 159  


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