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 | */ |
33 | function 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 | } |
bd0f26bd |
40 | |
41 | /// hash the password |
42 | $user->password = hash_internal_user_password($user->password); |
43 | |
fb79269b |
44 | $user->timecreated = time(); |
bd0f26bd |
45 | $user->timemodified = $user->timecreated; |
fb79269b |
46 | |
47 | /// insert the user into the database |
48 | $newuserid = $DB->insert_record('user', $user); |
49 | |
21c968b4 |
50 | /// create USER context for this user |
a0760047 |
51 | get_context_instance(CONTEXT_USER, $newuserid); |
21c968b4 |
52 | |
fb79269b |
53 | return $newuserid; |
bd0f26bd |
54 | |
fb79269b |
55 | } |
56 | |
57 | /** |
58 | * Update a user with a user object (will compare against the ID) |
59 | * @param object $user - the user to update |
60 | */ |
61 | function user_update_user($user) { |
62 | global $DB; |
bd0f26bd |
63 | |
64 | /// set the timecreate field to the current time |
65 | if (!is_object($user)) { |
66 | $user = (object)$user; |
67 | } |
68 | |
69 | /// hash the password |
70 | $user->password = hash_internal_user_password($user->password); |
71 | |
72 | $user->timemodified = time(); |
fb79269b |
73 | $DB->update_record('user', $user); |
74 | } |
75 | |
76 | |
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. |
80 | * |
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 |
84 | */ |
85 | function user_delete_user($user) { |
45fb2cf8 |
86 | return delete_user($user); |
fb79269b |
87 | } |
88 | |
89 | /** |
90 | * Get users by id |
91 | * @param array $userids id of users to retrieve |
92 | * |
93 | */ |
94 | function user_get_users_by_id($userids) { |
95 | global $DB; |
96 | return $DB->get_records_list('user', 'id', $userids); |
97 | } |