Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.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 core_reportbuilder\event\audience_created;
  23  use core_reportbuilder\event\audience_deleted;
  24  use core_reportbuilder\event\audience_updated;
  25  use lang_string;
  26  use core\persistent;
  27  use core_reportbuilder\local\helpers\audience as helper;
  28  
  29  /**
  30   * Persistent class to represent a report audience
  31   *
  32   * @package     core_reportbuilder
  33   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class audience extends persistent {
  37  
  38      /** @var string Table name */
  39      public const TABLE = 'reportbuilder_audience';
  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              'reportid' => [
  49                  'type' => PARAM_INT,
  50              ],
  51              'heading' => [
  52                  'type' => PARAM_TEXT,
  53                  'null' => NULL_ALLOWED,
  54                  'default' => null,
  55              ],
  56              'classname' => [
  57                  'type' => PARAM_TEXT,
  58              ],
  59              'configdata' => [
  60                  'type' => PARAM_RAW,
  61                  'default' => '{}',
  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       * Hook to execute after creation
  90       */
  91      protected function after_create(): void {
  92          audience_created::create_from_object($this)->trigger();
  93          helper::purge_caches();
  94      }
  95  
  96      /**
  97       * Hook to execute after update
  98       *
  99       * @param bool $result
 100       */
 101      protected function after_update($result): void {
 102          if ($result) {
 103              audience_updated::create_from_object($this)->trigger();
 104              helper::purge_caches();
 105          }
 106      }
 107  
 108      /**
 109       * Hook to execute after deletion
 110       *
 111       * @param bool $result
 112       */
 113      protected function after_delete($result): void {
 114          if ($result) {
 115              audience_deleted::create_from_object($this)->trigger();
 116              helper::purge_caches();
 117          }
 118      }
 119  
 120      /**
 121       * Return the report this audience belongs to
 122       *
 123       * @return report
 124       */
 125      public function get_report(): report {
 126          return new report($this->get('reportid'));
 127      }
 128  
 129      /**
 130       * Return formatted audience heading
 131       *
 132       * @param context|null $context If the context of the report is already known, it should be passed here
 133       * @return string
 134       */
 135      public function get_formatted_heading(?context $context = null): string {
 136          if ($context === null) {
 137              $context = $this->get_report()->get_context();
 138          }
 139  
 140          return format_string($this->raw_get('heading'), true, ['context' => $context]);
 141      }
 142  }