3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
23 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 * @param object $user user to create
31 * @return int id of the newly created user
33 function user_create_user($user) {
36 /// set the timecreate field to the current time
37 if (!is_object($user)) {
38 $user = (object)$user;
42 $user->password
= hash_internal_user_password($user->password
);
44 $user->timecreated
= time();
45 $user->timemodified
= $user->timecreated
;
47 /// insert the user into the database
48 $newuserid = $DB->insert_record('user', $user);
50 /// create USER context for this user
51 get_context_instance(CONTEXT_USER
, $newuserid);
58 * Update a user with a user object (will compare against the ID)
59 * @param object $user - the user to update
61 function user_update_user($user) {
64 /// set the timecreate field to the current time
65 if (!is_object($user)) {
66 $user = (object)$user;
70 $user->password
= hash_internal_user_password($user->password
);
72 $user->timemodified
= time();
73 $DB->update_record('user', $user);
78 * Marks user deleted in internal user database and notifies the auth plugin.
79 * Also unenrols user from all roles and does other cleanup.
81 * @todo Decide if this transaction is really needed (look for internal TODO:)
82 * @param object $user Userobject before delete (without system magic quotes)
83 * @return boolean success
85 function user_delete_user($user) {
87 require_once($CFG->libdir
.'/grouplib.php');
88 require_once($CFG->libdir
.'/gradelib.php');
89 require_once($CFG->dirroot
.'/message/lib.php');
91 // delete all grades - backup is kept in grade_grades_history table
92 if ($grades = grade_grade
::fetch_all(array('userid'=>$user->id
))) {
93 foreach ($grades as $grade) {
94 $grade->delete('userdelete');
98 //move unread messages from this user to read
99 message_move_userfrom_unread2read($user->id
);
101 // unconditionally unenrol from all courses
102 enrol_user_delete($user);
104 // remove from all groups
105 $DB->delete_records('groups_members', array('userid'=>$user->id
));
107 // unenrol from all roles in all contexts
108 role_unassign_all(array('userid'=>$user->id
)); // this might be slow but it is really needed - modules might do some extra cleanup!
110 // now do a final accesslib cleanup - removes all role assingments in user context and context itself
111 delete_context(CONTEXT_USER
, $user->id
);
113 require_once($CFG->dirroot
.'/tag/lib.php');
114 tag_set('user', $user->id
, array());
116 // workaround for bulk deletes of users with the same email address
117 $delname = "$user->email.".time();
118 while ($DB->record_exists('user', array('username'=>$delname))) { // no need to use mnethostid here
122 // mark internal user record as "deleted"
123 $updateuser = new object();
124 $updateuser->id
= $user->id
;
125 $updateuser->deleted
= 1;
126 $updateuser->username
= $delname; // Remember it just in case
127 $updateuser->email
= md5($user->username
);// Store hash of username, useful importing/restoring users
128 $updateuser->idnumber
= ''; // Clear this field to free it up
129 $updateuser->timemodified
= time();
131 $DB->update_record('user', $updateuser);
133 // notify auth plugin - do not block the delete even when plugin fails
134 $authplugin = get_auth_plugin($user->auth
);
135 $authplugin->user_delete($user);
137 events_trigger('user_deleted', $user);
144 * @param array $userids id of users to retrieve
147 function user_get_users_by_id($userids) {
149 return $DB->get_records_list('user', 'id', $userids);