[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/user/filters/ -> checkbox.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   * Generic checkbox filter.
  19   *
  20   * This will create generic filter with checkbox option and can be used for
  21   * disabling other elements for specific condition.
  22   *
  23   * @package   core_user
  24   * @category  user
  25   * @copyright 2011 Rajesh Taneja
  26   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  require_once($CFG->dirroot.'/user/filters/lib.php');
  32  
  33  /**
  34   * Generic filter based for checkbox and can be used for disabling items
  35   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class user_filter_checkbox extends user_filter_type {
  39      /**
  40       * list of all the fields which needs to be disabled, if checkbox is checked
  41       * @var array
  42       */
  43      protected $disableelements = array();
  44  
  45      /**
  46       * name of user table field/fields on which data needs to be compared
  47       * @var mixed
  48       */
  49      protected $field;
  50  
  51      /**
  52       * Constructor, initalize user_filter_type and sets $disableelements array
  53       * with list of elements to be diabled by checkbox.
  54       *
  55       * @param string $name the name of the filter instance
  56       * @param string $label the label of the filter instance
  57       * @param boolean $advanced advanced form element flag
  58       * @param mixed $field user table field/fields name for comparison
  59       * @param array $disableelements name of fields which should be disabled if this checkbox is checked.
  60       */
  61      public function __construct($name, $label, $advanced, $field, $disableelements=null) {
  62          parent::__construct($name, $label, $advanced);
  63          $this->field   = $field;
  64          if (!empty($disableelements)) {
  65              if (!is_array($disableelements)) {
  66                  $this->disableelements = array($disableelements);
  67              } else {
  68                  $this->disableelements = $disableelements;
  69              }
  70          }
  71      }
  72  
  73      /**
  74       * Adds controls specific to this filter in the form.
  75       *
  76       * @param moodleform $mform a MoodleQuickForm object in which element will be added
  77       */
  78      public function setupForm(&$mform) {
  79          $objs = array();
  80  
  81          $objs[] = $mform->createElement('checkbox', $this->_name, null, '');
  82          $grp = $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
  83  
  84          if ($this->_advanced) {
  85              $mform->setAdvanced($this->_name.'_grp');
  86          }
  87          // Check if disable if options are set. if yes then set rules.
  88          if (!empty($this->disableelements) && is_array($this->disableelements)) {
  89              foreach ($this->disableelements as $disableelement) {
  90                  $mform->disabledIf($disableelement, $this->_name, 'checked');
  91              }
  92          }
  93      }
  94  
  95      /**
  96       * Retrieves data from the form data
  97       *
  98       * @param object $formdata data submited with the form
  99       * @return mixed array filter data or false when filter not set
 100       */
 101      public function check_data($formdata) {
 102          $field = $this->_name;
 103          // Check if disable if options are set. if yes then don't add this..
 104          if (!empty($this->disableelements) && is_array($this->disableelements)) {
 105              foreach ($this->disableelements as $disableelement) {
 106                  if (array_key_exists($disableelement, $formdata)) {
 107                      return false;
 108                  }
 109              }
 110          }
 111          if (array_key_exists($field, $formdata) and $formdata->$field !== '') {
 112              return array('value' => (string)$formdata->$field);
 113          }
 114          return false;
 115      }
 116  
 117      /**
 118       * Returns the condition to be used with SQL where
 119       *
 120       * @param array $data filter settings
 121       * @return array sql string and $params
 122       */
 123      public function get_sql_filter($data) {
 124          $field  = $this->field;
 125          if (is_array($field)) {
 126              $res = " {$field[0]} = {$field[1]} ";
 127          } else {
 128              $res = " {$field} = 0 ";
 129          }
 130          return array($res, array());
 131      }
 132  
 133      /**
 134       * Returns a human friendly description of the filter used as label.
 135       *
 136       * @param array $data filter settings
 137       * @return string active filter label
 138       */
 139      public function get_label($data) {
 140          return $this->_label;
 141      }
 142  }


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