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.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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 created event.
  19   *
  20   * @package    core
  21   * @copyright  2013 Rajesh Taneja <rajesh@moodle.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   * Event when new user profile is created.
  31   *
  32   * @package    core
  33   * @since      Moodle 2.6
  34   * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class user_created extends base {
  38  
  39      /**
  40       * Initialise required event data properties.
  41       */
  42      protected function init() {
  43          $this->data['objecttable'] = 'user';
  44          $this->data['crud'] = 'c';
  45          $this->data['edulevel'] = self::LEVEL_OTHER;
  46      }
  47  
  48      /**
  49       * Returns localised event name.
  50       *
  51       * @return string
  52       */
  53      public static function get_name() {
  54          return get_string('eventusercreated');
  55      }
  56  
  57      /**
  58       * Returns non-localised event description with id's for admin use only.
  59       *
  60       * @return string
  61       */
  62      public function get_description() {
  63          $description = "The user with id '$this->userid' created the user with id '$this->objectid'";
  64          if (!empty($this->other['restoreid'])) {
  65              $courseid = $this->other['courseid'] ?? 0;
  66              return "{$description} during restore of the course with id '$courseid'.";
  67          }
  68          return "{$description}.";
  69      }
  70  
  71      /**
  72       * Returns relevant URL.
  73       *
  74       * @return \moodle_url
  75       */
  76      public function get_url() {
  77          return new \moodle_url('/user/view.php', array('id' => $this->objectid));
  78      }
  79  
  80      /**
  81       * Custom validation.
  82       *
  83       * @throws \coding_exception
  84       * @return void
  85       */
  86      protected function validate_data() {
  87          parent::validate_data();
  88  
  89          if (!isset($this->relateduserid)) {
  90              debugging('The \'relateduserid\' value must be specified in the event.', DEBUG_DEVELOPER);
  91              $this->relateduserid = $this->objectid;
  92          }
  93      }
  94  
  95      /**
  96       * Create instance of event.
  97       *
  98       * @since Moodle 2.6.4, 2.7.1
  99       *
 100       * @param int $userid id of user
 101       * @return user_created
 102       */
 103      public static function create_from_userid($userid) {
 104          $data = array(
 105              'objectid' => $userid,
 106              'relateduserid' => $userid,
 107              'context' => \context_user::instance($userid)
 108          );
 109  
 110          // Create user_created event.
 111          $event = self::create($data);
 112          return $event;
 113      }
 114  
 115      /**
 116       * Create instance of event when user is created during the course restore process.
 117       *
 118       * @param int $userid id of user
 119       * @param string $restoreid
 120       * @param int $courseid
 121       * @return user_created
 122       */
 123      public static function create_from_user_id_on_restore(int $userid, string $restoreid,
 124              int $courseid): user_created {
 125          $data = [
 126              'objectid' => $userid,
 127              'relateduserid' => $userid,
 128              'context' => \context_user::instance($userid),
 129              'other' => ['restoreid' => $restoreid, 'courseid' => $courseid],
 130          ];
 131  
 132          // Create user_created event.
 133          return self::create($data);
 134      }
 135  
 136      public static function get_objectid_mapping() {
 137          return array('db' => 'user', 'restore' => 'user');
 138      }
 139  }