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.
   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\report;
  24  use moodle_url;
  25  
  26  /**
  27   * Report builder custom report updated 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   *      - string    name:      The name of the report
  37   *      - string    source:    The report source class
  38   * }
  39   */
  40  class report_updated extends base {
  41  
  42      /**
  43       * Initialise the event data.
  44       */
  45      protected function init() {
  46          $this->data['objecttable'] = report::TABLE;
  47          $this->data['crud'] = 'u';
  48          $this->data['edulevel'] = self::LEVEL_OTHER;
  49      }
  50  
  51      /**
  52       * Creates an instance from a report object
  53       *
  54       * @param report $report
  55       * @return self
  56       */
  57      public static function create_from_object(report $report): self {
  58          $eventparams = [
  59              'context'  => $report->get_context(),
  60              'objectid' => $report->get('id'),
  61              'other' => [
  62                  'name'     => $report->get('name'),
  63                  'source'   => $report->get('source'),
  64              ]
  65          ];
  66          $event = self::create($eventparams);
  67          $event->add_record_snapshot($event->objecttable, $report->to_record());
  68          return $event;
  69      }
  70  
  71      /**
  72       * Returns localised general event name.
  73       *
  74       * @return string
  75       */
  76      public static function get_name() {
  77          return get_string('reportupdated', 'core_reportbuilder');
  78      }
  79  
  80      /**
  81       * Returns non-localised description of what happened.
  82       *
  83       * @return string
  84       */
  85      public function get_description() {
  86          return "The user with id '$this->userid' updated the custom report with id '$this->objectid'.";
  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      }
 100  
 101      /**
 102       * Returns relevant URL.
 103       *
 104       * @return moodle_url
 105       */
 106      public function get_url(): moodle_url {
 107          return new moodle_url('/reportbuilder/edit.php', ['id' => $this->objectid]);
 108      }
 109  }