Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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\event;
  20  
  21  use coding_exception;
  22  use core\event\base;
  23  use core_reportbuilder\local\models\audience;
  24  use moodle_url;
  25  
  26  /**
  27   * Report builder custom report audience created event class.
  28   *
  29   * @package     core_reportbuilder
  30   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  31   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   *
  33   * @property-read array $other {
  34   *      Extra information about the event.
  35   *
  36   *      - int    reportid:      The id of the report
  37   * }
  38   */
  39  class audience_updated extends base {
  40  
  41      /**
  42       * Initialise the event data.
  43       */
  44      protected function init() {
  45          $this->data['objecttable'] = audience::TABLE;
  46          $this->data['crud'] = 'u';
  47          $this->data['edulevel'] = self::LEVEL_OTHER;
  48      }
  49  
  50      /**
  51       * Creates an instance from a report audience object
  52       *
  53       * @param audience $audience
  54       * @return self
  55       */
  56      public static function create_from_object(audience $audience): self {
  57          $eventparams = [
  58              'context'  => $audience->get_report()->get_context(),
  59              'objectid' => $audience->get('id'),
  60              'other' => [
  61                  'reportid' => $audience->get('reportid'),
  62              ]
  63          ];
  64          $event = self::create($eventparams);
  65          $event->add_record_snapshot($event->objecttable, $audience->to_record());
  66          return $event;
  67      }
  68  
  69      /**
  70       * Returns localised general event name.
  71       *
  72       * @return string
  73       */
  74      public static function get_name() {
  75          return get_string('audienceupdated', 'core_reportbuilder');
  76      }
  77  
  78      /**
  79       * Returns non-localised description of what happened.
  80       *
  81       * @return string
  82       */
  83      public function get_description() {
  84          $reportid = $this->other['reportid'];
  85          return "The user with id '$this->userid' updated the audience with id '$this->objectid' in the custom report" .
  86              " with id '$reportid'.";
  87      }
  88  
  89      /**
  90       * Custom validations.
  91       *
  92       * @throws coding_exception
  93       */
  94      protected function validate_data(): void {
  95          parent::validate_data();
  96          if (!isset($this->objectid)) {
  97              throw new coding_exception('The \'objectid\' must be set.');
  98          }
  99          if (!isset($this->other['reportid'])) {
 100              throw new coding_exception('The \'reportid\' must be set in other.');
 101          }
 102      }
 103  
 104      /**
 105       * Returns relevant URL.
 106       *
 107       * @return moodle_url
 108       */
 109      public function get_url(): moodle_url {
 110          return new moodle_url('/reportbuilder/edit.php', ['id' => $this->other['reportid']], 'audience');
 111      }
 112  }