Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
    /** @var int Store current Unix timestamp. */
68
    protected int $now = 0;
69
 
70
    /**
71
     * Constructor.
72
     *
73
     * Use self::instance() instead to get cached copies of the class. Instances obtained
74
     * through this constructor will not be cached.
75
     *
76
     * @param int|\stdClass $user User id
77
     * @param \context|null $context
78
     * @return void
79
     */
80
    public function __construct($user, ?\context $context = null) {
81
 
82
        if (is_scalar($user)) {
83
            $this->user = new \stdClass();
84
            $this->user->id = $user;
85
        } else {
86
            $this->user = $user;
87
        }
88
 
89
        if (!is_null($context)) {
90
            $this->usercontext = $context;
91
        }
92
    }
93
 
94
    /**
95
     * Returns an analytics user instance.
96
     *
97
     * Lazy load of analysable data.
98
     *
99
     * @param int|\stdClass $user User object or user id
100
     * @param \context|null $context
101
     * @return \core_analytics\user
102
     */
103
    public static function instance($user, ?\context $context = null) {
104
 
105
        $userid = $user;
106
        if (!is_scalar($userid)) {
107
            $userid = $user->id;
108
        }
109
 
110
        if (self::$cachedid === $userid) {
111
            return self::$cachedinstance;
112
        }
113
 
114
        $cachedinstance = new \core_analytics\user($user, $context);
115
        self::$cachedinstance = $cachedinstance;
116
        self::$cachedid = (int)$userid;
117
        return self::$cachedinstance;
118
    }
119
 
120
    /**
121
     * get_id
122
     *
123
     * @return int
124
     */
125
    public function get_id() {
126
        return $this->user->id;
127
    }
128
 
129
    /**
130
     * Loads the analytics user object.
131
     *
132
     * @return void
133
     */
134
    protected function load() {
135
 
136
        // The instance constructor could be already loaded with the full user object. Using email
137
        // because it is a required user field.
138
        if (empty($this->user->email)) {
139
            $this->user = \core_user::get_user($this->user->id);
140
        }
141
 
142
        $this->usercontext = $this->get_context();
143
 
144
        $this->now = time();
145
 
146
        // Flag the instance as loaded.
147
        $this->loaded = true;
148
    }
149
 
150
    /**
151
     * The user full name.
152
     *
153
     * @return string
154
     */
155
    public function get_name() {
156
 
157
        if (!$this->loaded) {
158
            $this->load();
159
        }
160
        return fullname($this->user);
161
    }
162
 
163
    /**
164
     * get_context
165
     *
166
     * @return \context
167
     */
168
    public function get_context() {
169
        if ($this->usercontext === null) {
170
            $this->usercontext = \context_user::instance($this->user->id);
171
        }
172
        return $this->usercontext;
173
    }
174
 
175
    /**
176
     * Get the start timestamp.
177
     *
178
     * @return int
179
     */
180
    public function get_start() {
181
 
182
        if (!$this->loaded) {
183
            $this->load();
184
        }
185
        return $this->user->timecreated;
186
    }
187
 
188
    /**
189
     * Get the end timestamp.
190
     *
191
     * @return int
192
     */
193
    public function get_end() {
194
        return self::MAX_TIME;
195
    }
196
 
197
    /**
198
     * Returns a user plain object.
199
     *
200
     * @return \stdClass
201
     */
202
    public function get_user_data() {
203
 
204
        if (!$this->loaded) {
205
            $this->load();
206
        }
207
 
208
        return $this->user;
209
    }
210
}