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.
   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   * Boolean report filter
  27   *
  28   * This filter accepts an expression that evaluates to 1 or 0, either a simple field such as "u.suspended", or a more complex
  29   * expression such as "CASE WHEN <EXPRESSION> THEN 1 ELSE 0 END"
  30   *
  31   * @package     core_reportbuilder
  32   * @copyright   2021 Paul Holden <paulh@moodle.com>
  33   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class boolean_select extends base {
  36  
  37      /** @var int Any value */
  38      public const ANY_VALUE = 0;
  39  
  40      /** @var int Checked */
  41      public const CHECKED = 1;
  42  
  43      /** @var int Not checked */
  44      public const NOT_CHECKED = 2;
  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::ANY_VALUE => new lang_string('filterisanyvalue', 'core_reportbuilder'),
  54              self::CHECKED => new lang_string('yes'),
  55              self::NOT_CHECKED => new lang_string('no'),
  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          $operatorlabel = get_string('filterfieldoperator', 'core_reportbuilder', $this->get_header());
  68          $mform->addElement('select', "{$this->name}_operator", $operatorlabel, $this->get_operators())
  69              ->setHiddenLabel(true);
  70  
  71          $mform->setType("{$this->name}_operator", PARAM_INT);
  72          $mform->setDefault("{$this->name}_operator", self::ANY_VALUE);
  73      }
  74  
  75      /**
  76       * Return filter SQL
  77       *
  78       * @param array $values
  79       * @return array
  80       */
  81      public function get_sql_filter(array $values): array {
  82          $fieldsql = $this->filter->get_field_sql();
  83          $params = $this->filter->get_field_params();
  84  
  85          $paramname = database::generate_param_name();
  86  
  87          $operator = $values["{$this->name}_operator"] ?? self::ANY_VALUE;
  88          switch ($operator) {
  89              case self::CHECKED:
  90                  $fieldsql .= " = :{$paramname}";
  91                  $params[$paramname] = 1;
  92                  break;
  93              case self::NOT_CHECKED:
  94                  $fieldsql .= " = :{$paramname}";
  95                  $params[$paramname] = 0;
  96                  break;
  97              default:
  98                  // Invalid or inactive filter.
  99                  return ['', []];
 100          }
 101  
 102          return [$fieldsql, $params];
 103      }
 104  
 105      /**
 106       * Return sample filter values
 107       *
 108       * @return array
 109       */
 110      public function get_sample_values(): array {
 111          return [
 112              "{$this->name}_operator" => self::CHECKED,
 113          ];
 114      }
 115  }