Merge branch 'wip-mdl-29001-m19' of git://github.com/rajeshtaneja/moodle into MOODLE_...
[moodle.git] / admin / uploaduser.php
blobf917566f58cae803204ea23a11742903f6efba83
1 <?php // $Id$
3 /// Bulk user registration script from a comma separated file
4 /// Returns list of users with their user ids
6 require('../config.php');
7 require_once($CFG->libdir.'/adminlib.php');
8 require_once($CFG->libdir.'/csvlib.class.php');
9 require_once($CFG->dirroot.'/user/profile/lib.php');
10 require_once('uploaduser_form.php');
12 $iid = optional_param('iid', '', PARAM_INT);
13 $previewrows = optional_param('previewrows', 10, PARAM_INT);
14 $readcount = optional_param('readcount', 0, PARAM_INT);
16 define('UU_ADDNEW', 0);
17 define('UU_ADDINC', 1);
18 define('UU_ADD_UPDATE', 2);
19 define('UU_UPDATE', 3);
21 @set_time_limit(3600); // 1 hour should be enough
22 @raise_memory_limit('256M');
23 if (function_exists('apache_child_terminate')) {
24 // if we are running from Apache, give httpd a hint that
25 // it can recycle the process after it's done. Apache's
26 // memory management is truly awful but we can help it.
27 @apache_child_terminate();
30 admin_externalpage_setup('uploadusers');
31 require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM));
33 $textlib = textlib_get_instance();
34 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
36 $struserrenamed = get_string('userrenamed', 'admin');
37 $strusernotrenamedexists = get_string('usernotrenamedexists', 'error');
38 $strusernotrenamedmissing = get_string('usernotrenamedmissing', 'error');
39 $strusernotrenamedoff = get_string('usernotrenamedoff', 'error');
40 $strusernotrenamedadmin = get_string('usernotrenamedadmin', 'error');
42 $struserupdated = get_string('useraccountupdated', 'admin');
43 $strusernotupdated = get_string('usernotupdatederror', 'error');
44 $strusernotupdatednotexists = get_string('usernotupdatednotexists', 'error');
45 $strusernotupdatedadmin = get_string('usernotupdatedadmin', 'error');
47 $struseradded = get_string('newuser');
48 $strusernotadded = get_string('usernotaddedregistered', 'error');
49 $strusernotaddederror = get_string('usernotaddederror', 'error');
51 $struserdeleted = get_string('userdeleted', 'admin');
52 $strusernotdeletederror = get_string('usernotdeletederror', 'error');
53 $strusernotdeletedmissing = get_string('usernotdeletedmissing', 'error');
54 $strusernotdeletedoff = get_string('usernotdeletedoff', 'error');
55 $strusernotdeletedadmin = get_string('usernotdeletedadmin', 'error');
57 $strcannotassignrole = get_string('cannotassignrole', 'error');
58 $strduplicateusername = get_string('duplicateusername', 'error');
60 $struserauthunsupported = get_string('userauthunsupported', 'error');
61 $stremailduplicate = get_string('useremailduplicate', 'error');;
63 $errorstr = get_string('error');
65 $returnurl = $CFG->wwwroot.'/'.$CFG->admin.'/uploaduser.php';
66 $bulknurl = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php';
68 // array of all valid fields for validation
69 $STD_FIELDS = array('id', 'firstname', 'lastname', 'username', 'email',
70 'city', 'country', 'lang', 'auth', 'timezone', 'mailformat',
71 'maildisplay', 'maildigest', 'htmleditor', 'ajax', 'autosubscribe',
72 'mnethostid', 'institution', 'department', 'idnumber', 'skype',
73 'msn', 'aim', 'yahoo', 'icq', 'phone1', 'phone2', 'address',
74 'url', 'description', 'oldusername', 'emailstop', 'deleted',
75 'password');
77 $PRF_FIELDS = array();
79 if ($prof_fields = get_records('user_info_field')) {
80 foreach ($prof_fields as $prof_field) {
81 $PRF_FIELDS[] = 'profile_field_'.$prof_field->shortname;
83 unset($prof_fields);
86 if (empty($iid)) {
87 $mform = new admin_uploaduser_form1();
89 if ($formdata = $mform->get_data()) {
90 $iid = csv_import_reader::get_new_iid('uploaduser');
91 $cir = new csv_import_reader($iid, 'uploaduser');
93 $content = $mform->get_file_content('userfile');
95 $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name, 'validate_user_upload_columns');
96 unset($content);
98 if ($readcount === false) {
99 error($cir->get_error(), $returnurl);
100 } else if ($readcount == 0) {
101 print_error('csvemptyfile', 'error', $returnurl);
103 // continue to form2
105 } else {
106 admin_externalpage_print_header();
107 print_heading_with_help(get_string('uploadusers'), 'uploadusers2');
108 $mform->display();
109 admin_externalpage_print_footer();
110 die;
112 } else {
113 $cir = new csv_import_reader($iid, 'uploaduser');
116 if (!$columns = $cir->get_columns()) {
117 error('Error reading temporary file', $returnurl);
119 $mform = new admin_uploaduser_form2(null, $columns);
120 // get initial date from form1
121 $mform->set_data(array('iid'=>$iid, 'previewrows'=>$previewrows, 'readcount'=>$readcount));
123 // If a file has been uploaded, then process it
124 if ($formdata = $mform->is_cancelled()) {
125 $cir->cleanup(true);
126 redirect($returnurl);
128 } else if ($formdata = $mform->get_data(false)) { // no magic quotes here!!!
129 // Print the header
130 admin_externalpage_print_header();
131 print_heading(get_string('uploadusersresult', 'admin'));
133 $optype = $formdata->uutype;
135 $createpasswords = (!empty($formdata->uupasswordnew) and $optype != UU_UPDATE);
136 $updatepasswords = (!empty($formdata->uupasswordold) and $optype != UU_ADDNEW and $optype != UU_ADDINC);
137 $allowrenames = (!empty($formdata->uuallowrenames) and $optype != UU_ADDNEW and $optype != UU_ADDINC);
138 $allowdeletes = (!empty($formdata->uuallowdeletes) and $optype != UU_ADDNEW and $optype != UU_ADDINC);
139 $updatetype = isset($formdata->uuupdatetype) ? $formdata->uuupdatetype : 0;
140 $bulk = $formdata->uubulk;
141 $noemailduplicates = $formdata->uunoemailduplicates;
143 // verification moved to two places: after upload and into form2
144 $usersnew = 0;
145 $usersupdated = 0;
146 $userserrors = 0;
147 $deletes = 0;
148 $deleteerrors = 0;
149 $renames = 0;
150 $renameerrors = 0;
151 $usersskipped = 0;
152 $weakpasswords = 0;
154 // caches
155 $ccache = array(); // course cache - do not fetch all courses here, we will not probably use them all anyway!
156 $rolecache = array(); // roles lookup cache
158 $allowedauths = uu_allowed_auths();
159 $allowedauths = array_keys($allowedauths);
160 $availableauths = get_list_of_plugins('auth');
162 $allowedroles = uu_allowed_roles(true);
163 foreach ($allowedroles as $rid=>$rname) {
164 $rolecache[$rid] = new object();
165 $rolecache[$rid]->id = $rid;
166 $rolecache[$rid]->name = $rname;
167 if (!is_numeric($rname)) { // only non-numeric shornames are supported!!!
168 $rolecache[$rname] = new object();
169 $rolecache[$rname]->id = $rid;
170 $rolecache[$rname]->name = $rname;
173 unset($allowedroles);
175 // clear bulk selection
176 if ($bulk) {
177 $SESSION->bulk_users = array();
180 // init csv import helper
181 $cir->init();
182 $linenum = 1; //column header is first line
184 // init upload progress tracker
185 $upt = new uu_progress_tracker();
186 $upt->init(); // start table
188 while ($line = $cir->next()) {
189 $upt->flush();
190 $linenum++;
192 $upt->track('line', $linenum);
194 $forcechangepassword = false;
196 $user = new object();
197 // by default, use the local mnet id (this may be changed in the file)
198 $user->mnethostid = $CFG->mnet_localhost_id;
199 // add fields to user object
200 foreach ($line as $key => $value) {
201 if ($value !== '') {
202 $key = $columns[$key];
203 // password is special field
204 if ($key == 'password') {
205 if ($value !== '') {
206 $user->password = hash_internal_user_password($value);
207 if (!empty($CFG->passwordpolicy) and !check_password_policy($value, $errmsg)) {
208 $forcechangepassword = true;
209 $weakpasswords++;
212 } else {
213 $user->$key = $value;
214 if (in_array($key, $upt->columns)) {
215 $upt->track($key, $value);
221 // get username, first/last name now - we need them in templates!!
222 if ($optype == UU_UPDATE) {
223 // when updating only username is required
224 if (!isset($user->username)) {
225 $upt->track('status', get_string('missingfield', 'error', 'username'), 'error');
226 $upt->track('username', $errorstr, 'error');
227 $userserrors++;
228 continue;
231 } else {
232 $error = false;
233 // when all other ops need firstname and lastname
234 if (!isset($user->firstname) or $user->firstname === '') {
235 $upt->track('status', get_string('missingfield', 'error', 'firstname'), 'error');
236 $upt->track('firstname', $errorstr, 'error');
237 $error = true;
239 if (!isset($user->lastname) or $user->lastname === '') {
240 $upt->track('status', get_string('missingfield', 'error', 'lastname'), 'error');
241 $upt->track('lastname', $errorstr, 'error');
242 $error = true;
244 if ($error) {
245 $userserrors++;
246 continue;
248 // we require username too - we might use template for it though
249 if (!isset($user->username)) {
250 if (!isset($formdata->username) or $formdata->username === '') {
251 $upt->track('status', get_string('missingfield', 'error', 'username'), 'error');
252 $upt->track('username', $errorstr, 'error');
253 $userserrors++;
254 continue;
255 } else {
256 $user->username = process_template($formdata->username, $user);
257 $upt->track('username', $user->username);
262 // normalize username
263 $user->username = $textlib->strtolower($user->username);
264 if (empty($CFG->extendedusernamechars)) {
265 $user->username = eregi_replace('[^(-\.[:alnum:])]', '', $user->username);
267 if (empty($user->username)) {
268 $upt->track('status', get_string('missingfield', 'error', 'username'), 'error');
269 $upt->track('username', $errorstr, 'error');
270 $userserrors++;
271 continue;
274 if ($existinguser = get_record('user', 'username', addslashes($user->username), 'mnethostid', $user->mnethostid)) {
275 $upt->track('id', $existinguser->id, 'normal', false);
278 // find out in username incrementing required
279 if ($existinguser and $optype == UU_ADDINC) {
280 $oldusername = $user->username;
281 $user->username = increment_username($user->username, $user->mnethostid);
282 $upt->track('username', '', 'normal', false); // clear previous
283 $upt->track('username', $oldusername.'-->'.$user->username, 'info');
284 $existinguser = false;
287 // add default values for remaining fields
288 foreach ($STD_FIELDS as $field) {
289 if (isset($user->$field)) {
290 continue;
292 // all validation moved to form2
293 if (isset($formdata->$field)) {
294 // process templates
295 $user->$field = process_template($formdata->$field, $user);
298 foreach ($PRF_FIELDS as $field) {
299 if (isset($user->$field)) {
300 continue;
302 if (isset($formdata->$field)) {
303 // process templates
304 $user->$field = process_template($formdata->$field, $user);
308 // delete user
309 if (!empty($user->deleted)) {
310 if (!$allowdeletes) {
311 $usersskipped++;
312 $upt->track('status', $strusernotdeletedoff, 'warning');
313 continue;
315 if ($existinguser) {
316 if (has_capability('moodle/site:doanything', $systemcontext, $existinguser->id)) {
317 $upt->track('status', $strusernotdeletedadmin, 'error');
318 $deleteerrors++;
319 continue;
321 if (delete_user($existinguser)) {
322 $upt->track('status', $struserdeleted);
323 $deletes++;
324 } else {
325 $upt->track('status', $strusernotdeletederror, 'error');
326 $deleteerrors++;
328 } else {
329 $upt->track('status', $strusernotdeletedmissing, 'error');
330 $deleteerrors++;
332 continue;
334 // we do not need the deleted flag anymore
335 unset($user->deleted);
337 // renaming requested?
338 if (!empty($user->oldusername) ) {
339 $oldusername = $textlib->strtolower($user->oldusername);
340 if (!$allowrenames) {
341 $usersskipped++;
342 $upt->track('status', $strusernotrenamedoff, 'warning');
343 continue;
346 if ($existinguser) {
347 $upt->track('status', $strusernotrenamedexists, 'error');
348 $renameerrors++;
349 continue;
352 if ($olduser = get_record('user', 'username', addslashes($oldusername), 'mnethostid', addslashes($user->mnethostid))) {
353 $upt->track('id', $olduser->id, 'normal', false);
354 if (has_capability('moodle/site:doanything', $systemcontext, $olduser->id)) {
355 $upt->track('status', $strusernotrenamedadmin, 'error');
356 $renameerrors++;
357 continue;
359 if (set_field('user', 'username', addslashes($user->username), 'id', $olduser->id)) {
360 $upt->track('username', '', 'normal', false); // clear previous
361 $upt->track('username', $oldusername.'-->'.$user->username, 'info');
362 $upt->track('status', $struserrenamed);
363 $renames++;
364 } else {
365 $upt->track('status', $strusernotrenamedexists, 'error');
366 $renameerrors++;
367 continue;
369 } else {
370 $upt->track('status', $strusernotrenamedmissing, 'error');
371 $renameerrors++;
372 continue;
374 $existinguser = $olduser;
375 $existinguser->username = $user->username;
378 // can we process with update or insert?
379 $skip = false;
380 switch ($optype) {
381 case UU_ADDNEW:
382 if ($existinguser) {
383 $usersskipped++;
384 $upt->track('status', $strusernotadded, 'warning');
385 $skip = true;;
387 break;
389 case UU_ADDINC:
390 if ($existinguser) {
391 //this should not happen!
392 $upt->track('status', $strusernotaddederror, 'error');
393 $userserrors++;
394 continue;
396 break;
398 case UU_ADD_UPDATE:
399 break;
401 case UU_UPDATE:
402 if (!$existinguser) {
403 $usersskipped++;
404 $upt->track('status', $strusernotupdatednotexists, 'warning');
405 $skip = true;
407 break;
410 if ($skip) {
411 continue;
414 if ($existinguser) {
415 $user->id = $existinguser->id;
417 if (has_capability('moodle/site:doanything', $systemcontext, $user->id)) {
418 $upt->track('status', $strusernotupdatedadmin, 'error');
419 $userserrors++;
420 continue;
423 if (!$updatetype) {
424 // no updates of existing data at all
425 } else {
426 $existinguser->timemodified = time();
427 //load existing profile data
428 profile_load_data($existinguser);
430 $allowed = array();
431 if ($updatetype == 1) {
432 $allowed = $columns;
433 } else if ($updatetype == 2 or $updatetype == 3) {
434 $allowed = array_merge($STD_FIELDS, $PRF_FIELDS);
436 foreach ($allowed as $column) {
437 if ($column == 'username') {
438 continue;
440 if ($column == 'password') {
441 if (!$updatepasswords or $updatetype == 3) {
442 continue;
443 } else if (!empty($user->password)) {
444 $upt->track('password', get_string('updated'));
445 if ($forcechangepassword) {
446 set_user_preference('auth_forcepasswordchange', 1, $existinguser->id);
450 if ((array_key_exists($column, $existinguser) and array_key_exists($column, $user)) or in_array($column, $PRF_FIELDS)) {
451 if ($updatetype == 3 and $existinguser->$column !== '') {
452 //missing == non-empty only
453 continue;
455 if ($existinguser->$column !== $user->$column) {
456 if ($column == 'email') {
457 if (record_exists('user', 'email', addslashes($user->email))) {
458 if ($noemailduplicates) {
459 $upt->track('email', $stremailduplicate, 'error');
460 $upt->track('status', $strusernotupdated, 'error');
461 $userserrors++;
462 continue 2;
463 } else {
464 $upt->track('email', $stremailduplicate, 'warning');
468 if ($column != 'password' and in_array($column, $upt->columns)) {
469 $upt->track($column, '', 'normal', false); // clear previous
470 $upt->track($column, $existinguser->$column.'-->'.$user->$column, 'info');
472 $existinguser->$column = $user->$column;
477 // do not update record if new auth plguin does not exist!
478 if (!in_array($existinguser->auth, $availableauths)) {
479 $upt->track('auth', get_string('userautherror', 'error', $existinguser->auth), 'error');
480 $upt->track('status', $strusernotupdated, 'error');
481 $userserrors++;
482 continue;
483 } else if (!in_array($existinguser->auth, $allowedauths)) {
484 $upt->track('auth', $struserauthunsupported, 'warning');
487 if (update_record('user', addslashes_recursive($existinguser))) {
488 $upt->track('status', $struserupdated);
489 $usersupdated++;
490 } else {
491 $upt->track('status', $strusernotupdated, 'error');
492 $userserrors++;
493 continue;
495 // save custom profile fields data from csv file
496 profile_save_data(addslashes_recursive($existinguser));
499 if ($bulk == 2 or $bulk == 3) {
500 if (!in_array($user->id, $SESSION->bulk_users)) {
501 $SESSION->bulk_users[] = $user->id;
505 } else {
506 // save the user to the database
507 $user->confirmed = 1;
508 $user->timemodified = time();
510 if (!$createpasswords and empty($user->password)) {
511 $upt->track('password', get_string('missingfield', 'error', 'password'), 'error');
512 $upt->track('status', $strusernotaddederror, 'error');
513 $userserrors++;
514 continue;
517 // do not insert record if new auth plguin does not exist!
518 if (isset($user->auth)) {
519 if (!in_array($user->auth, $availableauths)) {
520 $upt->track('auth', get_string('userautherror', 'error', $user->auth), 'error');
521 $upt->track('status', $strusernotaddederror, 'error');
522 $userserrors++;
523 continue;
524 } else if (!in_array($user->auth, $allowedauths)) {
525 $upt->track('auth', $struserauthunsupported, 'warning');
529 if (record_exists('user', 'email', addslashes($user->email))) {
530 if ($noemailduplicates) {
531 $upt->track('email', $stremailduplicate, 'error');
532 $upt->track('status', $strusernotaddederror, 'error');
533 $userserrors++;
534 continue;
535 } else {
536 $upt->track('email', $stremailduplicate, 'warning');
540 if ($user->id = insert_record('user', addslashes_recursive($user))) {
541 $info = ': ' . $user->username .' (ID = ' . $user->id . ')';
542 $upt->track('status', $struseradded);
543 $upt->track('id', $user->id, 'normal', false);
544 $usersnew++;
545 if ($createpasswords and empty($user->password)) {
546 // passwords will be created and sent out on cron
547 set_user_preference('create_password', 1, $user->id);
548 set_user_preference('auth_forcepasswordchange', 1, $user->id);
549 $upt->track('password', get_string('new'));
551 if ($forcechangepassword) {
552 set_user_preference('auth_forcepasswordchange', 1, $user->id);
554 } else {
555 // Record not added -- possibly some other error
556 $upt->track('status', $strusernotaddederror, 'error');
557 $userserrors++;
558 continue;
560 // save custom profile fields data
561 profile_save_data(addslashes_recursive($user));
563 // make sure user context exists
564 get_context_instance(CONTEXT_USER, $user->id);
566 if ($bulk == 1 or $bulk == 3) {
567 if (!in_array($user->id, $SESSION->bulk_users)) {
568 $SESSION->bulk_users[] = $user->id;
573 // find course enrolments, groups and roles/types
574 foreach ($columns as $column) {
575 if (!preg_match('/^course\d+$/', $column)) {
576 continue;
578 $i = substr($column, 6);
580 $shortname = $user->{'course'.$i};
581 if (!array_key_exists($shortname, $ccache)) {
582 if (!$course = get_record('course', 'shortname', addslashes($shortname), '', '', '', '', 'id, shortname, defaultrole')) {
583 $upt->track('enrolments', get_string('unknowncourse', 'error', $shortname), 'error');
584 continue;
586 $ccache[$shortname] = $course;
587 $ccache[$shortname]->groups = null;
589 $courseid = $ccache[$shortname]->id;
590 $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
592 // find role
593 $rid = false;
594 if (!empty($user->{'role'.$i})) {
595 $addrole = $user->{'role'.$i};
596 if (array_key_exists($addrole, $rolecache)) {
597 $rid = $rolecache[$addrole]->id;
598 } else {
599 $upt->track('enrolments', get_string('unknownrole', 'error', $addrole), 'error');
600 continue;
603 } else if (!empty($user->{'type'.$i})) {
604 // if no role, then find "old" enrolment type
605 $addtype = $user->{'type'.$i};
606 if ($addtype < 1 or $addtype > 3) {
607 $upt->track('enrolments', $strerror.': typeN = 1|2|3', 'error');
608 continue;
609 } else if ($addtype == 1 and empty($formdata->uulegacy1)) {
610 if (empty($ccache[$shortname]->defaultrole)) {
611 $rid = $CFG->defaultcourseroleid;
612 } else {
613 $rid = $ccache[$shortname]->defaultrole;
615 } else {
616 $rid = $formdata->{'uulegacy'.$addtype};
619 } else {
620 // no role specified, use the default
621 if (empty($ccache[$shortname]->defaultrole)) {
622 $rid = $CFG->defaultcourseroleid;
623 } else {
624 $rid = $ccache[$shortname]->defaultrole;
627 if ($rid) {
628 $a = new object();
629 $a->course = $shortname;
630 $a->role = $rolecache[$rid]->name;
631 if (role_assign($rid, $user->id, 0, $coursecontext->id)) {
632 $upt->track('enrolments', get_string('enrolledincourserole', '', $a));
633 } else {
634 $upt->track('enrolments', get_string('enrolledincoursenotrole', '', $a), 'error');
638 // find group to add to
639 if (!empty($user->{'group'.$i})) {
640 // make sure user is enrolled into course before adding into groups
641 if (!has_capability('moodle/course:view', $coursecontext, $user->id, false)) {
642 $upt->track('enrolments', get_string('addedtogroupnotenrolled', '', $gname), 'error');
643 continue;
645 //build group cache
646 if (is_null($ccache[$shortname]->groups)) {
647 $ccache[$shortname]->groups = array();
648 if ($groups = get_groups($courseid)) {
649 foreach ($groups as $gid=>$group) {
650 $ccache[$shortname]->groups[$gid] = new object();
651 $ccache[$shortname]->groups[$gid]->id = $gid;
652 $ccache[$shortname]->groups[$gid]->name = $group->name;
653 if (!is_numeric($group->name)) { // only non-numeric names are supported!!!
654 $ccache[$shortname]->groups[$group->name] = new object();
655 $ccache[$shortname]->groups[$group->name]->id = $gid;
656 $ccache[$shortname]->groups[$group->name]->name = $group->name;
661 // group exists?
662 $addgroup = $user->{'group'.$i};
663 if (!array_key_exists($addgroup, $ccache[$shortname]->groups)) {
664 // if group doesn't exist, create it
665 $newgroupdata = new object();
666 $newgroupdata->name = $addgroup;
667 $newgroupdata->courseid = $ccache[$shortname]->id;
668 if ($ccache[$shortname]->groups[$addgroup]->id = groups_create_group(addslashes_recursive($newgroupdata))){
669 $ccache[$shortname]->groups[$addgroup]->name = $newgroupdata->name;
670 } else {
671 $upt->track('enrolments', get_string('unknowngroup', 'error', $addgroup), 'error');
672 continue;
675 $gid = $ccache[$shortname]->groups[$addgroup]->id;
676 $gname = $ccache[$shortname]->groups[$addgroup]->name;
678 if (groups_add_member($gid, $user->id)) {
679 $upt->track('enrolments', get_string('addedtogroup', '', $gname));
680 } else {
681 $upt->track('enrolments', get_string('addedtogroupnot', '', $gname), 'error');
682 continue;
687 $upt->flush();
688 $upt->close(); // close table
690 $cir->close();
691 $cir->cleanup(true);
693 print_box_start('boxwidthnarrow boxaligncenter generalbox', 'uploadresults');
694 echo '<p>';
695 if ($optype != UU_UPDATE) {
696 echo get_string('userscreated', 'admin').': '.$usersnew.'<br />';
698 if ($optype == UU_UPDATE or $optype == UU_ADD_UPDATE) {
699 echo get_string('usersupdated', 'admin').': '.$usersupdated.'<br />';
701 if ($allowdeletes) {
702 echo get_string('usersdeleted', 'admin').': '.$deletes.'<br />';
703 echo get_string('deleteerrors', 'admin').': '.$deleteerrors.'<br />';
705 if ($allowrenames) {
706 echo get_string('usersrenamed', 'admin').': '.$renames.'<br />';
707 echo get_string('renameerrors', 'admin').': '.$renameerrors.'<br />';
709 if ($usersskipped) {
710 echo get_string('usersskipped', 'admin').': '.$usersskipped.'<br />';
712 echo get_string('usersweakpassword', 'admin').': '.$weakpasswords.'<br />';
713 echo get_string('errors', 'admin').': '.$userserrors.'</p>';
714 print_box_end();
716 if ($bulk) {
717 print_continue($bulknurl);
718 } else {
719 print_continue($returnurl);
721 admin_externalpage_print_footer();
722 die;
725 // Print the header
726 admin_externalpage_print_header();
728 /// Print the form
729 print_heading_with_help(get_string('uploaduserspreview', 'admin'), 'uploadusers2');
731 $ci = 0;
732 $ri = 0;
734 echo '<table id="uupreview" class="generaltable boxaligncenter" summary="'.get_string('uploaduserspreview', 'admin').'">';
735 echo '<tr class="heading r'.$ri++.'">';
736 foreach ($columns as $col) {
737 echo '<th class="header c'.$ci++.'" scope="col">'.s($col).'</th>';
739 echo '</tr>';
741 $cir->init();
742 while ($fields = $cir->next()) {
743 if ($ri > $previewrows) {
744 echo '<tr class="r'.$ri++.'">';
745 foreach ($fields as $field) {
746 echo '<td class="cell c'.$ci++.'">...</td>';;
748 break;
750 $ci = 0;
751 echo '<tr class="r'.$ri++.'">';
752 foreach ($fields as $field) {
753 echo '<td class="cell c'.$ci++.'">'.s($field).'</td>';;
755 echo '</tr>';
757 $cir->close();
759 echo '</table>';
760 echo '<div class="centerpara">'.get_string('uupreprocessedcount', 'admin', $readcount).'</div>';
761 $mform->display();
762 admin_externalpage_print_footer();
763 die;
765 /////////////////////////////////////
766 /// Utility functions and classes ///
767 /////////////////////////////////////
769 class uu_progress_tracker {
770 var $_row;
771 var $columns = array('status', 'line', 'id', 'username', 'firstname', 'lastname', 'email', 'password', 'auth', 'enrolments', 'deleted');
773 function uu_progress_tracker() {
776 function init() {
777 $ci = 0;
778 echo '<table id="uuresults" class="generaltable boxaligncenter" summary="'.get_string('uploadusersresult', 'admin').'">';
779 echo '<tr class="heading r0">';
780 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('status').'</th>';
781 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('uucsvline', 'admin').'</th>';
782 echo '<th class="header c'.$ci++.'" scope="col">ID</th>';
783 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('username').'</th>';
784 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('firstname').'</th>';
785 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('lastname').'</th>';
786 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('email').'</th>';
787 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('password').'</th>';
788 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('authentication').'</th>';
789 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('enrolments').'</th>';
790 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('delete').'</th>';
791 echo '</tr>';
792 $this->_row = null;
795 function flush() {
796 if (empty($this->_row) or empty($this->_row['line']['normal'])) {
797 $this->_row = array();
798 foreach ($this->columns as $col) {
799 $this->_row[$col] = array('normal'=>'', 'info'=>'', 'warning'=>'', 'error'=>'');
801 return;
803 $ci = 0;
804 $ri = 1;
805 echo '<tr class="r'.$ri++.'">';
806 foreach ($this->_row as $field) {
807 foreach ($field as $type=>$content) {
808 if ($field[$type] !== '') {
809 $field[$type] = '<span class="uu'.$type.'">'.$field[$type].'</span>';
810 } else {
811 unset($field[$type]);
814 echo '<td class="cell c'.$ci++.'">';
815 if (!empty($field)) {
816 echo implode('<br />', $field);
817 } else {
818 echo '&nbsp;';
820 echo '</td>';
822 echo '</tr>';
823 foreach ($this->columns as $col) {
824 $this->_row[$col] = array('normal'=>'', 'info'=>'', 'warning'=>'', 'error'=>'');
828 function track($col, $msg, $level='normal', $merge=true) {
829 if (empty($this->_row)) {
830 $this->flush(); //init arrays
832 if (!in_array($col, $this->columns)) {
833 debugging('Incorrect column:'.$col);
834 return;
836 if ($merge) {
837 if ($this->_row[$col][$level] != '') {
838 $this->_row[$col][$level] .='<br />';
840 $this->_row[$col][$level] .= s($msg);
841 } else {
842 $this->_row[$col][$level] = s($msg);
846 function close() {
847 echo '</table>';
852 * Validation callback function - verified the column line of csv file.
853 * Converts column names to lowercase too.
855 function validate_user_upload_columns(&$columns) {
856 global $STD_FIELDS, $PRF_FIELDS;
858 if (count($columns) < 2) {
859 return get_string('csvfewcolumns', 'error');
862 // test columns
863 $processed = array();
864 foreach ($columns as $key=>$unused) {
865 $columns[$key] = strtolower($columns[$key]); // no unicode expected here, ignore case
866 $field = $columns[$key];
867 if (!in_array($field, $STD_FIELDS) && !in_array($field, $PRF_FIELDS) &&// if not a standard field and not an enrolment field, then we have an error
868 !preg_match('/^course\d+$/', $field) && !preg_match('/^group\d+$/', $field) &&
869 !preg_match('/^type\d+$/', $field) && !preg_match('/^role\d+$/', $field)) {
870 return get_string('invalidfieldname', 'error', $field);
872 if (in_array($field, $processed)) {
873 return get_string('csvcolumnduplicates', 'error');
875 $processed[] = $field;
877 return true;
881 * Increments username - increments trailing number or adds it if not present.
882 * Varifies that the new username does not exist yet
883 * @param string $username
884 * @return incremented username which does not exist yet
886 function increment_username($username, $mnethostid) {
887 if (!preg_match_all('/(.*?)([0-9]+)$/', $username, $matches)) {
888 $username = $username.'2';
889 } else {
890 $username = $matches[1][0].($matches[2][0]+1);
893 if (record_exists('user', 'username', addslashes($username), 'mnethostid', addslashes($mnethostid))) {
894 return increment_username($username, $mnethostid);
895 } else {
896 return $username;
901 * Check if default field contains templates and apply them.
902 * @param string template - potential tempalte string
903 * @param object user object- we need username, firstname and lastname
904 * @return string field value
906 function process_template($template, $user) {
907 if (strpos($template, '%') === false) {
908 return $template;
911 // very very ugly hack!
912 global $template_globals;
913 $template_globals = new object();
914 $template_globals->username = isset($user->username) ? $user->username : '';
915 $template_globals->firstname = isset($user->firstname) ? $user->firstname : '';
916 $template_globals->lastname = isset($user->lastname) ? $user->lastname : '';
918 $result = preg_replace_callback('/(?<!%)%([+-~])?(\d)*([flu])/', 'process_template_callback', $template);
920 $template_globals = null;
922 if (is_null($result)) {
923 return $template; //error during regex processing??
924 } else {
925 return $result;
930 * Internal callback function.
932 function process_template_callback($block) {
933 global $template_globals;
934 $textlib = textlib_get_instance();
935 $repl = $block[0];
937 switch ($block[3]) {
938 case 'u': $repl = $template_globals->username; break;
939 case 'f': $repl = $template_globals->firstname; break;
940 case 'l': $repl = $template_globals->lastname; break;
942 switch ($block[1]) {
943 case '+': $repl = $textlib->strtoupper($repl); break;
944 case '-': $repl = $textlib->strtolower($repl); break;
945 case '~': $repl = $textlib->strtotitle($repl); break;
947 if (!empty($block[2])) {
948 $repl = $textlib->substr($repl, 0 , $block[2]);
951 return $repl;
955 * Returns list of auth plugins that are enabled and known to work.
957 function uu_allowed_auths() {
958 global $CFG;
960 // only following plugins are guaranteed to work properly
961 // TODO: add support for more plguins in 2.0
962 $whitelist = array('manual', 'nologin', 'none', 'email');
963 $plugins = get_enabled_auth_plugins();
964 $choices = array();
965 foreach ($plugins as $plugin) {
966 $choices[$plugin] = auth_get_plugin_title ($plugin);
969 return $choices;
973 * Returns list of non administrator roles
975 function uu_allowed_roles($shortname=false) {
976 global $CFG;
978 $roles = get_all_roles();
979 $choices = array();
980 foreach($roles as $role) {
981 if ($shortname) {
982 $choices[$role->id] = $role->shortname;
983 } else {
984 $choices[$role->id] = format_string($role->name);
987 // get rid of all admin roles
988 if ($adminroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW)) {
989 foreach($adminroles as $adminrole) {
990 unset($choices[$adminrole->id]);
994 return $choices;