Merge branch 'wip-MDL-31738-MOODLE_21_STABLE' of git://github.com/marinaglancy/moodle...
[moodle.git] / user / lib.php
blobc32606d3ab433a51dbe8c68b9dd3f45d3d6e9a10
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * External user API
21 * @package moodlecore
22 * @subpackage user
23 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 /**
29 * Creates a user
31 * @param object $user user to create
32 * @return int id of the newly created user
34 function user_create_user($user) {
35 global $DB;
37 // set the timecreate field to the current time
38 if (!is_object($user)) {
39 $user = (object)$user;
42 // save the password in a temp value for later
43 if (isset($user->password)) {
44 $userpassword = $user->password;
45 unset($user->password);
48 $user->timecreated = time();
49 $user->timemodified = $user->timecreated;
51 // insert the user into the database
52 $newuserid = $DB->insert_record('user', $user);
54 // trigger user_created event on the full database user row
55 $newuser = $DB->get_record('user', array('id' => $newuserid));
56 events_trigger('user_created', $newuser);
58 // create USER context for this user
59 get_context_instance(CONTEXT_USER, $newuserid);
61 // update user password if necessary
62 if (isset($userpassword)) {
63 update_internal_user_password($newuser, $userpassword);
66 return $newuserid;
70 /**
71 * Update a user with a user object (will compare against the ID)
73 * @param object $user the user to update
75 function user_update_user($user) {
76 global $DB;
78 // set the timecreate field to the current time
79 if (!is_object($user)) {
80 $user = (object)$user;
83 // unset password here, for updating later
84 if (isset($user->password)) {
85 $passwd = $user->password;
86 unset($user->password);
89 $user->timemodified = time();
90 $DB->update_record('user', $user);
92 // trigger user_updated event on the full database user row
93 $updateduser = $DB->get_record('user', array('id' => $user->id));
94 events_trigger('user_updated', $updateduser);
96 // if password was set, then update its hash
97 if (isset($passwd)) {
98 update_internal_user_password($updateduser, $passwd);
104 * Marks user deleted in internal user database and notifies the auth plugin.
105 * Also unenrols user from all roles and does other cleanup.
107 * @todo Decide if this transaction is really needed (look for internal TODO:)
108 * @param object $user Userobject before delete (without system magic quotes)
109 * @return boolean success
111 function user_delete_user($user) {
112 return delete_user($user);
116 * Get users by id
117 * @param array $userids id of users to retrieve
120 function user_get_users_by_id($userids) {
121 global $DB;
122 return $DB->get_records_list('user', 'id', $userids);
128 * Give user record from mdl_user, build an array conntains
129 * all user details
130 * @param stdClass $user user record from mdl_user
131 * @param stdClass $context context object
132 * @param stdClass $course moodle course
133 * @return array
135 function user_get_user_details($user, $course = null) {
136 global $USER, $DB, $CFG;
137 require_once($CFG->dirroot . "/user/profile/lib.php"); //custom field library
138 require_once($CFG->dirroot . "/lib/filelib.php"); // file handling on description and friends
140 if (!empty($course)) {
141 $context = get_context_instance(CONTEXT_COURSE, $course->id);
142 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
143 $canviewdetailscap = (has_capability('moodle/user:viewdetails', $context) || has_capability('moodle/user:viewdetails', $usercontext));
144 } else {
145 $context = get_context_instance(CONTEXT_USER, $user->id);
146 $usercontext = $context;
147 $canviewdetailscap = has_capability('moodle/user:viewdetails', $usercontext);
150 $currentuser = ($user->id == $USER->id);
151 $isadmin = is_siteadmin($USER);
153 if (!empty($course)) {
154 $canviewhiddenuserfields = has_capability('moodle/course:viewhiddenuserfields', $context);
155 } else {
156 $canviewhiddenuserfields = has_capability('moodle/user:viewhiddendetails', $context);
158 $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
159 if (!empty($course)) {
160 $canviewuseremail = has_capability('moodle/course:useremail', $context);
161 } else {
162 $canviewuseremail = false;
164 $cannotviewdescription = !empty($CFG->profilesforenrolledusersonly) && !$currentuser && !$DB->record_exists('role_assignments', array('userid'=>$user->id));
165 if (!empty($course)) {
166 $canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
167 } else {
168 $canaccessallgroups = false;
171 if (!$currentuser && !$canviewdetailscap && !has_coursecontact_role($user->id)) {
172 // skip this user details
173 return null;
176 $userdetails = array();
177 $userdetails['id'] = $user->id;
179 if ($isadmin or $currentuser) {
180 $userdetails['username'] = $user->username;
182 if ($isadmin or $canviewfullnames) {
183 $userdetails['firstname'] = $user->firstname;
184 $userdetails['lastname'] = $user->lastname;
186 $userdetails['fullname'] = fullname($user);
188 $fields = $DB->get_recordset_sql("SELECT f.*
189 FROM {user_info_field} f
190 JOIN {user_info_category} c
191 ON f.categoryid=c.id
192 ORDER BY c.sortorder ASC, f.sortorder ASC");
193 $userdetails['customfields'] = array();
194 foreach ($fields as $field) {
195 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
196 $newfield = 'profile_field_'.$field->datatype;
197 $formfield = new $newfield($field->id, $user->id);
198 if ($formfield->is_visible() and !$formfield->is_empty()) {
199 $userdetails['customfields'][] =
200 array('name' => $formfield->field->name, 'value' => $formfield->data,
201 'type' => $field->datatype, 'shortname' => $formfield->field->shortname);
204 $fields->close();
205 // unset customfields if it's empty
206 if (empty($userdetails['customfields'])) {
207 unset($userdetails['customfields']);
210 // profile image
211 $profileimageurl = moodle_url::make_pluginfile_url($usercontext->id, 'user', 'icon', NULL, '/', 'f1');
212 $userdetails['profileimageurl'] = $profileimageurl->out(false);
213 $profileimageurlsmall = moodle_url::make_pluginfile_url($usercontext->id, 'user', 'icon', NULL, '/', 'f2');
214 $userdetails['profileimageurlsmall'] = $profileimageurlsmall->out(false);
216 //hidden user field
217 if ($canviewhiddenuserfields) {
218 $hiddenfields = array();
219 // address, phone1 and phone2 not appears in hidden fields list
220 // but require viewhiddenfields capability
221 // according to user/profile.php
222 if ($user->address) {
223 $userdetails['address'] = $user->address;
225 if ($user->phone1) {
226 $userdetails['phone1'] = $user->phone1;
228 if ($user->phone2) {
229 $userdetails['phone2'] = $user->phone2;
231 } else {
232 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
235 if (isset($user->description) && (!isset($hiddenfields['description']) or $isadmin)) {
236 if (!$cannotviewdescription) {
237 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
238 $userdetails['description'] = $user->description;
239 $userdetails['descriptionformat'] = $user->descriptionformat;
243 if ((!isset($hiddenfields['country']) or $isadmin) && $user->country) {
244 $userdetails['country'] = $user->country;
247 if ((!isset($hiddenfields['city']) or $isadmin) && $user->city) {
248 $userdetails['city'] = $user->city;
251 if ($user->url && (!isset($hiddenfields['webpage']) or $isadmin)) {
252 $url = $user->url;
253 if (strpos($user->url, '://') === false) {
254 $url = 'http://'. $url;
256 $user->url = clean_param($user->url, PARAM_URL);
257 $userdetails['url'] = $user->url;
260 if ($user->icq && (!isset($hiddenfields['icqnumber']) or $isadmin)) {
261 $userdetails['icq'] = $user->icq;
264 if ($user->skype && (!isset($hiddenfields['skypeid']) or $isadmin)) {
265 $userdetails['skype'] = $user->skype;
267 if ($user->yahoo && (!isset($hiddenfields['yahooid']) or $isadmin)) {
268 $userdetails['yahoo'] = $user->yahoo;
270 if ($user->aim && (!isset($hiddenfields['aimid']) or $isadmin)) {
271 $userdetails['aim'] = $user->aim;
273 if ($user->msn && (!isset($hiddenfields['msnid']) or $isadmin)) {
274 $userdetails['msn'] = $user->msn;
277 if (!isset($hiddenfields['firstaccess']) or $isadmin) {
278 if ($user->firstaccess) {
279 $userdetails['firstaccess'] = $user->firstaccess;
280 } else {
281 $userdetails['firstaccess'] = 0;
284 if (!isset($hiddenfields['lastaccess']) or $isadmin) {
285 if ($user->lastaccess) {
286 $userdetails['lastaccess'] = $user->lastaccess;
287 } else {
288 $userdetails['lastaccess'] = 0;
292 if ($currentuser
293 or $canviewuseremail // this is a capability in course context, it will be false in usercontext
294 or $user->maildisplay == 1
295 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER))) {
296 $userdetails['email'] = $user->email;;
299 if (!empty($CFG->usetags)) {
300 require_once($CFG->dirroot . '/tag/lib.php');
301 if ($interests = tag_get_tags_csv('user', $user->id, TAG_RETURN_TEXT) ) {
302 $userdetails['interests'] = $interests;
306 //Departement/Institution are not displayed on any profile, however you can get them from editing profile.
307 if ($isadmin or $currentuser) {
308 if ($user->institution) {
309 $userdetails['institution'] = $user->institution;
311 if (isset($user->department)) { //isset because it's ok to have department 0
312 $userdetails['department'] = $user->department;
316 if (!empty($course)) {
317 // not a big secret
318 $roles = get_user_roles($context, $user->id, false);
319 $userdetails['roles'] = array();
320 foreach ($roles as $role) {
321 $userdetails['roles'][] = array(
322 'roleid' => $role->roleid,
323 'name' => $role->name,
324 'shortname' => $role->shortname,
325 'sortorder' => $role->sortorder
330 // If groups are in use and enforced throughout the course, then make sure we can meet in at least one course level group
331 if (!empty($course) && $canaccessallgroups) {
332 $usergroups = groups_get_all_groups($course->id, $user->id, $course->defaultgroupingid, 'g.id, g.name,g.description');
333 $userdetails['groups'] = array();
334 foreach ($usergroups as $group) {
335 $group->description = file_rewrite_pluginfile_urls($group->description, 'pluginfile.php', $context->id, 'group', 'description', $group->id);
336 $userdetails['groups'][] = array('id'=>$group->id, 'name'=>$group->name, 'description'=>$group->description);
339 //list of courses where the user is enrolled
340 if (!isset($hiddenfields['mycourses'])) {
341 $enrolledcourses = array();
342 if ($mycourses = enrol_get_users_courses($user->id, true)) {
343 foreach ($mycourses as $mycourse) {
344 if ($mycourse->category) {
345 $coursecontext = get_context_instance(CONTEXT_COURSE, $mycourse->id);
346 $enrolledcourse = array();
347 $enrolledcourse['id'] = $mycourse->id;
348 $enrolledcourse['fullname'] = format_string($mycourse->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $mycourse->id)));
349 $enrolledcourse['shortname'] = format_string($mycourse->shortname, true, array('context' => $coursecontext));
350 $enrolledcourses[] = $enrolledcourse;
353 $userdetails['enrolledcourses'] = $enrolledcourses;
357 //user preferences
358 if ($currentuser) {
359 $preferences = array();
360 $userpreferences = get_user_preferences();
361 foreach($userpreferences as $prefname => $prefvalue) {
362 $preferences[] = array('name' => $prefname, 'value' => $prefvalue);
364 $userdetails['preferences'] = $preferences;
366 return $userdetails;
370 * Return a list of page types
371 * @param string $pagetype current page type
372 * @param stdClass $parentcontext Block's parent context
373 * @param stdClass $currentcontext Current context of block
375 function user_page_type_list($pagetype, $parentcontext, $currentcontext) {
376 return array('user-profile'=>get_string('page-user-profile', 'pagetype'));