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  /**
  18   * User profile field created event.
  19   *
  20   * @package    core
  21   * @copyright  2017 Web Courseworks, Ltd. {@link http://www.webcourseworks.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\event;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * User profile info field created event.
  31   *
  32   * @property-read array $other {
  33   *      Extra information about event.
  34   *
  35   *      - string shortname: the shortname of the field.
  36   *      - string name: the name of the field.
  37   *      - string datatype: the data type of the field.
  38   * }
  39   *
  40   * @package    core
  41   * @copyright  2017 Web Courseworks, Ltd. {@link http://www.webcourseworks.com}
  42   * @since      Moodle 3.4
  43   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class user_info_field_created extends base {
  46  
  47      /**
  48       * Initialise the event data.
  49       */
  50      protected function init() {
  51          $this->data['objecttable'] = 'user_info_field';
  52          $this->data['crud'] = 'c';
  53          $this->data['edulevel'] = self::LEVEL_OTHER;
  54      }
  55  
  56      /**
  57       * Creates an event from a profile field.
  58       *
  59       * @since Moodle 3.4
  60       * @param \stdClass $field A snapshot of the created field.
  61       * @return \core\event\base
  62       */
  63      public static function create_from_field($field) {
  64          $event = self::create(array(
  65              'objectid' => $field->id,
  66              'context' => \context_system::instance(),
  67              'other' => array(
  68                  'shortname' => $field->shortname,
  69                  'name'      => $field->name,
  70                  'datatype'  => $field->datatype,
  71              )
  72          ));
  73  
  74          $event->add_record_snapshot('user_info_field', $field);
  75  
  76          return $event;
  77      }
  78  
  79      /**
  80       * Returns localised event name.
  81       *
  82       * @return string
  83       */
  84      public static function get_name() {
  85          return get_string('eventuserinfofieldcreated');
  86      }
  87  
  88      /**
  89       * Returns non-localised event description with id's for admin use only.
  90       *
  91       * @return string
  92       */
  93      public function get_description() {
  94          $name = s($this->other['name']);
  95          return "The user with id '$this->userid' created the user profile field '$name' with id '$this->objectid'.";
  96      }
  97  
  98      /**
  99       * Returns relevant URL.
 100       *
 101       * @return \moodle_url
 102       */
 103      public function get_url() {
 104          return new \moodle_url('/user/profile/index.php', array(
 105              'action' => 'editfield',
 106              'id' => $this->objectid,
 107              'datatype' => $this->other['datatype']
 108          ));
 109      }
 110  
 111      /**
 112       * Custom validation.
 113       *
 114       * @throws \coding_exception
 115       * @return void
 116       */
 117      protected function validate_data() {
 118          parent::validate_data();
 119  
 120          if (!isset($this->other['shortname'])) {
 121              throw new \coding_exception('The \'shortname\' value must be set in other.');
 122          }
 123  
 124          if (!isset($this->other['name'])) {
 125              throw new \coding_exception('The \'name\' value must be set in other.');
 126          }
 127  
 128          if (!isset($this->other['datatype'])) {
 129              throw new \coding_exception('The \'datatype\' value must be set in other.');
 130          }
 131      }
 132  
 133      /**
 134       * Get the backup/restore table mapping for this event.
 135       *
 136       * @return string
 137       */
 138      public static function get_objectid_mapping() {
 139          return base::NOT_MAPPED;
 140      }
 141  }