Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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  /**
  18   * Provides {@link tool_policy\event\acceptance_base} class.
  19   *
  20   * @package     tool_policy
  21   * @copyright   2018 Marina Glancy
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_policy\event;
  26  
  27  use core\event\base;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * Base class for acceptance_created and acceptance_updated events.
  33   *
  34   * @package     tool_policy
  35   * @copyright   2018 Marina Glancy
  36   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  abstract class acceptance_base extends base {
  39  
  40      /**
  41       * Initialise the event.
  42       */
  43      protected function init() {
  44          $this->data['objecttable'] = 'tool_policy_acceptances';
  45          $this->data['edulevel'] = self::LEVEL_OTHER;
  46      }
  47  
  48      /**
  49       * Create event from record.
  50       *
  51       * @param stdClass $record
  52       * @return acceptance_created
  53       */
  54      public static function create_from_record($record) {
  55          $event = static::create([
  56              'objectid' => $record->id,
  57              'relateduserid' => $record->userid,
  58              'context' => \context_user::instance($record->userid),
  59              'other' => [
  60                  'policyversionid' => $record->policyversionid,
  61                  'note' => $record->note,
  62                  'status' => $record->status,
  63              ],
  64          ]);
  65          $event->add_record_snapshot($event->objecttable, $record);
  66          return $event;
  67      }
  68  
  69      /**
  70       * Get URL related to the action.
  71       *
  72       * @return \moodle_url
  73       */
  74      public function get_url() {
  75          return new \moodle_url('/admin/tool/policy/acceptance.php', array('userid' => $this->relateduserid,
  76              'versionid' => $this->other['policyversionid']));
  77      }
  78  
  79      /**
  80       * Get the object ID mapping.
  81       *
  82       * @return array
  83       */
  84      public static function get_objectid_mapping() {
  85          return array('db' => 'tool_policy', 'restore' => \core\event\base::NOT_MAPPED);
  86      }
  87  
  88      /**
  89       * Custom validation.
  90       *
  91       * @throws \coding_exception
  92       */
  93      protected function validate_data() {
  94          parent::validate_data();
  95  
  96          if (empty($this->other['policyversionid'])) {
  97              throw new \coding_exception('The \'policyversionid\' value must be set');
  98          }
  99  
 100          if (!isset($this->other['status'])) {
 101              throw new \coding_exception('The \'status\' value must be set');
 102          }
 103  
 104          if (empty($this->relateduserid)) {
 105              throw new \coding_exception('The \'relateduserid\' must be set.');
 106          }
 107      }
 108  
 109      /**
 110       * No mapping required for this event because this event is not backed up.
 111       *
 112       * @return bool
 113       */
 114      public static function get_other_mapping() {
 115          return false;
 116      }
 117  }