Merge branch 'MDL-44745-master' of git://github.com/damyon/moodle
[moodle.git] / user / lib.php
CommitLineData
fb79269b 1<?php
fb79269b 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 * External user API
19 *
a2ed6e69
SH
20 * @package core_user
21 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
fb79269b 23 */
24
25
26/**
27 * Creates a user
adfb459c 28 *
a2ed6e69 29 * @throws moodle_exception
bb78e249
RT
30 * @param stdClass $user user to create
31 * @param bool $updatepassword if true, authentication plugin will update password.
fb79269b 32 * @return int id of the newly created user
33 */
bb78e249 34function user_create_user($user, $updatepassword = true) {
8bf0f207 35 global $CFG, $DB;
fb79269b 36
bb78e249 37 // Set the timecreate field to the current time.
fb79269b 38 if (!is_object($user)) {
8bf0f207 39 $user = (object) $user;
fb79269b 40 }
bd0f26bd 41
bb78e249 42 // Check username.
2f1e464a 43 if ($user->username !== core_text::strtolower($user->username)) {
45b4464c
JM
44 throw new moodle_exception('usernamelowercase');
45 } else {
46 if ($user->username !== clean_param($user->username, PARAM_USERNAME)) {
47 throw new moodle_exception('invalidusername');
48 }
49 }
50
bb78e249
RT
51 // Save the password in a temp value for later.
52 if ($updatepassword && isset($user->password)) {
adfb459c 53
bb78e249 54 // Check password toward the password policy.
adfb459c
JM
55 if (!check_password_policy($user->password, $errmsg)) {
56 throw new moodle_exception($errmsg);
57 }
58
59 $userpassword = $user->password;
60 unset($user->password);
61 }
bd0f26bd 62
8bf0f207
MN
63 // Make sure calendartype, if set, is valid.
64 if (!empty($user->calendartype)) {
65 $availablecalendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
66 if (empty($availablecalendartypes[$user->calendartype])) {
67 $user->calendartype = $CFG->calendartype;
68 }
69 } else {
70 $user->calendartype = $CFG->calendartype;
71 }
72
fb79269b 73 $user->timecreated = time();
bd0f26bd 74 $user->timemodified = $user->timecreated;
fb79269b 75
bb78e249 76 // Insert the user into the database.
fb79269b 77 $newuserid = $DB->insert_record('user', $user);
78
bb78e249
RT
79 // Create USER context for this user.
80 $usercontext = context_user::instance($newuserid);
b6dcb7d9 81
bb78e249 82 // Update user password if necessary.
adfb459c 83 if (isset($userpassword)) {
bb78e249
RT
84 // Get full database user row, in case auth is default.
85 $newuser = $DB->get_record('user', array('id' => $newuserid));
adfb459c
JM
86 $authplugin = get_auth_plugin($newuser->auth);
87 $authplugin->user_update_password($newuser, $userpassword);
88 }
89
bb78e249
RT
90 // Trigger event.
91 $event = \core\event\user_created::create(
92 array(
93 'objectid' => $newuserid,
94 'context' => $usercontext
95 )
96 );
97 $event->trigger();
adfb459c 98
fb79269b 99 return $newuserid;
fb79269b 100}
101
102/**
103 * Update a user with a user object (will compare against the ID)
adfb459c 104 *
a2ed6e69 105 * @throws moodle_exception
bb78e249
RT
106 * @param stdClass $user the user to update
107 * @param bool $updatepassword if true, authentication plugin will update password.
fb79269b 108 */
bb78e249 109function user_update_user($user, $updatepassword = true) {
fb79269b 110 global $DB;
bd0f26bd 111
a2ed6e69 112 // Set the timecreate field to the current time.
bd0f26bd 113 if (!is_object($user)) {
8bf0f207 114 $user = (object) $user;
bd0f26bd 115 }
adfb459c 116
a2ed6e69 117 // Check username.
45b4464c 118 if (isset($user->username)) {
2f1e464a 119 if ($user->username !== core_text::strtolower($user->username)) {
45b4464c
JM
120 throw new moodle_exception('usernamelowercase');
121 } else {
122 if ($user->username !== clean_param($user->username, PARAM_USERNAME)) {
123 throw new moodle_exception('invalidusername');
124 }
125 }
126 }
127
bb78e249
RT
128 // Unset password here, for updating later, if password update is required.
129 if ($updatepassword && isset($user->password)) {
adfb459c 130
a2ed6e69 131 // Check password toward the password policy.
adfb459c
JM
132 if (!check_password_policy($user->password, $errmsg)) {
133 throw new moodle_exception($errmsg);
134 }
135
9e63c0ff
FS
136 $passwd = $user->password;
137 unset($user->password);
138 }
bd0f26bd 139
8bf0f207
MN
140 // Make sure calendartype, if set, is valid.
141 if (!empty($user->calendartype)) {
142 $availablecalendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
143 // If it doesn't exist, then unset this value, we do not want to update the user's value.
144 if (empty($availablecalendartypes[$user->calendartype])) {
145 unset($user->calendartype);
146 }
147 } else {
148 // Unset this variable, must be an empty string, which we do not want to update the calendartype to.
149 unset($user->calendartype);
150 }
151
bd0f26bd 152 $user->timemodified = time();
fb79269b 153 $DB->update_record('user', $user);
b6dcb7d9 154
bb78e249
RT
155 if ($updatepassword) {
156 // Get full user record.
157 $updateduser = $DB->get_record('user', array('id' => $user->id));
9e63c0ff 158
a2ed6e69 159 // If password was set, then update its hash.
bb78e249
RT
160 if (isset($passwd)) {
161 $authplugin = get_auth_plugin($updateduser->auth);
162 if ($authplugin->can_change_password()) {
163 $authplugin->user_update_password($updateduser, $passwd);
164 }
adfb459c
JM
165 }
166 }
9e63c0ff 167
bb78e249
RT
168 // Trigger event.
169 $event = \core\event\user_updated::create(
170 array(
171 'objectid' => $user->id,
172 'context' => context_user::instance($user->id)
173 )
174 );
175 $event->trigger();
adfb459c 176}
fb79269b 177
178/**
179 * Marks user deleted in internal user database and notifies the auth plugin.
180 * Also unenrols user from all roles and does other cleanup.
181 *
182 * @todo Decide if this transaction is really needed (look for internal TODO:)
183 * @param object $user Userobject before delete (without system magic quotes)
184 * @return boolean success
185 */
186function user_delete_user($user) {
45fb2cf8 187 return delete_user($user);
fb79269b 188}
189
190/**
191 * Get users by id
fb79269b 192 *
a2ed6e69
SH
193 * @param array $userids id of users to retrieve
194 * @return array
fb79269b 195 */
196function user_get_users_by_id($userids) {
197 global $DB;
198 return $DB->get_records_list('user', 'id', $userids);
199}
b1627a92 200
61c8e0d7
FM
201/**
202 * Returns the list of default 'displayable' fields
203 *
204 * Contains database field names but also names used to generate information, such as enrolledcourses
205 *
206 * @return array of user fields
207 */
208function user_get_default_fields() {
209 return array( 'id', 'username', 'fullname', 'firstname', 'lastname', 'email',
210 'address', 'phone1', 'phone2', 'icq', 'skype', 'yahoo', 'aim', 'msn', 'department',
211 'institution', 'interests', 'firstaccess', 'lastaccess', 'auth', 'confirmed',
212 'idnumber', 'lang', 'theme', 'timezone', 'mailformat', 'description', 'descriptionformat',
213 'city', 'url', 'country', 'profileimageurlsmall', 'profileimageurl', 'customfields',
214 'groups', 'roles', 'preferences', 'enrolledcourses'
215 );
216}
01479290
DC
217
218/**
219 *
a2ed6e69 220 * Give user record from mdl_user, build an array contains all user details.
93ce0e82
JM
221 *
222 * Warning: description file urls are 'webservice/pluginfile.php' is use.
223 * it can be changed with $CFG->moodlewstextformatlinkstoimagesfile
224 *
a2ed6e69 225 * @throws moodle_exception
01479290 226 * @param stdClass $user user record from mdl_user
01479290 227 * @param stdClass $course moodle course
ad7612f5 228 * @param array $userfields required fields
d6731600 229 * @return array|null
01479290 230 */
ad7612f5 231function user_get_user_details($user, $course = null, array $userfields = array()) {
01479290 232 global $USER, $DB, $CFG;
a2ed6e69
SH
233 require_once($CFG->dirroot . "/user/profile/lib.php"); // Custom field library.
234 require_once($CFG->dirroot . "/lib/filelib.php"); // File handling on description and friends.
01479290 235
61c8e0d7 236 $defaultfields = user_get_default_fields();
ad7612f5
DC
237
238 if (empty($userfields)) {
239 $userfields = $defaultfields;
240 }
241
242 foreach ($userfields as $thefield) {
243 if (!in_array($thefield, $defaultfields)) {
244 throw new moodle_exception('invaliduserfield', 'error', '', $thefield);
245 }
246 }
247
a2ed6e69 248 // Make sure id and fullname are included.
ad7612f5
DC
249 if (!in_array('id', $userfields)) {
250 $userfields[] = 'id';
251 }
252
253 if (!in_array('fullname', $userfields)) {
254 $userfields[] = 'fullname';
255 }
256
01479290 257 if (!empty($course)) {
43731030
FM
258 $context = context_course::instance($course->id);
259 $usercontext = context_user::instance($user->id);
1e539f64 260 $canviewdetailscap = (has_capability('moodle/user:viewdetails', $context) || has_capability('moodle/user:viewdetails', $usercontext));
01479290 261 } else {
43731030 262 $context = context_user::instance($user->id);
01479290 263 $usercontext = $context;
1e539f64 264 $canviewdetailscap = has_capability('moodle/user:viewdetails', $usercontext);
01479290
DC
265 }
266
267 $currentuser = ($user->id == $USER->id);
268 $isadmin = is_siteadmin($USER);
269
48a7b182
JM
270 $showuseridentityfields = get_extra_user_fields($context);
271
01479290
DC
272 if (!empty($course)) {
273 $canviewhiddenuserfields = has_capability('moodle/course:viewhiddenuserfields', $context);
274 } else {
275 $canviewhiddenuserfields = has_capability('moodle/user:viewhiddendetails', $context);
276 }
86477112 277 $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
01479290
DC
278 if (!empty($course)) {
279 $canviewuseremail = has_capability('moodle/course:useremail', $context);
280 } else {
281 $canviewuseremail = false;
282 }
a2ed6e69 283 $cannotviewdescription = !empty($CFG->profilesforenrolledusersonly) && !$currentuser && !$DB->record_exists('role_assignments', array('userid' => $user->id));
01479290
DC
284 if (!empty($course)) {
285 $canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
286 } else {
287 $canaccessallgroups = false;
288 }
289
290 if (!$currentuser && !$canviewdetailscap && !has_coursecontact_role($user->id)) {
a2ed6e69 291 // Skip this user details.
01479290
DC
292 return null;
293 }
294
295 $userdetails = array();
296 $userdetails['id'] = $user->id;
297
ad7612f5 298 if (($isadmin or $currentuser) and in_array('username', $userfields)) {
01479290
DC
299 $userdetails['username'] = $user->username;
300 }
301 if ($isadmin or $canviewfullnames) {
ad7612f5
DC
302 if (in_array('firstname', $userfields)) {
303 $userdetails['firstname'] = $user->firstname;
304 }
305 if (in_array('lastname', $userfields)) {
306 $userdetails['lastname'] = $user->lastname;
307 }
01479290
DC
308 }
309 $userdetails['fullname'] = fullname($user);
310
ad7612f5
DC
311 if (in_array('customfields', $userfields)) {
312 $fields = $DB->get_recordset_sql("SELECT f.*
313 FROM {user_info_field} f
314 JOIN {user_info_category} c
315 ON f.categoryid=c.id
316 ORDER BY c.sortorder ASC, f.sortorder ASC");
317 $userdetails['customfields'] = array();
318 foreach ($fields as $field) {
319 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
320 $newfield = 'profile_field_'.$field->datatype;
321 $formfield = new $newfield($field->id, $user->id);
322 if ($formfield->is_visible() and !$formfield->is_empty()) {
323 $userdetails['customfields'][] =
324 array('name' => $formfield->field->name, 'value' => $formfield->data,
325 'type' => $field->datatype, 'shortname' => $formfield->field->shortname);
326 }
327 }
328 $fields->close();
a2ed6e69 329 // Unset customfields if it's empty.
ad7612f5
DC
330 if (empty($userdetails['customfields'])) {
331 unset($userdetails['customfields']);
01479290 332 }
01479290
DC
333 }
334
a2ed6e69 335 // Profile image.
ad7612f5 336 if (in_array('profileimageurl', $userfields)) {
a2ed6e69 337 $profileimageurl = moodle_url::make_pluginfile_url($usercontext->id, 'user', 'icon', null, '/', 'f1');
ad7612f5
DC
338 $userdetails['profileimageurl'] = $profileimageurl->out(false);
339 }
340 if (in_array('profileimageurlsmall', $userfields)) {
a2ed6e69 341 $profileimageurlsmall = moodle_url::make_pluginfile_url($usercontext->id, 'user', 'icon', null, '/', 'f2');
ad7612f5
DC
342 $userdetails['profileimageurlsmall'] = $profileimageurlsmall->out(false);
343 }
01479290 344
a2ed6e69 345 // Hidden user field.
01479290
DC
346 if ($canviewhiddenuserfields) {
347 $hiddenfields = array();
a2ed6e69
SH
348 // Address, phone1 and phone2 not appears in hidden fields list but require viewhiddenfields capability
349 // according to user/profile.php.
ad7612f5 350 if ($user->address && in_array('address', $userfields)) {
01479290
DC
351 $userdetails['address'] = $user->address;
352 }
01479290
DC
353 } else {
354 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
355 }
356
48a7b182 357 if ($user->phone1 && in_array('phone1', $userfields) &&
58f739c5 358 (in_array('phone1', $showuseridentityfields) or $canviewhiddenuserfields)) {
48a7b182
JM
359 $userdetails['phone1'] = $user->phone1;
360 }
361 if ($user->phone2 && in_array('phone2', $userfields) &&
58f739c5 362 (in_array('phone2', $showuseridentityfields) or $canviewhiddenuserfields)) {
48a7b182
JM
363 $userdetails['phone2'] = $user->phone2;
364 }
365
acf64596
JM
366 if (isset($user->description) &&
367 ((!isset($hiddenfields['description']) && !$cannotviewdescription) or $isadmin)) {
368 if (in_array('description', $userfields)) {
369 // Always return the descriptionformat if description is requested.
370 list($userdetails['description'], $userdetails['descriptionformat']) =
371 external_format_text($user->description, $user->descriptionformat,
372 $usercontext->id, 'user', 'profile', null);
01479290
DC
373 }
374 }
375
ad7612f5 376 if (in_array('country', $userfields) && (!isset($hiddenfields['country']) or $isadmin) && $user->country) {
01479290
DC
377 $userdetails['country'] = $user->country;
378 }
379
ad7612f5 380 if (in_array('city', $userfields) && (!isset($hiddenfields['city']) or $isadmin) && $user->city) {
01479290
DC
381 $userdetails['city'] = $user->city;
382 }
383
ad7612f5 384 if (in_array('url', $userfields) && $user->url && (!isset($hiddenfields['webpage']) or $isadmin)) {
01479290
DC
385 $url = $user->url;
386 if (strpos($user->url, '://') === false) {
387 $url = 'http://'. $url;
388 }
389 $user->url = clean_param($user->url, PARAM_URL);
390 $userdetails['url'] = $user->url;
391 }
392
ad7612f5 393 if (in_array('icq', $userfields) && $user->icq && (!isset($hiddenfields['icqnumber']) or $isadmin)) {
01479290
DC
394 $userdetails['icq'] = $user->icq;
395 }
396
ad7612f5 397 if (in_array('skype', $userfields) && $user->skype && (!isset($hiddenfields['skypeid']) or $isadmin)) {
01479290
DC
398 $userdetails['skype'] = $user->skype;
399 }
ad7612f5 400 if (in_array('yahoo', $userfields) && $user->yahoo && (!isset($hiddenfields['yahooid']) or $isadmin)) {
01479290
DC
401 $userdetails['yahoo'] = $user->yahoo;
402 }
ad7612f5 403 if (in_array('aim', $userfields) && $user->aim && (!isset($hiddenfields['aimid']) or $isadmin)) {
01479290
DC
404 $userdetails['aim'] = $user->aim;
405 }
ad7612f5 406 if (in_array('msn', $userfields) && $user->msn && (!isset($hiddenfields['msnid']) or $isadmin)) {
01479290
DC
407 $userdetails['msn'] = $user->msn;
408 }
409
ad7612f5 410 if (in_array('firstaccess', $userfields) && (!isset($hiddenfields['firstaccess']) or $isadmin)) {
01479290
DC
411 if ($user->firstaccess) {
412 $userdetails['firstaccess'] = $user->firstaccess;
413 } else {
414 $userdetails['firstaccess'] = 0;
415 }
416 }
ad7612f5 417 if (in_array('lastaccess', $userfields) && (!isset($hiddenfields['lastaccess']) or $isadmin)) {
01479290
DC
418 if ($user->lastaccess) {
419 $userdetails['lastaccess'] = $user->lastaccess;
420 } else {
421 $userdetails['lastaccess'] = 0;
422 }
423 }
424
a2ed6e69
SH
425 if (in_array('email', $userfields) && ($isadmin // The admin is allowed the users email.
426 or $currentuser // Of course the current user is as well.
427 or $canviewuseremail // This is a capability in course context, it will be false in usercontext.
58f739c5 428 or in_array('email', $showuseridentityfields)
01479290 429 or $user->maildisplay == 1
ca774c94 430 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER)))) {
90e30b96 431 $userdetails['email'] = $user->email;
01479290
DC
432 }
433
ad7612f5 434 if (in_array('interests', $userfields) && !empty($CFG->usetags)) {
01479290
DC
435 require_once($CFG->dirroot . '/tag/lib.php');
436 if ($interests = tag_get_tags_csv('user', $user->id, TAG_RETURN_TEXT) ) {
437 $userdetails['interests'] = $interests;
438 }
439 }
440
a2ed6e69 441 // Departement/Institution/Idnumber are not displayed on any profile, however you can get them from editing profile.
58f739c5 442 if ($isadmin or $currentuser or in_array('idnumber', $showuseridentityfields)) {
48a7b182 443 if (in_array('idnumber', $userfields) && $user->idnumber) {
3a3f3b22
CW
444 $userdetails['idnumber'] = $user->idnumber;
445 }
48a7b182 446 }
58f739c5 447 if ($isadmin or $currentuser or in_array('institution', $showuseridentityfields)) {
ad7612f5 448 if (in_array('institution', $userfields) && $user->institution) {
01479290
DC
449 $userdetails['institution'] = $user->institution;
450 }
48a7b182 451 }
58f739c5 452 if ($isadmin or $currentuser or in_array('department', $showuseridentityfields)) {
a2ed6e69 453 if (in_array('department', $userfields) && isset($user->department)) { // Isset because it's ok to have department 0.
01479290
DC
454 $userdetails['department'] = $user->department;
455 }
456 }
457
ad7612f5 458 if (in_array('roles', $userfields) && !empty($course)) {
a2ed6e69 459 // Not a big secret.
01479290
DC
460 $roles = get_user_roles($context, $user->id, false);
461 $userdetails['roles'] = array();
462 foreach ($roles as $role) {
463 $userdetails['roles'][] = array(
464 'roleid' => $role->roleid,
465 'name' => $role->name,
466 'shortname' => $role->shortname,
467 'sortorder' => $role->sortorder
468 );
469 }
470 }
471
a2ed6e69 472 // If groups are in use and enforced throughout the course, then make sure we can meet in at least one course level group.
ad7612f5 473 if (in_array('groups', $userfields) && !empty($course) && $canaccessallgroups) {
93ce0e82
JM
474 $usergroups = groups_get_all_groups($course->id, $user->id, $course->defaultgroupingid,
475 'g.id, g.name,g.description,g.descriptionformat');
01479290
DC
476 $userdetails['groups'] = array();
477 foreach ($usergroups as $group) {
93ce0e82
JM
478 list($group->description, $group->descriptionformat) =
479 external_format_text($group->description, $group->descriptionformat,
480 $context->id, 'group', 'description', $group->id);
a2ed6e69
SH
481 $userdetails['groups'][] = array('id' => $group->id, 'name' => $group->name,
482 'description' => $group->description, 'descriptionformat' => $group->descriptionformat);
01479290
DC
483 }
484 }
a2ed6e69 485 // List of courses where the user is enrolled.
ad7612f5 486 if (in_array('enrolledcourses', $userfields) && !isset($hiddenfields['mycourses'])) {
01479290
DC
487 $enrolledcourses = array();
488 if ($mycourses = enrol_get_users_courses($user->id, true)) {
489 foreach ($mycourses as $mycourse) {
490 if ($mycourse->category) {
43731030 491 $coursecontext = context_course::instance($mycourse->id);
01479290
DC
492 $enrolledcourse = array();
493 $enrolledcourse['id'] = $mycourse->id;
43ff71a0 494 $enrolledcourse['fullname'] = format_string($mycourse->fullname, true, array('context' => $coursecontext));
8ebbb06a 495 $enrolledcourse['shortname'] = format_string($mycourse->shortname, true, array('context' => $coursecontext));
01479290
DC
496 $enrolledcourses[] = $enrolledcourse;
497 }
498 }
499 $userdetails['enrolledcourses'] = $enrolledcourses;
500 }
501 }
502
a2ed6e69 503 // User preferences.
ad7612f5 504 if (in_array('preferences', $userfields) && $currentuser) {
01479290
DC
505 $preferences = array();
506 $userpreferences = get_user_preferences();
a2ed6e69 507 foreach ($userpreferences as $prefname => $prefvalue) {
01479290 508 $preferences[] = array('name' => $prefname, 'value' => $prefvalue);
a2ed6e69
SH
509 }
510 $userdetails['preferences'] = $preferences;
01479290 511 }
ad7612f5 512
01479290
DC
513 return $userdetails;
514}
515
86477112
FS
516/**
517 * Tries to obtain user details, either recurring directly to the user's system profile
c70b9853 518 * or through one of the user's course enrollments (course profile).
86477112 519 *
a2ed6e69 520 * @param stdClass $user The user.
c70b9853 521 * @return array if unsuccessful or the allowed user details.
86477112
FS
522 */
523function user_get_user_details_courses($user) {
524 global $USER;
525 $userdetails = null;
526
a2ed6e69 527 // Get the courses that the user is enrolled in (only active).
86477112
FS
528 $courses = enrol_get_users_courses($user->id, true);
529
c70b9853
JM
530 $systemprofile = false;
531 if (can_view_user_details_cap($user) || ($user->id == $USER->id) || has_coursecontact_role($user->id)) {
532 $systemprofile = true;
533 }
86477112 534
c70b9853 535 // Try using system profile.
86477112
FS
536 if ($systemprofile) {
537 $userdetails = user_get_user_details($user, null);
538 } else {
c70b9853 539 // Try through course profile.
86477112 540 foreach ($courses as $course) {
854859e1 541 if (can_view_user_details_cap($user, $course) || ($user->id == $USER->id) || has_coursecontact_role($user->id)) {
86477112
FS
542 $userdetails = user_get_user_details($user, $course);
543 }
544 }
545 }
546
547 return $userdetails;
548}
549
550/**
c70b9853
JM
551 * Check if $USER have the necessary capabilities to obtain user details.
552 *
a2ed6e69
SH
553 * @param stdClass $user
554 * @param stdClass $course if null then only consider system profile otherwise also consider the course's profile.
c70b9853 555 * @return bool true if $USER can view user details.
86477112
FS
556 */
557function can_view_user_details_cap($user, $course = null) {
c70b9853 558 // Check $USER has the capability to view the user details at user context.
bea86e84 559 $usercontext = context_user::instance($user->id);
c70b9853
JM
560 $result = has_capability('moodle/user:viewdetails', $usercontext);
561 // Otherwise can $USER see them at course context.
562 if (!$result && !empty($course)) {
bea86e84 563 $context = context_course::instance($course->id);
c70b9853 564 $result = has_capability('moodle/user:viewdetails', $context);
86477112
FS
565 }
566 return $result;
567}
568
b1627a92
DC
569/**
570 * Return a list of page types
571 * @param string $pagetype current page type
572 * @param stdClass $parentcontext Block's parent context
573 * @param stdClass $currentcontext Current context of block
a2ed6e69 574 * @return array
b1627a92 575 */
b38e2e28 576function user_page_type_list($pagetype, $parentcontext, $currentcontext) {
a2ed6e69 577 return array('user-profile' => get_string('page-user-profile', 'pagetype'));
d6731600 578}