forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
354 lines (311 loc) · 12.8 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* External user API
*
* @package moodlecore
* @subpackage user
* @copyright 2009 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Creates a user
* @param object $user user to create
* @return int id of the newly created user
*/
function user_create_user($user) {
global $DB;
/// set the timecreate field to the current time
if (!is_object($user)) {
$user = (object)$user;
}
/// hash the password
$user->password = hash_internal_user_password($user->password);
$user->timecreated = time();
$user->timemodified = $user->timecreated;
/// insert the user into the database
$newuserid = $DB->insert_record('user', $user);
/// create USER context for this user
get_context_instance(CONTEXT_USER, $newuserid);
return $newuserid;
}
/**
* Update a user with a user object (will compare against the ID)
* @param object $user - the user to update
*/
function user_update_user($user) {
global $DB;
/// set the timecreate field to the current time
if (!is_object($user)) {
$user = (object)$user;
}
/// hash the password
$user->password = hash_internal_user_password($user->password);
$user->timemodified = time();
$DB->update_record('user', $user);
}
/**
* Marks user deleted in internal user database and notifies the auth plugin.
* Also unenrols user from all roles and does other cleanup.
*
* @todo Decide if this transaction is really needed (look for internal TODO:)
* @param object $user Userobject before delete (without system magic quotes)
* @return boolean success
*/
function user_delete_user($user) {
return delete_user($user);
}
/**
* Get users by id
* @param array $userids id of users to retrieve
*
*/
function user_get_users_by_id($userids) {
global $DB;
return $DB->get_records_list('user', 'id', $userids);
}
/**
*
* Give user record from mdl_user, build an array conntains
* all user details
* @param stdClass $user user record from mdl_user
* @param stdClass $context context object
* @param stdClass $course moodle course
* @return array
*/
function user_get_user_details($user, $course = null) {
global $USER, $DB, $CFG;
require_once($CFG->dirroot . "/user/profile/lib.php"); //custom field library
require_once($CFG->dirroot . "/lib/filelib.php"); // file handling on description and friends
if (!empty($course)) {
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$usercontext = get_context_instance(CONTEXT_USER, $user->id);
$canviewdetailscap = (has_capability('moodle/user:viewdetails', $context) || has_capability('moodle/user:viewdetails', $usercontext));
} else {
$context = get_context_instance(CONTEXT_USER, $user->id);
$usercontext = $context;
$canviewdetailscap = has_capability('moodle/user:viewdetails', $usercontext);
}
$currentuser = ($user->id == $USER->id);
$isadmin = is_siteadmin($USER);
if (!empty($course)) {
$canviewhiddenuserfields = has_capability('moodle/course:viewhiddenuserfields', $context);
} else {
$canviewhiddenuserfields = has_capability('moodle/user:viewhiddendetails', $context);
}
$canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
if (!empty($course)) {
$canviewuseremail = has_capability('moodle/course:useremail', $context);
} else {
$canviewuseremail = false;
}
$cannotviewdescription = !empty($CFG->profilesforenrolledusersonly) && !$currentuser && !$DB->record_exists('role_assignments', array('userid'=>$user->id));
if (!empty($course)) {
$canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
} else {
$canaccessallgroups = false;
}
if (!$currentuser && !$canviewdetailscap && !has_coursecontact_role($user->id)) {
// skip this user details
return null;
}
$userdetails = array();
$userdetails['id'] = $user->id;
if ($isadmin or $currentuser) {
$userdetails['username'] = $user->username;
}
if ($isadmin or $canviewfullnames) {
$userdetails['firstname'] = $user->firstname;
$userdetails['lastname'] = $user->lastname;
}
$userdetails['fullname'] = fullname($user);
$fields = $DB->get_recordset_sql("SELECT f.*
FROM {user_info_field} f
JOIN {user_info_category} c
ON f.categoryid=c.id
ORDER BY c.sortorder ASC, f.sortorder ASC");
$userdetails['customfields'] = array();
foreach ($fields as $field) {
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id, $user->id);
if ($formfield->is_visible() and !$formfield->is_empty()) {
$userdetails['customfields'][] =
array('name' => $formfield->field->name, 'value' => $formfield->data,
'type' => $field->datatype, 'shortname' => $formfield->field->shortname);
}
}
$fields->close();
// unset customfields if it's empty
if (empty($userdetails['customfields'])) {
unset($userdetails['customfields']);
}
// profile image
$profileimageurl = moodle_url::make_pluginfile_url($usercontext->id, 'user', 'icon', NULL, '/', 'f1');
$userdetails['profileimageurl'] = $profileimageurl->out(false);
$profileimageurlsmall = moodle_url::make_pluginfile_url($usercontext->id, 'user', 'icon', NULL, '/', 'f2');
$userdetails['profileimageurlsmall'] = $profileimageurlsmall->out(false);
//hidden user field
if ($canviewhiddenuserfields) {
$hiddenfields = array();
// address, phone1 and phone2 not appears in hidden fields list
// but require viewhiddenfields capability
// according to user/profile.php
if ($user->address) {
$userdetails['address'] = $user->address;
}
if ($user->phone1) {
$userdetails['phone1'] = $user->phone1;
}
if ($user->phone2) {
$userdetails['phone2'] = $user->phone2;
}
} else {
$hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
}
if (isset($user->description) && (!isset($hiddenfields['description']) or $isadmin)) {
if (!$cannotviewdescription) {
$user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
$userdetails['description'] = $user->description;
$userdetails['descriptionformat'] = $user->descriptionformat;
}
}
if ((!isset($hiddenfields['country']) or $isadmin) && $user->country) {
$userdetails['country'] = $user->country;
}
if ((!isset($hiddenfields['city']) or $isadmin) && $user->city) {
$userdetails['city'] = $user->city;
}
if ($user->url && (!isset($hiddenfields['webpage']) or $isadmin)) {
$url = $user->url;
if (strpos($user->url, '://') === false) {
$url = 'http://'. $url;
}
$user->url = clean_param($user->url, PARAM_URL);
$userdetails['url'] = $user->url;
}
if ($user->icq && (!isset($hiddenfields['icqnumber']) or $isadmin)) {
$userdetails['icq'] = $user->icq;
}
if ($user->skype && (!isset($hiddenfields['skypeid']) or $isadmin)) {
$userdetails['skype'] = $user->skype;
}
if ($user->yahoo && (!isset($hiddenfields['yahooid']) or $isadmin)) {
$userdetails['yahoo'] = $user->yahoo;
}
if ($user->aim && (!isset($hiddenfields['aimid']) or $isadmin)) {
$userdetails['aim'] = $user->aim;
}
if ($user->msn && (!isset($hiddenfields['msnid']) or $isadmin)) {
$userdetails['msn'] = $user->msn;
}
if (!isset($hiddenfields['firstaccess']) or $isadmin) {
if ($user->firstaccess) {
$userdetails['firstaccess'] = $user->firstaccess;
} else {
$userdetails['firstaccess'] = 0;
}
}
if (!isset($hiddenfields['lastaccess']) or $isadmin) {
if ($user->lastaccess) {
$userdetails['lastaccess'] = $user->lastaccess;
} else {
$userdetails['lastaccess'] = 0;
}
}
if ($currentuser
or $canviewuseremail // this is a capability in course context, it will be false in usercontext
or $user->maildisplay == 1
or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER))) {
$userdetails['email'] = $user->email;;
}
if (!empty($CFG->usetags)) {
require_once($CFG->dirroot . '/tag/lib.php');
if ($interests = tag_get_tags_csv('user', $user->id, TAG_RETURN_TEXT) ) {
$userdetails['interests'] = $interests;
}
}
//Departement/Institution are not displayed on any profile, however you can get them from editing profile.
if ($isadmin or $currentuser) {
if ($user->institution) {
$userdetails['institution'] = $user->institution;
}
if (isset($user->department)) { //isset because it's ok to have department 0
$userdetails['department'] = $user->department;
}
}
if (!empty($course)) {
// not a big secret
$roles = get_user_roles($context, $user->id, false);
$userdetails['roles'] = array();
foreach ($roles as $role) {
$userdetails['roles'][] = array(
'roleid' => $role->roleid,
'name' => $role->name,
'shortname' => $role->shortname,
'sortorder' => $role->sortorder
);
}
}
// If groups are in use and enforced throughout the course, then make sure we can meet in at least one course level group
if (!empty($course) && $canaccessallgroups) {
$usergroups = groups_get_all_groups($course->id, $user->id, $course->defaultgroupingid, 'g.id, g.name,g.description');
$userdetails['groups'] = array();
foreach ($usergroups as $group) {
$group->description = file_rewrite_pluginfile_urls($group->description, 'pluginfile.php', $context->id, 'group', 'description', $group->id);
$userdetails['groups'][] = array('id'=>$group->id, 'name'=>$group->name, 'description'=>$group->description);
}
}
//list of courses where the user is enrolled
if (!isset($hiddenfields['mycourses'])) {
$enrolledcourses = array();
if ($mycourses = enrol_get_users_courses($user->id, true)) {
foreach ($mycourses as $mycourse) {
if ($mycourse->category) {
$coursecontext = get_context_instance(CONTEXT_COURSE, $mycourse->id);
$enrolledcourse = array();
$enrolledcourse['id'] = $mycourse->id;
$enrolledcourse['fullname'] = format_string($mycourse->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $mycourse->id)));
$enrolledcourse['shortname'] = format_string($mycourse->shortname, true, array('context' => $coursecontext));
$enrolledcourses[] = $enrolledcourse;
}
}
$userdetails['enrolledcourses'] = $enrolledcourses;
}
}
//user preferences
if ($currentuser) {
$preferences = array();
$userpreferences = get_user_preferences();
foreach($userpreferences as $prefname => $prefvalue) {
$preferences[] = array('name' => $prefname, 'value' => $prefvalue);
}
$userdetails['preferences'] = $preferences;
}
return $userdetails;
}
/**
* Return a list of page types
* @param string $pagetype current page type
* @param stdClass $parentcontext Block's parent context
* @param stdClass $currentcontext Current context of block
*/
function user_page_type_list($pagetype, $parentcontext, $currentcontext) {
return array(
'user-profile'=>get_string('page-user-profile', 'pagetype'),
'my-index'=>get_string('page-my-index', 'pagetype')
);
}