output MDL-20204 Fixed regression incorrect param type
[moodle.git] / user / lib.php
CommitLineData
fb79269b 1<?php
2
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.
14//
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/>.
17
18/**
19 * External user API
20 *
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
25 */
26
27
28/**
29 * Creates a user
30 * @param object $user user to create
31 * @return int id of the newly created user
32 */
33function user_create_user($user) {
34 global $DB;
35
36/// set the timecreate field to the current time
37 if (!is_object($user)) {
38 $user = (object)$user;
39 }
40 $user->timecreated = time();
41
42/// insert the user into the database
43 $newuserid = $DB->insert_record('user', $user);
44
45 return $newuserid;
46
47}
48
49/**
50 * Update a user with a user object (will compare against the ID)
51 * @param object $user - the user to update
52 */
53function user_update_user($user) {
54 global $DB;
9baf3a7b 55 $user['timemodified'] = time();
fb79269b 56 $DB->update_record('user', $user);
57}
58
59
60/**
61 * Marks user deleted in internal user database and notifies the auth plugin.
62 * Also unenrols user from all roles and does other cleanup.
63 *
64 * @todo Decide if this transaction is really needed (look for internal TODO:)
65 * @param object $user Userobject before delete (without system magic quotes)
66 * @return boolean success
67 */
68function user_delete_user($user) {
69 global $CFG, $DB;
70 require_once($CFG->libdir.'/grouplib.php');
71 require_once($CFG->libdir.'/gradelib.php');
72 require_once($CFG->dirroot.'/message/lib.php');
73
74 // delete all grades - backup is kept in grade_grades_history table
75 if ($grades = grade_grade::fetch_all(array('userid'=>$user->id))) {
76 foreach ($grades as $grade) {
77 $grade->delete('userdelete');
78 }
79 }
80
81 //move unread messages from this user to read
82 message_move_userfrom_unread2read($user->id);
83
84 // remove from all groups
85 $DB->delete_records('groups_members', array('userid'=>$user->id));
86
87 // unenrol from all roles in all contexts
88 role_unassign(0, $user->id); // this might be slow but it is really needed - modules might do some extra cleanup!
89
90 // now do a final accesslib cleanup - removes all role assingments in user context and context itself
91 delete_context(CONTEXT_USER, $user->id);
92
93 require_once($CFG->dirroot.'/tag/lib.php');
94 tag_set('user', $user->id, array());
95
96 // workaround for bulk deletes of users with the same email address
97 $delname = "$user->email.".time();
98 while ($DB->record_exists('user', array('username'=>$delname))) { // no need to use mnethostid here
99 $delname++;
100 }
101
102 // mark internal user record as "deleted"
103 $updateuser = new object();
104 $updateuser->id = $user->id;
105 $updateuser->deleted = 1;
106 $updateuser->username = $delname; // Remember it just in case
107 $updateuser->email = md5($user->username);// Store hash of username, useful importing/restoring users
108 $updateuser->idnumber = ''; // Clear this field to free it up
109 $updateuser->timemodified = time();
110
111 $DB->update_record('user', $updateuser);
112
113 // notify auth plugin - do not block the delete even when plugin fails
114 $authplugin = get_auth_plugin($user->auth);
115 $authplugin->user_delete($user);
116
117 events_trigger('user_deleted', $user);
118
119 return true;
120}
121
122/**
123 * Get users by id
124 * @param array $userids id of users to retrieve
125 *
126 */
127function user_get_users_by_id($userids) {
128 global $DB;
129 return $DB->get_records_list('user', 'id', $userids);
130}