Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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