MDL-31560 Messages: Added support for noreply and support users
[moodle.git] / lib / classes / user.php
CommitLineData
3bcf6b3c
RT
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 class
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
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * User class to access user details.
29 *
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
34 */
35class core_user {
36 /**
37 * No reply user id.
38 */
39 const NOREPLY_USER = -10;
40
41 /**
42 * Suppport user id.
43 */
44 const SUPPORT_USER = -20;
45
46 /** @var stdClass keep record of noreply user */
47 public static $noreplyuser = false;
48
49 /** @var stdClass keep record of support user */
50 public static $supportuser = false;
51
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.
56 *
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.
65 */
66 public static function get_user($userid, $fields = '*', $strictness = IGNORE_MISSING) {
67 global $DB;
68
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);
79 }
80 }
81
82 /**
83 * Helper function to return dummy noreply user record.
84 *
85 * @return stdClass
86 */
87 protected static function get_dummy_user_record() {
88 global $CFG;
89
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;
107 }
108
109 /**
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.
114 *
115 * @return stdClass user record.
116 */
117 public static function get_noreply_user() {
118 global $CFG;
119
120 if (!empty(self::$noreplyuser)) {
121 return self::$noreplyuser;
122 }
123
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);
127 }
128
129 if (empty(self::$noreplyuser)) {
130 self::$noreplyuser = self::get_dummy_user_record();
131 }
132 self::$noreplyuser->emailstop = 1; // Force msg stop for this user.
133 return self::$noreplyuser;
134 }
135
136 /**
137 * Return support user record, this is currently used in messaging
138 * system only for sending messages to support email.
139 * $CFG->supportuserid is set then returns user record
140 * $CFG->supportemail is set then return dummy record with $CFG->supportemail
141 * else return admin user record with hard-coded $user->emailstop = 0, so user
142 * gets support message.
143 *
144 * @return stdClass user record.
145 */
146 public static function get_support_user() {
147 global $CFG;
148
149 if (!empty(self::$supportuser)) {
150 return self::$supportuser;
151 }
152
153 // If custom support user is set then use it, else if supportemail is set then use it, else use noreply.
154 if (!empty($CFG->supportuserid)) {
155 self::$supportuser = self::get_user($CFG->supportuserid, '*', MUST_EXIST);
156 }
157
158 // Try sending it to support email if support user is not set.
159 if (empty(self::$supportuser) && !empty($CFG->supportemail)) {
160 self::$supportuser = self::get_dummy_user_record();
161 self::$supportuser->id = self::SUPPORT_USER;
162 self::$supportuser->email = $CFG->supportemail;
163 self::$supportuser->firstname = $CFG->supportname ? $CFG->supportname : $supportuser->firstname;
164 self::$supportuser->username = 'support';
165 self::$supportuser->maildisplay = true;
166 }
167
168 // Send support msg to admin user if nothing is set above.
169 if (empty(self::$supportuser)) {
170 self::$supportuser = get_admin();
171 }
172
173 // Unset emailstop to make sure support message is sent.
174 self::$supportuser->emailstop = 0;
175 return self::$supportuser;
176 }
177
178 /**
179 * Reset self::$noreplyuser and self::$supportuser.
180 * This is only used by phpunit, and there is no other use case for this function.
181 * Please don't use it outside phpunit.
182 */
183 public static function reset_internal_users() {
184 if (PHPUNIT_TEST) {
185 self::$noreplyuser = false;
186 self::$supportuser = false;
187 } else {
188 debugging('reset_internal_users() should not be used outside phpunit.', DEBUG_DEVELOPER);
189 }
190 }
191
192 /**
193 * Return true is user id is greater than self::NOREPLY_USER and
194 * alternetely check db.
195 *
196 * @param int $userid user id.
197 * @param bool $checkdb if true userid will be checked in db. By default it's false, and
198 * userid is compared with NOREPLY_USER for performance.
199 * @return bool true is real user else false.
200 */
201 public static function is_real_user($userid, $checkdb = false) {
202 if ($userid < 0) {
203 return false;
204 }
205 if ($checkdb) {
206 return $DB->record_exists('user', array('id' => $userid));
207 } else {
208 return true;
209 }
210 }
211}