Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 403]

   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  declare(strict_types=1);
  18  
  19  namespace core_reportbuilder\local\filters;
  20  
  21  use lang_string;
  22  use MoodleQuickForm;
  23  use core_reportbuilder\local\helpers\database;
  24  
  25  /**
  26   * Duration report filter
  27   *
  28   * This filter accepts a number of seconds to perform filtering on
  29   *
  30   * @package     core_reportbuilder
  31   * @copyright   2021 Paul Holden <paulh@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class duration extends base {
  35  
  36      /** @var int Any value */
  37      public const DURATION_ANY = 0;
  38  
  39      /** @var int Maximum duration */
  40      public const DURATION_MAXIMUM = 1;
  41  
  42      /** @var int Minimum duration */
  43      public const DURATION_MINIMUM = 2;
  44  
  45  
  46      /**
  47       * Return an array of operators available for this filter
  48       *
  49       * @return lang_string[]
  50       */
  51      private function get_operators(): array {
  52          $operators = [
  53              self::DURATION_ANY => new lang_string('filterisanyvalue', 'core_reportbuilder'),
  54              self::DURATION_MAXIMUM => new lang_string('filterlessthan', 'core_reportbuilder'),
  55              self::DURATION_MINIMUM => new lang_string('filtergreaterthan', 'core_reportbuilder'),
  56          ];
  57  
  58          return $this->filter->restrict_limited_operators($operators);
  59      }
  60  
  61      /**
  62       * Setup form
  63       *
  64       * @param MoodleQuickForm $mform
  65       */
  66      public function setup_form(MoodleQuickForm $mform): void {
  67          $elements = [];
  68  
  69          // Operator.
  70          $operatorlabel = get_string('filterfieldoperator', 'core_reportbuilder', $this->get_header());
  71  
  72          $elements[] = $mform->createElement('select', "{$this->name}_operator", $operatorlabel, $this->get_operators());
  73          $mform->setType("{$this->name}_operator", PARAM_INT);
  74          $mform->setDefault("{$this->name}_operator", self::DURATION_ANY);
  75  
  76          // Value.
  77          $valuelabel = get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header());
  78  
  79          $elements[] = $mform->createElement('text', "{$this->name}_value", $valuelabel, ['size' => 3]);
  80          $mform->setType("{$this->name}_value", PARAM_FLOAT);
  81          $mform->setDefault("{$this->name}_value", 0);
  82          $mform->hideIf("{$this->name}_value", "{$this->name}_operator", 'eq', self::DURATION_ANY);
  83  
  84          // Unit.
  85          $unitlabel = get_string('filterdurationunit', 'core_reportbuilder', $this->get_header());
  86          $units = [
  87              1 => get_string('filterdateseconds', 'core_reportbuilder'),
  88              MINSECS => get_string('filterdateminutes', 'core_reportbuilder'),
  89              HOURSECS => get_string('filterdatehours', 'core_reportbuilder'),
  90              DAYSECS => get_string('filterdatedays', 'core_reportbuilder'),
  91          ];
  92  
  93          $elements[] = $mform->createElement('select', "{$this->name}_unit", $unitlabel, $units);
  94          $mform->setType("{$this->name}_unit", PARAM_INT);
  95          $mform->setDefault("{$this->name}_unit", 1);
  96          $mform->hideIf("{$this->name}_unit", "{$this->name}_operator", 'eq', self::DURATION_ANY);
  97  
  98          $mform->addGroup($elements, "{$this->name}_group", '', '', false);
  99      }
 100  
 101      /**
 102       * Return filter SQL
 103       *
 104       * @param array $values
 105       * @return array
 106       */
 107      public function get_sql_filter(array $values): array {
 108          $fieldsql = $this->filter->get_field_sql();
 109          $params = $this->filter->get_field_params();
 110  
 111          $durationvalue = unformat_float($values["{$this->name}_value"] ?? 0);
 112          $durationunit = (int) ($values["{$this->name}_unit"] ?? 0);
 113  
 114          $operator = $values["{$this->name}_operator"] ?? self::DURATION_ANY;
 115          switch ($operator) {
 116              case self::DURATION_MAXIMUM:
 117                  $paramduration = database::generate_param_name();
 118  
 119                  $sql = "{$fieldsql} <= :{$paramduration}";
 120                  $params[$paramduration] = $durationvalue * $durationunit;
 121  
 122                  break;
 123              case self::DURATION_MINIMUM:
 124                  $paramduration = database::generate_param_name();
 125  
 126                  $sql = "{$fieldsql} >= :{$paramduration}";
 127                  $params[$paramduration] = $durationvalue * $durationunit;
 128  
 129                  break;
 130              default:
 131                  // Invalid or inactive filter.
 132                  return ['', []];
 133          }
 134  
 135          return [$sql, $params];
 136      }
 137  
 138      /**
 139       * Return sample filter values
 140       *
 141       * @return array
 142       */
 143      public function get_sample_values(): array {
 144          return [
 145              "{$this->name}_operator" => self::DURATION_MAXIMUM,
 146              "{$this->name}_value" => 2,
 147              "{$this->name}_unit" => MINSECS,
 148          ];
 149      }
 150  }