diff --git a/admin/uploaduser.php b/admin/uploaduser.php index 246951b318c..3be938c3a4d 100755 --- a/admin/uploaduser.php +++ b/admin/uploaduser.php @@ -3,23 +3,26 @@ /// Bulk user registration script from a comma separated file /// Returns list of users with their user ids -require_once('../config.php'); +require('../config.php'); require_once($CFG->libdir.'/adminlib.php'); +require_once($CFG->libdir.'/csvlib.class.php'); +require_once($CFG->dirroot.'/user/profile/lib.php'); require_once('uploaduser_form.php'); -$uplid = optional_param('uplid', '', PARAM_FILE); +$iid = optional_param('iid', '', PARAM_INT); $previewrows = optional_param('previewrows', 10, PARAM_INT); -$separator = optional_param('separator', 'comma', PARAM_ALPHA); +$readcount = optional_param('readcount', 0, PARAM_INT); -if (!defined('UP_LINE_MAX_SIZE')) { - define('UP_LINE_MAX_SIZE', 4096); -} +define('UU_ADDNEW', 0); +define('UU_ADDINC', 1); +define('UU_ADD_UPDATE', 2); +define('UU_UPDATE', 3); @set_time_limit(3600); // 1 hour should be enough @raise_memory_limit('256M'); if (function_exists('apache_child_terminate')) { - // if we are running from Apache, give httpd a hint that - // it can recycle the process after it's done. Apache's + // if we are running from Apache, give httpd a hint that + // it can recycle the process after it's done. Apache's // memory management is truly awful but we can help it. @apache_child_terminate(); } @@ -28,126 +31,72 @@ admin_externalpage_setup('uploadusers'); require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM)); $textlib = textlib_get_instance(); +$systemcontext = get_context_instance(CONTEXT_SYSTEM); -$struserrenamed = get_string('userrenamed', 'admin'); -$strusernotrenamedexists = get_string('usernotrenamedexists', 'error'); -$strusernotrenamedmissing = get_string('usernotrenamedmissing', 'error'); +$struserrenamed = get_string('userrenamed', 'admin'); +$strusernotrenamedexists = get_string('usernotrenamedexists', 'error'); +$strusernotrenamedmissing = get_string('usernotrenamedmissing', 'error'); +$strusernotrenamedoff = get_string('usernotrenamedoff', 'error'); +$strusernotrenamedadmin = get_string('usernotrenamedadmin', 'error'); -$struserupdated = get_string('useraccountupdated', 'admin'); -$strusernotupdated = get_string('usernotupdatederror', 'error'); +$struserupdated = get_string('useraccountupdated', 'admin'); +$strusernotupdated = get_string('usernotupdatederror', 'error'); +$strusernotupdatednotexists = get_string('usernotupdatednotexists', 'error'); +$strusernotupdatedadmin = get_string('usernotupdatedadmin', 'error'); -$struseradded = get_string('newuser'); -$strusernotadded = get_string('usernotaddedregistered', 'error'); -$strusernotaddederror = get_string('usernotaddederror', 'error'); +$struseradded = get_string('newuser'); +$strusernotadded = get_string('usernotaddedregistered', 'error'); +$strusernotaddederror = get_string('usernotaddederror', 'error'); -$struserdeleted = get_string('userdeleted', 'admin'); -$strusernotdeletederror = get_string('usernotdeletederror', 'error'); -$strusernotdeletedmissing = get_string('usernotdeletedmissing', 'error'); +$struserdeleted = get_string('userdeleted', 'admin'); +$strusernotdeletederror = get_string('usernotdeletederror', 'error'); +$strusernotdeletedmissing = get_string('usernotdeletedmissing', 'error'); +$strusernotdeletedoff = get_string('usernotdeletedoff', 'error'); +$strusernotdeletedadmin = get_string('usernotdeletedadmin', 'error'); -$strcannotassignrole = get_string('cannotassignrole', 'error'); -$strduplicateusername = get_string('duplicateusername', 'error'); -$strindent = '-->'; +$strcannotassignrole = get_string('cannotassignrole', 'error'); +$strduplicateusername = get_string('duplicateusername', 'error'); -$return = $CFG->wwwroot.'/'.$CFG->admin.'/uploaduser.php'; +$struserauthunsupported = get_string('userauthunsupported', 'error'); -// make arrays of valid fields for error checking -// the value associated to each field is: 0 = optional field, 1 = field required either in default values or in data file -$fields = array( - 'firstname' => 1, - 'lastname' => 1, - 'username' => 1, - 'email' => 1, - 'city' => 1, - 'country' => 1, - 'lang' => 1, - 'auth' => 1, - 'timezone' => 1, - 'mailformat' => 1, - 'maildisplay' => 1, - 'htmleditor' => 0, - 'ajax' => 0, - 'autosubscribe' => 1, - 'mnethostid' => 0, - 'institution' => 0, - 'department' => 0, - 'idnumber' => 0, - 'icq' => 0, - 'phone1' => 0, - 'phone2' => 0, - 'address' => 0, - 'url' => 0, - 'description' => 0, - 'icq' => 0, - 'oldusername' => 0, - 'emailstop' => 1, - 'deleted' => 0, - 'password' => 0, // changed later -); -if (empty($uplid)) { +$errorstr = get_string('error'); + +$returnurl = $CFG->wwwroot.'/'.$CFG->admin.'/uploaduser.php'; +$bulknurl = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php'; + +// array of all valid fields for validation +$STD_FIELDS = array('firstname', 'lastname', 'username', 'email', 'city', 'country', 'lang', 'auth', 'timezone', 'mailformat', 'maildisplay', 'htmleditor', + 'ajax', 'autosubscribe', 'mnethostid', 'institution', 'department', 'idnumber', 'icq', 'phone1', 'phone2', 'address', 'url', 'description', + 'icq', 'oldusername', 'emailstop', 'deleted', 'password'); + +$PRF_FIELDS = array(); + +if ($prof_fields = $fields = get_records_select('user_info_field')) { + foreach ($prof_fields as $prof_field) { + $PRF_FIELDS[] = 'profile_field_'.$prof_field->shortname; + } + unset($prof_fields); +} + +if (empty($iid)) { $mform = new admin_uploaduser_form1(); if ($formdata = $mform->get_data()) { - if (!$filename = make_upload_directory('temp/uploaduser/'.$USER->id, true)) { - error('Can not create temporary upload directory!', $return); - } - // use current (non-conflicting) time stamp - $uplid = time(); - while (file_exists($filename.'/'.$uplid)) { - $uplid--; - } - $filename = $filename.'/'.$uplid; + $iid = csv_import_reader::get_new_iid('uploaduser'); + $cir = new csv_import_reader($iid, 'uploaduser'); - $text = $mform->get_file_content('userfile'); - // convert to utf-8 encoding - $text = $textlib->convert($text, $formdata->encoding, 'utf-8'); - // remove Unicode BOM from first line - $text = $textlib->trim_utf8_bom($text); - // Fix mac/dos newlines - $text = preg_replace('!\r\n?!', "\n", $text); - //remove empty lines at the beginning and end - $text = trim($text); + $content = $mform->get_file_content('userfile'); - // verify each line has the same number of separators - this detects major breakage in files - $line = strtok($text, "\n"); - if ($line === false) { - error('Empty file', $return); //TODO: localize + $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name, 'validate_user_upload_columns'); + unset($content); + + if ($readcount === false) { + error($cir->get_error(), $returnurl); + } else if ($readcount == 0) { + error(get_string('csvemptyfile', 'error'), $returnurl); } - - // test headers - $csv_delimiter = get_upload_csv_delimiter($separator); - $col_count = substr_count($line, $csv_delimiter); - if ($col_count < 2) { - error('Not enough columns, please verify the separator setting!', $return); //TODO: localize - } - - $line = explode($csv_delimiter, $line); - foreach ($line as $key => $value) { - $value = trim($value); // remove whitespace - if (!array_key_exists($value, $fields) && // if not a standard field and not an enrolment field, then we have an error - !preg_match('/^course\d+$/', $value) && !preg_match('/^group\d+$/', $value) && - !preg_match('/^type\d+$/', $value) && !preg_match('/^role\d+$/', $value)) { - error(get_string('invalidfieldname', 'error', $value), $return); - } - } - - $line = strtok("\n"); - if ($line === false) { - error('Only one row present, can not continue!', $return); //TODO: localize - } - - while ($line !== false) { - if (substr_count($line, $csv_delimiter) !== $col_count) { - error('Incorrect file format - number of columns is not constant!', $return); //TODO: localize - } - $line = strtok("\n"); - } - - // store file - $fp = fopen($filename, "w"); - fwrite($fp,$text); - fclose($fp); - // continue to second form + // continue to form2 } else { admin_externalpage_print_header(); @@ -156,351 +105,566 @@ if (empty($uplid)) { admin_externalpage_print_footer(); die; } +} else { + $cir = new csv_import_reader($iid, 'uploaduser'); } -$mform = new admin_uploaduser_form2(); -// set initial date from form1 -$mform->set_data(array('separator'=>$separator, 'uplid'=>$uplid, 'previewrows'=>$previewrows)); +if (!$columns = $cir->get_columns()) { + error('Error reading temporary file', $returnurl); +} +$mform = new admin_uploaduser_form2(null, $columns); +// get initial date from form1 +$mform->set_data(array('iid'=>$iid, 'previewrows'=>$previewrows, 'readcount'=>$readcount)); // If a file has been uploaded, then process it if ($formdata = $mform->is_cancelled()) { - user_upload_cleanup($uplid); - redirect($return); + $cir->cleanup(true); + redirect($returnurl); -} else if ($formdata = $mform->get_data()) { +} else if ($formdata = $mform->get_data(false)) { // no magic quotes here!!! // Print the header admin_externalpage_print_header(); - print_heading(get_string('uploadusers')); + print_heading(get_string('uploadusersresult', 'admin')); - $createpassword = $formdata->createpassword; - $updateaccounts = $formdata->updateaccounts; - $allowrenames = $formdata->allowrenames; - $skipduplicates = $formdata->duplicatehandling; + $optype = $formdata->uutype; - $fields['password'] = !$createpassword; + $createpasswords = (!empty($formdata->uupasswordnew) and $optype != UU_UPDATE); + $updatepasswords = (!empty($formdata->uupasswordold) and $optype != UU_ADDNEW and $optype != UU_ADDINC); + $allowrenames = (!empty($formdata->uuallowrenames) and $optype != UU_ADDNEW and $optype != UU_ADDINC); + $allowdeletes = (!empty($formdata->uuallowdeletes) and $optype != UU_ADDNEW and $optype != UU_ADDINC); + $updatetype = isset($formdata->uuupdatetype) ? $formdata->uuupdatetype : 0; + $bulk = $formdata->uubulk; - $filename = $CFG->dataroot.'/temp/uploaduser/'.$USER->id.'/'.$uplid; - if (!file_exists($filename)) { - user_upload_cleanup($uplid); - error('Error reading temporary file!', $return); //TODO: localize - } - if (!$fp = fopen($filename, "r")) { - user_upload_cleanup($uplid); - error('Error reading temporary file!', $return); //TODO: localize - } - - $csv_delimiter = get_upload_csv_delimiter($separator); - $csv_encode = get_upload_csv_encode($csv_delimiter); + // verification moved to two places: after upload and into form2 + $usersnew = 0; + $usersupdated = 0; + $userserrors = 0; + $deletes = 0; + $deleteerrors = 0; + $renames = 0; + $renameerrors = 0; + $usersskipped = 0; - // find header row - $headers = array(); - $linenum = 1; - $line = explode($csv_delimiter, fgets($fp, UP_LINE_MAX_SIZE)); + // caches + $ccache = array(); // course cache - do not fetch all courses here, we will not probably use them all anyway! + $rolecache = array(); // roles lookup cache - // prepare headers - foreach ($line as $key => $value) { - $headers[$key] = trim($value); - } + $allowedauths = uu_allowed_auths(); + $allowedauths = array_keys($allowedauths); + $availableauths = get_list_of_plugins('auth'); - // check that required fields are present or a default value for them exists - $headersOk = true; - // disable the check if we also have deleting information (ie. deleted column) - if (!in_array('deleted', $headers)) { - foreach ($fields as $key => $required) { - if($required && !in_array($key, $headers) && (!isset($formdata->$key) || $formdata->$key==='')) { - notify(get_string('missingfield', 'error', $key)); - $headersOk = false; - } + $allowedroles = uu_allowed_roles(true); + foreach ($allowedroles as $rid=>$rname) { + $rolecache[$rid] = new object(); + $rolecache[$rid]->id = $rid; + $rolecache[$rid]->name = $rname; + if (!is_numeric($rname)) { // only non-numeric shornames are supported!!! + $rolecache[$rname] = new object(); + $rolecache[$rname]->id = $rid; + $rolecache[$rname]->name = $rname; } } - if ($headersOk) { - $usersnew = 0; - $usersupdated = 0; - $userserrors = 0; - $usersdeleted = 0; - $renames = 0; - $renameerrors = 0; - $deleteerrors = 0; - $newusernames = array(); - // We'll need courses a lot, so fetch it early and keep it in memory, indexed by their shortname - $tmp =& get_courses('all','','id,shortname,visible'); - $courses = array(); - foreach ($tmp as $c) { - $courses[$c->shortname] = $c; - } - unset($tmp); + unset($allowedroles); - echo '
';
- while (!feof($fp)) {
- $linenum++;
- $line = explode($csv_delimiter, fgets($fp, UP_LINE_MAX_SIZE));
- $errors = '';
- $user = new object();
- // by default, use the local mnet id (this may be changed in the file)
- $user->mnethostid = $CFG->mnet_localhost_id;
- // add fields to user object
- foreach ($line as $key => $value) {
- if($value !== '') {
- $key = $headers[$key];
- //decode encoded commas
- $value = str_replace($csv_encode,$csv_delimiter,trim($value));
- // special fields: password and username
- if ($key == 'password' && !empty($value)) {
- $user->$key = hash_internal_user_password($value);
- } else if($key == 'username') {
- $value = $textlib->strtolower(addslashes($value));
- if(empty($CFG->extendedusernamechars)) {
- $value = eregi_replace('[^(-\.[:alnum:])]', '', $value);
- }
- @$newusernames[$value]++;
- $user->$key = $value;
- } else {
- $user->$key = addslashes($value);
- }
- }
- }
+ // clear bilk selection
+ if ($bulk) {
+ $SESSION->bulk_susers = array();
+ }
- // add default values for remaining fields
- foreach ($fields as $key => $required) {
- if(isset($user->$key)) {
- continue;
- }
- if(!isset($formdata->$key) || $formdata->$key==='') { // no default value was submited
- // if the field is required, give an error only if we are adding the user or deleting a user with unkown username
- if($required && (empty($user->deleted) || $key == 'username')) {
- $errors .= get_string('missingfield', 'error', $key) . ' ';
- }
- continue;
- }
- // process templates
- $template = $formdata->$key;
- $templatelen = strlen($template);
- $value = '';
- for ($i = 0 ; $i < $templatelen; ++$i) {
- if($template[$i] == '%') {
- $case = 0; // 1=lowercase, 2=uppercase
- $len = 0; // number of characters to keep
- $info = null; // data to process
- for($j = $i + 1; is_null($info) && $j < $templatelen; ++$j) {
- $car = $template[$j];
- if ($car >= '0' && $car <= '9') {
- $len = $len * 10 + (int)$car;
- } else if($car == '-') {
- $case = 1;
- } else if($car == '+') {
- $case = 2;
- } else if($car == 'f') { // first name
- $info = @$user->firstname;
- } else if($car == 'l') { // last name
- $info = @$user->lastname;
- } else if($car == 'u') { // username
- $info = @$user->username;
- } else if($car == '%' && $j == $i+1) {
- $info = '%';
- } else { // invalid character
- $info = '';
- }
- }
- if($info==='' || is_null($info)) { // invalid template
- continue;
- }
- $i = $j - 1;
- // change case
- if($case == 1) {
- $info = $textlib->strtolower($info);
- } else if($case == 2) {
- $info = $textlib->strtoupper($info);
- }
- if($len) { // truncate data
- $info = $textlib->substr($info, 0, $len);
- }
- $value .= $info;
- } else {
- $value .= $template[$i];
- }
- }
+ // init csv import helper
+ $cir->init();
+ $linenum = 1; //column header is first line
- if($key == 'username') {
- $value = $textlib->strtolower($value);
- if(empty($CFG->extendedusernamechars)) {
- $value = eregi_replace('[^(-\.[:alnum:])]', '', $value);
- }
- @$newusernames[$value]++;
- // check for new username duplicates
- if($newusernames[$value] > 1) {
- if($skipduplicates) {
- $errors .= $strduplicateusername . ' (' . stripslashes($value) . '). ';
- continue;
- } else {
- $value .= $newusernames[$value];
- }
- }
- }
- $user->$key = $value;
- }
- if($errors) {
- notify(get_string('erroronline', 'error', $linenum). ': ' . $errors);
- ++$userserrors;
- continue;
- }
+ // init upload progress tracker
+ $upt = new uu_progress_tracker();
+ $upt->init(); // start table
- // delete user
- if(@$user->deleted) {
- $info = ': ' . stripslashes($user->username) . '. ';
- if($user =& get_record('user', 'username', $user->username, 'mnethostid', $user->mnethostid)) {
- $user->timemodified = time();
- $user->username = addslashes($user->email . $user->timemodified); // Remember it just in case
- $user->deleted = 1;
- $user->email = ''; // Clear this field to free it up
- $user->idnumber = ''; // Clear this field to free it up
- if (update_record('user', $user)) {
- // not sure if this is needed. unenrol_student($user->id); // From all courses
- delete_records('role_assignments', 'userid', $user->id); // unassign all roles
- // remove all context assigned on this user?
- echo $struserdeleted . $info . '
';
- ++$usersdeleted;
- } else {
- notify(get_string('erroronline', 'error', $linenum). ': ' . $strusernotdeletederror . $info);
- ++$deleteerrors;
+ while ($line = $cir->next()) {
+ $upt->flush();
+ $linenum++;
+
+ $upt->track('line', $linenum);
+
+ $user = new object();
+ // by default, use the local mnet id (this may be changed in the file)
+ $user->mnethostid = $CFG->mnet_localhost_id;
+ // add fields to user object
+ foreach ($line as $key => $value) {
+ if ($value !== '') {
+ $key = $columns[$key];
+ // password is special field
+ if ($key == 'password') {
+ if ($value !== '') {
+ $user->password = hash_internal_user_password($value);
}
} else {
- notify(get_string('erroronline', 'error', $linenum). ': ' . $strusernotdeletedmissing . $info);
- ++$deleteerrors;
+ $user->$key = $value;
+ if (in_array($key, $upt->columns)) {
+ $upt->track($key, $value);
+ }
}
+ }
+ }
+
+ // get username, first/last name now - we need them in templates!!
+ if ($optype == UU_UPDATE) {
+ // when updating only username is required
+ if (!isset($user->username)) {
+ $upt->track('status', get_string('missingfield', 'error', 'username'), 'error');
+ $upt->track('username', $errorstr, 'error');
+ $userserrors++;
continue;
}
+ } else {
+ $error = false;
+ // when all other ops need firstname and lastname
+ if (!isset($user->firstname) or $user->firstname === '') {
+ $upt->track('status', get_string('missingfield', 'error', 'firstname'), 'error');
+ $upt->track('firstname', $errorstr, 'error');
+ $error = true;
+ }
+ if (!isset($user->lastname) or $user->lastname === '') {
+ $upt->track('status', get_string('missingfield', 'error', 'lastname'), 'error');
+ $upt->track('lastname', $errorstr, 'error');
+ $error = true;
+ }
+ if ($error) {
+ $userserrors++;
+ continue;
+ }
+ // we require username too - we might use template for it though
+ if (!isset($user->username)) {
+ if (!isset($formdata->username) or $formdata->username === '') {
+ $upt->track('status', get_string('missingfield', 'error', 'username'), 'error');
+ $upt->track('username', $errorstr, 'error');
+ $userserrors++;
+ continue;
+ } else {
+ $user->username = process_template($formdata->username, $user);
+ $upt->track('username', $user->username);
+ }
+ }
+ }
+
+ // normalize username
+ $user->username = $textlib->strtolower($user->username);
+ if (empty($CFG->extendedusernamechars)) {
+ $user->username = eregi_replace('[^(-\.[:alnum:])]', '', $user->username);
+ }
+ if (empty($user->username)) {
+ $upt->track('status', get_string('missingfield', 'error', 'username'), 'error');
+ $upt->track('username', $errorstr, 'error');
+ $userserrors++;
+ continue;
+ }
+
+ if ($existinguser = get_record('user', 'username', addslashes($user->username), 'mnethostid', $user->mnethostid)) {
+ $upt->track('id', $existinguser->id, 'normal', false);
+ }
+
+ // find out in username incrementing required
+ if ($existinguser and $optype == UU_ADDINC) {
+ $oldusername = $user->username;
+ $user->username = increment_username($user->username, $user->mnethostid);
+ $upt->track('username', '', 'normal', false); // clear previous
+ $upt->track('username', $oldusername.'-->'.$user->username, 'info');
+ $existinguser = false;
+ }
+
+ // add default values for remaining fields
+ foreach ($STD_FIELDS as $field) {
+ if (isset($user->$field)) {
+ continue;
+ }
+ // all validation moved to form2
+ if (isset($formdata->$field)) {
+ // process templates
+ $user->$field = process_template($formdata->$field, $user);
+ }
+ }
+ foreach ($PRF_FIELDS as $field) {
+ if (isset($user->$field)) {
+ continue;
+ }
+ if (isset($formdata->$field)) {
+ // process templates
+ $user->$field = process_template($formdata->$field, $user);
+ }
+ }
+
+ // delete user
+ if (!empty($user->deleted)) {
+ if (!$allowdeletes) {
+ $usersskipped++;
+ $upt->track('status', $strusernotdeletedoff, 'warning');
+ continue;
+ }
+ if ($existinguser) {
+ if (has_capability('moodle/site:doanything', $systemcontext, $existinguser->id)) {
+ $upt->track('status', $strusernotdeletedadmin, 'error');
+ $deleteerrors++;
+ continue;
+ }
+ if (delete_user($existinguser)) {
+ $upt->track('status', $struserdeleted);
+ $deletes++;
+ } else {
+ $upt->track('status', $strusernotdeletederror, 'error');
+ $deleteerrors++;
+ }
+ } else {
+ $upt->track('status', $strusernotdeletedmissing, 'error');
+ $deleteerrors++;
+ }
+ continue;
+ }
+ // we do not need the deleted flag anymore
+ unset($user->deleted);
+
+ // renaming requested?
+ if (!empty($user->oldusername) ) {
+ $oldusername = $textlib->strtolower($user->oldusername);
+ if (!$allowrenames) {
+ $usersskipped++;
+ $upt->track('status', $strusernotrenamedoff, 'warning');
+ continue;
+ }
+
+ if ($existinguser) {
+ $upt->track('status', $strusernotrenamedexists, 'error');
+ $renameerrors++;
+ continue;
+ }
+
+ if ($olduser = get_record('user', 'username', addslashes($oldusername), 'mnethostid', $user->mnethostid)) {
+ $upt->track('id', $olduser->id, 'normal', false);
+ if (has_capability('moodle/site:doanything', $systemcontext, $olduser->id)) {
+ $upt->track('status', $strusernotrenamedadmin, 'error');
+ $renameerrors++;
+ continue;
+ }
+ if (set_field('user', 'username', addslashes($user->username), 'id', $olduser->id)) {
+ $upt->track('username', '', 'normal', false); // clear previous
+ $upt->track('username', $oldusername.'-->'.$user->username, 'info');
+ $upt->track('status', $struserrenamed);
+ $renames++;
+ } else {
+ $upt->track('status', $strusernotrenamedexists, 'error');
+ $renameerrors++;
+ continue;
+ }
+ } else {
+ $upt->track('status', $strusernotrenamedmissing, 'error');
+ $renameerrors++;
+ continue;
+ }
+ $existinguser = $olduser;
+ $existinguser->username = $user->username;
+ }
+
+ // can we process with update or insert?
+ $skip = false;
+ switch ($optype) {
+ case UU_ADDNEW:
+ if ($existinguser) {
+ $usersskipped++;
+ $upt->track('status', $strusernotadded, 'warning');
+ $skip = true;;
+ }
+ break;
+
+ case UU_ADDINC:
+ if ($existinguser) {
+ //this should not happen!
+ $upt->track('status', $strusernotaddederror, 'error');
+ $userserrors++;
+ continue;
+ }
+ break;
+
+ case UU_ADD_UPDATE:
+ break;
+
+ case UU_UPDATE:
+ if (!$existinguser) {
+ $usersskipped++;
+ $upt->track('status', $strusernotupdatednotexists, 'warning');
+ $skip = true;
+ }
+ break;
+ }
+
+ if ($skip) {
+ continue;
+ }
+
+ if ($existinguser) {
+ $user->id = $existinguser->id;
+
+ if (has_capability('moodle/site:doanything', $systemcontext, $user->id)) {
+ $upt->track('status', $strusernotupdatedadmin, 'error');
+ $userserrors++;
+ continue;
+ }
+
+ if (!$updatetype) {
+ // no updates of existing data at all
+ } else {
+ $existinguser->timemodified = time();
+ //load existing profile data
+ profile_load_data($existinguser);
+
+ $allowed = array();
+ if ($updatetype == 1) {
+ $allowed = $columns;
+ } else if ($updatetype == 2 or $updatetype == 3) {
+ $allowed = array_merge($STD_FIELDS, $PRF_FIELDS);
+ }
+ foreach ($allowed as $column) {
+ if ($column == 'username') {
+ continue;
+ }
+ if ($column == 'password') {
+ if (!$updatepasswords or $updatetype == 3) {
+ continue;
+ } else if (!empty($user->password)) {
+ $upt->track('password', get_string('updated'));
+ }
+ }
+ if ((array_key_exists($column, $existinguser) and array_key_exists($column, $user)) or in_array($column, $PRF_FIELDS)) {
+ if ($updatetype == 3 and $existinguser->$column !== '') {
+ //missing == non-empty only
+ continue;
+ }
+ if ($existinguser->$column !== $user->$column) {
+ if ($column != 'password' and in_array($column, $upt->columns)) {
+ $upt->track($column, '', 'normal', false); // clear previous
+ $upt->track($column, $existinguser->$column.'-->'.$user->$column, 'info');
+ }
+ $existinguser->$column = $user->$column;
+ }
+ }
+ }
+
+ // do not update record if new auth plguin does not exist!
+ if (!in_array($existinguser->auth, $availableauths)) {
+ $upt->track('auth', get_string('userautherror', 'error', $existinguser->auth), 'error');
+ $upt->track('status', $strusernotupdated, 'error');
+ $userserrors++;
+ continue;
+ } else if (!in_array($existinguser->auth, $allowedauths)) {
+ $upt->track('auth', $struserauthunsupported, 'warning');
+ }
+
+ if (update_record('user', addslashes_recursive($existinguser))) {
+ $upt->track('status', $struserupdated);
+ $usersupdated++;
+ } else {
+ $upt->track('status', $strusernotupdated, 'error');
+ $userserrors++;
+ continue;
+ }
+ // save custom profile fields data from csv file
+ profile_save_data(addslashes_recursive($existinguser));
+ }
+
+ if ($bulk == 2 or $bulk == 3) {
+ if (!in_array($user->id, $SESSION->bulk_susers)) {
+ $SESSION->bulk_susers[] = $user->id;
+ }
+ }
+
+ } else {
// save the user to the database
$user->confirmed = 1;
$user->timemodified = time();
- // before insert/update, check whether we should be updating an old record instead
- if ($allowrenames && !empty($user->oldusername) ) {
- $user->oldusername = $textlib->strtolower($user->oldusername);
- $info = ': ' . stripslashes($user->oldusername) . '-->' . stripslashes($user->username) . '. ';
- if ($olduser =& get_record('user', 'username', $user->oldusername, 'mnethostid', $user->mnethostid)) {
- if (set_field('user', 'username', $user->username, 'id', $olduser->id)) {
- echo $struserrenamed . $info;
- $renames++;
- } else {
- notify(get_string('erroronline', 'error', $linenum). ': ' . $strusernotrenamedexists . $info);
- $renameerrors++;
- continue;
- }
- } else {
- notify(get_string('erroronline', 'error', $linenum). ': ' . $strusernotrenamedmissing . $info);
- $renameerrors++;
+ if (!$createpasswords and empty($user->password)) {
+ $upt->track('password', get_string('missingfield', 'error', 'password'), 'error');
+ $upt->track('status', $strusernotaddederror, 'error');
+ $userserrors++;
+ continue;
+ }
+
+ // do not insert record if new auth plguin does not exist!
+ if (isset($user->auth)) {
+ if (!in_array($user->auth, $availableauths)) {
+ $upt->track('auth', get_string('userautherror', 'error', $user->auth), 'error');
+ $upt->track('status', $strusernotaddederror, 'error');
+ $userserrors++;
continue;
+ } else if (!in_array($user->auth, $allowedauths)) {
+ $upt->track('auth', $struserauthunsupported, 'warning');
}
}
- // save the information
- if ($olduser =& get_record('user', 'username', $user->username, 'mnethostid', $user->mnethostid)) {
- $user->id = $olduser->id;
- $info = ': ' . stripslashes($user->username) .' (ID = ' . $user->id . ')';
- if ($updateaccounts) {
- // Record is being updated
- if (update_record('user', $user)) {
- echo $struserupdated . $info . '
';
- $usersupdated++;
- } else {
- notify(get_string('erroronline', 'error', $linenum). ': ' . $strusernotupdated . $info);
- $userserrors++;
- continue;
- }
- } else {
- //Record not added - user is already registered
- //In this case, output userid from previous registration
- //This can be used to obtain a list of userids for existing users
- echo $strusernotadded . $info . '
';
- $userserrors++;
+ if ($user->id = insert_record('user', addslashes_recursive($user))) {
+ $info = ': ' . $user->username .' (ID = ' . $user->id . ')';
+ $upt->track('status', $struseradded);
+ $upt->track('id', $user->id, 'normal', false);
+ $usersnew++;
+ if ($createpasswords and empty($user->password)) {
+ // passwords will be created and sent out on cron
+ set_user_preference('create_password', 1, $user->id);
+ set_user_preference('auth_forcepasswordchange', 1, $user->id);
+ $upt->track('password', get_string('new'));
}
- } else { // new user
- if ($user->id = insert_record('user', $user)) {
- $info = ': ' . stripslashes($user->username) .' (ID = ' . $user->id . ')';
- echo $struseradded . $info . '
';
- $usersnew++;
- if (empty($user->password) && $createpassword) {
- // passwords will be created and sent out on cron
- set_user_preference('create_password', 1, $user->id);
- set_user_preference('auth_forcepasswordchange', 1, $user->id);
+ } else {
+ // Record not added -- possibly some other error
+ $upt->track('status', $strusernotaddederror, 'error');
+ $userserrors++;
+ continue;
+ }
+ // save custom profile fields data
+ profile_save_data($user);
+
+ if ($bulk == 1 or $bulk == 3) {
+ if (!in_array($user->id, $SESSION->bulk_susers)) {
+ $SESSION->bulk_susers[] = $user->id;
+ }
+ }
+ }
+
+ // find course enrolments, groups and roles/types
+ foreach ($columns as $column) {
+ if (!preg_match('/^course\d+$/', $column)) {
+ continue;
+ }
+ $i = substr($column, 6);
+
+ $shortname = $user->{'course'.$i};
+ if (!array_key_exists($shortname, $ccache)) {
+ if (!$course = get_record('course', 'shortname', $shortname, '', '', '', '', 'id, shortname, defaultrole')) {
+ $upt->track('enrolments', get_string('unknowncourse', 'error', $shortname), 'error');
+ continue;
+ }
+ $ccache[$shortname] = $course;
+ $ccache[$shortname]->groups = null;
+ }
+ $courseid = $ccache[$shortname]->id;
+ $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
+
+ // find role
+ $rid = false;
+ if (!empty($user->{'role'.$i})) {
+ $addrole = $user->{'role'.$i};
+ if (array_key_exists($addrole, $rolecache)) {
+ $rid = $rolecache[$addrole]->id;
+ } else {
+ $upt->track('enrolments', get_string('unknownrole', 'error', $addrole), 'error');
+ continue;
+ }
+
+ } else if (!empty($user->{'type'.$i})) {
+ // if no role, then find "old" enrolment type
+ $addtype = $user->{'type'.$i};
+ if ($addtype < 1 or $addtype > 3) {
+ $upt->track('enrolments', $strerror.': typeN = 1|2|3', 'error');
+ continue;
+ } else if ($addtype == 1 and empty($formdata->uulegacy1)) {
+ if (empty($ccache[$shortname]->defaultrole)) {
+ $rid = $CFG->defaultcourseroleid;
+ } else {
+ $rid = $ccache[$shortname]->defaultrole;
}
} else {
- // Record not added -- possibly some other error
- notify(get_string('erroronline', 'error', $linenum). ': ' . $strusernotaddederror . ': ' . stripslashes($user->username));
- $userserrors++;
- continue;
+ $rid = $formdata->{'uulegacy'.$addtype};
+ }
+
+ } else {
+ // no role specified, use the default
+ if (empty($ccache[$shortname]->defaultrole)) {
+ $rid = $CFG->defaultcourseroleid;
+ } else {
+ $rid = $ccache[$shortname]->defaultrole;
+ }
+ }
+ if ($rid) {
+ $a = new object();
+ $a->course = $shortname;
+ $a->role = $rolecache[$rid]->name;
+ if (role_assign($rid, $user->id, 0, $coursecontext->id)) {
+ $upt->track('enrolments', get_string('enrolledincourserole', '', $a));
+ } else {
+ $upt->track('enrolments', get_string('enrolledincoursenotrole', '', $a), 'error');
}
}
- // find course enrolments, groups and roles/types
- for($ncourses = 1; $addcourse = @$user->{'course' . $ncourses}; ++$ncourses) {
- // find course
- if(!$course = @$courses[$addcourse]) {
- notify(get_string('erroronline', 'error', $linenum). ': ' . get_string('unknowncourse', 'error', $addcourse));
+ // find group to add to
+ if (!empty($user->{'group'.$i})) {
+ // make sure user is enrolled into course before adding into groups
+ if (!has_capability('moodle/course:view', $coursecontext, $user->id, false)) {
+ $upt->track('enrolments', get_string('addedtogroupnotenrolled', '', $gname), 'error');
continue;
}
- // find role
- if ($addrole = @$user->{'role' . $ncourses}) {
- $coursecontext =& get_context_instance(CONTEXT_COURSE, $course->id);
- if (!$ok = role_assign($addrole, $user->id, 0, $coursecontext->id)) {
- echo $strindent . $strcannotassignrole . '
';
- }
- } else {
- // if no role, then find "old" enrolment type
- switch ($addtype = @$user->{'type' . $ncourses}) {
- case 2: // teacher
- $ok = add_teacher($user->id, $course->id, 1);
- break;
- case 3: // non-editing teacher
- $ok = add_teacher($user->id, $course->id, 0);
- break;
- case 1: // student
- default:
- $ok = enrol_student($user->id, $course->id);
- break;
- }
- }
- if ($ok) { // OK
- echo $strindent . get_string('enrolledincourse', '', $addcourse) . '
';
- } else {
- notify(get_string('erroronline', 'error', $linenum). ': ' . get_string('enrolledincoursenot', '', $addcourse));
- }
-
- // find group to add to
- if ($addgroup = @$user->{'group' . $ncourses}) {
- if ($gid =& groups_get_group_by_name($course->id, $addgroup)) {
- $coursecontext =& get_context_instance(CONTEXT_COURSE, $course->id);
- if (count(get_user_roles($coursecontext, $user->id))) {
- if (groups_add_member($gid, $user->id)) {
- echo $strindent . get_string('addedtogroup','',$addgroup) . '
';
- } else {
- notify(get_string('erroronline', 'error', $linenum). ': ' . get_string('addedtogroupnot','',$addgroup));
+ //build group cache
+ if (is_null($ccache[$shortname]->groups)) {
+ $ccache[$shortname]->groups = array();
+ if ($groups = get_groups($courseid)) {
+ foreach ($groups as $gid=>$group) {
+ $ccache[$shortname]->groups[$gid] = new object();
+ $ccache[$shortname]->groups[$gid]->id = $gid;
+ $ccache[$shortname]->groups[$gid]->name = $group->name;
+ if (!is_numeric($group->name)) { // only non-numeric names are supported!!!
+ $ccache[$shortname]->groups[$group->name] = new object();
+ $ccache[$shortname]->groups[$group->name]->id = $gid;
+ $ccache[$shortname]->groups[$group->name]->name = $group->name;
}
- } else {
- notify(get_string('erroronline', 'error', $linenum). ': ' . get_string('addedtogroupnotenrolled','',$addgroup));
}
- } else {
- notify(get_string('erroronline', 'error', $linenum). ': ' . get_string('groupunknown','error',$addgroup));
}
}
+ // group exists?
+ $addgroup = $user->{'group'.$i};
+ if (!array_key_exists($addgroup, $ccache[$shortname]->groups)) {
+ $upt->track('enrolments', get_string('unknowgroup', 'error', $addgroup), 'error');
+ continue;
+ }
+ $gid = $ccache[$shortname]->groups[$addgroup]->id;
+ $gname = $ccache[$shortname]->groups[$addgroup]->name;
+
+ if (groups_add_member($gid, $user->id)) {
+ $upt->track('enrolments', get_string('addedtogroup', '', $gname));
+ } else {
+ $upt->track('enrolments', get_string('addedtogroupnot', '', $gname), 'error');
+ continue;
+ }
}
}
- echo '
';
+ if ($optype != UU_UPDATE) {
+ echo get_string('userscreated', 'admin').': '.$usersnew.'
';
+ }
+ if ($optype == UU_UPDATE or $optype == UU_ADD_UPDATE) {
+ echo get_string('usersupdated', 'admin').': '.$usersupdated.'
';
+ }
+ if ($allowdeletes) {
+ echo get_string('usersdeleted', 'admin').': '.$deletes.'
';
+ echo get_string('deleteerrors', 'admin').': '.$deleteerrors.'
';
+ }
+ if ($allowrenames) {
+ echo get_string('usersrenamed', 'admin').': '.$renames.'
';
+ echo get_string('renameerrors', 'admin').': '.$renameerrors.'
';
+ }
+ if ($usersskipped) {
+ echo get_string('usersskipped', 'admin').': '.$usersskipped.'
';
+ }
+ echo get_string('errors', 'admin').': '.$userserrors.'
| '.trim($h).' | '; +echo '
|---|
| '.s($col).' | '; } echo '|
|---|---|
| ... | ';; + } + break; + } + $ci = 0; + echo '|
| '.trim(str_replace($csv_encode, $csv_delimiter, $field)).' | ';; + echo ''.s($field).' | ';; } echo '
| ... | ';; - } -} -echo '
| '.get_string('status').' | '; + echo ''.get_string('uucsvline', 'admin').' | '; + echo 'ID | '; + echo ''.get_string('username').' | '; + echo ''.get_string('firstname').' | '; + echo ''.get_string('lastname').' | '; + echo ''.get_string('email').' | '; + echo ''.get_string('password').' | '; + echo ''.get_string('authentication').' | '; + echo ''.get_string('enrolments').' | '; + echo ''.get_string('delete').' | '; + echo '
|---|---|---|---|---|---|---|---|---|---|---|
| ';
+ if (!empty($field)) {
+ echo implode(' ', $field); + } else { + echo ' '; + } + echo ' | ';
+ }
+ echo '