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 column
  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 column extends persistent {
  33  
  34      /** @var string The table name. */
  35      public const TABLE = 'reportbuilder_column';
  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              'aggregation' => [
  51                  'type' => PARAM_ALPHANUMEXT,
  52                  'default' => null,
  53                  'null' => NULL_ALLOWED,
  54              ],
  55              'heading' => [
  56                  'type' => PARAM_TEXT,
  57                  'default' => null,
  58                  'null' => NULL_ALLOWED,
  59              ],
  60              'columnorder' => [
  61                  'type' => PARAM_INT,
  62              ],
  63              'sortenabled' => [
  64                  'type' => PARAM_BOOL,
  65                  'default' => false,
  66              ],
  67              'sortdirection' => [
  68                  'type' => PARAM_INT,
  69                  'choices' => [SORT_ASC, SORT_DESC],
  70                  'default' => SORT_ASC,
  71              ],
  72              'sortorder' => [
  73                  'type' => PARAM_INT,
  74                  'default' => null,
  75                  'null' => NULL_ALLOWED,
  76              ],
  77              'usercreated' => [
  78                  'type' => PARAM_INT,
  79                  'default' => static function(): int {
  80                      global $USER;
  81  
  82                      return (int) $USER->id;
  83                  },
  84              ],
  85          ];
  86      }
  87  
  88      /**
  89       * Validate reportid property
  90       *
  91       * @param int $reportid
  92       * @return bool|lang_string
  93       */
  94      protected function validate_reportid(int $reportid) {
  95          if (!report::record_exists($reportid)) {
  96              return new lang_string('invaliddata', 'error');
  97          }
  98  
  99          return true;
 100      }
 101  
 102      /**
 103       * Return the report this column belongs to
 104       *
 105       * @return report
 106       */
 107      public function get_report(): report {
 108          return new report($this->get('reportid'));
 109      }
 110  
 111      /**
 112       * Helper method to return the current maximum column order value for a report
 113       *
 114       * @param int $reportid
 115       * @param string $columnname
 116       * @return int
 117       */
 118      public static function get_max_columnorder(int $reportid, string $columnname): int {
 119          global $DB;
 120  
 121          return (int) $DB->get_field(static::TABLE, "MAX({$columnname})", ['reportid' => $reportid], MUST_EXIST);
 122      }
 123  
 124      /**
 125       * Return formatted column name
 126       *
 127       * @param context|null $context If the context of the report is already known, it should be passed here
 128       * @return string
 129       */
 130      public function get_formatted_heading(?context $context = null): string {
 131          if ($context === null) {
 132              $context = $this->get_report()->get_context();
 133          }
 134  
 135          return format_string($this->raw_get('heading'), true, ['context' => $context]);
 136      }
 137  }