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