weekly release 2.9dev
[moodle.git] / admin / tool / uploaduser / index.php
CommitLineData
9e492db0 1<?php
8bdb31ed
PS
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
0a6150ca 16
8bdb31ed
PS
17/**
18 * Bulk user registration script from a comma separated file
19 *
ce15d56d
PS
20 * @package tool
21 * @subpackage uploaduser
8bdb31ed
PS
22 * @copyright 2004 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
0a6150ca 25
ce15d56d 26require('../../../config.php');
cc891abe 27require_once($CFG->libdir.'/adminlib.php');
e4e38544 28require_once($CFG->libdir.'/csvlib.class.php');
29require_once($CFG->dirroot.'/user/profile/lib.php');
bb78e249 30require_once($CFG->dirroot.'/user/lib.php');
59186c92 31require_once($CFG->dirroot.'/group/lib.php');
92b59a56 32require_once($CFG->dirroot.'/cohort/lib.php');
ce15d56d
PS
33require_once('locallib.php');
34require_once('user_form.php');
1ae083e4 35
e4e38544 36$iid = optional_param('iid', '', PARAM_INT);
df7ecfe4 37$previewrows = optional_param('previewrows', 10, PARAM_INT);
066bfbfe 38
3ef7279f 39core_php_time_limit::raise(60*60); // 1 hour should be enough
8bdb31ed 40raise_memory_limit(MEMORY_HUGE);
0a6150ca 41
ebff4779 42require_login();
ce15d56d 43admin_externalpage_setup('tooluploaduser');
bf006d2c 44require_capability('moodle/site:uploadusers', context_system::instance());
0a6150ca 45
ce15d56d 46$struserrenamed = get_string('userrenamed', 'tool_uploaduser');
e4e38544 47$strusernotrenamedexists = get_string('usernotrenamedexists', 'error');
48$strusernotrenamedmissing = get_string('usernotrenamedmissing', 'error');
49$strusernotrenamedoff = get_string('usernotrenamedoff', 'error');
50$strusernotrenamedadmin = get_string('usernotrenamedadmin', 'error');
51
ce15d56d 52$struserupdated = get_string('useraccountupdated', 'tool_uploaduser');
e4e38544 53$strusernotupdated = get_string('usernotupdatederror', 'error');
54$strusernotupdatednotexists = get_string('usernotupdatednotexists', 'error');
55$strusernotupdatedadmin = get_string('usernotupdatedadmin', 'error');
56
ce15d56d 57$struseruptodate = get_string('useraccountuptodate', 'tool_uploaduser');
8bdb31ed 58
e4e38544 59$struseradded = get_string('newuser');
60$strusernotadded = get_string('usernotaddedregistered', 'error');
61$strusernotaddederror = get_string('usernotaddederror', 'error');
62
ce15d56d 63$struserdeleted = get_string('userdeleted', 'tool_uploaduser');
e4e38544 64$strusernotdeletederror = get_string('usernotdeletederror', 'error');
65$strusernotdeletedmissing = get_string('usernotdeletedmissing', 'error');
66$strusernotdeletedoff = get_string('usernotdeletedoff', 'error');
67$strusernotdeletedadmin = get_string('usernotdeletedadmin', 'error');
68
69$strcannotassignrole = get_string('cannotassignrole', 'error');
e4e38544 70
71$struserauthunsupported = get_string('userauthunsupported', 'error');
3fe2cfb5 72$stremailduplicate = get_string('useremailduplicate', 'error');
e4e38544 73
3f12c146 74$strinvalidpasswordpolicy = get_string('invalidpasswordpolicy', 'error');
e4e38544 75$errorstr = get_string('error');
ca23a9c9 76
08157bfe
PS
77$stryes = get_string('yes');
78$strno = get_string('no');
79$stryesnooptions = array(0=>$strno, 1=>$stryes);
80
ce15d56d 81$returnurl = new moodle_url('/admin/tool/uploaduser/index.php');
8bdb31ed 82$bulknurl = new moodle_url('/admin/user/user_bulk.php');
e4e38544 83
2a6dcb72
PS
84$today = time();
85$today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
86
e4e38544 87// array of all valid fields for validation
c00252fb 88$STD_FIELDS = array('id', 'username', 'email',
8bdb31ed 89 'city', 'country', 'lang', 'timezone', 'mailformat',
a80b5a0c 90 'maildisplay', 'maildigest', 'htmleditor', 'autosubscribe',
8bdb31ed 91 'institution', 'department', 'idnumber', 'skype',
6dbcacee 92 'msn', 'aim', 'yahoo', 'icq', 'phone1', 'phone2', 'address',
8bdb31ed
PS
93 'url', 'description', 'descriptionformat', 'password',
94 'auth', // watch out when changing auth type or using external auth plugins!
95 'oldusername', // use when renaming users - this is the original username
08157bfe 96 'suspended', // 1 means suspend user account, 0 means activate user account, nothing means keep as is for existing users
8bdb31ed 97 'deleted', // 1 means delete user
9dcae46d 98 'mnethostid', // Can not be used for adding, updating or deleting of users - only for enrolments, groups, cohorts and suspending.
8bdb31ed 99 );
c00252fb 100// Include all name fields.
6f4ece9f 101$STD_FIELDS = array_merge($STD_FIELDS, get_all_user_name_fields());
e4e38544 102
103$PRF_FIELDS = array();
104
b650fe6a
RT
105if ($proffields = $DB->get_records('user_info_field')) {
106 foreach ($proffields as $key => $proffield) {
107 $profilefieldname = 'profile_field_'.$proffield->shortname;
108 $PRF_FIELDS[] = $profilefieldname;
109 // Re-index $proffields with key as shortname. This will be
110 // used while checking if profile data is key and needs to be converted (eg. menu profile field)
111 $proffields[$profilefieldname] = $proffield;
112 unset($proffields[$key]);
e4e38544 113 }
e4e38544 114}
115
116if (empty($iid)) {
8bdb31ed 117 $mform1 = new admin_uploaduser_form1();
0a6150ca 118
8bdb31ed 119 if ($formdata = $mform1->get_data()) {
e4e38544 120 $iid = csv_import_reader::get_new_iid('uploaduser');
121 $cir = new csv_import_reader($iid, 'uploaduser');
df7ecfe4 122
8bdb31ed
PS
123 $content = $mform1->get_file_content('userfile');
124
125 $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name);
a2071fbf 126 $csvloaderror = $cir->get_error();
e4e38544 127 unset($content);
df7ecfe4 128
a2071fbf
AG
129 if (!is_null($csvloaderror)) {
130 print_error('csvloaderror', '', $returnurl, $csvloaderror);
ed22a01b 131 }
8bdb31ed
PS
132 // test if columns ok
133 $filecolumns = uu_validate_user_upload_columns($cir, $STD_FIELDS, $PRF_FIELDS, $returnurl);
e4e38544 134 // continue to form2
df7ecfe4 135
136 } else {
61ef8f9f 137 echo $OUTPUT->header();
9e492db0 138
ce15d56d 139 echo $OUTPUT->heading_with_help(get_string('uploadusers', 'tool_uploaduser'), 'uploadusers', 'tool_uploaduser');
9e492db0 140
8bdb31ed 141 $mform1->display();
73d6f52f 142 echo $OUTPUT->footer();
df7ecfe4 143 die;
144 }
e4e38544 145} else {
146 $cir = new csv_import_reader($iid, 'uploaduser');
8bdb31ed 147 $filecolumns = uu_validate_user_upload_columns($cir, $STD_FIELDS, $PRF_FIELDS, $returnurl);
df7ecfe4 148}
149
8bdb31ed 150$mform2 = new admin_uploaduser_form2(null, array('columns'=>$filecolumns, 'data'=>array('iid'=>$iid, 'previewrows'=>$previewrows)));
df7ecfe4 151
152// If a file has been uploaded, then process it
8bdb31ed 153if ($formdata = $mform2->is_cancelled()) {
e4e38544 154 $cir->cleanup(true);
155 redirect($returnurl);
df7ecfe4 156
8bdb31ed 157} else if ($formdata = $mform2->get_data()) {
df7ecfe4 158 // Print the header
61ef8f9f 159 echo $OUTPUT->header();
ce15d56d 160 echo $OUTPUT->heading(get_string('uploadusersresult', 'tool_uploaduser'));
e4e38544 161
162 $optype = $formdata->uutype;
163
0c4807ab 164 $updatetype = isset($formdata->uuupdatetype) ? $formdata->uuupdatetype : 0;
8bdb31ed
PS
165 $createpasswords = (!empty($formdata->uupasswordnew) and $optype != UU_USER_UPDATE);
166 $updatepasswords = (!empty($formdata->uupasswordold) and $optype != UU_USER_ADDNEW and $optype != UU_USER_ADDINC and ($updatetype == UU_UPDATE_FILEOVERRIDE or $updatetype == UU_UPDATE_ALLOVERRIDE));
167 $allowrenames = (!empty($formdata->uuallowrenames) and $optype != UU_USER_ADDNEW and $optype != UU_USER_ADDINC);
168 $allowdeletes = (!empty($formdata->uuallowdeletes) and $optype != UU_USER_ADDNEW and $optype != UU_USER_ADDINC);
08157bfe 169 $allowsuspends = (!empty($formdata->uuallowsuspends));
0c4807ab 170 $bulk = $formdata->uubulk;
171 $noemailduplicates = $formdata->uunoemailduplicates;
8bdb31ed
PS
172 $standardusernames = $formdata->uustandardusernames;
173 $resetpasswords = isset($formdata->uuforcepasswordchange) ? $formdata->uuforcepasswordchange : UU_PWRESET_NONE;
e4e38544 174
175 // verification moved to two places: after upload and into form2
8bdb31ed
PS
176 $usersnew = 0;
177 $usersupdated = 0;
178 $usersuptodate = 0; //not printed yet anywhere
179 $userserrors = 0;
180 $deletes = 0;
181 $deleteerrors = 0;
182 $renames = 0;
183 $renameerrors = 0;
184 $usersskipped = 0;
d6bb2d2f 185 $weakpasswords = 0;
feecd4d6 186
e4e38544 187 // caches
8bdb31ed 188 $ccache = array(); // course cache - do not fetch all courses here, we will not probably use them all anyway!
92b59a56 189 $cohorts = array();
8bdb31ed
PS
190 $rolecache = uu_allowed_roles_cache(); // roles lookup cache
191 $manualcache = array(); // cache of used manual enrol plugins in each course
192 $supportedauths = uu_supported_auths(); // officially supported plugins that are enabled
e4e38544 193
df997f84
PS
194 // we use only manual enrol plugin here, if it is disabled no enrol is done
195 if (enrol_is_enabled('manual')) {
196 $manual = enrol_get_plugin('manual');
197 } else {
198 $manual = NULL;
e4e38544 199 }
df7ecfe4 200
b4bd91ce 201 // clear bulk selection
e4e38544 202 if ($bulk) {
cd1edf9e 203 $SESSION->bulk_users = array();
e4e38544 204 }
df7ecfe4 205
e4e38544 206 // init csv import helper
207 $cir->init();
208 $linenum = 1; //column header is first line
df7ecfe4 209
e4e38544 210 // init upload progress tracker
211 $upt = new uu_progress_tracker();
8bdb31ed 212 $upt->start(); // start table
a2ce7344 213
e4e38544 214 while ($line = $cir->next()) {
215 $upt->flush();
216 $linenum++;
217
218 $upt->track('line', $linenum);
219
a226a972 220 $user = new stdClass();
8bdb31ed 221
e4e38544 222 // add fields to user object
8bdb31ed
PS
223 foreach ($line as $keynum => $value) {
224 if (!isset($filecolumns[$keynum])) {
225 // this should not happen
226 continue;
227 }
228 $key = $filecolumns[$keynum];
229 if (strpos($key, 'profile_field_') === 0) {
230 //NOTE: bloody mega hack alert!!
231 if (isset($USER->$key) and is_array($USER->$key)) {
232 // this must be some hacky field that is abusing arrays to store content and format
233 $user->$key = array();
234 $user->$key['text'] = $value;
235 $user->$key['format'] = FORMAT_MOODLE;
236 } else {
7ca44a49 237 $user->$key = trim($value);
e4e38544 238 }
c3231d1d 239 } else {
7ca44a49 240 $user->$key = trim($value);
a7db355a 241 }
e4e38544 242
8bdb31ed
PS
243 if (in_array($key, $upt->columns)) {
244 // default value in progress tracking table, can be changed later
245 $upt->track($key, s($value), 'normal');
e4e38544 246 }
8bdb31ed
PS
247 }
248 if (!isset($user->username)) {
ce8df92d 249 // prevent warnings below
8bdb31ed
PS
250 $user->username = '';
251 }
e4e38544 252
8bdb31ed
PS
253 if ($optype == UU_USER_ADDNEW or $optype == UU_USER_ADDINC) {
254 // user creation is a special case - the username may be constructed from templates using firstname and lastname
255 // better never try this in mixed update types
e4e38544 256 $error = false;
e4e38544 257 if (!isset($user->firstname) or $user->firstname === '') {
258 $upt->track('status', get_string('missingfield', 'error', 'firstname'), 'error');
259 $upt->track('firstname', $errorstr, 'error');
260 $error = true;
261 }
262 if (!isset($user->lastname) or $user->lastname === '') {
263 $upt->track('status', get_string('missingfield', 'error', 'lastname'), 'error');
264 $upt->track('lastname', $errorstr, 'error');
265 $error = true;
266 }
267 if ($error) {
268 $userserrors++;
269 continue;
270 }
271 // we require username too - we might use template for it though
8bdb31ed
PS
272 if (empty($user->username) and !empty($formdata->username)) {
273 $user->username = uu_process_template($formdata->username, $user);
274 $upt->track('username', s($user->username));
e4e38544 275 }
a7db355a 276 }
e4e38544 277
278 // normalize username
8bdb31ed
PS
279 $originalusername = $user->username;
280 if ($standardusernames) {
281 $user->username = clean_param($user->username, PARAM_USERNAME);
282 }
07ed083e 283
8bdb31ed 284 // make sure we really have username
e4e38544 285 if (empty($user->username)) {
286 $upt->track('status', get_string('missingfield', 'error', 'username'), 'error');
287 $upt->track('username', $errorstr, 'error');
288 $userserrors++;
289 continue;
8bdb31ed
PS
290 } else if ($user->username === 'guest') {
291 $upt->track('status', get_string('guestnoeditprofileother', 'error'), 'error');
292 $userserrors++;
293 continue;
e4e38544 294 }
9dcae46d 295
7bf99ee5
AA
296 if ($user->username !== clean_param($user->username, PARAM_USERNAME)) {
297 $upt->track('status', get_string('invalidusername', 'error', 'username'), 'error');
298 $upt->track('username', $errorstr, 'error');
299 $userserrors++;
300 }
397ccf13 301
9dcae46d
PS
302 if (empty($user->mnethostid)) {
303 $user->mnethostid = $CFG->mnet_localhost_id;
304 }
305
306 if ($existinguser = $DB->get_record('user', array('username'=>$user->username, 'mnethostid'=>$user->mnethostid))) {
e4e38544 307 $upt->track('id', $existinguser->id, 'normal', false);
308 }
309
9dcae46d
PS
310 if ($user->mnethostid == $CFG->mnet_localhost_id) {
311 $remoteuser = false;
312
313 // Find out if username incrementing required.
314 if ($existinguser and $optype == UU_USER_ADDINC) {
315 $user->username = uu_increment_username($user->username);
316 $existinguser = false;
317 }
318
319 } else {
320 if (!$existinguser or $optype == UU_USER_ADDINC) {
321 $upt->track('status', get_string('errormnetadd', 'tool_uploaduser'), 'error');
322 $userserrors++;
323 continue;
324 }
325
326 $remoteuser = true;
327
328 // Make sure there are no changes of existing fields except the suspended status.
329 foreach ((array)$existinguser as $k => $v) {
330 if ($k === 'suspended') {
331 continue;
332 }
333 if (property_exists($user, $k)) {
334 $user->$k = $v;
335 }
336 if (in_array($k, $upt->columns)) {
337 if ($k === 'password' or $k === 'oldusername' or $k === 'deleted') {
338 $upt->track($k, '', 'normal', false);
339 } else {
340 $upt->track($k, s($v), 'normal', false);
341 }
342 }
343 }
344 unset($user->oldusername);
345 unset($user->password);
346 $user->auth = $existinguser->auth;
e4e38544 347 }
348
8bdb31ed
PS
349 // notify about nay username changes
350 if ($originalusername !== $user->username) {
351 $upt->track('username', '', 'normal', false); // clear previous
352 $upt->track('username', s($originalusername).'-->'.s($user->username), 'info');
353 } else {
354 $upt->track('username', s($user->username), 'normal', false);
355 }
356
e4e38544 357 // add default values for remaining fields
8bdb31ed 358 $formdefaults = array();
e4e38544 359 foreach ($STD_FIELDS as $field) {
360 if (isset($user->$field)) {
361 continue;
362 }
363 // all validation moved to form2
364 if (isset($formdata->$field)) {
365 // process templates
8bdb31ed
PS
366 $user->$field = uu_process_template($formdata->$field, $user);
367 $formdefaults[$field] = true;
43070e61
PS
368 if (in_array($field, $upt->columns)) {
369 $upt->track($field, s($user->$field), 'normal');
370 }
e4e38544 371 }
372 }
373 foreach ($PRF_FIELDS as $field) {
374 if (isset($user->$field)) {
375 continue;
376 }
377 if (isset($formdata->$field)) {
378 // process templates
8bdb31ed 379 $user->$field = uu_process_template($formdata->$field, $user);
b650fe6a
RT
380
381 // Form contains key and later code expects value.
382 // Convert key to value for required profile fields.
383 require_once($CFG->dirroot.'/user/profile/field/'.$proffields[$field]->datatype.'/field.class.php');
384 $profilefieldclass = 'profile_field_'.$proffields[$field]->datatype;
385 $profilefield = new $profilefieldclass($proffields[$field]->id);
386 if (method_exists($profilefield, 'convert_external_data')) {
387 $user->$field = $profilefield->edit_save_data_preprocess($user->$field, null);
388 }
389
8bdb31ed 390 $formdefaults[$field] = true;
e4e38544 391 }
392 }
393
394 // delete user
395 if (!empty($user->deleted)) {
9dcae46d 396 if (!$allowdeletes or $remoteuser) {
e4e38544 397 $usersskipped++;
398 $upt->track('status', $strusernotdeletedoff, 'warning');
399 continue;
400 }
401 if ($existinguser) {
4f0c2d00 402 if (is_siteadmin($existinguser->id)) {
e4e38544 403 $upt->track('status', $strusernotdeletedadmin, 'error');
404 $deleteerrors++;
405 continue;
406 }
407 if (delete_user($existinguser)) {
408 $upt->track('status', $struserdeleted);
409 $deletes++;
410 } else {
411 $upt->track('status', $strusernotdeletederror, 'error');
412 $deleteerrors++;
6b09974b 413 }
e4e38544 414 } else {
415 $upt->track('status', $strusernotdeletedmissing, 'error');
416 $deleteerrors++;
417 }
418 continue;
419 }
420 // we do not need the deleted flag anymore
421 unset($user->deleted);
422
423 // renaming requested?
424 if (!empty($user->oldusername) ) {
e4e38544 425 if (!$allowrenames) {
426 $usersskipped++;
427 $upt->track('status', $strusernotrenamedoff, 'warning');
428 continue;
429 }
430
431 if ($existinguser) {
432 $upt->track('status', $strusernotrenamedexists, 'error');
433 $renameerrors++;
434 continue;
16a1fed4 435 }
ed22a01b 436
8bdb31ed
PS
437 if ($user->username === 'guest') {
438 $upt->track('status', get_string('guestnoeditprofileother', 'error'), 'error');
439 $renameerrors++;
440 continue;
441 }
442
443 if ($standardusernames) {
444 $oldusername = clean_param($user->oldusername, PARAM_USERNAME);
445 } else {
446 $oldusername = $user->oldusername;
447 }
448
449 // no guessing when looking for old username, it must be exact match
450 if ($olduser = $DB->get_record('user', array('username'=>$oldusername, 'mnethostid'=>$CFG->mnet_localhost_id))) {
e4e38544 451 $upt->track('id', $olduser->id, 'normal', false);
4f0c2d00 452 if (is_siteadmin($olduser->id)) {
e4e38544 453 $upt->track('status', $strusernotrenamedadmin, 'error');
454 $renameerrors++;
a7db355a 455 continue;
0063abee 456 }
df997f84
PS
457 $DB->set_field('user', 'username', $user->username, array('id'=>$olduser->id));
458 $upt->track('username', '', 'normal', false); // clear previous
8bdb31ed 459 $upt->track('username', s($oldusername).'-->'.s($user->username), 'info');
df997f84
PS
460 $upt->track('status', $struserrenamed);
461 $renames++;
e4e38544 462 } else {
463 $upt->track('status', $strusernotrenamedmissing, 'error');
464 $renameerrors++;
465 continue;
466 }
467 $existinguser = $olduser;
468 $existinguser->username = $user->username;
469 }
470
471 // can we process with update or insert?
472 $skip = false;
473 switch ($optype) {
8bdb31ed 474 case UU_USER_ADDNEW:
e4e38544 475 if ($existinguser) {
476 $usersskipped++;
477 $upt->track('status', $strusernotadded, 'warning');
3fe2cfb5 478 $skip = true;
e4e38544 479 }
480 break;
481
8bdb31ed 482 case UU_USER_ADDINC:
e4e38544 483 if ($existinguser) {
484 //this should not happen!
485 $upt->track('status', $strusernotaddederror, 'error');
486 $userserrors++;
8bdb31ed 487 $skip = true;
e4e38544 488 }
489 break;
490
8bdb31ed 491 case UU_USER_ADD_UPDATE:
e4e38544 492 break;
493
8bdb31ed 494 case UU_USER_UPDATE:
e4e38544 495 if (!$existinguser) {
496 $usersskipped++;
497 $upt->track('status', $strusernotupdatednotexists, 'warning');
498 $skip = true;
499 }
500 break;
8bdb31ed
PS
501
502 default:
503 // unknown type
504 $skip = true;
e4e38544 505 }
506
507 if ($skip) {
508 continue;
509 }
510
511 if ($existinguser) {
512 $user->id = $existinguser->id;
513
8bdb31ed 514 $upt->track('username', html_writer::link(new moodle_url('/user/profile.php', array('id'=>$existinguser->id)), s($existinguser->username)), 'normal', false);
08157bfe 515 $upt->track('suspended', $stryesnooptions[$existinguser->suspended] , 'normal', false);
628e9b20 516 $upt->track('auth', $existinguser->auth, 'normal', false);
8bdb31ed 517
4f0c2d00 518 if (is_siteadmin($user->id)) {
e4e38544 519 $upt->track('status', $strusernotupdatedadmin, 'error');
520 $userserrors++;
521 continue;
522 }
523
8bdb31ed
PS
524 $existinguser->timemodified = time();
525 // do NOT mess with timecreated or firstaccess here!
526
527 //load existing profile data
528 profile_load_data($existinguser);
3fe2cfb5 529
8bdb31ed 530 $doupdate = false;
08157bfe 531 $dologout = false;
8bdb31ed 532
9dcae46d 533 if ($updatetype != UU_UPDATE_NOCHANGES and !$remoteuser) {
8bdb31ed
PS
534 if (!empty($user->auth) and $user->auth !== $existinguser->auth) {
535 $upt->track('auth', s($existinguser->auth).'-->'.s($user->auth), 'info', false);
536 $existinguser->auth = $user->auth;
537 if (!isset($supportedauths[$user->auth])) {
538 $upt->track('auth', $struserauthunsupported, 'warning');
539 }
df833016 540 $doupdate = true;
08157bfe
PS
541 if ($existinguser->auth === 'nologin') {
542 $dologout = true;
543 }
e4e38544 544 }
8bdb31ed
PS
545 $allcolumns = array_merge($STD_FIELDS, $PRF_FIELDS);
546 foreach ($allcolumns as $column) {
08157bfe 547 if ($column === 'username' or $column === 'password' or $column === 'auth' or $column === 'suspended') {
8bdb31ed 548 // these can not be changed here
e4e38544 549 continue;
550 }
8bdb31ed 551 if (!property_exists($user, $column) or !property_exists($existinguser, $column)) {
8bdb31ed
PS
552 continue;
553 }
554 if ($updatetype == UU_UPDATE_MISSING) {
555 if (!is_null($existinguser->$column) and $existinguser->$column !== '') {
e4e38544 556 continue;
a7db355a 557 }
8bdb31ed
PS
558 } else if ($updatetype == UU_UPDATE_ALLOVERRIDE) {
559 // we override everything
3f12c146 560
8bdb31ed
PS
561 } else if ($updatetype == UU_UPDATE_FILEOVERRIDE) {
562 if (!empty($formdefaults[$column])) {
563 // do not override with form defaults
564 continue;
565 }
566 }
567 if ($existinguser->$column !== $user->$column) {
568 if ($column === 'email') {
569 if ($DB->record_exists('user', array('email'=>$user->email))) {
570 if ($noemailduplicates) {
571 $upt->track('email', $stremailduplicate, 'error');
572 $upt->track('status', $strusernotupdated, 'error');
573 $userserrors++;
574 continue 2;
575 } else {
576 $upt->track('email', $stremailduplicate, 'warning');
3f12c146
RW
577 }
578 }
8bdb31ed
PS
579 if (!validate_email($user->email)) {
580 $upt->track('email', get_string('invalidemail'), 'warning');
e4e38544 581 }
8bdb31ed 582 }
3f12c146 583
4d3cd148
PS
584 if ($column === 'lang') {
585 if (empty($user->lang)) {
586 // Do not change to not-set value.
587 continue;
588 } else if (clean_param($user->lang, PARAM_LANG) === '') {
589 $upt->track('status', get_string('cannotfindlang', 'error', $user->lang), 'warning');
590 continue;
591 }
592 }
593
8bdb31ed
PS
594 if (in_array($column, $upt->columns)) {
595 $upt->track($column, s($existinguser->$column).'-->'.s($user->$column), 'info', false);
a7db355a 596 }
8bdb31ed
PS
597 $existinguser->$column = $user->$column;
598 $doupdate = true;
6b09974b 599 }
a7db355a 600 }
8bdb31ed 601 }
ed22a01b 602
8bdb31ed 603 try {
3f12c146 604 $auth = get_auth_plugin($existinguser->auth);
8bdb31ed
PS
605 } catch (Exception $e) {
606 $upt->track('auth', get_string('userautherror', 'error', s($existinguser->auth)), 'error');
607 $upt->track('status', $strusernotupdated, 'error');
608 $userserrors++;
609 continue;
610 }
611 $isinternalauth = $auth->is_internal();
3f12c146 612
08157bfe
PS
613 // deal with suspending and activating of accounts
614 if ($allowsuspends and isset($user->suspended) and $user->suspended !== '') {
615 $user->suspended = $user->suspended ? 1 : 0;
616 if ($existinguser->suspended != $user->suspended) {
617 $upt->track('suspended', '', 'normal', false);
618 $upt->track('suspended', $stryesnooptions[$existinguser->suspended].'-->'.$stryesnooptions[$user->suspended], 'info', false);
619 $existinguser->suspended = $user->suspended;
620 $doupdate = true;
621 if ($existinguser->suspended) {
622 $dologout = true;
623 }
624 }
625 }
626
8bdb31ed
PS
627 // changing of passwords is a special case
628 // do not force password changes for external auth plugins!
629 $oldpw = $existinguser->password;
9dcae46d
PS
630
631 if ($remoteuser) {
632 // Do not mess with passwords of remote users.
633
634 } else if (!$isinternalauth) {
ec2d8ceb 635 $existinguser->password = AUTH_PASSWORD_NOT_CACHED;
8bdb31ed
PS
636 $upt->track('password', '-', 'normal', false);
637 // clean up prefs
638 unset_user_preference('create_password', $existinguser);
639 unset_user_preference('auth_forcepasswordchange', $existinguser);
640
641 } else if (!empty($user->password)) {
642 if ($updatepasswords) {
ec2d8ceb
SC
643 // Check for passwords that we want to force users to reset next
644 // time they log in.
8bdb31ed 645 $errmsg = null;
faceca91 646 $weak = !check_password_policy($user->password, $errmsg);
8bdb31ed
PS
647 if ($resetpasswords == UU_PWRESET_ALL or ($resetpasswords == UU_PWRESET_WEAK and $weak)) {
648 if ($weak) {
649 $weakpasswords++;
650 $upt->track('password', $strinvalidpasswordpolicy, 'warning');
651 }
652 set_user_preference('auth_forcepasswordchange', 1, $existinguser);
653 } else {
654 unset_user_preference('auth_forcepasswordchange', $existinguser);
655 }
656 unset_user_preference('create_password', $existinguser); // no need to create password any more
ec2d8ceb
SC
657
658 // Use a low cost factor when generating bcrypt hash otherwise
659 // hashing would be slow when uploading lots of users. Hashes
660 // will be automatically updated to a higher cost factor the first
661 // time the user logs in.
662 $existinguser->password = hash_internal_user_password($user->password, true);
2197b642 663 $upt->track('password', $user->password, 'normal', false);
3f12c146 664 } else {
8bdb31ed
PS
665 // do not print password when not changed
666 $upt->track('password', '', 'normal', false);
3f12c146 667 }
8bdb31ed 668 }
3f12c146 669
8bdb31ed 670 if ($doupdate or $existinguser->password !== $oldpw) {
bb78e249 671 // We want only users that were really updated.
9363073b 672 user_update_user($existinguser, false, false);
3f12c146 673
df997f84
PS
674 $upt->track('status', $struserupdated);
675 $usersupdated++;
9dcae46d
PS
676
677 if (!$remoteuser) {
678 // pre-process custom profile menu fields data from csv file
679 $existinguser = uu_pre_process_custom_profile_data($existinguser);
680 // save custom profile fields data from csv file
681 profile_save_data($existinguser);
682 }
e6f74ba3 683
8bdb31ed
PS
684 if ($bulk == UU_BULK_UPDATED or $bulk == UU_BULK_ALL) {
685 if (!in_array($user->id, $SESSION->bulk_users)) {
686 $SESSION->bulk_users[] = $user->id;
687 }
688 }
689
9363073b
RT
690 // Trigger event.
691 \core\event\user_updated::create_from_userid($existinguser->id)->trigger();
692
8bdb31ed
PS
693 } else {
694 // no user information changed
695 $upt->track('status', $struseruptodate);
696 $usersuptodate++;
697
698 if ($bulk == UU_BULK_ALL) {
699 if (!in_array($user->id, $SESSION->bulk_users)) {
700 $SESSION->bulk_users[] = $user->id;
701 }
ed22a01b 702 }
066bfbfe 703 }
ed22a01b 704
08157bfe 705 if ($dologout) {
d79d5ac2 706 \core\session\manager::kill_user_sessions($existinguser->id);
08157bfe
PS
707 }
708
e4e38544 709 } else {
8bdb31ed
PS
710 // save the new user to the database
711 $user->confirmed = 1;
a7db355a 712 $user->timemodified = time();
8bdb31ed
PS
713 $user->timecreated = time();
714 $user->mnethostid = $CFG->mnet_localhost_id; // we support ONLY local accounts here, sorry
066bfbfe 715
08157bfe
PS
716 if (!isset($user->suspended) or $user->suspended === '') {
717 $user->suspended = 0;
718 } else {
719 $user->suspended = $user->suspended ? 1 : 0;
720 }
721 $upt->track('suspended', $stryesnooptions[$user->suspended], 'normal', false);
722
8bdb31ed 723 if (empty($user->auth)) {
3f12c146
RW
724 $user->auth = 'manual';
725 }
8bdb31ed 726 $upt->track('auth', $user->auth, 'normal', false);
3f12c146 727
8bdb31ed
PS
728 // do not insert record if new auth plugin does not exist!
729 try {
730 $auth = get_auth_plugin($user->auth);
731 } catch (Exception $e) {
732 $upt->track('auth', get_string('userautherror', 'error', s($user->auth)), 'error');
733 $upt->track('status', $strusernotaddederror, 'error');
734 $userserrors++;
735 continue;
e4e38544 736 }
8bdb31ed
PS
737 if (!isset($supportedauths[$user->auth])) {
738 $upt->track('auth', $struserauthunsupported, 'warning');
066bfbfe 739 }
a2ce7344 740
8bdb31ed
PS
741 $isinternalauth = $auth->is_internal();
742
43070e61
PS
743 if (empty($user->email)) {
744 $upt->track('email', get_string('invalidemail'), 'error');
745 $upt->track('status', $strusernotaddederror, 'error');
746 $userserrors++;
747 continue;
748
749 } else if ($DB->record_exists('user', array('email'=>$user->email))) {
0c4807ab 750 if ($noemailduplicates) {
751 $upt->track('email', $stremailduplicate, 'error');
752 $upt->track('status', $strusernotaddederror, 'error');
753 $userserrors++;
754 continue;
755 } else {
756 $upt->track('email', $stremailduplicate, 'warning');
757 }
758 }
8bdb31ed
PS
759 if (!validate_email($user->email)) {
760 $upt->track('email', get_string('invalidemail'), 'warning');
3f12c146 761 }
0c4807ab 762
4d3cd148
PS
763 if (empty($user->lang)) {
764 $user->lang = '';
765 } else if (clean_param($user->lang, PARAM_LANG) === '') {
766 $upt->track('status', get_string('cannotfindlang', 'error', $user->lang), 'warning');
767 $user->lang = '';
768 }
769
8bdb31ed
PS
770 $forcechangepassword = false;
771
772 if ($isinternalauth) {
773 if (empty($user->password)) {
774 if ($createpasswords) {
775 $user->password = 'to be generated';
776 $upt->track('password', '', 'normal', false);
ce15d56d 777 $upt->track('password', get_string('uupasswordcron', 'tool_uploaduser'), 'warning', false);
8bdb31ed
PS
778 } else {
779 $upt->track('password', '', 'normal', false);
780 $upt->track('password', get_string('missingfield', 'error', 'password'), 'error');
781 $upt->track('status', $strusernotaddederror, 'error');
782 $userserrors++;
783 continue;
784 }
3f12c146 785 } else {
8bdb31ed 786 $errmsg = null;
faceca91 787 $weak = !check_password_policy($user->password, $errmsg);
8bdb31ed
PS
788 if ($resetpasswords == UU_PWRESET_ALL or ($resetpasswords == UU_PWRESET_WEAK and $weak)) {
789 if ($weak) {
790 $weakpasswords++;
791 $upt->track('password', $strinvalidpasswordpolicy, 'warning');
792 }
793 $forcechangepassword = true;
794 }
ec2d8ceb
SC
795 // Use a low cost factor when generating bcrypt hash otherwise
796 // hashing would be slow when uploading lots of users. Hashes
797 // will be automatically updated to a higher cost factor the first
798 // time the user logs in.
799 $user->password = hash_internal_user_password($user->password, true);
3f12c146 800 }
8bdb31ed 801 } else {
ec2d8ceb 802 $user->password = AUTH_PASSWORD_NOT_CACHED;
8bdb31ed 803 $upt->track('password', '-', 'normal', false);
df997f84
PS
804 }
805
9363073b 806 $user->id = user_create_user($user, false, false);
8bdb31ed
PS
807 $upt->track('username', html_writer::link(new moodle_url('/user/profile.php', array('id'=>$user->id)), s($user->username)), 'normal', false);
808
d6aea4cc 809 // pre-process custom profile menu fields data from csv file
bd8dc9ba 810 $user = uu_pre_process_custom_profile_data($user);
e4e38544 811 // save custom profile fields data
812 profile_save_data($user);
813
8bdb31ed
PS
814 if ($forcechangepassword) {
815 set_user_preference('auth_forcepasswordchange', 1, $user);
816 }
817 if ($user->password === 'to be generated') {
818 set_user_preference('create_password', 1, $user);
819 }
820
9363073b
RT
821 // Trigger event.
822 \core\event\user_created::create_from_userid($user->id)->trigger();
823
8bdb31ed
PS
824 $upt->track('status', $struseradded);
825 $upt->track('id', $user->id, 'normal', false);
826 $usersnew++;
827
2304f629 828 // make sure user context exists
bf006d2c 829 context_user::instance($user->id);
2304f629 830
8bdb31ed 831 if ($bulk == UU_BULK_NEW or $bulk == UU_BULK_ALL) {
cd1edf9e 832 if (!in_array($user->id, $SESSION->bulk_users)) {
833 $SESSION->bulk_users[] = $user->id;
066bfbfe 834 }
066bfbfe 835 }
e4e38544 836 }
837
92b59a56
PS
838
839 // add to cohort first, it might trigger enrolments indirectly - do NOT create cohorts here!
840 foreach ($filecolumns as $column) {
841 if (!preg_match('/^cohort\d+$/', $column)) {
842 continue;
843 }
844
845 if (!empty($user->$column)) {
846 $addcohort = $user->$column;
847 if (!isset($cohorts[$addcohort])) {
848 if (is_number($addcohort)) {
849 // only non-numeric idnumbers!
850 $cohort = $DB->get_record('cohort', array('id'=>$addcohort));
851 } else {
852 $cohort = $DB->get_record('cohort', array('idnumber'=>$addcohort));
e7bc9157
MG
853 if (empty($cohort) && has_capability('moodle/cohort:manage', context_system::instance())) {
854 // Cohort was not found. Create a new one.
855 $cohortid = cohort_add_cohort((object)array(
856 'idnumber' => $addcohort,
857 'name' => $addcohort,
858 'contextid' => context_system::instance()->id
859 ));
860 $cohort = $DB->get_record('cohort', array('id'=>$cohortid));
861 }
92b59a56
PS
862 }
863
864 if (empty($cohort)) {
865 $cohorts[$addcohort] = get_string('unknowncohort', 'core_cohort', s($addcohort));
866 } else if (!empty($cohort->component)) {
867 // cohorts synchronised with external sources must not be modified!
868 $cohorts[$addcohort] = get_string('external', 'core_cohort');
869 } else {
870 $cohorts[$addcohort] = $cohort;
871 }
872 }
873
874 if (is_object($cohorts[$addcohort])) {
875 $cohort = $cohorts[$addcohort];
876 if (!$DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id))) {
877 cohort_add_member($cohort->id, $user->id);
878 // we might add special column later, for now let's abuse enrolments
879 $upt->track('enrolments', get_string('useradded', 'core_cohort', s($cohort->name)));
880 }
881 } else {
882 // error message
883 $upt->track('enrolments', $cohorts[$addcohort], 'error');
884 }
885 }
886 }
887
888
2686b6c3 889 // find course enrolments, groups, roles/types and enrol periods
8bdb31ed
PS
890 // this is again a special case, we always do this for any updated or created users
891 foreach ($filecolumns as $column) {
e4e38544 892 if (!preg_match('/^course\d+$/', $column)) {
893 continue;
894 }
895 $i = substr($column, 6);
6b09974b 896
df997f84
PS
897 if (empty($user->{'course'.$i})) {
898 continue;
899 }
e4e38544 900 $shortname = $user->{'course'.$i};
901 if (!array_key_exists($shortname, $ccache)) {
df997f84 902 if (!$course = $DB->get_record('course', array('shortname'=>$shortname), 'id, shortname')) {
8bdb31ed 903 $upt->track('enrolments', get_string('unknowncourse', 'error', s($shortname)), 'error');
a7db355a 904 continue;
066bfbfe 905 }
e4e38544 906 $ccache[$shortname] = $course;
907 $ccache[$shortname]->groups = null;
908 }
909 $courseid = $ccache[$shortname]->id;
bf006d2c 910 $coursecontext = context_course::instance($courseid);
df997f84 911 if (!isset($manualcache[$courseid])) {
4cb02426
PS
912 $manualcache[$courseid] = false;
913 if ($manual) {
914 if ($instances = enrol_get_instances($courseid, false)) {
915 foreach ($instances as $instance) {
916 if ($instance->enrol === 'manual') {
917 $manualcache[$courseid] = $instance;
918 break;
919 }
920 }
921 }
e4e38544 922 }
df997f84 923 }
e4e38544 924
968083cd
PS
925 if ($courseid == SITEID) {
926 // Technically frontpage does not have enrolments, but only role assignments,
927 // let's not invent new lang strings here for this rarely used feature.
928
929 if (!empty($user->{'role'.$i})) {
930 $addrole = $user->{'role'.$i};
931 if (array_key_exists($addrole, $rolecache)) {
932 $rid = $rolecache[$addrole]->id;
933 } else {
934 $upt->track('enrolments', get_string('unknownrole', 'error', s($addrole)), 'error');
935 continue;
936 }
937
938 role_assign($rid, $user->id, context_course::instance($courseid));
939
940 $a = new stdClass();
941 $a->course = $shortname;
942 $a->role = $rolecache[$rid]->name;
943 $upt->track('enrolments', get_string('enrolledincourserole', 'enrol_manual', $a));
944 }
945
946 } else if ($manual and $manualcache[$courseid]) {
df997f84
PS
947
948 // find role
949 $rid = false;
950 if (!empty($user->{'role'.$i})) {
951 $addrole = $user->{'role'.$i};
952 if (array_key_exists($addrole, $rolecache)) {
953 $rid = $rolecache[$addrole]->id;
e4e38544 954 } else {
8bdb31ed 955 $upt->track('enrolments', get_string('unknownrole', 'error', s($addrole)), 'error');
df997f84 956 continue;
a7db355a 957 }
e4e38544 958
df997f84
PS
959 } else if (!empty($user->{'type'.$i})) {
960 // if no role, then find "old" enrolment type
961 $addtype = $user->{'type'.$i};
962 if ($addtype < 1 or $addtype > 3) {
963 $upt->track('enrolments', $strerror.': typeN = 1|2|3', 'error');
964 continue;
965 } else if (empty($formdata->{'uulegacy'.$addtype})) {
966 continue;
967 } else {
968 $rid = $formdata->{'uulegacy'.$addtype};
969 }
e4e38544 970 } else {
df997f84
PS
971 // no role specified, use the default from manual enrol plugin
972 $rid = $manualcache[$courseid]->roleid;
a7db355a 973 }
2686b6c3 974
df997f84 975 if ($rid) {
6afe875e
JH
976 // Find duration and/or enrol status.
977 $timeend = 0;
978 $status = null;
979
980 if (isset($user->{'enrolstatus'.$i})) {
7ca44a49 981 $enrolstatus = $user->{'enrolstatus'.$i};
6afe875e
JH
982 if ($enrolstatus == '') {
983 $status = null;
984 } else if ($enrolstatus === (string)ENROL_USER_ACTIVE) {
985 $status = ENROL_USER_ACTIVE;
986 } else if ($enrolstatus === (string)ENROL_USER_SUSPENDED) {
987 $status = ENROL_USER_SUSPENDED;
988 } else {
989 debugging('Unknown enrolment status.');
990 }
991 }
992
df997f84 993 if (!empty($user->{'enrolperiod'.$i})) {
8bdb31ed 994 $duration = (int)$user->{'enrolperiod'.$i} * 60*60*24; // convert days to seconds
df997f84 995 if ($duration > 0) { // sanity check
8bdb31ed 996 $timeend = $today + $duration;
df997f84 997 }
b1d76077
AO
998 } else if ($manualcache[$courseid]->enrolperiod > 0) {
999 $timeend = $today + $manualcache[$courseid]->enrolperiod;
df997f84 1000 }
2686b6c3 1001
6afe875e 1002 $manual->enrol_user($manualcache[$courseid], $user->id, $rid, $today, $timeend, $status);
df997f84 1003
a226a972 1004 $a = new stdClass();
df997f84
PS
1005 $a->course = $shortname;
1006 $a->role = $rolecache[$rid]->name;
1007 $upt->track('enrolments', get_string('enrolledincourserole', 'enrol_manual', $a));
066bfbfe 1008 }
e4e38544 1009 }
6b09974b 1010
e4e38544 1011 // find group to add to
1012 if (!empty($user->{'group'.$i})) {
1013 // make sure user is enrolled into course before adding into groups
4f0c2d00 1014 if (!is_enrolled($coursecontext, $user->id)) {
d85f2634 1015 $upt->track('enrolments', get_string('addedtogroupnotenrolled', '', $user->{'group'.$i}), 'error');
e4e38544 1016 continue;
1017 }
1018 //build group cache
1019 if (is_null($ccache[$shortname]->groups)) {
1020 $ccache[$shortname]->groups = array();
35987665 1021 if ($groups = groups_get_all_groups($courseid)) {
e4e38544 1022 foreach ($groups as $gid=>$group) {
a226a972 1023 $ccache[$shortname]->groups[$gid] = new stdClass();
e4e38544 1024 $ccache[$shortname]->groups[$gid]->id = $gid;
1025 $ccache[$shortname]->groups[$gid]->name = $group->name;
1026 if (!is_numeric($group->name)) { // only non-numeric names are supported!!!
8bdb31ed
PS
1027 $ccache[$shortname]->groups[$group->name] = new stdClass();
1028 $ccache[$shortname]->groups[$group->name]->id = $gid;
1029 $ccache[$shortname]->groups[$group->name]->name = $group->name;
a7db355a 1030 }
a5702569 1031 }
0063abee 1032 }
6b09974b 1033 }
e4e38544 1034 // group exists?
1035 $addgroup = $user->{'group'.$i};
1036 if (!array_key_exists($addgroup, $ccache[$shortname]->groups)) {
b225cf36 1037 // if group doesn't exist, create it
a226a972 1038 $newgroupdata = new stdClass();
b225cf36 1039 $newgroupdata->name = $addgroup;
1040 $newgroupdata->courseid = $ccache[$shortname]->id;
e95bf5b5
PS
1041 $newgroupdata->description = '';
1042 $gid = groups_create_group($newgroupdata);
1043 if ($gid){
1044 $ccache[$shortname]->groups[$addgroup] = new stdClass();
1045 $ccache[$shortname]->groups[$addgroup]->id = $gid;
b225cf36 1046 $ccache[$shortname]->groups[$addgroup]->name = $newgroupdata->name;
1047 } else {
8bdb31ed 1048 $upt->track('enrolments', get_string('unknowngroup', 'error', s($addgroup)), 'error');
b225cf36 1049 continue;
1050 }
e4e38544 1051 }
1052 $gid = $ccache[$shortname]->groups[$addgroup]->id;
1053 $gname = $ccache[$shortname]->groups[$addgroup]->name;
1054
9c000a99 1055 try {
1056 if (groups_add_member($gid, $user->id)) {
8bdb31ed 1057 $upt->track('enrolments', get_string('addedtogroup', '', s($gname)));
df997f84 1058 } else {
8bdb31ed 1059 $upt->track('enrolments', get_string('addedtogroupnot', '', s($gname)), 'error');
9c000a99 1060 }
1061 } catch (moodle_exception $e) {
8bdb31ed 1062 $upt->track('enrolments', get_string('addedtogroupnot', '', s($gname)), 'error');
e4e38544 1063 continue;
1064 }
a5702569 1065 }
066bfbfe 1066 }
1067 }
e4e38544 1068 $upt->close(); // close table
1069
1070 $cir->close();
1071 $cir->cleanup(true);
1072
20486a5a 1073 echo $OUTPUT->box_start('boxwidthnarrow boxaligncenter generalbox', 'uploadresults');
e4e38544 1074 echo '<p>';
8bdb31ed 1075 if ($optype != UU_USER_UPDATE) {
ce15d56d 1076 echo get_string('userscreated', 'tool_uploaduser').': '.$usersnew.'<br />';
e4e38544 1077 }
8bdb31ed 1078 if ($optype == UU_USER_UPDATE or $optype == UU_USER_ADD_UPDATE) {
ce15d56d 1079 echo get_string('usersupdated', 'tool_uploaduser').': '.$usersupdated.'<br />';
e4e38544 1080 }
1081 if ($allowdeletes) {
ce15d56d
PS
1082 echo get_string('usersdeleted', 'tool_uploaduser').': '.$deletes.'<br />';
1083 echo get_string('deleteerrors', 'tool_uploaduser').': '.$deleteerrors.'<br />';
e4e38544 1084 }
1085 if ($allowrenames) {
ce15d56d
PS
1086 echo get_string('usersrenamed', 'tool_uploaduser').': '.$renames.'<br />';
1087 echo get_string('renameerrors', 'tool_uploaduser').': '.$renameerrors.'<br />';
e4e38544 1088 }
1089 if ($usersskipped) {
ce15d56d 1090 echo get_string('usersskipped', 'tool_uploaduser').': '.$usersskipped.'<br />';
e4e38544 1091 }
ce15d56d
PS
1092 echo get_string('usersweakpassword', 'tool_uploaduser').': '.$weakpasswords.'<br />';
1093 echo get_string('errors', 'tool_uploaduser').': '.$userserrors.'</p>';
20486a5a 1094 echo $OUTPUT->box_end();
e4e38544 1095
1096 if ($bulk) {
8fbce1c8 1097 echo $OUTPUT->continue_button($bulknurl);
e4e38544 1098 } else {
8fbce1c8 1099 echo $OUTPUT->continue_button($returnurl);
e4e38544 1100 }
73d6f52f 1101 echo $OUTPUT->footer();
df7ecfe4 1102 die;
6b09974b 1103}
0a6150ca 1104
df7ecfe4 1105// Print the header
61ef8f9f 1106echo $OUTPUT->header();
df7ecfe4 1107
ce15d56d 1108echo $OUTPUT->heading(get_string('uploaduserspreview', 'tool_uploaduser'));
df7ecfe4 1109
8bdb31ed
PS
1110// NOTE: this is JUST csv processing preview, we must not prevent import from here if there is something in the file!!
1111// this was intended for validation of csv formatting and encoding, not filtering the data!!!!
1112// we definitely must not process the whole file!
1113
1114// preview table data
1115$data = array();
e4e38544 1116$cir->init();
8bdb31ed 1117$linenum = 1; //column header is first line
1b4d2d56 1118$noerror = true; // Keep status of any error.
8bdb31ed
PS
1119while ($linenum <= $previewrows and $fields = $cir->next()) {
1120 $linenum++;
3fe2cfb5 1121 $rowcols = array();
8bdb31ed
PS
1122 $rowcols['line'] = $linenum;
1123 foreach($fields as $key => $field) {
7ca44a49 1124 $rowcols[$filecolumns[$key]] = s(trim($field));
3fe2cfb5 1125 }
8bdb31ed 1126 $rowcols['status'] = array();
3fe2cfb5 1127
8bdb31ed
PS
1128 if (isset($rowcols['username'])) {
1129 $stdusername = clean_param($rowcols['username'], PARAM_USERNAME);
1130 if ($rowcols['username'] !== $stdusername) {
1131 $rowcols['status'][] = get_string('invalidusernameupload');
3fe2cfb5 1132 }
8bdb31ed
PS
1133 if ($userid = $DB->get_field('user', 'id', array('username'=>$stdusername, 'mnethostid'=>$CFG->mnet_localhost_id))) {
1134 $rowcols['username'] = html_writer::link(new moodle_url('/user/profile.php', array('id'=>$userid)), $rowcols['username']);
e4e38544 1135 }
8bdb31ed
PS
1136 } else {
1137 $rowcols['status'][] = get_string('missingusername');
df7ecfe4 1138 }
07ed083e 1139
43070e61
PS
1140 if (isset($rowcols['email'])) {
1141 if (!validate_email($rowcols['email'])) {
1142 $rowcols['status'][] = get_string('invalidemail');
1143 }
1144 if ($DB->record_exists('user', array('email'=>$rowcols['email']))) {
1145 $rowcols['status'][] = $stremailduplicate;
1146 }
07ed083e 1147 }
fd9672ac
RW
1148
1149 if (isset($rowcols['city'])) {
7ca44a49 1150 $rowcols['city'] = $rowcols['city'];
fd9672ac 1151 }
1b4d2d56
RT
1152 // Check if rowcols have custom profile field with correct data and update error state.
1153 $noerror = uu_check_custom_profile_data($rowcols) && $noerror;
8bdb31ed
PS
1154 $rowcols['status'] = implode('<br />', $rowcols['status']);
1155 $data[] = $rowcols;
07ed083e 1156}
8bdb31ed
PS
1157if ($fields = $cir->next()) {
1158 $data[] = array_fill(0, count($fields) + 2, '...');
07ed083e 1159}
8bdb31ed 1160$cir->close();
3fe2cfb5
RW
1161
1162$table = new html_table();
1163$table->id = "uupreview";
16be8974 1164$table->attributes['class'] = 'generaltable';
3fe2cfb5 1165$table->tablealign = 'center';
ce15d56d 1166$table->summary = get_string('uploaduserspreview', 'tool_uploaduser');
3fe2cfb5 1167$table->head = array();
8bdb31ed 1168$table->data = $data;
3fe2cfb5 1169
ce15d56d 1170$table->head[] = get_string('uucsvline', 'tool_uploaduser');
8bdb31ed
PS
1171foreach ($filecolumns as $column) {
1172 $table->head[] = $column;
07ed083e 1173}
8bdb31ed 1174$table->head[] = get_string('status');
07ed083e 1175
3f12c146 1176echo html_writer::tag('div', html_writer::table($table), array('class'=>'flexible-wrap'));
3fe2cfb5 1177
1b4d2d56
RT
1178// Print the form if valid values are available
1179if ($noerror) {
1180 $mform2->display();
1181}
73d6f52f 1182echo $OUTPUT->footer();
df7ecfe4 1183die;
1184