Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / admin / uploaduser.php
blob0bb0d3a3c71353c9d9d42f05e721a2933d86fd1f
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;
153 // caches
154 $ccache = array(); // course cache - do not fetch all courses here, we will not probably use them all anyway!
155 $rolecache = array(); // roles lookup cache
157 $allowedauths = uu_allowed_auths();
158 $allowedauths = array_keys($allowedauths);
159 $availableauths = get_list_of_plugins('auth');
161 $allowedroles = uu_allowed_roles(true);
162 foreach ($allowedroles as $rid=>$rname) {
163 $rolecache[$rid] = new object();
164 $rolecache[$rid]->id = $rid;
165 $rolecache[$rid]->name = $rname;
166 if (!is_numeric($rname)) { // only non-numeric shornames are supported!!!
167 $rolecache[$rname] = new object();
168 $rolecache[$rname]->id = $rid;
169 $rolecache[$rname]->name = $rname;
172 unset($allowedroles);
174 // clear bilk selection
175 if ($bulk) {
176 $SESSION->bulk_users = array();
179 // init csv import helper
180 $cir->init();
181 $linenum = 1; //column header is first line
183 // init upload progress tracker
184 $upt = new uu_progress_tracker();
185 $upt->init(); // start table
187 while ($line = $cir->next()) {
188 $upt->flush();
189 $linenum++;
191 $upt->track('line', $linenum);
193 $user = new object();
194 // by default, use the local mnet id (this may be changed in the file)
195 $user->mnethostid = $CFG->mnet_localhost_id;
196 // add fields to user object
197 foreach ($line as $key => $value) {
198 if ($value !== '') {
199 $key = $columns[$key];
200 // password is special field
201 if ($key == 'password') {
202 if ($value !== '') {
203 $user->password = hash_internal_user_password($value);
205 } else {
206 $user->$key = $value;
207 if (in_array($key, $upt->columns)) {
208 $upt->track($key, $value);
214 // get username, first/last name now - we need them in templates!!
215 if ($optype == UU_UPDATE) {
216 // when updating only username is required
217 if (!isset($user->username)) {
218 $upt->track('status', get_string('missingfield', 'error', 'username'), 'error');
219 $upt->track('username', $errorstr, 'error');
220 $userserrors++;
221 continue;
224 } else {
225 $error = false;
226 // when all other ops need firstname and lastname
227 if (!isset($user->firstname) or $user->firstname === '') {
228 $upt->track('status', get_string('missingfield', 'error', 'firstname'), 'error');
229 $upt->track('firstname', $errorstr, 'error');
230 $error = true;
232 if (!isset($user->lastname) or $user->lastname === '') {
233 $upt->track('status', get_string('missingfield', 'error', 'lastname'), 'error');
234 $upt->track('lastname', $errorstr, 'error');
235 $error = true;
237 if ($error) {
238 $userserrors++;
239 continue;
241 // we require username too - we might use template for it though
242 if (!isset($user->username)) {
243 if (!isset($formdata->username) or $formdata->username === '') {
244 $upt->track('status', get_string('missingfield', 'error', 'username'), 'error');
245 $upt->track('username', $errorstr, 'error');
246 $userserrors++;
247 continue;
248 } else {
249 $user->username = process_template($formdata->username, $user);
250 $upt->track('username', $user->username);
255 // normalize username
256 $user->username = $textlib->strtolower($user->username);
257 if (empty($CFG->extendedusernamechars)) {
258 $user->username = eregi_replace('[^(-\.[:alnum:])]', '', $user->username);
260 if (empty($user->username)) {
261 $upt->track('status', get_string('missingfield', 'error', 'username'), 'error');
262 $upt->track('username', $errorstr, 'error');
263 $userserrors++;
264 continue;
267 if ($existinguser = get_record('user', 'username', addslashes($user->username), 'mnethostid', $user->mnethostid)) {
268 $upt->track('id', $existinguser->id, 'normal', false);
271 // find out in username incrementing required
272 if ($existinguser and $optype == UU_ADDINC) {
273 $oldusername = $user->username;
274 $user->username = increment_username($user->username, $user->mnethostid);
275 $upt->track('username', '', 'normal', false); // clear previous
276 $upt->track('username', $oldusername.'-->'.$user->username, 'info');
277 $existinguser = false;
280 // add default values for remaining fields
281 foreach ($STD_FIELDS as $field) {
282 if (isset($user->$field)) {
283 continue;
285 // all validation moved to form2
286 if (isset($formdata->$field)) {
287 // process templates
288 $user->$field = process_template($formdata->$field, $user);
291 foreach ($PRF_FIELDS as $field) {
292 if (isset($user->$field)) {
293 continue;
295 if (isset($formdata->$field)) {
296 // process templates
297 $user->$field = process_template($formdata->$field, $user);
301 // delete user
302 if (!empty($user->deleted)) {
303 if (!$allowdeletes) {
304 $usersskipped++;
305 $upt->track('status', $strusernotdeletedoff, 'warning');
306 continue;
308 if ($existinguser) {
309 if (has_capability('moodle/site:doanything', $systemcontext, $existinguser->id)) {
310 $upt->track('status', $strusernotdeletedadmin, 'error');
311 $deleteerrors++;
312 continue;
314 if (delete_user($existinguser)) {
315 $upt->track('status', $struserdeleted);
316 $deletes++;
317 } else {
318 $upt->track('status', $strusernotdeletederror, 'error');
319 $deleteerrors++;
321 } else {
322 $upt->track('status', $strusernotdeletedmissing, 'error');
323 $deleteerrors++;
325 continue;
327 // we do not need the deleted flag anymore
328 unset($user->deleted);
330 // renaming requested?
331 if (!empty($user->oldusername) ) {
332 $oldusername = $textlib->strtolower($user->oldusername);
333 if (!$allowrenames) {
334 $usersskipped++;
335 $upt->track('status', $strusernotrenamedoff, 'warning');
336 continue;
339 if ($existinguser) {
340 $upt->track('status', $strusernotrenamedexists, 'error');
341 $renameerrors++;
342 continue;
345 if ($olduser = get_record('user', 'username', addslashes($oldusername), 'mnethostid', addslashes($user->mnethostid))) {
346 $upt->track('id', $olduser->id, 'normal', false);
347 if (has_capability('moodle/site:doanything', $systemcontext, $olduser->id)) {
348 $upt->track('status', $strusernotrenamedadmin, 'error');
349 $renameerrors++;
350 continue;
352 if (set_field('user', 'username', addslashes($user->username), 'id', $olduser->id)) {
353 $upt->track('username', '', 'normal', false); // clear previous
354 $upt->track('username', $oldusername.'-->'.$user->username, 'info');
355 $upt->track('status', $struserrenamed);
356 $renames++;
357 } else {
358 $upt->track('status', $strusernotrenamedexists, 'error');
359 $renameerrors++;
360 continue;
362 } else {
363 $upt->track('status', $strusernotrenamedmissing, 'error');
364 $renameerrors++;
365 continue;
367 $existinguser = $olduser;
368 $existinguser->username = $user->username;
371 // can we process with update or insert?
372 $skip = false;
373 switch ($optype) {
374 case UU_ADDNEW:
375 if ($existinguser) {
376 $usersskipped++;
377 $upt->track('status', $strusernotadded, 'warning');
378 $skip = true;;
380 break;
382 case UU_ADDINC:
383 if ($existinguser) {
384 //this should not happen!
385 $upt->track('status', $strusernotaddederror, 'error');
386 $userserrors++;
387 continue;
389 break;
391 case UU_ADD_UPDATE:
392 break;
394 case UU_UPDATE:
395 if (!$existinguser) {
396 $usersskipped++;
397 $upt->track('status', $strusernotupdatednotexists, 'warning');
398 $skip = true;
400 break;
403 if ($skip) {
404 continue;
407 if ($existinguser) {
408 $user->id = $existinguser->id;
410 if (has_capability('moodle/site:doanything', $systemcontext, $user->id)) {
411 $upt->track('status', $strusernotupdatedadmin, 'error');
412 $userserrors++;
413 continue;
416 if (!$updatetype) {
417 // no updates of existing data at all
418 } else {
419 $existinguser->timemodified = time();
420 //load existing profile data
421 profile_load_data($existinguser);
423 $allowed = array();
424 if ($updatetype == 1) {
425 $allowed = $columns;
426 } else if ($updatetype == 2 or $updatetype == 3) {
427 $allowed = array_merge($STD_FIELDS, $PRF_FIELDS);
429 foreach ($allowed as $column) {
430 if ($column == 'username') {
431 continue;
433 if ($column == 'password') {
434 if (!$updatepasswords or $updatetype == 3) {
435 continue;
436 } else if (!empty($user->password)) {
437 $upt->track('password', get_string('updated'));
440 if ((array_key_exists($column, $existinguser) and array_key_exists($column, $user)) or in_array($column, $PRF_FIELDS)) {
441 if ($updatetype == 3 and $existinguser->$column !== '') {
442 //missing == non-empty only
443 continue;
445 if ($existinguser->$column !== $user->$column) {
446 if ($column == 'email') {
447 if (record_exists('user', 'email', addslashes($user->email))) {
448 if ($noemailduplicates) {
449 $upt->track('email', $stremailduplicate, 'error');
450 $upt->track('status', $strusernotupdated, 'error');
451 $userserrors++;
452 continue 2;
453 } else {
454 $upt->track('email', $stremailduplicate, 'warning');
458 if ($column != 'password' and in_array($column, $upt->columns)) {
459 $upt->track($column, '', 'normal', false); // clear previous
460 $upt->track($column, $existinguser->$column.'-->'.$user->$column, 'info');
462 $existinguser->$column = $user->$column;
467 // do not update record if new auth plguin does not exist!
468 if (!in_array($existinguser->auth, $availableauths)) {
469 $upt->track('auth', get_string('userautherror', 'error', $existinguser->auth), 'error');
470 $upt->track('status', $strusernotupdated, 'error');
471 $userserrors++;
472 continue;
473 } else if (!in_array($existinguser->auth, $allowedauths)) {
474 $upt->track('auth', $struserauthunsupported, 'warning');
477 if (update_record('user', addslashes_recursive($existinguser))) {
478 $upt->track('status', $struserupdated);
479 $usersupdated++;
480 } else {
481 $upt->track('status', $strusernotupdated, 'error');
482 $userserrors++;
483 continue;
485 // save custom profile fields data from csv file
486 profile_save_data(addslashes_recursive($existinguser));
489 if ($bulk == 2 or $bulk == 3) {
490 if (!in_array($user->id, $SESSION->bulk_users)) {
491 $SESSION->bulk_users[] = $user->id;
495 } else {
496 // save the user to the database
497 $user->confirmed = 1;
498 $user->timemodified = time();
500 if (!$createpasswords and empty($user->password)) {
501 $upt->track('password', get_string('missingfield', 'error', 'password'), 'error');
502 $upt->track('status', $strusernotaddederror, 'error');
503 $userserrors++;
504 continue;
507 // do not insert record if new auth plguin does not exist!
508 if (isset($user->auth)) {
509 if (!in_array($user->auth, $availableauths)) {
510 $upt->track('auth', get_string('userautherror', 'error', $user->auth), 'error');
511 $upt->track('status', $strusernotaddederror, 'error');
512 $userserrors++;
513 continue;
514 } else if (!in_array($user->auth, $allowedauths)) {
515 $upt->track('auth', $struserauthunsupported, 'warning');
519 if (record_exists('user', 'email', addslashes($user->email))) {
520 if ($noemailduplicates) {
521 $upt->track('email', $stremailduplicate, 'error');
522 $upt->track('status', $strusernotaddederror, 'error');
523 $userserrors++;
524 continue;
525 } else {
526 $upt->track('email', $stremailduplicate, 'warning');
530 if ($user->id = insert_record('user', addslashes_recursive($user))) {
531 $info = ': ' . $user->username .' (ID = ' . $user->id . ')';
532 $upt->track('status', $struseradded);
533 $upt->track('id', $user->id, 'normal', false);
534 $usersnew++;
535 if ($createpasswords and empty($user->password)) {
536 // passwords will be created and sent out on cron
537 set_user_preference('create_password', 1, $user->id);
538 set_user_preference('auth_forcepasswordchange', 1, $user->id);
539 $upt->track('password', get_string('new'));
541 } else {
542 // Record not added -- possibly some other error
543 $upt->track('status', $strusernotaddederror, 'error');
544 $userserrors++;
545 continue;
547 // save custom profile fields data
548 profile_save_data($user);
550 // make sure user context exists
551 get_context_instance(CONTEXT_USER, $user->id);
553 if ($bulk == 1 or $bulk == 3) {
554 if (!in_array($user->id, $SESSION->bulk_users)) {
555 $SESSION->bulk_users[] = $user->id;
560 // find course enrolments, groups and roles/types
561 foreach ($columns as $column) {
562 if (!preg_match('/^course\d+$/', $column)) {
563 continue;
565 $i = substr($column, 6);
567 $shortname = $user->{'course'.$i};
568 if (!array_key_exists($shortname, $ccache)) {
569 if (!$course = get_record('course', 'shortname', addslashes($shortname), '', '', '', '', 'id, shortname, defaultrole')) {
570 $upt->track('enrolments', get_string('unknowncourse', 'error', $shortname), 'error');
571 continue;
573 $ccache[$shortname] = $course;
574 $ccache[$shortname]->groups = null;
576 $courseid = $ccache[$shortname]->id;
577 $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
579 // find role
580 $rid = false;
581 if (!empty($user->{'role'.$i})) {
582 $addrole = $user->{'role'.$i};
583 if (array_key_exists($addrole, $rolecache)) {
584 $rid = $rolecache[$addrole]->id;
585 } else {
586 $upt->track('enrolments', get_string('unknownrole', 'error', $addrole), 'error');
587 continue;
590 } else if (!empty($user->{'type'.$i})) {
591 // if no role, then find "old" enrolment type
592 $addtype = $user->{'type'.$i};
593 if ($addtype < 1 or $addtype > 3) {
594 $upt->track('enrolments', $strerror.': typeN = 1|2|3', 'error');
595 continue;
596 } else if ($addtype == 1 and empty($formdata->uulegacy1)) {
597 if (empty($ccache[$shortname]->defaultrole)) {
598 $rid = $CFG->defaultcourseroleid;
599 } else {
600 $rid = $ccache[$shortname]->defaultrole;
602 } else {
603 $rid = $formdata->{'uulegacy'.$addtype};
606 } else {
607 // no role specified, use the default
608 if (empty($ccache[$shortname]->defaultrole)) {
609 $rid = $CFG->defaultcourseroleid;
610 } else {
611 $rid = $ccache[$shortname]->defaultrole;
614 if ($rid) {
615 $a = new object();
616 $a->course = $shortname;
617 $a->role = $rolecache[$rid]->name;
618 if (role_assign($rid, $user->id, 0, $coursecontext->id)) {
619 $upt->track('enrolments', get_string('enrolledincourserole', '', $a));
620 } else {
621 $upt->track('enrolments', get_string('enrolledincoursenotrole', '', $a), 'error');
625 // find group to add to
626 if (!empty($user->{'group'.$i})) {
627 // make sure user is enrolled into course before adding into groups
628 if (!has_capability('moodle/course:view', $coursecontext, $user->id, false)) {
629 $upt->track('enrolments', get_string('addedtogroupnotenrolled', '', $gname), 'error');
630 continue;
632 //build group cache
633 if (is_null($ccache[$shortname]->groups)) {
634 $ccache[$shortname]->groups = array();
635 if ($groups = get_groups($courseid)) {
636 foreach ($groups as $gid=>$group) {
637 $ccache[$shortname]->groups[$gid] = new object();
638 $ccache[$shortname]->groups[$gid]->id = $gid;
639 $ccache[$shortname]->groups[$gid]->name = $group->name;
640 if (!is_numeric($group->name)) { // only non-numeric names are supported!!!
641 $ccache[$shortname]->groups[$group->name] = new object();
642 $ccache[$shortname]->groups[$group->name]->id = $gid;
643 $ccache[$shortname]->groups[$group->name]->name = $group->name;
648 // group exists?
649 $addgroup = $user->{'group'.$i};
650 if (!array_key_exists($addgroup, $ccache[$shortname]->groups)) {
651 // if group doesn't exist, create it
652 $newgroupdata = new object();
653 $newgroupdata->name = $addgroup;
654 $newgroupdata->courseid = $ccache[$shortname]->id;
655 if ($ccache[$shortname]->groups[$addgroup]->id = groups_create_group($newgroupdata)){
656 $ccache[$shortname]->groups[$addgroup]->name = $newgroupdata->name;
657 } else {
658 $upt->track('enrolments', get_string('unknowngroup', 'error', $addgroup), 'error');
659 continue;
662 $gid = $ccache[$shortname]->groups[$addgroup]->id;
663 $gname = $ccache[$shortname]->groups[$addgroup]->name;
665 if (groups_add_member($gid, $user->id)) {
666 $upt->track('enrolments', get_string('addedtogroup', '', $gname));
667 } else {
668 $upt->track('enrolments', get_string('addedtogroupnot', '', $gname), 'error');
669 continue;
674 $upt->flush();
675 $upt->close(); // close table
677 $cir->close();
678 $cir->cleanup(true);
680 print_box_start('boxwidthnarrow boxaligncenter generalbox', 'uploadresults');
681 echo '<p>';
682 if ($optype != UU_UPDATE) {
683 echo get_string('userscreated', 'admin').': '.$usersnew.'<br />';
685 if ($optype == UU_UPDATE or $optype == UU_ADD_UPDATE) {
686 echo get_string('usersupdated', 'admin').': '.$usersupdated.'<br />';
688 if ($allowdeletes) {
689 echo get_string('usersdeleted', 'admin').': '.$deletes.'<br />';
690 echo get_string('deleteerrors', 'admin').': '.$deleteerrors.'<br />';
692 if ($allowrenames) {
693 echo get_string('usersrenamed', 'admin').': '.$renames.'<br />';
694 echo get_string('renameerrors', 'admin').': '.$renameerrors.'<br />';
696 if ($usersskipped) {
697 echo get_string('usersskipped', 'admin').': '.$usersskipped.'<br />';
699 echo get_string('errors', 'admin').': '.$userserrors.'</p>';
700 print_box_end();
702 if ($bulk) {
703 print_continue($bulknurl);
704 } else {
705 print_continue($returnurl);
707 admin_externalpage_print_footer();
708 die;
711 // Print the header
712 admin_externalpage_print_header();
714 /// Print the form
715 print_heading_with_help(get_string('uploaduserspreview', 'admin'), 'uploadusers2');
717 $ci = 0;
718 $ri = 0;
720 echo '<table id="uupreview" class="generaltable boxaligncenter" summary="'.get_string('uploaduserspreview', 'admin').'">';
721 echo '<tr class="heading r'.$ri++.'">';
722 foreach ($columns as $col) {
723 echo '<th class="header c'.$ci++.'" scope="col">'.s($col).'</th>';
725 echo '</tr>';
727 $cir->init();
728 while ($fields = $cir->next()) {
729 if ($ri > $previewrows) {
730 echo '<tr class="r'.$ri++.'">';
731 foreach ($fields as $field) {
732 echo '<td class="cell c'.$ci++.'">...</td>';;
734 break;
736 $ci = 0;
737 echo '<tr class="r'.$ri++.'">';
738 foreach ($fields as $field) {
739 echo '<td class="cell c'.$ci++.'">'.s($field).'</td>';;
741 echo '</tr>';
743 $cir->close();
745 echo '</table>';
746 echo '<div class="centerpara">'.get_string('uupreprocessedcount', 'admin', $readcount).'</div>';
747 $mform->display();
748 admin_externalpage_print_footer();
749 die;
751 /////////////////////////////////////
752 /// Utility functions and classes ///
753 /////////////////////////////////////
755 class uu_progress_tracker {
756 var $_row;
757 var $columns = array('status', 'line', 'id', 'username', 'firstname', 'lastname', 'email', 'password', 'auth', 'enrolments', 'deleted');
759 function uu_progress_tracker() {
762 function init() {
763 $ci = 0;
764 echo '<table id="uuresults" class="generaltable boxaligncenter" summary="'.get_string('uploadusersresult', 'admin').'">';
765 echo '<tr class="heading r0">';
766 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('status').'</th>';
767 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('uucsvline', 'admin').'</th>';
768 echo '<th class="header c'.$ci++.'" scope="col">ID</th>';
769 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('username').'</th>';
770 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('firstname').'</th>';
771 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('lastname').'</th>';
772 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('email').'</th>';
773 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('password').'</th>';
774 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('authentication').'</th>';
775 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('enrolments').'</th>';
776 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('delete').'</th>';
777 echo '</tr>';
778 $this->_row = null;
781 function flush() {
782 if (empty($this->_row) or empty($this->_row['line']['normal'])) {
783 $this->_row = array();
784 foreach ($this->columns as $col) {
785 $this->_row[$col] = array('normal'=>'', 'info'=>'', 'warning'=>'', 'error'=>'');
787 return;
789 $ci = 0;
790 $ri = 1;
791 echo '<tr class="r'.$ri++.'">';
792 foreach ($this->_row as $field) {
793 foreach ($field as $type=>$content) {
794 if ($field[$type] !== '') {
795 $field[$type] = '<span class="uu'.$type.'">'.$field[$type].'</span>';
796 } else {
797 unset($field[$type]);
800 echo '<td class="cell c'.$ci++.'">';
801 if (!empty($field)) {
802 echo implode('<br />', $field);
803 } else {
804 echo '&nbsp;';
806 echo '</td>';
808 echo '</tr>';
809 foreach ($this->columns as $col) {
810 $this->_row[$col] = array('normal'=>'', 'info'=>'', 'warning'=>'', 'error'=>'');
814 function track($col, $msg, $level='normal', $merge=true) {
815 if (empty($this->_row)) {
816 $this->flush(); //init arrays
818 if (!in_array($col, $this->columns)) {
819 debugging('Incorrect column:'.$col);
820 return;
822 if ($merge) {
823 if ($this->_row[$col][$level] != '') {
824 $this->_row[$col][$level] .='<br />';
826 $this->_row[$col][$level] .= s($msg);
827 } else {
828 $this->_row[$col][$level] = s($msg);
832 function close() {
833 echo '</table>';
838 * Validation callback function - verified the column line of csv file.
839 * Converts column names to lowercase too.
841 function validate_user_upload_columns(&$columns) {
842 global $STD_FIELDS, $PRF_FIELDS;
844 if (count($columns) < 2) {
845 return get_string('csvfewcolumns', 'error');
848 // test columns
849 $processed = array();
850 foreach ($columns as $key=>$unused) {
851 $columns[$key] = strtolower($columns[$key]); // no unicode expected here, ignore case
852 $field = $columns[$key];
853 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
854 !preg_match('/^course\d+$/', $field) && !preg_match('/^group\d+$/', $field) &&
855 !preg_match('/^type\d+$/', $field) && !preg_match('/^role\d+$/', $field)) {
856 return get_string('invalidfieldname', 'error', $field);
858 if (in_array($field, $processed)) {
859 return get_string('csvcolumnduplicates', 'error');
861 $processed[] = $field;
863 return true;
867 * Increments username - increments trailing number or adds it if not present.
868 * Varifies that the new username does not exist yet
869 * @param string $username
870 * @return incremented username which does not exist yet
872 function increment_username($username, $mnethostid) {
873 if (!preg_match_all('/(.*?)([0-9]+)$/', $username, $matches)) {
874 $username = $username.'2';
875 } else {
876 $username = $matches[1][0].($matches[2][0]+1);
879 if (record_exists('user', 'username', addslashes($username), 'mnethostid', addslashes($mnethostid))) {
880 return increment_username($username, $mnethostid);
881 } else {
882 return $username;
887 * Check if default field contains templates and apply them.
888 * @param string template - potential tempalte string
889 * @param object user object- we need username, firstname and lastname
890 * @return string field value
892 function process_template($template, $user) {
893 if (strpos($template, '%') === false) {
894 return $template;
897 // very very ugly hack!
898 global $template_globals;
899 $template_globals = new object();
900 $template_globals->username = isset($user->username) ? $user->username : '';
901 $template_globals->firstname = isset($user->firstname) ? $user->firstname : '';
902 $template_globals->lastname = isset($user->lastname) ? $user->lastname : '';
904 $result = preg_replace_callback('/(?<!%)%([+-~])?(\d)*([flu])/', 'process_template_callback', $template);
906 $template_globals = null;
908 if (is_null($result)) {
909 return $template; //error during regex processing??
910 } else {
911 return $result;
916 * Internal callback function.
918 function process_template_callback($block) {
919 global $template_globals;
920 $textlib = textlib_get_instance();
921 $repl = $block[0];
923 switch ($block[3]) {
924 case 'u': $repl = $template_globals->username; break;
925 case 'f': $repl = $template_globals->firstname; break;
926 case 'l': $repl = $template_globals->lastname; break;
928 switch ($block[1]) {
929 case '+': $repl = $textlib->strtoupper($repl); break;
930 case '-': $repl = $textlib->strtolower($repl); break;
931 case '~': $repl = $textlib->strtotitle($repl); break;
933 if (!empty($block[2])) {
934 $repl = $textlib->substr($repl, 0 , $block[2]);
937 return $repl;
941 * Returns list of auth plugins that are enabled and known to work.
943 function uu_allowed_auths() {
944 global $CFG;
946 // only following plugins are guaranteed to work properly
947 // TODO: add support for more plguins in 2.0
948 $whitelist = array('manual', 'nologin', 'none', 'email');
949 $plugins = get_enabled_auth_plugins();
950 $choices = array();
951 foreach ($plugins as $plugin) {
952 $choices[$plugin] = get_string('auth_'.$plugin.'title', 'auth');
955 return $choices;
959 * Returns list of non administrator roles
961 function uu_allowed_roles($shortname=false) {
962 global $CFG;
964 $roles = get_all_roles();
965 $choices = array();
966 foreach($roles as $role) {
967 if ($shortname) {
968 $choices[$role->id] = $role->shortname;
969 } else {
970 $choices[$role->id] = format_string($role->name);
973 // get rid of all admin roles
974 if ($adminroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW)) {
975 foreach($adminroles as $adminrole) {
976 unset($choices[$adminrole->id]);
980 return $choices;