Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 402] [Versions 39 and 403]

   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 login event.
  19   *
  20   * @package    core
  21   * @copyright  2013 Frédéric Massart
  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 login event class.
  31   *
  32   * @property-read array $other {
  33   *      Extra information about event.
  34   *
  35   *      - string username: name of user.
  36   * }
  37   *
  38   * @package    core
  39   * @since      Moodle 2.6
  40   * @copyright  2013 Frédéric Massart
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class user_loggedin extends base {
  44  
  45      /**
  46       * Returns non-localised event description with id's for admin use only.
  47       *
  48       * @return string
  49       */
  50      public function get_description() {
  51          return "The user with id '$this->userid' has logged in.";
  52      }
  53  
  54      /**
  55       * Return legacy data for add_to_log().
  56       *
  57       * @return array
  58       */
  59      protected function get_legacy_logdata() {
  60          return array(SITEID, 'user', 'login', 'view.php?id=' . $this->data['objectid'] . '&course=' . SITEID,
  61              $this->data['objectid'], 0, $this->data['objectid']);
  62      }
  63  
  64      /**
  65       * Return localised event name.
  66       *
  67       * @return string
  68       */
  69      public static function get_name() {
  70          return get_string('eventuserloggedin', 'auth');
  71      }
  72  
  73      /**
  74       * Get URL related to the action.
  75       *
  76       * @return \moodle_url
  77       */
  78      public function get_url() {
  79          return new \moodle_url('/user/profile.php', array('id' => $this->data['objectid']));
  80      }
  81  
  82      /**
  83       * Return the username of the logged in user.
  84       *
  85       * @return string
  86       */
  87      public function get_username() {
  88          return $this->other['username'];
  89      }
  90  
  91      /**
  92       * Init method.
  93       *
  94       * @return void
  95       */
  96      protected function init() {
  97          $this->context = \context_system::instance();
  98          $this->data['crud'] = 'r';
  99          $this->data['edulevel'] = self::LEVEL_OTHER;
 100          $this->data['objecttable'] = 'user';
 101      }
 102  
 103      /**
 104       * Custom validation.
 105       *
 106       * @throws \coding_exception when validation does not pass.
 107       * @return void
 108       */
 109      protected function validate_data() {
 110          parent::validate_data();
 111  
 112          if (!isset($this->other['username'])) {
 113              throw new \coding_exception('The \'username\' value must be set in other.');
 114          }
 115      }
 116  
 117      public static function get_objectid_mapping() {
 118          return array('db' => 'user', 'restore' => 'user');
 119      }
 120  
 121      public static function get_other_mapping() {
 122          return false;
 123      }
 124  }