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',
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
;
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');
98 if ($readcount === false) {
99 error($cir->get_error(), $returnurl);
100 } else if ($readcount == 0) {
101 print_error('csvemptyfile', 'error', $returnurl);
106 admin_externalpage_print_header();
107 print_heading_with_help(get_string('uploadusers'), 'uploadusers2');
109 admin_externalpage_print_footer();
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()) {
126 redirect($returnurl);
128 } else if ($formdata = $mform->get_data(false)) { // no magic quotes here!!!
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
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
176 $SESSION->bulk_users
= array();
179 // init csv import helper
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()) {
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) {
199 $key = $columns[$key];
200 // password is special field
201 if ($key == 'password') {
203 $user->password
= hash_internal_user_password($value);
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');
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');
232 if (!isset($user->lastname
) or $user->lastname
=== '') {
233 $upt->track('status', get_string('missingfield', 'error', 'lastname'), 'error');
234 $upt->track('lastname', $errorstr, 'error');
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');
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');
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)) {
285 // all validation moved to form2
286 if (isset($formdata->$field)) {
288 $user->$field = process_template($formdata->$field, $user);
291 foreach ($PRF_FIELDS as $field) {
292 if (isset($user->$field)) {
295 if (isset($formdata->$field)) {
297 $user->$field = process_template($formdata->$field, $user);
302 if (!empty($user->deleted
)) {
303 if (!$allowdeletes) {
305 $upt->track('status', $strusernotdeletedoff, 'warning');
309 if (has_capability('moodle/site:doanything', $systemcontext, $existinguser->id
)) {
310 $upt->track('status', $strusernotdeletedadmin, 'error');
314 if (delete_user($existinguser)) {
315 $upt->track('status', $struserdeleted);
318 $upt->track('status', $strusernotdeletederror, 'error');
322 $upt->track('status', $strusernotdeletedmissing, 'error');
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) {
335 $upt->track('status', $strusernotrenamedoff, 'warning');
340 $upt->track('status', $strusernotrenamedexists, 'error');
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');
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);
358 $upt->track('status', $strusernotrenamedexists, 'error');
363 $upt->track('status', $strusernotrenamedmissing, 'error');
367 $existinguser = $olduser;
368 $existinguser->username
= $user->username
;
371 // can we process with update or insert?
377 $upt->track('status', $strusernotadded, 'warning');
384 //this should not happen!
385 $upt->track('status', $strusernotaddederror, 'error');
395 if (!$existinguser) {
397 $upt->track('status', $strusernotupdatednotexists, 'warning');
408 $user->id
= $existinguser->id
;
410 if (has_capability('moodle/site:doanything', $systemcontext, $user->id
)) {
411 $upt->track('status', $strusernotupdatedadmin, 'error');
417 // no updates of existing data at all
419 $existinguser->timemodified
= time();
420 //load existing profile data
421 profile_load_data($existinguser);
424 if ($updatetype == 1) {
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') {
433 if ($column == 'password') {
434 if (!$updatepasswords or $updatetype == 3) {
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
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');
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');
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);
481 $upt->track('status', $strusernotupdated, 'error');
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
;
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');
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');
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');
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);
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'));
542 // Record not added -- possibly some other error
543 $upt->track('status', $strusernotaddederror, 'error');
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)) {
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');
573 $ccache[$shortname] = $course;
574 $ccache[$shortname]->groups
= null;
576 $courseid = $ccache[$shortname]->id
;
577 $coursecontext = get_context_instance(CONTEXT_COURSE
, $courseid);
581 if (!empty($user->{'role'.$i})) {
582 $addrole = $user->{'role'.$i};
583 if (array_key_exists($addrole, $rolecache)) {
584 $rid = $rolecache[$addrole]->id
;
586 $upt->track('enrolments', get_string('unknownrole', 'error', $addrole), 'error');
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');
596 } else if ($addtype == 1 and empty($formdata->uulegacy1
)) {
597 if (empty($ccache[$shortname]->defaultrole
)) {
598 $rid = $CFG->defaultcourseroleid
;
600 $rid = $ccache[$shortname]->defaultrole
;
603 $rid = $formdata->{'uulegacy'.$addtype};
607 // no role specified, use the default
608 if (empty($ccache[$shortname]->defaultrole
)) {
609 $rid = $CFG->defaultcourseroleid
;
611 $rid = $ccache[$shortname]->defaultrole
;
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));
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');
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
;
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
;
658 $upt->track('enrolments', get_string('unknowngroup', 'error', $addgroup), 'error');
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));
668 $upt->track('enrolments', get_string('addedtogroupnot', '', $gname), 'error');
675 $upt->close(); // close table
680 print_box_start('boxwidthnarrow boxaligncenter generalbox', 'uploadresults');
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 />';
689 echo get_string('usersdeleted', 'admin').': '.$deletes.'<br />';
690 echo get_string('deleteerrors', 'admin').': '.$deleteerrors.'<br />';
693 echo get_string('usersrenamed', 'admin').': '.$renames.'<br />';
694 echo get_string('renameerrors', 'admin').': '.$renameerrors.'<br />';
697 echo get_string('usersskipped', 'admin').': '.$usersskipped.'<br />';
699 echo get_string('errors', 'admin').': '.$userserrors.'</p>';
703 print_continue($bulknurl);
705 print_continue($returnurl);
707 admin_externalpage_print_footer();
712 admin_externalpage_print_header();
715 print_heading_with_help(get_string('uploaduserspreview', 'admin'), 'uploadusers2');
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>';
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>';;
737 echo '<tr class="r'.$ri++
.'">';
738 foreach ($fields as $field) {
739 echo '<td class="cell c'.$ci++
.'">'.s($field).'</td>';;
746 echo '<div class="centerpara">'.get_string('uupreprocessedcount', 'admin', $readcount).'</div>';
748 admin_externalpage_print_footer();
751 /////////////////////////////////////
752 /// Utility functions and classes ///
753 /////////////////////////////////////
755 class uu_progress_tracker
{
757 var $columns = array('status', 'line', 'id', 'username', 'firstname', 'lastname', 'email', 'password', 'auth', 'enrolments', 'deleted');
759 function uu_progress_tracker() {
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>';
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'=>'');
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>';
797 unset($field[$type]);
800 echo '<td class="cell c'.$ci++
.'">';
801 if (!empty($field)) {
802 echo implode('<br />', $field);
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);
823 if ($this->_row
[$col][$level] != '') {
824 $this->_row
[$col][$level] .='<br />';
826 $this->_row
[$col][$level] .= s($msg);
828 $this->_row
[$col][$level] = s($msg);
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');
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;
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';
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);
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) {
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??
916 * Internal callback function.
918 function process_template_callback($block) {
919 global $template_globals;
920 $textlib = textlib_get_instance();
924 case 'u': $repl = $template_globals->username
; break;
925 case 'f': $repl = $template_globals->firstname
; break;
926 case 'l': $repl = $template_globals->lastname
; break;
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]);
941 * Returns list of auth plugins that are enabled and known to work.
943 function uu_allowed_auths() {
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();
951 foreach ($plugins as $plugin) {
952 $choices[$plugin] = get_string('auth_'.$plugin.'title', 'auth');
959 * Returns list of non administrator roles
961 function uu_allowed_roles($shortname=false) {
964 $roles = get_all_roles();
966 foreach($roles as $role) {
968 $choices[$role->id
] = $role->shortname
;
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
]);