|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
use Chamilo\CoreBundle\Entity\ExtraFieldOptions; |
|
6
|
|
|
use ChamiloSession as Session; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* This tool allows platform admins to add users by uploading a CSV or XML file. |
|
10
|
|
|
*/ |
|
11
|
|
|
$cidReset = true; |
|
12
|
|
|
require_once __DIR__.'/../inc/global.inc.php'; |
|
13
|
|
|
|
|
14
|
|
|
// Set this option to true to enforce strict purification for usenames. |
|
15
|
|
|
$purification_option_for_usernames = false; |
|
16
|
|
|
$userId = api_get_user_id(); |
|
17
|
|
|
|
|
18
|
|
|
api_protect_admin_script(true, null); |
|
19
|
|
|
api_protect_limit_for_session_admin(); |
|
20
|
|
|
set_time_limit(0); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param array $users |
|
24
|
|
|
* @param bool $checkUniqueEmail |
|
25
|
|
|
* |
|
26
|
|
|
* @return array |
|
27
|
|
|
*/ |
|
28
|
|
|
function validate_data($users, $checkUniqueEmail = false) |
|
29
|
|
|
{ |
|
30
|
|
|
global $defined_auth_sources; |
|
31
|
|
|
$usernames = []; |
|
32
|
|
|
|
|
33
|
|
|
// 1. Check if mandatory fields are set. |
|
34
|
|
|
$mandatory_fields = ['LastName', 'FirstName']; |
|
35
|
|
|
if ('true' === api_get_setting('registration', false, 'email') || $checkUniqueEmail) { |
|
36
|
|
|
$mandatory_fields[] = 'Email'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$classExistList = []; |
|
40
|
|
|
$usergroup = new UserGroupModel(); |
|
41
|
|
|
foreach ($users as &$user) { |
|
42
|
|
|
$user['has_error'] = false; |
|
43
|
|
|
$user['message'] = ''; |
|
44
|
|
|
|
|
45
|
|
|
foreach ($mandatory_fields as $field) { |
|
46
|
|
|
if (empty($user[$field])) { |
|
47
|
|
|
$user['message'] .= Display::return_message(get_lang($field.'Mandatory'), 'warning'); |
|
48
|
|
|
$user['has_error'] = true; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$username = isset($user['UserName']) ? $user['UserName'] : ''; |
|
53
|
|
|
// 2. Check username, first, check whether it is empty. |
|
54
|
|
|
if (!UserManager::is_username_empty($username)) { |
|
55
|
|
|
// 2.1. Check whether username is too long. |
|
56
|
|
|
if (UserManager::is_username_too_long($username)) { |
|
57
|
|
|
$user['message'] .= Display::return_message(get_lang('This login is too long'), 'warning'); |
|
58
|
|
|
$user['has_error'] = true; |
|
59
|
|
|
} |
|
60
|
|
|
// 2.1.1 |
|
61
|
|
|
$hasDash = strpos($username, '-'); |
|
62
|
|
|
if (false !== $hasDash) { |
|
63
|
|
|
$user['message'] .= Display::return_message( |
|
64
|
|
|
get_lang('The username cannot contain the \' - \' character'), |
|
65
|
|
|
'warning' |
|
66
|
|
|
); |
|
67
|
|
|
$user['has_error'] = true; |
|
68
|
|
|
} |
|
69
|
|
|
// 2.2. Check whether the username was used twice in import file. |
|
70
|
|
|
if (isset($usernames[$username])) { |
|
71
|
|
|
$user['message'] .= Display::return_message(get_lang('Login is used twice'), 'warning'); |
|
72
|
|
|
$user['has_error'] = true; |
|
73
|
|
|
} |
|
74
|
|
|
$usernames[$username] = 1; |
|
75
|
|
|
// 2.3. Check whether username is already occupied. |
|
76
|
|
|
if (!UserManager::is_username_available($username)) { |
|
77
|
|
|
$user['message'] .= Display::return_message(get_lang('This login is not available'), 'warning'); |
|
78
|
|
|
$user['has_error'] = true; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (isset($user['Email'])) { |
|
83
|
|
|
$result = api_valid_email($user['Email']); |
|
84
|
|
|
if (false === $result) { |
|
85
|
|
|
$user['message'] .= Display::return_message( |
|
86
|
|
|
get_lang('Please enter a valid e-mail address !'), |
|
87
|
|
|
'warning' |
|
88
|
|
|
); |
|
89
|
|
|
$user['has_error'] = true; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ($checkUniqueEmail) { |
|
94
|
|
|
if (isset($user['Email'])) { |
|
95
|
|
|
$userFromEmail = api_get_user_info_from_email($user['Email']); |
|
96
|
|
|
if (!empty($userFromEmail)) { |
|
97
|
|
|
$user['message'] .= Display::return_message(get_lang('This email is already in use on the platform.'), 'warning'); |
|
98
|
|
|
$user['has_error'] = true; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// 3. Check status. |
|
104
|
|
|
if (isset($user['Status']) && !api_status_exists($user['Status'])) { |
|
105
|
|
|
$user['message'] .= Display::return_message(get_lang('This status doesn\'t exist'), 'warning'); |
|
106
|
|
|
$user['has_error'] = true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// 4. Check ClassId |
|
110
|
|
|
if (!empty($user['ClassId'])) { |
|
111
|
|
|
$classId = explode('|', trim($user['ClassId'])); |
|
112
|
|
|
foreach ($classId as $id) { |
|
113
|
|
|
if (in_array($id, $classExistList)) { |
|
114
|
|
|
continue; |
|
115
|
|
|
} |
|
116
|
|
|
$info = $usergroup->get($id); |
|
117
|
|
|
if (empty($info)) { |
|
118
|
|
|
$user['message'] .= Display::return_message( |
|
119
|
|
|
sprintf(get_lang('Class ID does not exist'), $id), |
|
120
|
|
|
'warning' |
|
121
|
|
|
); |
|
122
|
|
|
$user['has_error'] = true; |
|
123
|
|
|
} else { |
|
124
|
|
|
$classExistList[] = $info['id']; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// 5. Check authentication source |
|
130
|
|
|
if (!empty($user['AuthSource'])) { |
|
131
|
|
|
if (!in_array($user['AuthSource'], $defined_auth_sources)) { |
|
132
|
|
|
$user['message'] .= Display::return_message(get_lang('Authentication source unavailable.'), 'warning'); |
|
133
|
|
|
$user['has_error'] = true; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $users; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Add missing user-information (which isn't required, like password, username etc). |
|
143
|
|
|
* |
|
144
|
|
|
* @param array $user |
|
145
|
|
|
*/ |
|
146
|
|
|
function complete_missing_data($user) |
|
147
|
|
|
{ |
|
148
|
|
|
global $purification_option_for_usernames; |
|
149
|
|
|
|
|
150
|
|
|
$username = isset($user['UserName']) ? $user['UserName'] : ''; |
|
151
|
|
|
|
|
152
|
|
|
// 1. Create a username if necessary. |
|
153
|
|
|
if (UserManager::is_username_empty($username)) { |
|
154
|
|
|
$user['UserName'] = UserManager::create_unique_username( |
|
155
|
|
|
$user['FirstName'], |
|
156
|
|
|
$user['LastName'] |
|
157
|
|
|
); |
|
158
|
|
|
} else { |
|
159
|
|
|
$user['UserName'] = UserManager::purify_username( |
|
160
|
|
|
$user['UserName'], |
|
161
|
|
|
$purification_option_for_usernames |
|
162
|
|
|
); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
// 2. Generate a password if necessary. |
|
166
|
|
|
if (empty($user['Password'])) { |
|
167
|
|
|
$user['Password'] = api_generate_password(); |
|
168
|
|
|
} |
|
169
|
|
|
// 3. Set status if not allready set. |
|
170
|
|
|
if (empty($user['Status'])) { |
|
171
|
|
|
$user['Status'] = 'user'; |
|
172
|
|
|
} |
|
173
|
|
|
// 4. Set authsource if not allready set. |
|
174
|
|
|
if (empty($user['AuthSource'])) { |
|
175
|
|
|
$user['AuthSource'] = PLATFORM_AUTH_SOURCE; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
if (empty($user['ExpiryDate'])) { |
|
179
|
|
|
$user['ExpiryDate'] = ''; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
if (!isset($user['OfficialCode'])) { |
|
183
|
|
|
$user['OfficialCode'] = ''; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
if (!isset($user['language'])) { |
|
187
|
|
|
$user['language'] = ''; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
if (!isset($user['PhoneNumber'])) { |
|
191
|
|
|
$user['PhoneNumber'] = ''; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
if (!isset($user['OfficialCode'])) { |
|
195
|
|
|
$user['OfficialCode'] = ''; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
return $user; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Save the imported data. |
|
203
|
|
|
* |
|
204
|
|
|
* @param array $users List of users |
|
205
|
|
|
* @param bool $sendMail |
|
206
|
|
|
* |
|
207
|
|
|
* @uses \global variable $inserted_in_course, which returns the list of |
|
208
|
|
|
* courses the user was inserted in |
|
209
|
|
|
*/ |
|
210
|
|
|
function save_data($users, $sendMail = false) |
|
211
|
|
|
{ |
|
212
|
|
|
global $inserted_in_course, $extra_fields; |
|
213
|
|
|
|
|
214
|
|
|
// Not all scripts declare the $inserted_in_course array (although they should). |
|
215
|
|
|
if (!isset($inserted_in_course)) { |
|
216
|
|
|
$inserted_in_course = []; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$usergroup = new UserGroupModel(); |
|
220
|
|
|
if (is_array($users)) { |
|
221
|
|
|
$efo = new ExtraFieldOption('user'); |
|
222
|
|
|
|
|
223
|
|
|
$optionsByField = []; |
|
224
|
|
|
|
|
225
|
|
|
foreach ($users as &$user) { |
|
226
|
|
|
if ($user['has_error']) { |
|
227
|
|
|
continue; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
$user = complete_missing_data($user); |
|
231
|
|
|
$user['Status'] = api_status_key($user['Status']); |
|
232
|
|
|
$redirection = isset($user['Redirection']) ? $user['Redirection'] : ''; |
|
233
|
|
|
|
|
234
|
|
|
$user_id = UserManager::create_user( |
|
235
|
|
|
$user['FirstName'], |
|
236
|
|
|
$user['LastName'], |
|
237
|
|
|
$user['Status'], |
|
238
|
|
|
$user['Email'], |
|
239
|
|
|
$user['UserName'], |
|
240
|
|
|
$user['Password'], |
|
241
|
|
|
$user['OfficialCode'], |
|
242
|
|
|
$user['language'], |
|
243
|
|
|
$user['PhoneNumber'], |
|
244
|
|
|
'', |
|
245
|
|
|
$user['AuthSource'], |
|
246
|
|
|
$user['ExpiryDate'], |
|
247
|
|
|
1, |
|
248
|
|
|
0, |
|
249
|
|
|
null, |
|
250
|
|
|
null, |
|
251
|
|
|
$sendMail, |
|
252
|
|
|
false, |
|
253
|
|
|
'', |
|
254
|
|
|
false, |
|
255
|
|
|
null, |
|
256
|
|
|
null, |
|
257
|
|
|
null, |
|
258
|
|
|
$redirection |
|
259
|
|
|
); |
|
260
|
|
|
|
|
261
|
|
|
if ($user_id) { |
|
262
|
|
|
$returnMessage = Display::return_message(get_lang('The user has been added'), 'success'); |
|
263
|
|
|
|
|
264
|
|
|
if (isset($user['Courses']) && is_array($user['Courses'])) { |
|
265
|
|
|
foreach ($user['Courses'] as $course) { |
|
266
|
|
|
if (CourseManager::course_exists($course)) { |
|
267
|
|
|
$course_info = api_get_course_info($course); |
|
268
|
|
|
|
|
269
|
|
|
$result = CourseManager::subscribeUser($user_id, $course_info['real_id'], $user['Status']); |
|
270
|
|
|
if ($result) { |
|
271
|
|
|
$inserted_in_course[$course] = $course_info['title']; |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
if (isset($user['Sessions']) && is_array($user['Sessions'])) { |
|
278
|
|
|
foreach ($user['Sessions'] as $sessionId) { |
|
279
|
|
|
$sessionInfo = api_get_session_info($sessionId); |
|
280
|
|
|
if (!empty($sessionInfo)) { |
|
281
|
|
|
SessionManager::subscribeUsersToSession( |
|
282
|
|
|
$sessionId, |
|
283
|
|
|
[$user_id], |
|
284
|
|
|
SESSION_VISIBLE_READ_ONLY, |
|
285
|
|
|
false |
|
286
|
|
|
); |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
if (!empty($user['ClassId'])) { |
|
292
|
|
|
$classId = explode('|', trim($user['ClassId'])); |
|
293
|
|
|
foreach ($classId as $id) { |
|
294
|
|
|
$usergroup->subscribe_users_to_usergroup($id, [$user_id], false); |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
// We are sure that the extra field exists. |
|
299
|
|
|
foreach ($extra_fields as $extras) { |
|
300
|
|
|
if (!isset($user[$extras[1]])) { |
|
301
|
|
|
continue; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
$key = $extras[1]; |
|
305
|
|
|
$value = $user[$key]; |
|
306
|
|
|
|
|
307
|
|
|
if (!array_key_exists($key, $optionsByField)) { |
|
308
|
|
|
$optionsByField[$key] = $efo->getOptionsByFieldVariable($key); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** @var ExtraFieldOptions $option */ |
|
312
|
|
|
foreach ($optionsByField[$key] as $option) { |
|
313
|
|
|
if ($option->getDisplayText() === $value) { |
|
314
|
|
|
$value = $option->getValue(); |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
UserManager::update_extra_field_value($user_id, $key, $value); |
|
319
|
|
|
} |
|
320
|
|
|
} else { |
|
321
|
|
|
$returnMessage = Display::return_message(get_lang('Error'), 'warning'); |
|
322
|
|
|
} |
|
323
|
|
|
$user['message'] = $returnMessage; |
|
324
|
|
|
} |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
return $users; |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* @param array $users |
|
332
|
|
|
* @param string $fileName |
|
333
|
|
|
* @param int $sendEmail |
|
334
|
|
|
* @param bool $checkUniqueEmail |
|
335
|
|
|
* @param bool $resumeImport |
|
336
|
|
|
* |
|
337
|
|
|
* @return array |
|
338
|
|
|
*/ |
|
339
|
|
|
function parse_csv_data($users, $fileName, $sendEmail = 0, $checkUniqueEmail = true, $resumeImport = false) |
|
340
|
|
|
{ |
|
341
|
|
|
$usersFromOrigin = $users; |
|
342
|
|
|
$allowRandom = ('true' === api_get_setting('platform.generate_random_login')); |
|
343
|
|
|
if ($allowRandom) { |
|
344
|
|
|
$factory = new RandomLib\Factory(); |
|
345
|
|
|
$generator = $factory->getLowStrengthGenerator(); |
|
346
|
|
|
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
$readMax = 50; |
|
350
|
|
|
$userId = api_get_user_id(); |
|
351
|
|
|
$logMessages = ''; |
|
352
|
|
|
$importData = Session::read('user_import_data_'.$userId); |
|
353
|
|
|
if (!empty($importData)) { |
|
354
|
|
|
$counter = $importData['counter']; |
|
355
|
|
|
$users = $importData['complete_list']; |
|
356
|
|
|
$users = array_splice($users, $counter, $readMax); |
|
357
|
|
|
$logMessages = $importData['log_messages']; |
|
358
|
|
|
} else { |
|
359
|
|
|
$users = array_splice($users, 0, $readMax); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
if (false === $resumeImport) { |
|
363
|
|
|
$users = $usersFromOrigin; |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
$counter = 0; |
|
367
|
|
|
foreach ($users as $index => $user) { |
|
368
|
|
|
if ($resumeImport) { |
|
369
|
|
|
if ($counter >= $readMax) { |
|
370
|
|
|
$users = array_splice($users, $counter, $readMax); |
|
371
|
|
|
break; |
|
372
|
|
|
} |
|
373
|
|
|
} |
|
374
|
|
|
$counter++; |
|
375
|
|
|
if (empty($user['UserName'])) { |
|
376
|
|
|
if ($allowRandom) { |
|
377
|
|
|
$username = $generator->generateString(10, $chars); |
|
378
|
|
|
$user['UserName'] = $username; |
|
379
|
|
|
} |
|
380
|
|
|
} |
|
381
|
|
|
if (isset($user['Courses'])) { |
|
382
|
|
|
$user['Courses'] = explode('|', trim($user['Courses'])); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
if (isset($user['Sessions'])) { |
|
386
|
|
|
$user['Sessions'] = explode('|', trim($user['Sessions'])); |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
// Lastname is needed. |
|
390
|
|
|
if (!isset($user['LastName']) || (isset($user['LastName']) && empty($user['LastName']))) { |
|
391
|
|
|
unset($users[$index]); |
|
392
|
|
|
continue; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
// FirstName is needed. |
|
396
|
|
|
if (!isset($user['FirstName']) || (isset($user['FirstName']) && empty($user['FirstName']))) { |
|
397
|
|
|
unset($users[$index]); |
|
398
|
|
|
continue; |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
$users[$index] = $user; |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
$globalCounter = $counter; |
|
405
|
|
|
if (!empty($importData)) { |
|
406
|
|
|
$globalCounter = $importData['counter'] + $counter; |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
$importData = [ |
|
410
|
|
|
'complete_list' => $usersFromOrigin, |
|
411
|
|
|
'filename' => $fileName, |
|
412
|
|
|
'counter' => $globalCounter, |
|
413
|
|
|
'check_unique_email' => $checkUniqueEmail, |
|
414
|
|
|
'send_email' => $sendEmail, |
|
415
|
|
|
'date' => api_get_utc_datetime(), |
|
416
|
|
|
'log_messages' => $logMessages, |
|
417
|
|
|
'resume' => $resumeImport, |
|
418
|
|
|
]; |
|
419
|
|
|
|
|
420
|
|
|
Session::write('user_import_data_'.$userId, $importData); |
|
421
|
|
|
|
|
422
|
|
|
return $users; |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
/** |
|
426
|
|
|
* Read the XML-file. |
|
427
|
|
|
* |
|
428
|
|
|
* @param string $file Path to the XML-file |
|
429
|
|
|
* |
|
430
|
|
|
* @return array All user information read from the file |
|
431
|
|
|
*/ |
|
432
|
|
|
function parse_xml_data($file) |
|
433
|
|
|
{ |
|
434
|
|
|
$crawler = new \Symfony\Component\DomCrawler\Crawler(); |
|
435
|
|
|
$crawler->addXmlContent(file_get_contents($file)); |
|
436
|
|
|
$crawler = $crawler->filter('Contacts > Contact '); |
|
437
|
|
|
$array = []; |
|
438
|
|
|
foreach ($crawler as $domElement) { |
|
439
|
|
|
$row = []; |
|
440
|
|
|
foreach ($domElement->childNodes as $node) { |
|
441
|
|
|
if ('#text' != $node->nodeName) { |
|
442
|
|
|
$row[$node->nodeName] = $node->nodeValue; |
|
443
|
|
|
} |
|
444
|
|
|
} |
|
445
|
|
|
if (!empty($row)) { |
|
446
|
|
|
$array[] = $row; |
|
447
|
|
|
} |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
return $array; |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
/** |
|
454
|
|
|
* @param array $users |
|
455
|
|
|
* @param bool $sendMail |
|
456
|
|
|
*/ |
|
457
|
|
|
function processUsers(&$users, $sendMail) |
|
458
|
|
|
{ |
|
459
|
|
|
$users = save_data($users, $sendMail); |
|
460
|
|
|
|
|
461
|
|
|
$warningMessage = ''; |
|
462
|
|
|
if (!empty($users)) { |
|
463
|
|
|
$table = new HTML_Table(['class' => 'table table-responsive']); |
|
464
|
|
|
$headers = [ |
|
465
|
|
|
get_lang('User'), |
|
466
|
|
|
get_lang('Status'), |
|
467
|
|
|
]; |
|
468
|
|
|
$row = 0; |
|
469
|
|
|
$column = 0; |
|
470
|
|
|
foreach ($headers as $header) { |
|
471
|
|
|
$table->setHeaderContents($row, $column, $header); |
|
472
|
|
|
$column++; |
|
473
|
|
|
} |
|
474
|
|
|
$row++; |
|
475
|
|
|
foreach ($users as $user) { |
|
476
|
|
|
$column = 0; |
|
477
|
|
|
$email = isset($user['Email']) ? ' - '.$user['Email'] : null; |
|
478
|
|
|
$userData = |
|
479
|
|
|
'<strong>'.$user['UserName'].'</strong> - '. |
|
480
|
|
|
api_get_person_name( |
|
481
|
|
|
$user['FirstName'], |
|
482
|
|
|
$user['LastName'] |
|
483
|
|
|
).' '.$email; |
|
484
|
|
|
$table->setCellContents($row, $column, $userData); |
|
485
|
|
|
$table->setCellContents($row, ++$column, $user['message']); |
|
486
|
|
|
$row++; |
|
487
|
|
|
} |
|
488
|
|
|
$warningMessage = $table->toHtml(); |
|
489
|
|
|
} |
|
490
|
|
|
|
|
491
|
|
|
// if the warning message is too long then we display the warning message trough a session |
|
492
|
|
|
Display::addFlash(Display::return_message(get_lang('File imported'), 'confirmation', false)); |
|
493
|
|
|
|
|
494
|
|
|
$importData = Session::read('user_import_data_'.api_get_user_id()); |
|
495
|
|
|
if (!empty($importData)) { |
|
496
|
|
|
if (isset($importData['log_messages'])) { |
|
497
|
|
|
$importData['log_messages'] .= $warningMessage; |
|
498
|
|
|
} else { |
|
499
|
|
|
$importData['log_messages'] = $warningMessage; |
|
500
|
|
|
} |
|
501
|
|
|
Session::write('user_import_data_'.api_get_user_id(), $importData); |
|
502
|
|
|
} |
|
503
|
|
|
} |
|
504
|
|
|
|
|
505
|
|
|
$this_section = SECTION_PLATFORM_ADMIN; |
|
506
|
|
|
$defined_auth_sources[] = PLATFORM_AUTH_SOURCE; |
|
507
|
|
|
if (isset($extAuthSource) && is_array($extAuthSource)) { |
|
508
|
|
|
$defined_auth_sources = array_merge($defined_auth_sources, array_keys($extAuthSource)); |
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
$tool_name = get_lang('Import users list'); |
|
512
|
|
|
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')]; |
|
513
|
|
|
$reloadImport = (isset($_REQUEST['reload_import']) && 1 === (int) $_REQUEST['reload_import']); |
|
514
|
|
|
|
|
515
|
|
|
$extra_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC', true); |
|
516
|
|
|
|
|
517
|
|
|
if (isset($_POST['formSent']) && $_POST['formSent'] && 0 !== $_FILES['import_file']['size']) { |
|
518
|
|
|
$file_type = $_POST['file_type']; |
|
519
|
|
|
Security::clear_token(); |
|
520
|
|
|
$tok = Security::get_token(); |
|
521
|
|
|
$allowed_file_mimetype = ['csv', 'xml']; |
|
522
|
|
|
$error_kind_file = true; |
|
523
|
|
|
|
|
524
|
|
|
$checkUniqueEmail = isset($_POST['check_unique_email']) ? $_POST['check_unique_email'] : null; |
|
525
|
|
|
$sendMail = $_POST['sendMail'] ? true : false; |
|
526
|
|
|
$resume = isset($_POST['resume_import']) ? true : false; |
|
527
|
|
|
$uploadInfo = pathinfo($_FILES['import_file']['name']); |
|
528
|
|
|
$ext_import_file = $uploadInfo['extension']; |
|
529
|
|
|
|
|
530
|
|
|
$users = []; |
|
531
|
|
|
if (in_array($ext_import_file, $allowed_file_mimetype)) { |
|
532
|
|
|
if (0 === strcmp($file_type, 'csv') && |
|
533
|
|
|
$ext_import_file == $allowed_file_mimetype[0] |
|
534
|
|
|
) { |
|
535
|
|
|
Session::erase('user_import_data_'.$userId); |
|
536
|
|
|
$users = Import::csvToArray($_FILES['import_file']['tmp_name']); |
|
537
|
|
|
$users = parse_csv_data( |
|
538
|
|
|
$users, |
|
539
|
|
|
$_FILES['import_file']['name'], |
|
540
|
|
|
$sendMail, |
|
541
|
|
|
$checkUniqueEmail, |
|
542
|
|
|
$resume |
|
543
|
|
|
); |
|
544
|
|
|
$users = validate_data($users, $checkUniqueEmail); |
|
545
|
|
|
$error_kind_file = false; |
|
546
|
|
|
} elseif (0 === strcmp($file_type, 'xml') && $ext_import_file == $allowed_file_mimetype[1]) { |
|
547
|
|
|
$users = parse_xml_data($_FILES['import_file']['tmp_name']); |
|
548
|
|
|
$users = validate_data($users, $checkUniqueEmail); |
|
549
|
|
|
$error_kind_file = false; |
|
550
|
|
|
} |
|
551
|
|
|
|
|
552
|
|
|
processUsers($users, $sendMail); |
|
553
|
|
|
|
|
554
|
|
|
if ($error_kind_file) { |
|
555
|
|
|
Display::addFlash( |
|
556
|
|
|
Display::return_message( |
|
557
|
|
|
get_lang('You must import a file corresponding to the selected format'), |
|
558
|
|
|
'error', |
|
559
|
|
|
false |
|
560
|
|
|
) |
|
561
|
|
|
); |
|
562
|
|
|
} else { |
|
563
|
|
|
$reload = ''; |
|
564
|
|
|
if ($resume) { |
|
565
|
|
|
$reload = '?reload_import=1'; |
|
566
|
|
|
} |
|
567
|
|
|
header('Location: '.api_get_self().$reload); |
|
568
|
|
|
exit; |
|
569
|
|
|
} |
|
570
|
|
|
} else { |
|
571
|
|
|
Display::addFlash( |
|
572
|
|
|
Display::return_message( |
|
573
|
|
|
get_lang('You must import a file corresponding to the selected format'), |
|
574
|
|
|
'error', |
|
575
|
|
|
false |
|
576
|
|
|
) |
|
577
|
|
|
); |
|
578
|
|
|
//header('Location: '.api_get_path(WEB_CODE_PATH).'admin/user_list.php?sec_token='.$tok); |
|
579
|
|
|
header('Location: '.api_get_self()); |
|
580
|
|
|
exit; |
|
581
|
|
|
} |
|
582
|
|
|
} |
|
583
|
|
|
|
|
584
|
|
|
$importData = Session::read('user_import_data_'.$userId); |
|
585
|
|
|
|
|
586
|
|
|
$formContinue = false; |
|
587
|
|
|
$resumeStop = true; |
|
588
|
|
|
if (!empty($importData)) { |
|
589
|
|
|
$isResume = $importData['resume']; |
|
590
|
|
|
|
|
591
|
|
|
$formContinue = new FormValidator('user_import_continue', 'post', api_get_self()); |
|
592
|
|
|
$label = get_lang('Results and feedback and feedback'); |
|
593
|
|
|
if ($isResume) { |
|
594
|
|
|
$label = get_lang('ContinueLastImport'); |
|
595
|
|
|
} |
|
596
|
|
|
$formContinue->addHeader($label); |
|
597
|
|
|
$formContinue->addLabel(get_lang('File'), $importData['filename']); |
|
598
|
|
|
|
|
599
|
|
|
$resumeStop = true; |
|
600
|
|
|
if ($isResume) { |
|
601
|
|
|
$totalUsers = isset($importData['complete_list']) ? count($importData['complete_list']) : 0; |
|
602
|
|
|
$counter = isset($importData['counter']) ? $importData['counter'] : 0; |
|
603
|
|
|
$bar = ''; |
|
604
|
|
|
if (!empty($totalUsers)) { |
|
605
|
|
|
$bar = Display::bar_progress($counter / $totalUsers * 100); |
|
606
|
|
|
} |
|
607
|
|
|
$formContinue->addLabel(get_lang('Status'), $bar); |
|
608
|
|
|
$formContinue->addLabel( |
|
609
|
|
|
get_lang('Users added'), |
|
610
|
|
|
$importData['counter'].' / '.count($importData['complete_list']) |
|
611
|
|
|
); |
|
612
|
|
|
} else { |
|
613
|
|
|
$formContinue->addLabel( |
|
614
|
|
|
get_lang('Users'), |
|
615
|
|
|
count($importData['complete_list']) |
|
616
|
|
|
); |
|
617
|
|
|
} |
|
618
|
|
|
|
|
619
|
|
|
$formContinue->addLabel( |
|
620
|
|
|
get_lang('Check unique e-mail'), |
|
621
|
|
|
$importData['check_unique_email'] ? get_lang('Yes') : get_lang('No') |
|
622
|
|
|
); |
|
623
|
|
|
$formContinue->addLabel(get_lang('Send a mail to users'), $importData['send_email'] ? get_lang('Yes') : get_lang('No')); |
|
624
|
|
|
$formContinue->addLabel(get_lang('Date'), Display::dateToStringAgoAndLongDate($importData['date'])); |
|
625
|
|
|
|
|
626
|
|
|
if ($isResume) { |
|
627
|
|
|
$resumeStop = $importData['counter'] >= count($importData['complete_list']); |
|
628
|
|
|
if (false == $resumeStop) { |
|
629
|
|
|
$formContinue->addButtonImport(get_lang('ContinueImport'), 'import_continue'); |
|
630
|
|
|
} |
|
631
|
|
|
} |
|
632
|
|
|
|
|
633
|
|
|
$formContinue->addHtml(get_lang('Results and feedback and feedback').'<br />'.$importData['log_messages']); |
|
634
|
|
|
|
|
635
|
|
|
if ($formContinue->validate()) { |
|
636
|
|
|
$users = parse_csv_data( |
|
637
|
|
|
$importData['complete_list'], |
|
638
|
|
|
$importData['filename'], |
|
639
|
|
|
$importData['send_email'], |
|
640
|
|
|
$importData['check_unique_email'], |
|
641
|
|
|
true |
|
642
|
|
|
); |
|
643
|
|
|
$users = validate_data($users, $importData['check_unique_email']); |
|
644
|
|
|
|
|
645
|
|
|
processUsers($users, $importData['send_email']); |
|
646
|
|
|
|
|
647
|
|
|
$reload = ''; |
|
648
|
|
|
if ($isResume && false === $resumeStop) { |
|
649
|
|
|
$reload = '?reload_import=1'; |
|
650
|
|
|
} |
|
651
|
|
|
|
|
652
|
|
|
header('Location: '.api_get_self().$reload); |
|
653
|
|
|
exit; |
|
654
|
|
|
} |
|
655
|
|
|
} |
|
656
|
|
|
|
|
657
|
|
|
Display::display_header($tool_name); |
|
658
|
|
|
|
|
659
|
|
|
$form = new FormValidator('user_import', 'post', api_get_self()); |
|
660
|
|
|
$form->addHeader($tool_name); |
|
661
|
|
|
$form->addElement('hidden', 'formSent'); |
|
662
|
|
|
$form->addElement('file', 'import_file', get_lang('Import marks in an assessment')); |
|
663
|
|
|
$group = [ |
|
664
|
|
|
$form->createElement( |
|
665
|
|
|
'radio', |
|
666
|
|
|
'file_type', |
|
667
|
|
|
'', |
|
668
|
|
|
'CSV (<a href="example.csv" target="_blank" download>'.get_lang('Example CSV file').'</a>)', |
|
669
|
|
|
'csv' |
|
670
|
|
|
), |
|
671
|
|
|
$form->createElement( |
|
672
|
|
|
'radio', |
|
673
|
|
|
'file_type', |
|
674
|
|
|
null, |
|
675
|
|
|
'XML (<a href="example.xml" target="_blank" download>'.get_lang('Example XML file').'</a>)', |
|
676
|
|
|
'xml' |
|
677
|
|
|
), |
|
678
|
|
|
]; |
|
679
|
|
|
|
|
680
|
|
|
$form->addGroup($group, '', get_lang('File type')); |
|
681
|
|
|
|
|
682
|
|
|
$group = [ |
|
683
|
|
|
$form->createElement('radio', 'sendMail', '', get_lang('Yes'), 1), |
|
684
|
|
|
$form->createElement('radio', 'sendMail', null, get_lang('No'), 0), |
|
685
|
|
|
]; |
|
686
|
|
|
$form->addGroup($group, '', get_lang('Send a mail to users')); |
|
687
|
|
|
|
|
688
|
|
|
$form->addElement( |
|
689
|
|
|
'checkbox', |
|
690
|
|
|
'check_unique_email', |
|
691
|
|
|
'', |
|
692
|
|
|
get_lang('Check unique e-mail') |
|
693
|
|
|
); |
|
694
|
|
|
|
|
695
|
|
|
$form->addElement( |
|
696
|
|
|
'checkbox', |
|
697
|
|
|
'resume_import', |
|
698
|
|
|
'', |
|
699
|
|
|
get_lang('Resume import') |
|
700
|
|
|
); |
|
701
|
|
|
|
|
702
|
|
|
$form->addButtonImport(get_lang('Import')); |
|
703
|
|
|
|
|
704
|
|
|
$defaults['formSent'] = 1; |
|
705
|
|
|
$defaults['sendMail'] = 0; |
|
706
|
|
|
$defaults['file_type'] = 'csv'; |
|
707
|
|
|
|
|
708
|
|
|
$extraSettings = api_get_setting('profile.user_import_settings', true); |
|
709
|
|
|
if (!empty($extraSettings) && isset($extraSettings['options']) && |
|
710
|
|
|
isset($extraSettings['options']['send_mail_default_option']) |
|
711
|
|
|
) { |
|
712
|
|
|
$defaults['sendMail'] = $extraSettings['options']['send_mail_default_option']; |
|
713
|
|
|
} |
|
714
|
|
|
|
|
715
|
|
|
$form->setDefaults($defaults); |
|
716
|
|
|
$form->display(); |
|
717
|
|
|
|
|
718
|
|
|
if ($formContinue) { |
|
719
|
|
|
$formContinue->display(); |
|
720
|
|
|
} |
|
721
|
|
|
|
|
722
|
|
|
if ($reloadImport) { |
|
723
|
|
|
echo '<script> |
|
724
|
|
|
|
|
725
|
|
|
$(function() { |
|
726
|
|
|
function reload() { |
|
727
|
|
|
$("#user_import_continue").submit(); |
|
728
|
|
|
} |
|
729
|
|
|
setTimeout(reload, 3000); |
|
730
|
|
|
}); |
|
731
|
|
|
</script>'; |
|
732
|
|
|
} |
|
733
|
|
|
|
|
734
|
|
|
$list = []; |
|
735
|
|
|
$list_reponse = []; |
|
736
|
|
|
$result_xml = ''; |
|
737
|
|
|
$i = 0; |
|
738
|
|
|
$count_fields = count($extra_fields); |
|
739
|
|
|
if ($count_fields > 0) { |
|
740
|
|
|
foreach ($extra_fields as $extra) { |
|
741
|
|
|
$list[] = $extra[1]; |
|
742
|
|
|
$list_reponse[] = 'xxx'; |
|
743
|
|
|
$spaces = ' '; |
|
744
|
|
|
$result_xml .= $spaces.'<'.$extra[1].'>xxx</'.$extra[1].'>'; |
|
745
|
|
|
if ($i != $count_fields - 1) { |
|
746
|
|
|
$result_xml .= '<br/>'; |
|
747
|
|
|
} |
|
748
|
|
|
$i++; |
|
749
|
|
|
} |
|
750
|
|
|
} |
|
751
|
|
|
|
|
752
|
|
|
if ('true' === api_get_setting('admin.plugin_redirection_enabled')) { |
|
753
|
|
|
$list[] = 'Redirection'; |
|
754
|
|
|
$list_reponse[] = api_get_path(WEB_PATH); |
|
755
|
|
|
} |
|
756
|
|
|
|
|
757
|
|
|
$content = '<p>'.get_lang('The CSV file must look like this').' ('.get_lang('Fields in <strong>bold</strong> are mandatory.').') :</p> |
|
758
|
|
|
<blockquote> |
|
759
|
|
|
<pre> |
|
760
|
|
|
<b>LastName</b>;<b>FirstName</b>;<b>Email</b>;UserName;Password;AuthSource;OfficialCode;language;PhoneNumber;Status;ExpiryDate;<span style="color:red;">'; |
|
761
|
|
|
|
|
762
|
|
|
if (count($list) > 0) { |
|
763
|
|
|
$content .= implode(';', $list).';'; |
|
764
|
|
|
} |
|
765
|
|
|
$content .= '</span>Courses;Sessions;ClassId; |
|
766
|
|
|
<b>xxx</b>;<b>xxx</b>;<b>xxx</b>;xxx;xxx;'.implode( |
|
767
|
|
|
'/', |
|
768
|
|
|
$defined_auth_sources |
|
769
|
|
|
).';xxx;english/spanish/(other);xxx;user/teacher/drh;0000-00-00 00:00:00;<span style="color:red;">'; |
|
770
|
|
|
|
|
771
|
|
|
if (count($list_reponse) > 0) { |
|
772
|
|
|
$content .= implode(';', $list_reponse).';'; |
|
773
|
|
|
} |
|
774
|
|
|
$content .= ' |
|
775
|
|
|
</span>xxx1|xxx2|xxx3;sessionId|sessionId|sessionId;1;<br /> |
|
776
|
|
|
</pre> |
|
777
|
|
|
</blockquote> |
|
778
|
|
|
<p>'.get_lang('The XML file must look like this').' ('.get_lang('Fields in <strong>bold</strong> are mandatory.').') :</p> |
|
779
|
|
|
<blockquote> |
|
780
|
|
|
<pre> |
|
781
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
|
782
|
|
|
<Contacts> |
|
783
|
|
|
<Contact> |
|
784
|
|
|
<b><LastName>xxx</LastName></b> |
|
785
|
|
|
<b><FirstName>xxx</FirstName></b> |
|
786
|
|
|
<UserName>xxx</UserName> |
|
787
|
|
|
<Password>xxx</Password> |
|
788
|
|
|
<AuthSource>'.implode(' / ', $defined_auth_sources).'</AuthSource> |
|
789
|
|
|
<b><Email>xxx</Email></b> |
|
790
|
|
|
<OfficialCode>xxx</OfficialCode> |
|
791
|
|
|
<language>english/spanish/(other)</language> |
|
792
|
|
|
<PhoneNumber>xxx</PhoneNumber> |
|
793
|
|
|
<Status>user/teacher/drh</Status>'; |
|
794
|
|
|
|
|
795
|
|
|
if ('' != $result_xml) { |
|
796
|
|
|
$content .= ' <br /><span style="color:red;">'.$result_xml; |
|
797
|
|
|
$content .= ' </span><br />'; |
|
798
|
|
|
} |
|
799
|
|
|
|
|
800
|
|
|
$content .= ' |
|
801
|
|
|
<Courses>xxx1|xxx2|xxx3</Courses> |
|
802
|
|
|
<Sessions>sessionId|sessionId|sessionId</Sessions> |
|
803
|
|
|
<ClassId>1</ClassId> |
|
804
|
|
|
</Contact> |
|
805
|
|
|
</Contacts> |
|
806
|
|
|
</pre> |
|
807
|
|
|
</blockquote>'; |
|
808
|
|
|
|
|
809
|
|
|
echo Display::prose($content); |
|
810
|
|
|
|
|
811
|
|
|
Display::display_footer(); |
|
812
|
|
|
|