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 400 and 401]

   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\models;
  20  
  21  use context;
  22  use lang_string;
  23  use core\persistent;
  24  use core_reportbuilder\datasource;
  25  
  26  /**
  27   * Persistent class to represent a report filter/condition
  28   *
  29   * @package     core_reportbuilder
  30   * @copyright   2021 Paul Holden <paulh@moodle.com>
  31   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class filter extends persistent {
  34  
  35      /** @var string The table name. */
  36      public const TABLE = 'reportbuilder_filter';
  37  
  38      /**
  39       * Return the definition of the properties of this model.
  40       *
  41       * @return array
  42       */
  43      protected static function define_properties(): array {
  44          return [
  45              'reportid' => [
  46                  'type' => PARAM_INT,
  47              ],
  48              'uniqueidentifier' => [
  49                  'type' => PARAM_RAW,
  50              ],
  51              'heading' => [
  52                  'type' => PARAM_TEXT,
  53                  'null' => NULL_ALLOWED,
  54                  'default' => null,
  55              ],
  56              'iscondition' => [
  57                  'type' => PARAM_BOOL,
  58                  'default' => false,
  59              ],
  60              'filterorder' => [
  61                  'type' => PARAM_INT,
  62              ],
  63              'usercreated' => [
  64                  'type' => PARAM_INT,
  65                  'default' => static function(): int {
  66                      global $USER;
  67  
  68                      return (int) $USER->id;
  69                  },
  70              ],
  71          ];
  72      }
  73  
  74      /**
  75       * Validate reportid property
  76       *
  77       * @param int $reportid
  78       * @return bool|lang_string
  79       */
  80      protected function validate_reportid(int $reportid) {
  81          if (!report::record_exists($reportid)) {
  82              return new lang_string('invaliddata', 'error');
  83          }
  84  
  85          return true;
  86      }
  87  
  88      /**
  89       * Ensure report source is notified of new filter
  90       */
  91      protected function after_create(): void {
  92          datasource::report_elements_modified($this->get('reportid'));
  93      }
  94  
  95      /**
  96       * Ensure report source is notified of updated filter
  97       *
  98       * @param bool $result
  99       */
 100      protected function after_update($result): void {
 101          if ($result) {
 102              datasource::report_elements_modified($this->get('reportid'));
 103          }
 104      }
 105  
 106      /**
 107       * Ensure report source is notified of deleted filter
 108       *
 109       * @param bool $result
 110       */
 111      protected function after_delete($result): void {
 112          if ($result) {
 113              datasource::report_elements_modified($this->get('reportid'));
 114          }
 115      }
 116  
 117      /**
 118       * Return the report this filter belongs to
 119       *
 120       * @return report
 121       */
 122      public function get_report(): report {
 123          return new report($this->get('reportid'));
 124      }
 125  
 126      /**
 127       * Return filter record
 128       *
 129       * @param int $reportid
 130       * @param int $filterid
 131       * @return false|static
 132       */
 133      public static function get_filter_record(int $reportid, int $filterid) {
 134          return self::get_record(['id' => $filterid, 'reportid' => $reportid, 'iscondition' => 0]);
 135      }
 136  
 137      /**
 138       * Return filter records for report
 139       *
 140       * @param int $reportid
 141       * @param string $sort
 142       * @param string $order
 143       * @return static[]
 144       */
 145      public static function get_filter_records(int $reportid, string $sort = '', string $order = 'ASC'): array {
 146          return self::get_records(['reportid' => $reportid, 'iscondition' => 0], $sort, $order);
 147      }
 148  
 149      /**
 150       * Return condition record
 151       *
 152       * @param int $reportid
 153       * @param int $conditionid
 154       * @return false|static
 155       */
 156      public static function get_condition_record(int $reportid, int $conditionid) {
 157          return self::get_record(['id' => $conditionid, 'reportid' => $reportid, 'iscondition' => 1]);
 158      }
 159  
 160      /**
 161       * Return condition records for report
 162       *
 163       * @param int $reportid
 164       * @param string $sort
 165       * @param string $order
 166       * @return static[]
 167       */
 168      public static function get_condition_records(int $reportid, string $sort = '', string $order = 'ASC'): array {
 169          return self::get_records(['reportid' => $reportid, 'iscondition' => 1], $sort, $order);
 170      }
 171  
 172      /**
 173       * Helper method to return the current maximum filter order value for a report
 174       *
 175       * @param int $reportid
 176       * @param bool $iscondition
 177       * @return int
 178       */
 179      public static function get_max_filterorder(int $reportid, bool $iscondition = false): int {
 180          global $DB;
 181  
 182          $params = ['reportid' => $reportid, 'iscondition' => (int) $iscondition];
 183  
 184          return (int) $DB->get_field(static::TABLE, "MAX(filterorder)", $params, MUST_EXIST);
 185      }
 186  
 187      /**
 188       * Return formatted filter heading
 189       *
 190       * @param context|null $context If the context of the report is already known, it should be passed here
 191       * @return string
 192       */
 193      public function get_formatted_heading(?context $context = null): string {
 194          if ($context === null) {
 195              $context = $this->get_report()->get_context();
 196          }
 197  
 198          return format_string($this->raw_get('heading'), true, ['context' => $context]);
 199      }
 200  }