Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 402] [Versions 310 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   * Moodle user analysable
  19   *
  20   * @package   core_analytics
  21   * @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_analytics;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Moodle user analysable
  31   *
  32   * @package   core_analytics
  33   * @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class user implements \core_analytics\analysable {
  37  
  38      /**
  39       * @var bool Has this user data been already loaded.
  40       */
  41      protected $loaded = false;
  42  
  43      /**
  44       * @var int $cachedid self::$cachedinstance analysable id.
  45       */
  46      protected static $cachedid = 0;
  47  
  48      /**
  49       * @var \core_analytics\user $cachedinstance
  50       */
  51      protected static $cachedinstance = null;
  52  
  53      /**
  54       * User object
  55       *
  56       * @var \stdClass
  57       */
  58      protected $user = null;
  59  
  60      /**
  61       * The user context.
  62       *
  63       * @var \context_user
  64       */
  65      protected $usercontext = null;
  66  
  67      /**
  68       * Constructor.
  69       *
  70       * Use self::instance() instead to get cached copies of the class. Instances obtained
  71       * through this constructor will not be cached.
  72       *
  73       * @param int|\stdClass $user User id
  74       * @param \context|null $context
  75       * @return void
  76       */
  77      public function __construct($user, ?\context $context = null) {
  78  
  79          if (is_scalar($user)) {
  80              $this->user = new \stdClass();
  81              $this->user->id = $user;
  82          } else {
  83              $this->user = $user;
  84          }
  85  
  86          if (!is_null($context)) {
  87              $this->usercontext = $context;
  88          }
  89      }
  90  
  91      /**
  92       * Returns an analytics user instance.
  93       *
  94       * Lazy load of analysable data.
  95       *
  96       * @param int|\stdClass $user User object or user id
  97       * @param \context|null $context
  98       * @return \core_analytics\user
  99       */
 100      public static function instance($user, ?\context $context = null) {
 101  
 102          $userid = $user;
 103          if (!is_scalar($userid)) {
 104              $userid = $user->id;
 105          }
 106  
 107          if (self::$cachedid === $userid) {
 108              return self::$cachedinstance;
 109          }
 110  
 111          $cachedinstance = new \core_analytics\user($user, $context);
 112          self::$cachedinstance = $cachedinstance;
 113          self::$cachedid = (int)$userid;
 114          return self::$cachedinstance;
 115      }
 116  
 117      /**
 118       * get_id
 119       *
 120       * @return int
 121       */
 122      public function get_id() {
 123          return $this->user->id;
 124      }
 125  
 126      /**
 127       * Loads the analytics user object.
 128       *
 129       * @return void
 130       */
 131      protected function load() {
 132  
 133          // The instance constructor could be already loaded with the full user object. Using email
 134          // because it is a required user field.
 135          if (empty($this->user->email)) {
 136              $this->user = \core_user::get_user($this->user->id);
 137          }
 138  
 139          $this->usercontext = $this->get_context();
 140  
 141          $this->now = time();
 142  
 143          // Flag the instance as loaded.
 144          $this->loaded = true;
 145      }
 146  
 147      /**
 148       * The user full name.
 149       *
 150       * @return string
 151       */
 152      public function get_name() {
 153  
 154          if (!$this->loaded) {
 155              $this->load();
 156          }
 157          return fullname($this->user);
 158      }
 159  
 160      /**
 161       * get_context
 162       *
 163       * @return \context
 164       */
 165      public function get_context() {
 166          if ($this->usercontext === null) {
 167              $this->usercontext = \context_user::instance($this->user->id);
 168          }
 169          return $this->usercontext;
 170      }
 171  
 172      /**
 173       * Get the start timestamp.
 174       *
 175       * @return int
 176       */
 177      public function get_start() {
 178  
 179          if (!$this->loaded) {
 180              $this->load();
 181          }
 182          return $this->user->timecreated;
 183      }
 184  
 185      /**
 186       * Get the end timestamp.
 187       *
 188       * @return int
 189       */
 190      public function get_end() {
 191          return self::MAX_TIME;
 192      }
 193  
 194      /**
 195       * Returns a user plain object.
 196       *
 197       * @return \stdClass
 198       */
 199      public function get_user_data() {
 200  
 201          if (!$this->loaded) {
 202              $this->load();
 203          }
 204  
 205          return $this->user;
 206      }
 207  }