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\models;
  20  
  21  use context;
  22  use context_system;
  23  use core\persistent;
  24  use core_reportbuilder\event\report_created;
  25  use core_reportbuilder\event\report_deleted;
  26  use core_reportbuilder\event\report_updated;
  27  use core_reportbuilder\local\report\base;
  28  
  29  /**
  30   * Persistent class to represent a report
  31   *
  32   * @package     core_reportbuilder
  33   * @copyright   2018 Alberto Lara Hernández <albertolara@moodle.com>
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class report extends persistent {
  37  
  38      /** @var string The table name. */
  39      public const TABLE = 'reportbuilder_report';
  40  
  41      /**
  42       * Return the definition of the properties of this model
  43       *
  44       * @return array
  45       */
  46      protected static function define_properties(): array {
  47          return [
  48              'name' => [
  49                  'type' => PARAM_TEXT,
  50                  'null' => NULL_ALLOWED,
  51                  'default' => null,
  52              ],
  53              'source' => [
  54                  'type' => PARAM_RAW,
  55              ],
  56              'type' => [
  57                  'type' => PARAM_INT,
  58                  'choices' => [
  59                      base::TYPE_CUSTOM_REPORT,
  60                      base::TYPE_SYSTEM_REPORT,
  61                  ],
  62              ],
  63              'uniquerows' => [
  64                  'type' => PARAM_BOOL,
  65                  'default' => false,
  66              ],
  67              'conditiondata' => [
  68                  'type' => PARAM_RAW,
  69                  'null' => NULL_ALLOWED,
  70                  'default' => null,
  71              ],
  72              'settingsdata' => [
  73                  'type' => PARAM_RAW,
  74                  'null' => NULL_ALLOWED,
  75                  'default' => null,
  76              ],
  77              'contextid' => [
  78                  'type' => PARAM_INT,
  79                  'default' => static function(): int {
  80                      return context_system::instance()->id;
  81                  }
  82              ],
  83              'component' => [
  84                  'type' => PARAM_COMPONENT,
  85                  'default' => '',
  86              ],
  87              'area' => [
  88                  'type' => PARAM_AREA,
  89                  'default' => '',
  90              ],
  91              'itemid' => [
  92                  'type' => PARAM_INT,
  93                  'default' => 0,
  94              ],
  95              'usercreated' => [
  96                  'type' => PARAM_INT,
  97                  'default' => static function(): int {
  98                      global $USER;
  99  
 100                      return (int) $USER->id;
 101                  },
 102              ],
 103          ];
 104      }
 105  
 106      /**
 107       * Trigger report created event when persistent is created
 108       */
 109      protected function after_create(): void {
 110          if ($this->get('type') === base::TYPE_CUSTOM_REPORT) {
 111              report_created::create_from_object($this)->trigger();
 112          }
 113      }
 114  
 115      /**
 116       * Cascade report deletion, first deleting any linked persistents
 117       */
 118      protected function before_delete(): void {
 119          $reportparams = ['reportid' => $this->get('id')];
 120  
 121          // Columns.
 122          foreach (column::get_records($reportparams) as $column) {
 123              $column->delete();
 124          }
 125  
 126          // Filters.
 127          foreach (filter::get_records($reportparams) as $filter) {
 128              $filter->delete();
 129          }
 130  
 131          // Audiences.
 132          foreach (audience::get_records($reportparams) as $audience) {
 133              $audience->delete();
 134          }
 135  
 136          // Schedules.
 137          foreach (schedule::get_records($reportparams) as $schedule) {
 138              $schedule->delete();
 139          }
 140      }
 141  
 142      /**
 143       * Throw report deleted event when persistent is deleted
 144       *
 145       * @param bool $result
 146       */
 147      protected function after_delete($result): void {
 148          if (!$result || $this->get('type') === base::TYPE_SYSTEM_REPORT) {
 149              return;
 150          }
 151          report_deleted::create_from_object($this)->trigger();
 152      }
 153  
 154      /**
 155       * Throw report updated event when persistent is updated
 156       *
 157       * @param bool $result
 158       */
 159      protected function after_update($result): void {
 160          if (!$result || $this->get('type') === base::TYPE_SYSTEM_REPORT) {
 161              return;
 162          }
 163          report_updated::create_from_object($this)->trigger();
 164      }
 165  
 166      /**
 167       * Return report context, used by exporters
 168       *
 169       * @return context
 170       */
 171      public function get_context(): context {
 172          return context::instance_by_id($this->raw_get('contextid'));
 173      }
 174  
 175      /**
 176       * Return formatted report name
 177       *
 178       * @return string
 179       */
 180      public function get_formatted_name(): string {
 181          return format_string($this->raw_get('name'), true, ['context' => $this->get_context(), 'escape' => true]);
 182      }
 183  }