Merge branch 'MDL-42957-26' of https://github.com/jamiepratt/moodle into MOODLE_26_STABLE
[moodle.git] / lib / classes / user.php
blob7bcee3415af50a44303eb7af803070bde2246ff3
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.
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/>.
17 /**
18 * User class
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
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * User class to access user details.
30 * @todo move api's from user/lib.php and depreciate old ones.
31 * @package core
32 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class core_user {
36 /**
37 * No reply user id.
39 const NOREPLY_USER = -10;
41 /**
42 * Support user id.
44 const SUPPORT_USER = -20;
46 /** @var stdClass keep record of noreply user */
47 public static $noreplyuser = false;
49 /** @var stdClass keep record of support user */
50 public static $supportuser = false;
52 /**
53 * Return user object from db or create noreply or support user,
54 * if userid matches corse_user::NOREPLY_USER or corse_user::SUPPORT_USER
55 * respectively. If userid is not found, then return false.
57 * @param int $userid user id
58 * @param string $fields A comma separated list of user fields to be returned, support and noreply user
59 * will not be filtered by this.
60 * @param int $strictness IGNORE_MISSING means compatible mode, false returned if user not found, debug message if more found;
61 * IGNORE_MULTIPLE means return first user, ignore multiple user records found(not recommended);
62 * MUST_EXIST means throw an exception if no user record or multiple records found.
63 * @return stdClass|bool user record if found, else false.
64 * @throws dml_exception if user record not found and respective $strictness is set.
66 public static function get_user($userid, $fields = '*', $strictness = IGNORE_MISSING) {
67 global $DB;
69 // If noreply user then create fake record and return.
70 switch ($userid) {
71 case self::NOREPLY_USER:
72 return self::get_noreply_user($strictness);
73 break;
74 case self::SUPPORT_USER:
75 return self::get_support_user($strictness);
76 break;
77 default:
78 return $DB->get_record('user', array('id' => $userid), $fields, $strictness);
82 /**
83 * Helper function to return dummy noreply user record.
85 * @return stdClass
87 protected static function get_dummy_user_record() {
88 global $CFG;
90 $dummyuser = new stdClass();
91 $dummyuser->id = self::NOREPLY_USER;
92 $dummyuser->email = $CFG->noreplyaddress;
93 $dummyuser->firstname = get_string('noreplyname');
94 $dummyuser->username = 'noreply';
95 $dummyuser->lastname = '';
96 $dummyuser->confirmed = 1;
97 $dummyuser->suspended = 0;
98 $dummyuser->deleted = 0;
99 $dummyuser->picture = 0;
100 $dummyuser->auth = 'manual';
101 $dummyuser->firstnamephonetic = '';
102 $dummyuser->lastnamephonetic = '';
103 $dummyuser->middlename = '';
104 $dummyuser->alternatename = '';
105 $dummyuser->imagealt = '';
106 return $dummyuser;
110 * Return noreply user record, this is currently used in messaging
111 * system only for sending messages from noreply email.
112 * It will return record of $CFG->noreplyuserid if set else return dummy
113 * user object with hard-coded $user->emailstop = 1 so noreply can be sent to user.
115 * @return stdClass user record.
117 public static function get_noreply_user() {
118 global $CFG;
120 if (!empty(self::$noreplyuser)) {
121 return self::$noreplyuser;
124 // If noreply user is set then use it, else create one.
125 if (!empty($CFG->noreplyuserid)) {
126 self::$noreplyuser = self::get_user($CFG->noreplyuserid);
129 if (empty(self::$noreplyuser)) {
130 self::$noreplyuser = self::get_dummy_user_record();
131 self::$noreplyuser->maildisplay = '1'; // Show to all.
133 self::$noreplyuser->emailstop = 1; // Force msg stop for this user.
134 return self::$noreplyuser;
138 * Return support user record, this is currently used in messaging
139 * system only for sending messages to support email.
140 * $CFG->supportuserid is set then returns user record
141 * $CFG->supportemail is set then return dummy record with $CFG->supportemail
142 * else return admin user record with hard-coded $user->emailstop = 0, so user
143 * gets support message.
145 * @return stdClass user record.
147 public static function get_support_user() {
148 global $CFG;
150 if (!empty(self::$supportuser)) {
151 return self::$supportuser;
154 // If custom support user is set then use it, else if supportemail is set then use it, else use noreply.
155 if (!empty($CFG->supportuserid)) {
156 self::$supportuser = self::get_user($CFG->supportuserid, '*', MUST_EXIST);
159 // Try sending it to support email if support user is not set.
160 if (empty(self::$supportuser) && !empty($CFG->supportemail)) {
161 self::$supportuser = self::get_dummy_user_record();
162 self::$supportuser->id = self::SUPPORT_USER;
163 self::$supportuser->email = $CFG->supportemail;
164 if ($CFG->supportname) {
165 self::$supportuser->firstname = $CFG->supportname;
167 self::$supportuser->username = 'support';
168 self::$supportuser->maildisplay = '1'; // Show to all.
171 // Send support msg to admin user if nothing is set above.
172 if (empty(self::$supportuser)) {
173 self::$supportuser = get_admin();
176 // Unset emailstop to make sure support message is sent.
177 self::$supportuser->emailstop = 0;
178 return self::$supportuser;
182 * Reset self::$noreplyuser and self::$supportuser.
183 * This is only used by phpunit, and there is no other use case for this function.
184 * Please don't use it outside phpunit.
186 public static function reset_internal_users() {
187 if (PHPUNIT_TEST) {
188 self::$noreplyuser = false;
189 self::$supportuser = false;
190 } else {
191 debugging('reset_internal_users() should not be used outside phpunit.', DEBUG_DEVELOPER);
196 * Return true is user id is greater than self::NOREPLY_USER and
197 * alternatively check db.
199 * @param int $userid user id.
200 * @param bool $checkdb if true userid will be checked in db. By default it's false, and
201 * userid is compared with NOREPLY_USER for performance.
202 * @return bool true is real user else false.
204 public static function is_real_user($userid, $checkdb = false) {
205 global $DB;
207 if ($userid < 0) {
208 return false;
210 if ($checkdb) {
211 return $DB->record_exists('user', array('id' => $userid));
212 } else {
213 return true;