Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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   * Date 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 date.
  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_date extends user_filter_type {
  34      /**
  35       * the fields available for comparisson
  36       * @var string
  37       */
  38      public $_field;
  39  
  40      /**
  41       * Constructor
  42       * @param string $name the name of the filter instance
  43       * @param string $label the label of the filter instance
  44       * @param boolean $advanced advanced form element flag
  45       * @param string $field user table filed name
  46       */
  47      public function __construct($name, $label, $advanced, $field) {
  48          parent::__construct($name, $label, $advanced);
  49          $this->_field = $field;
  50      }
  51  
  52      /**
  53       * Old syntax of class constructor. Deprecated in PHP7.
  54       *
  55       * @deprecated since Moodle 3.1
  56       */
  57      public function user_filter_date($name, $label, $advanced, $field) {
  58          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  59          self::__construct($name, $label, $advanced, $field);
  60      }
  61  
  62      /**
  63       * Adds controls specific to this filter in the form.
  64       * @param object $mform a MoodleForm object to setup
  65       */
  66      public function setupForm(&$mform) {
  67          $objs = array();
  68  
  69          $objs[] = $mform->createElement('static', $this->_name.'_s1', null,
  70              html_writer::start_tag('div', array('class' => 'w-100 d-flex align-items-center')));
  71          $objs[] = $mform->createElement('static', $this->_name.'_s2', null,
  72              html_writer::tag('div', get_string('isafter', 'filters'), array('class' => 'mr-2')));
  73          $objs[] = $mform->createElement('date_selector', $this->_name.'_sdt', null, array('optional' => true));
  74          $objs[] = $mform->createElement('static', $this->_name.'_s3', null, html_writer::end_tag('div'));
  75          $objs[] = $mform->createElement('static', $this->_name.'_s4', null,
  76              html_writer::start_tag('div', array('class' => 'w-100 d-flex align-items-center')));
  77          $objs[] = $mform->createElement('static', $this->_name.'_s5', null,
  78              html_writer::tag('div', get_string('isbefore', 'filters'), array('class' => 'mr-2')));
  79          $objs[] = $mform->createElement('date_selector', $this->_name.'_edt', null, array('optional' => true));
  80          $objs[] = $mform->createElement('static', $this->_name.'_s6', null, html_writer::end_tag('div'));
  81  
  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      }
  88  
  89      /**
  90       * Retrieves data from the form data
  91       * @param object $formdata data submited with the form
  92       * @return mixed array filter data or false when filter not set
  93       */
  94      public function check_data($formdata) {
  95          $sdt = $this->_name.'_sdt';
  96          $edt = $this->_name.'_edt';
  97  
  98          if (!$formdata->$sdt and !$formdata->$edt) {
  99              return false;
 100          }
 101  
 102          $data = array();
 103          $data['after'] = $formdata->$sdt;
 104          $data['before'] = $formdata->$edt;
 105  
 106          return $data;
 107      }
 108  
 109      /**
 110       * Returns the condition to be used with SQL where
 111       * @param array $data filter settings
 112       * @return array sql string and $params
 113       */
 114      public function get_sql_filter($data) {
 115          $after  = (int)$data['after'];
 116          $before = (int)$data['before'];
 117  
 118          $field  = $this->_field;
 119  
 120          if (empty($after) and empty($before)) {
 121              return array('', array());
 122          }
 123  
 124          $res = " $field >= 0 ";
 125  
 126          if ($after) {
 127              $res .= " AND $field >= $after";
 128          }
 129  
 130          if ($before) {
 131              $res .= " AND $field <= $before";
 132          }
 133          return array($res, array());
 134      }
 135  
 136      /**
 137       * Returns a human friendly description of the filter used as label.
 138       * @param array $data filter settings
 139       * @return string active filter label
 140       */
 141      public function get_label($data) {
 142          $after  = $data['after'];
 143          $before = $data['before'];
 144          $field  = $this->_field;
 145  
 146          $a = new stdClass();
 147          $a->label  = $this->_label;
 148          $a->after  = userdate($after);
 149          $a->before = userdate($before);
 150  
 151          if ($after and $before) {
 152              return get_string('datelabelisbetween', 'filters', $a);
 153          } else if ($after) {
 154              return get_string('datelabelisafter', 'filters', $a);
 155          } else if ($before) {
 156              return get_string('datelabelisbefore', 'filters', $a);
 157          }
 158          return '';
 159      }
 160  }