MDL-20636 Fix upgrade of numerical questions.
[moodle.git] / admin / uploaduser.php
blobc2f4d520f8cfc5bbbf2b504fad9c6bd1e5d1ce07
1 <?php
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.
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/>.
17 /**
18 * Bulk user registration script from a comma separated file
20 * @package core
21 * @subpackage admin
22 * @copyright 2004 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require('../config.php');
27 require_once($CFG->libdir.'/adminlib.php');
28 require_once($CFG->libdir.'/csvlib.class.php');
29 require_once($CFG->dirroot.'/user/profile/lib.php');
30 require_once($CFG->dirroot.'/group/lib.php');
31 require_once('uploaduserlib.php');
32 require_once('uploaduser_form.php');
34 $iid = optional_param('iid', '', PARAM_INT);
35 $previewrows = optional_param('previewrows', 10, PARAM_INT);
37 @set_time_limit(60*60); // 1 hour should be enough
38 raise_memory_limit(MEMORY_HUGE);
40 require_login();
41 admin_externalpage_setup('uploadusers');
42 require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM));
44 $struserrenamed = get_string('userrenamed', 'admin');
45 $strusernotrenamedexists = get_string('usernotrenamedexists', 'error');
46 $strusernotrenamedmissing = get_string('usernotrenamedmissing', 'error');
47 $strusernotrenamedoff = get_string('usernotrenamedoff', 'error');
48 $strusernotrenamedadmin = get_string('usernotrenamedadmin', 'error');
50 $struserupdated = get_string('useraccountupdated', 'admin');
51 $strusernotupdated = get_string('usernotupdatederror', 'error');
52 $strusernotupdatednotexists = get_string('usernotupdatednotexists', 'error');
53 $strusernotupdatedadmin = get_string('usernotupdatedadmin', 'error');
55 $struseruptodate = get_string('useraccountuptodate', 'admin');
57 $struseradded = get_string('newuser');
58 $strusernotadded = get_string('usernotaddedregistered', 'error');
59 $strusernotaddederror = get_string('usernotaddederror', 'error');
61 $struserdeleted = get_string('userdeleted', 'admin');
62 $strusernotdeletederror = get_string('usernotdeletederror', 'error');
63 $strusernotdeletedmissing = get_string('usernotdeletedmissing', 'error');
64 $strusernotdeletedoff = get_string('usernotdeletedoff', 'error');
65 $strusernotdeletedadmin = get_string('usernotdeletedadmin', 'error');
67 $strcannotassignrole = get_string('cannotassignrole', 'error');
69 $struserauthunsupported = get_string('userauthunsupported', 'error');
70 $stremailduplicate = get_string('useremailduplicate', 'error');
72 $strinvalidpasswordpolicy = get_string('invalidpasswordpolicy', 'error');
73 $errorstr = get_string('error');
75 $returnurl = new moodle_url('/admin/uploaduser.php');
76 $bulknurl = new moodle_url('/admin/user/user_bulk.php');
78 $today = time();
79 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
81 // array of all valid fields for validation
82 $STD_FIELDS = array('id', 'firstname', 'lastname', 'username', 'email',
83 'city', 'country', 'lang', 'timezone', 'mailformat',
84 'maildisplay', 'maildigest', 'htmleditor', 'ajax', 'autosubscribe',
85 'institution', 'department', 'idnumber', 'skype',
86 'msn', 'aim', 'yahoo', 'icq', 'phone1', 'phone2', 'address',
87 'url', 'description', 'descriptionformat', 'password',
88 'auth', // watch out when changing auth type or using external auth plugins!
89 'oldusername', // use when renaming users - this is the original username
90 'deleted', // 1 means delete user
93 $PRF_FIELDS = array();
95 if ($prof_fields = $DB->get_records('user_info_field')) {
96 foreach ($prof_fields as $prof_field) {
97 $PRF_FIELDS[] = 'profile_field_'.$prof_field->shortname;
100 unset($prof_fields);
102 if (empty($iid)) {
103 $mform1 = new admin_uploaduser_form1();
105 if ($formdata = $mform1->get_data()) {
106 $iid = csv_import_reader::get_new_iid('uploaduser');
107 $cir = new csv_import_reader($iid, 'uploaduser');
109 $content = $mform1->get_file_content('userfile');
111 $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name);
112 unset($content);
114 if ($readcount === false) {
115 print_error('csvloaderror', '', $returnurl);
116 } else if ($readcount == 0) {
117 print_error('csvemptyfile', 'error', $returnurl);
119 // test if columns ok
120 $filecolumns = uu_validate_user_upload_columns($cir, $STD_FIELDS, $PRF_FIELDS, $returnurl);
121 // continue to form2
123 } else {
124 echo $OUTPUT->header();
126 echo $OUTPUT->heading_with_help(get_string('uploadusers', 'admin'), 'uploadusers', 'admin');
128 $mform1->display();
129 echo $OUTPUT->footer();
130 die;
132 } else {
133 $cir = new csv_import_reader($iid, 'uploaduser');
134 $filecolumns = uu_validate_user_upload_columns($cir, $STD_FIELDS, $PRF_FIELDS, $returnurl);
137 $mform2 = new admin_uploaduser_form2(null, array('columns'=>$filecolumns, 'data'=>array('iid'=>$iid, 'previewrows'=>$previewrows)));
139 // If a file has been uploaded, then process it
140 if ($formdata = $mform2->is_cancelled()) {
141 $cir->cleanup(true);
142 redirect($returnurl);
144 } else if ($formdata = $mform2->get_data()) {
145 // Print the header
146 echo $OUTPUT->header();
147 echo $OUTPUT->heading(get_string('uploadusersresult', 'admin'));
149 $optype = $formdata->uutype;
151 $updatetype = isset($formdata->uuupdatetype) ? $formdata->uuupdatetype : 0;
152 $createpasswords = (!empty($formdata->uupasswordnew) and $optype != UU_USER_UPDATE);
153 $updatepasswords = (!empty($formdata->uupasswordold) and $optype != UU_USER_ADDNEW and $optype != UU_USER_ADDINC and ($updatetype == UU_UPDATE_FILEOVERRIDE or $updatetype == UU_UPDATE_ALLOVERRIDE));
154 $allowrenames = (!empty($formdata->uuallowrenames) and $optype != UU_USER_ADDNEW and $optype != UU_USER_ADDINC);
155 $allowdeletes = (!empty($formdata->uuallowdeletes) and $optype != UU_USER_ADDNEW and $optype != UU_USER_ADDINC);
156 $bulk = $formdata->uubulk;
157 $noemailduplicates = $formdata->uunoemailduplicates;
158 $standardusernames = $formdata->uustandardusernames;
159 $resetpasswords = isset($formdata->uuforcepasswordchange) ? $formdata->uuforcepasswordchange : UU_PWRESET_NONE;
161 // verification moved to two places: after upload and into form2
162 $usersnew = 0;
163 $usersupdated = 0;
164 $usersuptodate = 0; //not printed yet anywhere
165 $userserrors = 0;
166 $deletes = 0;
167 $deleteerrors = 0;
168 $renames = 0;
169 $renameerrors = 0;
170 $usersskipped = 0;
171 $weakpasswords = 0;
173 // caches
174 $ccache = array(); // course cache - do not fetch all courses here, we will not probably use them all anyway!
175 $rolecache = uu_allowed_roles_cache(); // roles lookup cache
176 $manualcache = array(); // cache of used manual enrol plugins in each course
177 $supportedauths = uu_supported_auths(); // officially supported plugins that are enabled
179 // we use only manual enrol plugin here, if it is disabled no enrol is done
180 if (enrol_is_enabled('manual')) {
181 $manual = enrol_get_plugin('manual');
182 } else {
183 $manual = NULL;
186 // clear bulk selection
187 if ($bulk) {
188 $SESSION->bulk_users = array();
191 // init csv import helper
192 $cir->init();
193 $linenum = 1; //column header is first line
195 // init upload progress tracker
196 $upt = new uu_progress_tracker();
197 $upt->start(); // start table
199 while ($line = $cir->next()) {
200 $upt->flush();
201 $linenum++;
203 $upt->track('line', $linenum);
205 $user = new stdClass();
207 // add fields to user object
208 foreach ($line as $keynum => $value) {
209 if (!isset($filecolumns[$keynum])) {
210 // this should not happen
211 continue;
213 $key = $filecolumns[$keynum];
214 if (strpos($key, 'profile_field_') === 0) {
215 //NOTE: bloody mega hack alert!!
216 if (isset($USER->$key) and is_array($USER->$key)) {
217 // this must be some hacky field that is abusing arrays to store content and format
218 $user->$key = array();
219 $user->$key['text'] = $value;
220 $user->$key['format'] = FORMAT_MOODLE;
221 } else {
222 $user->$key = $value;
224 } else {
225 $user->$key = $value;
228 if (in_array($key, $upt->columns)) {
229 // default value in progress tracking table, can be changed later
230 $upt->track($key, s($value), 'normal');
233 if (!isset($user->username)) {
234 // prevent warnings bellow
235 $user->username = '';
238 if ($optype == UU_USER_ADDNEW or $optype == UU_USER_ADDINC) {
239 // user creation is a special case - the username may be constructed from templates using firstname and lastname
240 // better never try this in mixed update types
241 $error = false;
242 if (!isset($user->firstname) or $user->firstname === '') {
243 $upt->track('status', get_string('missingfield', 'error', 'firstname'), 'error');
244 $upt->track('firstname', $errorstr, 'error');
245 $error = true;
247 if (!isset($user->lastname) or $user->lastname === '') {
248 $upt->track('status', get_string('missingfield', 'error', 'lastname'), 'error');
249 $upt->track('lastname', $errorstr, 'error');
250 $error = true;
252 if ($error) {
253 $userserrors++;
254 continue;
256 // we require username too - we might use template for it though
257 if (empty($user->username) and !empty($formdata->username)) {
258 $user->username = uu_process_template($formdata->username, $user);
259 $upt->track('username', s($user->username));
263 // normalize username
264 $originalusername = $user->username;
265 if ($standardusernames) {
266 $user->username = clean_param($user->username, PARAM_USERNAME);
269 // make sure we really have username
270 if (empty($user->username)) {
271 $upt->track('status', get_string('missingfield', 'error', 'username'), 'error');
272 $upt->track('username', $errorstr, 'error');
273 $userserrors++;
274 continue;
275 } else if ($user->username === 'guest') {
276 $upt->track('status', get_string('guestnoeditprofileother', 'error'), 'error');
277 $userserrors++;
278 continue;
281 if ($existinguser = $DB->get_record('user', array('username'=>$user->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
282 $upt->track('id', $existinguser->id, 'normal', false);
285 // find out in username incrementing required
286 if ($existinguser and $optype == UU_USER_ADDINC) {
287 $user->username = uu_increment_username($user->username);
288 $existinguser = false;
291 // notify about nay username changes
292 if ($originalusername !== $user->username) {
293 $upt->track('username', '', 'normal', false); // clear previous
294 $upt->track('username', s($originalusername).'-->'.s($user->username), 'info');
295 } else {
296 $upt->track('username', s($user->username), 'normal', false);
299 // add default values for remaining fields
300 $formdefaults = array();
301 foreach ($STD_FIELDS as $field) {
302 if (isset($user->$field)) {
303 continue;
305 // all validation moved to form2
306 if (isset($formdata->$field)) {
307 // process templates
308 $user->$field = uu_process_template($formdata->$field, $user);
309 $formdefaults[$field] = true;
310 if (in_array($field, $upt->columns)) {
311 $upt->track($field, s($user->$field), 'normal');
315 foreach ($PRF_FIELDS as $field) {
316 if (isset($user->$field)) {
317 continue;
319 if (isset($formdata->$field)) {
320 // process templates
321 $user->$field = uu_process_template($formdata->$field, $user);
322 $formdefaults[$field] = true;
326 // delete user
327 if (!empty($user->deleted)) {
328 if (!$allowdeletes) {
329 $usersskipped++;
330 $upt->track('status', $strusernotdeletedoff, 'warning');
331 continue;
333 if ($existinguser) {
334 if (is_siteadmin($existinguser->id)) {
335 $upt->track('status', $strusernotdeletedadmin, 'error');
336 $deleteerrors++;
337 continue;
339 if (delete_user($existinguser)) {
340 $upt->track('status', $struserdeleted);
341 $deletes++;
342 } else {
343 $upt->track('status', $strusernotdeletederror, 'error');
344 $deleteerrors++;
346 } else {
347 $upt->track('status', $strusernotdeletedmissing, 'error');
348 $deleteerrors++;
350 continue;
352 // we do not need the deleted flag anymore
353 unset($user->deleted);
355 // renaming requested?
356 if (!empty($user->oldusername) ) {
357 if (!$allowrenames) {
358 $usersskipped++;
359 $upt->track('status', $strusernotrenamedoff, 'warning');
360 continue;
363 if ($existinguser) {
364 $upt->track('status', $strusernotrenamedexists, 'error');
365 $renameerrors++;
366 continue;
369 if ($user->username === 'guest') {
370 $upt->track('status', get_string('guestnoeditprofileother', 'error'), 'error');
371 $renameerrors++;
372 continue;
375 if ($standardusernames) {
376 $oldusername = clean_param($user->oldusername, PARAM_USERNAME);
377 } else {
378 $oldusername = $user->oldusername;
381 // no guessing when looking for old username, it must be exact match
382 if ($olduser = $DB->get_record('user', array('username'=>$oldusername, 'mnethostid'=>$CFG->mnet_localhost_id))) {
383 $upt->track('id', $olduser->id, 'normal', false);
384 if (is_siteadmin($olduser->id)) {
385 $upt->track('status', $strusernotrenamedadmin, 'error');
386 $renameerrors++;
387 continue;
389 $DB->set_field('user', 'username', $user->username, array('id'=>$olduser->id));
390 $upt->track('username', '', 'normal', false); // clear previous
391 $upt->track('username', s($oldusername).'-->'.s($user->username), 'info');
392 $upt->track('status', $struserrenamed);
393 $renames++;
394 } else {
395 $upt->track('status', $strusernotrenamedmissing, 'error');
396 $renameerrors++;
397 continue;
399 $existinguser = $olduser;
400 $existinguser->username = $user->username;
403 // can we process with update or insert?
404 $skip = false;
405 switch ($optype) {
406 case UU_USER_ADDNEW:
407 if ($existinguser) {
408 $usersskipped++;
409 $upt->track('status', $strusernotadded, 'warning');
410 $skip = true;
412 break;
414 case UU_USER_ADDINC:
415 if ($existinguser) {
416 //this should not happen!
417 $upt->track('status', $strusernotaddederror, 'error');
418 $userserrors++;
419 $skip = true;
421 break;
423 case UU_USER_ADD_UPDATE:
424 break;
426 case UU_USER_UPDATE:
427 if (!$existinguser) {
428 $usersskipped++;
429 $upt->track('status', $strusernotupdatednotexists, 'warning');
430 $skip = true;
432 break;
434 default:
435 // unknown type
436 $skip = true;
439 if ($skip) {
440 continue;
443 if ($existinguser) {
444 $user->id = $existinguser->id;
446 $upt->track('username', html_writer::link(new moodle_url('/user/profile.php', array('id'=>$existinguser->id)), s($existinguser->username)), 'normal', false);
448 if (is_siteadmin($user->id)) {
449 $upt->track('status', $strusernotupdatedadmin, 'error');
450 $userserrors++;
451 continue;
454 $existinguser->timemodified = time();
455 // do NOT mess with timecreated or firstaccess here!
457 //load existing profile data
458 profile_load_data($existinguser);
460 $upt->track('auth', $existinguser->auth, 'normal', false);
462 $doupdate = false;
464 if ($updatetype != UU_UPDATE_NOCHANGES) {
465 if (!empty($user->auth) and $user->auth !== $existinguser->auth) {
466 $upt->track('auth', s($existinguser->auth).'-->'.s($user->auth), 'info', false);
467 $existinguser->auth = $user->auth;
468 if (!isset($supportedauths[$user->auth])) {
469 $upt->track('auth', $struserauthunsupported, 'warning');
472 $allcolumns = array_merge($STD_FIELDS, $PRF_FIELDS);
473 foreach ($allcolumns as $column) {
474 if ($column === 'username' or $column === 'password' or $column === 'auth') {
475 // these can not be changed here
476 continue;
478 if (!property_exists($user, $column) or !property_exists($existinguser, $column)) {
479 // this should never happen
480 continue;
482 if ($updatetype == UU_UPDATE_MISSING) {
483 if (!is_null($existinguser->$column) and $existinguser->$column !== '') {
484 continue;
486 } else if ($updatetype == UU_UPDATE_ALLOVERRIDE) {
487 // we override everything
489 } else if ($updatetype == UU_UPDATE_FILEOVERRIDE) {
490 if (!empty($formdefaults[$column])) {
491 // do not override with form defaults
492 continue;
495 if ($existinguser->$column !== $user->$column) {
496 if ($column === 'email') {
497 if ($DB->record_exists('user', array('email'=>$user->email))) {
498 if ($noemailduplicates) {
499 $upt->track('email', $stremailduplicate, 'error');
500 $upt->track('status', $strusernotupdated, 'error');
501 $userserrors++;
502 continue 2;
503 } else {
504 $upt->track('email', $stremailduplicate, 'warning');
507 if (!validate_email($user->email)) {
508 $upt->track('email', get_string('invalidemail'), 'warning');
512 if (in_array($column, $upt->columns)) {
513 $upt->track($column, s($existinguser->$column).'-->'.s($user->$column), 'info', false);
515 $existinguser->$column = $user->$column;
516 $doupdate = true;
521 try {
522 $auth = get_auth_plugin($existinguser->auth);
523 } catch (Exception $e) {
524 $upt->track('auth', get_string('userautherror', 'error', s($existinguser->auth)), 'error');
525 $upt->track('status', $strusernotupdated, 'error');
526 $userserrors++;
527 continue;
529 $isinternalauth = $auth->is_internal();
531 // changing of passwords is a special case
532 // do not force password changes for external auth plugins!
533 $oldpw = $existinguser->password;
534 if (!$isinternalauth) {
535 $existinguser->password = 'not cached';
536 $upt->track('password', '-', 'normal', false);
537 // clean up prefs
538 unset_user_preference('create_password', $existinguser);
539 unset_user_preference('auth_forcepasswordchange', $existinguser);
541 } else if (!empty($user->password)) {
542 if ($updatepasswords) {
543 $errmsg = null;
544 $weak = !check_password_policy($user->password, $errmsg);
545 if ($resetpasswords == UU_PWRESET_ALL or ($resetpasswords == UU_PWRESET_WEAK and $weak)) {
546 if ($weak) {
547 $weakpasswords++;
548 $upt->track('password', $strinvalidpasswordpolicy, 'warning');
550 set_user_preference('auth_forcepasswordchange', 1, $existinguser);
551 } else {
552 unset_user_preference('auth_forcepasswordchange', $existinguser);
554 unset_user_preference('create_password', $existinguser); // no need to create password any more
555 $existinguser->password = hash_internal_user_password($user->password);
556 $upt->track('password', $user->password, 'normal', false);
557 } else {
558 // do not print password when not changed
559 $upt->track('password', '', 'normal', false);
563 if ($doupdate or $existinguser->password !== $oldpw) {
564 // we want only users that were really updated
566 $DB->update_record('user', $existinguser);
568 $upt->track('status', $struserupdated);
569 $usersupdated++;
570 // save custom profile fields data from csv file
571 profile_save_data($existinguser);
573 events_trigger('user_updated', $existinguser);
575 if ($bulk == UU_BULK_UPDATED or $bulk == UU_BULK_ALL) {
576 if (!in_array($user->id, $SESSION->bulk_users)) {
577 $SESSION->bulk_users[] = $user->id;
581 } else {
582 // no user information changed
583 $upt->track('status', $struseruptodate);
584 $usersuptodate++;
586 if ($bulk == UU_BULK_ALL) {
587 if (!in_array($user->id, $SESSION->bulk_users)) {
588 $SESSION->bulk_users[] = $user->id;
593 } else {
594 // save the new user to the database
595 $user->confirmed = 1;
596 $user->timemodified = time();
597 $user->timecreated = time();
598 $user->mnethostid = $CFG->mnet_localhost_id; // we support ONLY local accounts here, sorry
600 if (empty($user->auth)) {
601 $user->auth = 'manual';
603 $upt->track('auth', $user->auth, 'normal', false);
605 // do not insert record if new auth plugin does not exist!
606 try {
607 $auth = get_auth_plugin($user->auth);
608 } catch (Exception $e) {
609 $upt->track('auth', get_string('userautherror', 'error', s($user->auth)), 'error');
610 $upt->track('status', $strusernotaddederror, 'error');
611 $userserrors++;
612 continue;
614 if (!isset($supportedauths[$user->auth])) {
615 $upt->track('auth', $struserauthunsupported, 'warning');
618 $isinternalauth = $auth->is_internal();
620 if (empty($user->email)) {
621 $upt->track('email', get_string('invalidemail'), 'error');
622 $upt->track('status', $strusernotaddederror, 'error');
623 $userserrors++;
624 continue;
626 } else if ($DB->record_exists('user', array('email'=>$user->email))) {
627 if ($noemailduplicates) {
628 $upt->track('email', $stremailduplicate, 'error');
629 $upt->track('status', $strusernotaddederror, 'error');
630 $userserrors++;
631 continue;
632 } else {
633 $upt->track('email', $stremailduplicate, 'warning');
636 if (!validate_email($user->email)) {
637 $upt->track('email', get_string('invalidemail'), 'warning');
640 $forcechangepassword = false;
642 if ($isinternalauth) {
643 if (empty($user->password)) {
644 if ($createpasswords) {
645 $user->password = 'to be generated';
646 $upt->track('password', '', 'normal', false);
647 $upt->track('password', get_string('uupasswordcron', 'admin'), 'warning', false);
648 } else {
649 $upt->track('password', '', 'normal', false);
650 $upt->track('password', get_string('missingfield', 'error', 'password'), 'error');
651 $upt->track('status', $strusernotaddederror, 'error');
652 $userserrors++;
653 continue;
655 } else {
656 $errmsg = null;
657 $weak = !check_password_policy($user->password, $errmsg);
658 if ($resetpasswords == UU_PWRESET_ALL or ($resetpasswords == UU_PWRESET_WEAK and $weak)) {
659 if ($weak) {
660 $weakpasswords++;
661 $upt->track('password', $strinvalidpasswordpolicy, 'warning');
663 $forcechangepassword = true;
665 $user->password = hash_internal_user_password($user->password);
667 } else {
668 $user->password = 'not cached';
669 $upt->track('password', '-', 'normal', false);
672 // create user - insert_record ignores any extra properties
673 $user->id = $DB->insert_record('user', $user);
674 $upt->track('username', html_writer::link(new moodle_url('/user/profile.php', array('id'=>$user->id)), s($user->username)), 'normal', false);
676 // save custom profile fields data
677 profile_save_data($user);
679 if ($forcechangepassword) {
680 set_user_preference('auth_forcepasswordchange', 1, $user);
682 if ($user->password === 'to be generated') {
683 set_user_preference('create_password', 1, $user);
686 $upt->track('status', $struseradded);
687 $upt->track('id', $user->id, 'normal', false);
688 $usersnew++;
690 // make sure user context exists
691 get_context_instance(CONTEXT_USER, $user->id);
693 events_trigger('user_created', $user);
695 if ($bulk == UU_BULK_NEW or $bulk == UU_BULK_ALL) {
696 if (!in_array($user->id, $SESSION->bulk_users)) {
697 $SESSION->bulk_users[] = $user->id;
702 // find course enrolments, groups, roles/types and enrol periods
703 // this is again a special case, we always do this for any updated or created users
704 foreach ($filecolumns as $column) {
705 if (!preg_match('/^course\d+$/', $column)) {
706 continue;
708 $i = substr($column, 6);
710 if (empty($user->{'course'.$i})) {
711 continue;
713 $shortname = $user->{'course'.$i};
714 if (!array_key_exists($shortname, $ccache)) {
715 if (!$course = $DB->get_record('course', array('shortname'=>$shortname), 'id, shortname')) {
716 $upt->track('enrolments', get_string('unknowncourse', 'error', s($shortname)), 'error');
717 continue;
719 $ccache[$shortname] = $course;
720 $ccache[$shortname]->groups = null;
722 $courseid = $ccache[$shortname]->id;
723 $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
724 if (!isset($manualcache[$courseid])) {
725 $manualcache[$courseid] = false;
726 if ($manual) {
727 if ($instances = enrol_get_instances($courseid, false)) {
728 foreach ($instances as $instance) {
729 if ($instance->enrol === 'manual') {
730 $manualcache[$courseid] = $instance;
731 break;
738 if ($manual and $manualcache[$courseid]) {
740 // find role
741 $rid = false;
742 if (!empty($user->{'role'.$i})) {
743 $addrole = $user->{'role'.$i};
744 if (array_key_exists($addrole, $rolecache)) {
745 $rid = $rolecache[$addrole]->id;
746 } else {
747 $upt->track('enrolments', get_string('unknownrole', 'error', s($addrole)), 'error');
748 continue;
751 } else if (!empty($user->{'type'.$i})) {
752 // if no role, then find "old" enrolment type
753 $addtype = $user->{'type'.$i};
754 if ($addtype < 1 or $addtype > 3) {
755 $upt->track('enrolments', $strerror.': typeN = 1|2|3', 'error');
756 continue;
757 } else if (empty($formdata->{'uulegacy'.$addtype})) {
758 continue;
759 } else {
760 $rid = $formdata->{'uulegacy'.$addtype};
762 } else {
763 // no role specified, use the default from manual enrol plugin
764 $rid = $manualcache[$courseid]->roleid;
767 if ($rid) {
768 // find duration
769 $timeend = 0;
770 if (!empty($user->{'enrolperiod'.$i})) {
771 $duration = (int)$user->{'enrolperiod'.$i} * 60*60*24; // convert days to seconds
772 if ($duration > 0) { // sanity check
773 $timeend = $today + $duration;
777 $manual->enrol_user($manualcache[$courseid], $user->id, $rid, $today, $timeend);
779 $a = new stdClass();
780 $a->course = $shortname;
781 $a->role = $rolecache[$rid]->name;
782 $upt->track('enrolments', get_string('enrolledincourserole', 'enrol_manual', $a));
786 // find group to add to
787 if (!empty($user->{'group'.$i})) {
788 // make sure user is enrolled into course before adding into groups
789 if (!is_enrolled($coursecontext, $user->id)) {
790 $upt->track('enrolments', get_string('addedtogroupnotenrolled', '', $user->{'group'.$i}), 'error');
791 continue;
793 //build group cache
794 if (is_null($ccache[$shortname]->groups)) {
795 $ccache[$shortname]->groups = array();
796 if ($groups = groups_get_all_groups($courseid)) {
797 foreach ($groups as $gid=>$group) {
798 $ccache[$shortname]->groups[$gid] = new stdClass();
799 $ccache[$shortname]->groups[$gid]->id = $gid;
800 $ccache[$shortname]->groups[$gid]->name = $group->name;
801 if (!is_numeric($group->name)) { // only non-numeric names are supported!!!
802 $ccache[$shortname]->groups[$group->name] = new stdClass();
803 $ccache[$shortname]->groups[$group->name]->id = $gid;
804 $ccache[$shortname]->groups[$group->name]->name = $group->name;
809 // group exists?
810 $addgroup = $user->{'group'.$i};
811 if (!array_key_exists($addgroup, $ccache[$shortname]->groups)) {
812 // if group doesn't exist, create it
813 $newgroupdata = new stdClass();
814 $newgroupdata->name = $addgroup;
815 $newgroupdata->courseid = $ccache[$shortname]->id;
816 if ($ccache[$shortname]->groups[$addgroup]->id = groups_create_group($newgroupdata)){
817 $ccache[$shortname]->groups[$addgroup]->name = $newgroupdata->name;
818 } else {
819 $upt->track('enrolments', get_string('unknowngroup', 'error', s($addgroup)), 'error');
820 continue;
823 $gid = $ccache[$shortname]->groups[$addgroup]->id;
824 $gname = $ccache[$shortname]->groups[$addgroup]->name;
826 try {
827 if (groups_add_member($gid, $user->id)) {
828 $upt->track('enrolments', get_string('addedtogroup', '', s($gname)));
829 } else {
830 $upt->track('enrolments', get_string('addedtogroupnot', '', s($gname)), 'error');
832 } catch (moodle_exception $e) {
833 $upt->track('enrolments', get_string('addedtogroupnot', '', s($gname)), 'error');
834 continue;
839 $upt->close(); // close table
841 $cir->close();
842 $cir->cleanup(true);
844 echo $OUTPUT->box_start('boxwidthnarrow boxaligncenter generalbox', 'uploadresults');
845 echo '<p>';
846 if ($optype != UU_USER_UPDATE) {
847 echo get_string('userscreated', 'admin').': '.$usersnew.'<br />';
849 if ($optype == UU_USER_UPDATE or $optype == UU_USER_ADD_UPDATE) {
850 echo get_string('usersupdated', 'admin').': '.$usersupdated.'<br />';
852 if ($allowdeletes) {
853 echo get_string('usersdeleted', 'admin').': '.$deletes.'<br />';
854 echo get_string('deleteerrors', 'admin').': '.$deleteerrors.'<br />';
856 if ($allowrenames) {
857 echo get_string('usersrenamed', 'admin').': '.$renames.'<br />';
858 echo get_string('renameerrors', 'admin').': '.$renameerrors.'<br />';
860 if ($usersskipped) {
861 echo get_string('usersskipped', 'admin').': '.$usersskipped.'<br />';
863 echo get_string('usersweakpassword', 'admin').': '.$weakpasswords.'<br />';
864 echo get_string('errors', 'admin').': '.$userserrors.'</p>';
865 echo $OUTPUT->box_end();
867 if ($bulk) {
868 echo $OUTPUT->continue_button($bulknurl);
869 } else {
870 echo $OUTPUT->continue_button($returnurl);
872 echo $OUTPUT->footer();
873 die;
876 // Print the header
877 echo $OUTPUT->header();
879 echo $OUTPUT->heading(get_string('uploaduserspreview', 'admin'));
881 // NOTE: this is JUST csv processing preview, we must not prevent import from here if there is something in the file!!
882 // this was intended for validation of csv formatting and encoding, not filtering the data!!!!
883 // we definitely must not process the whole file!
885 // preview table data
886 $data = array();
887 $cir->init();
888 $linenum = 1; //column header is first line
889 while ($linenum <= $previewrows and $fields = $cir->next()) {
890 $linenum++;
891 $rowcols = array();
892 $rowcols['line'] = $linenum;
893 foreach($fields as $key => $field) {
894 $rowcols[$filecolumns[$key]] = s($field);
896 $rowcols['status'] = array();
898 if (isset($rowcols['username'])) {
899 $stdusername = clean_param($rowcols['username'], PARAM_USERNAME);
900 if ($rowcols['username'] !== $stdusername) {
901 $rowcols['status'][] = get_string('invalidusernameupload');
903 if ($userid = $DB->get_field('user', 'id', array('username'=>$stdusername, 'mnethostid'=>$CFG->mnet_localhost_id))) {
904 $rowcols['username'] = html_writer::link(new moodle_url('/user/profile.php', array('id'=>$userid)), $rowcols['username']);
906 } else {
907 $rowcols['status'][] = get_string('missingusername');
910 if (isset($rowcols['email'])) {
911 if (!validate_email($rowcols['email'])) {
912 $rowcols['status'][] = get_string('invalidemail');
914 if ($DB->record_exists('user', array('email'=>$rowcols['email']))) {
915 $rowcols['status'][] = $stremailduplicate;
918 $rowcols['status'] = implode('<br />', $rowcols['status']);
919 $data[] = $rowcols;
921 if ($fields = $cir->next()) {
922 $data[] = array_fill(0, count($fields) + 2, '...');
924 $cir->close();
926 $table = new html_table();
927 $table->id = "uupreview";
928 $table->attributes['class'] = 'generaltable';
929 $table->tablealign = 'center';
930 $table->summary = get_string('uploaduserspreview', 'admin');
931 $table->head = array();
932 $table->data = $data;
934 $table->head[] = get_string('uucsvline', 'admin');
935 foreach ($filecolumns as $column) {
936 $table->head[] = $column;
938 $table->head[] = get_string('status');
940 echo html_writer::tag('div', html_writer::table($table), array('class'=>'flexible-wrap'));
942 /// Print the form
944 $mform2->display();
945 echo $OUTPUT->footer();
946 die;