REPOSITORY MDL-24205, added more check of path and files
[moodle.git] / user / lib.php
blobf38be6ed3d0cdae69fa178f8c74c995a30b9d921
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
30 * @param object $user user to create
31 * @return int id of the newly created user
33 function user_create_user($user) {
34 global $DB;
36 /// set the timecreate field to the current time
37 if (!is_object($user)) {
38 $user = (object)$user;
41 /// hash the password
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);
53 return $newuserid;
57 /**
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) {
62 global $DB;
64 /// set the timecreate field to the current time
65 if (!is_object($user)) {
66 $user = (object)$user;
69 /// hash the password
70 $user->password = hash_internal_user_password($user->password);
72 $user->timemodified = time();
73 $DB->update_record('user', $user);
77 /**
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) {
86 global $CFG, $DB;
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
119 $delname++;
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);
139 return true;
143 * Get users by id
144 * @param array $userids id of users to retrieve
147 function user_get_users_by_id($userids) {
148 global $DB;
149 return $DB->get_records_list('user', 'id', $userids);