From ed22a01b3281f4f358ec7abe879c622007cf49f0 Mon Sep 17 00:00:00 2001 From: skodak Date: Thu, 1 Nov 2007 19:37:25 +0000 Subject: [PATCH] MDL-11996 bulk user upload - improvements, fixes and cleanup - part1 --- admin/uploaduser.php | 93 ++++++++++++++++++----------- admin/uploaduser_form.php | 122 +++++++++++++++++++------------------- lang/en_utf8/admin.php | 1 + 3 files changed, 123 insertions(+), 93 deletions(-) diff --git a/admin/uploaduser.php b/admin/uploaduser.php index b9f2cd0531..2cc4e6fbc8 100755 --- a/admin/uploaduser.php +++ b/admin/uploaduser.php @@ -57,8 +57,8 @@ if ( $formdata = $mform->get_data() ) { $updateaccounts = $formdata->updateaccounts; $allowrenames = $formdata->allowrenames; $skipduplicates = $formdata->duplicatehandling; - - // make arrays of valid fields for error checking + + // 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, @@ -72,9 +72,10 @@ if ( $formdata = $mform->get_data() ) { 'timezone' => 1, 'mailformat' => 1, 'maildisplay' => 1, - 'htmleditor' => 1, + 'htmleditor' => 0, + 'ajax' => 0, 'autosubscribe' => 1, - 'mnethostid' => 0, + 'mnethostid' => 0, 'institution' => 0, 'department' => 0, 'idnumber' => 0, @@ -91,20 +92,38 @@ if ( $formdata = $mform->get_data() ) { 'password' => !$createpassword, ); - $fp = fopen($mform->get_userfile_name(), 'r'); - $linenum = 1; // since header is line 1 - // get header (field names) and remove Unicode BOM from first line, if any - $line = explode($csv_delimiter, $textlib->trim_utf8_bom(fgets($fp,LINE_MAX_SIZE))); - // check for valid field names + $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); + + // find header row $headers = array(); - foreach ($line as $key => $value) { - $value = trim($value); // remove whitespace - if (!in_array($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), 'uploaduser.php?sesskey='.$USER->sesskey); + $linenum = 0; + $line = strtok($text, "\n"); + while ($line !== false) { + $linenum++; + $line = trim($line); + if ($line == '') { + //ignore empty lines + $line = strtok("\n"); + continue; } - $headers[$key] = $value; + $line = explode($csv_delimiter, $line); + // check for valid field names + foreach ($line as $key => $value) { + $value = trim($value); // remove whitespace + if (!in_array($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), 'uploaduser.php?sesskey='.$USER->sesskey); + } + $headers[$key] = $value; + } + $line = false; // found header line } // check that required fields are present or a default value for them exists @@ -112,13 +131,13 @@ if ( $formdata = $mform->get_data() ) { // 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==='')) { + if($required && !in_array($key, $headers) && (!isset($formdata->$key) || $formdata->$key==='')) { notify(get_string('missingfield', 'error', $key)); $headersOk = false; } } } - if($headersOk) { + if ($headersOk) { $usersnew = 0; $usersupdated = 0; $userserrors = 0; @@ -136,13 +155,20 @@ if ( $formdata = $mform->get_data() ) { unset($tmp); echo '

'; - while (!feof ($fp)) { + $line = strtok("\n"); + while ($line !== false) { + $linenum++; + $line = trim($line); + if ($line == '') { + //empty line?? + $line = strtok("\n"); + continue; + } + $line = explode($csv_delimiter, $line); $errors = ''; $user = new object(); // by default, use the local mnet id (this may be changed in the file) $user->mnethostid = $CFG->mnet_localhost_id; - $line = explode($csv_delimiter, fgets($fp,LINE_MAX_SIZE)); - ++$linenum; // add fields to user object foreach ($line as $key => $value) { if($value !== '') { @@ -164,7 +190,7 @@ if ( $formdata = $mform->get_data() ) { } } } - + // add default values for remaining fields foreach ($fields as $key => $required) { if(isset($user->$key)) { @@ -172,7 +198,7 @@ if ( $formdata = $mform->get_data() ) { } 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')) { + if($required && (empty($user->deleted) || $key == 'username')) { $errors .= get_string('missingfield', 'error', $key) . ' '; } continue; @@ -192,7 +218,7 @@ if ( $formdata = $mform->get_data() ) { $len = $len * 10 + (int)$car; } else if($car == '-') { $case = 1; - } else if($car == '+') { + } else if($car == '+') { $case = 2; } else if($car == 'f') { // first name $info = @$user->firstname; @@ -211,7 +237,7 @@ if ( $formdata = $mform->get_data() ) { } $i = $j - 1; // change case - if($case == 1) { + if($case == 1) { $info = $textlib->strtolower($info); } else if($case == 2) { $info = $textlib->strtoupper($info); @@ -224,7 +250,7 @@ if ( $formdata = $mform->get_data() ) { $value .= $template[$i]; } } - + if($key == 'username') { $value = $textlib->strtolower($value); if(empty($CFG->extendedusernamechars)) { @@ -249,7 +275,7 @@ if ( $formdata = $mform->get_data() ) { continue; } - // delete user + // delete user if(@$user->deleted) { $info = ': ' . stripslashes($user->username) . '. '; if($user =& get_record('user', 'username', $user->username, 'mnethostid', $user->mnethostid)) { @@ -271,10 +297,10 @@ if ( $formdata = $mform->get_data() ) { } else { notify(get_string('erroronline', 'error', $linenum). ': ' . $strusernotdeletedmissing . $info); ++$deleteerrors; - } + } continue; } - + // save the user to the database $user->confirmed = 1; $user->timemodified = time(); @@ -327,8 +353,8 @@ if ( $formdata = $mform->get_data() ) { $usersnew++; if (empty($user->password) && $createpassword) { // passwords will be created and sent out on cron - insert_record('user_preferences', array( 'userid' => $user->id, 'name' => 'create_password', 'value' => 1)); - insert_record('user_preferences', array( 'userid' => $user->id, 'name' => 'auth_forcepasswordchange', 'value' => 1)); + set_user_preference('create_password', 1, $user->id); + set_user_preference('auth_forcepasswordchange', 1, $user->id); } } else { // Record not added -- possibly some other error @@ -361,7 +387,7 @@ if ( $formdata = $mform->get_data() ) { $ok = add_teacher($user->id, $course->id, 0); break; case 1: // student - default: + default: $ok = enrol_student($user->id, $course->id); break; } @@ -390,6 +416,8 @@ if ( $formdata = $mform->get_data() ) { } } } + //read next line + $line = strtok("\n"); } echo '

'; notify(get_string('userscreated', 'admin') . ': ' . $usersnew); @@ -402,7 +430,6 @@ if ( $formdata = $mform->get_data() ) { } notify(get_string('errors', 'admin') . ': ' . $userserrors); } - fclose($fp); echo '
'; } diff --git a/admin/uploaduser_form.php b/admin/uploaduser_form.php index 13d7d95229..6edfdbc6a0 100644 --- a/admin/uploaduser_form.php +++ b/admin/uploaduser_form.php @@ -3,89 +3,113 @@ require_once $CFG->libdir.'/formslib.php'; class admin_uploaduser_form extends moodleform { function definition (){ - global $CFG; - $templateuser = $this->_customdata; - if(empty($templateuser)) { - if (!$templateuser = get_admin()) { - error('Could not find site admin'); - } - } - + global $CFG, $USER; + $mform =& $this->_form; + // I am the tamplate user + $templateuser = $USER; + +// upload settings and file + $mform->addElement('header', 'settingsheader', get_string('settings')); + $mform->addElement('file', 'userfile', get_string('file')); $mform->addRule('userfile', null, 'required'); + $textlib = textlib_get_instance(); + $choices = $textlib->get_encodings(); + $mform->addElement('select', 'encoding', get_string('encoding', 'admin'), $choices); + $mform->setDefault('encoding', 'UTF-8'); + + $choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth')); + $mform->addElement('select', 'createpassword', get_string('passwordhandling', 'auth'), $choices); + + $mform->addElement('selectyesno', 'updateaccounts', get_string('updateaccounts', 'admin')); + $mform->addElement('selectyesno', 'allowrenames', get_string('allowrenames', 'admin')); + + $choices = array(0 => get_string('addcounter', 'admin'), 1 => get_string('skipuser', 'admin')); + $mform->addElement('select', 'duplicatehandling', get_string('newusernamehandling', 'admin'), $choices); + $mform->setDefault('duplicatehandling', 1); // better skip, bc and safer + +// default values $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'admin')); $mform->addElement('text', 'username', get_string('username'), 'size="20"'); - $modules = get_list_of_plugins('auth'); + // only enabled plugins + $aplugins = get_enabled_auth_plugins(); $auth_options = array(); - foreach ($modules as $module) { - $auth_options[$module] = get_string("auth_$module"."title", "auth"); + foreach ($aplugins as $module) { + $auth_options[$module] = get_string('auth_'.$module.'title', 'auth'); } $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options); - $mform->setDefault('auth', $templateuser->auth); + $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default $mform->setHelpButton('auth', array('authchange', get_string('chooseauthmethod','auth'))); + $mform->setAdvanced('auth'); $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"'); - $choices = array( - get_string('emaildisplayno'), - get_string('emaildisplayyes'), - get_string('emaildisplaycourse'), - ); + + $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse')); $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices); $mform->setDefault('maildisplay', 2); - $choices = array( - get_string('emailenable'), - get_string('emaildisable'), - ); + $choices = array(0 => get_string('emailenable'), 1 => get_string('emaildisable')); $mform->addElement('select', 'emailstop', get_string('emailactive'), $choices); - $choices = array( - get_string('textformat'), - get_string('htmlformat'), - ); + $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat')); $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices); $mform->setDefault('mailformat', 1); + $mform->setAdvanced('mailformat'); - $choices = array( - get_string('autosubscribeyes'), - get_string('autosubscribeno'), - ); + $choices = array(0 => get_string('autosubscribeyes'), 1 => get_string('autosubscribeno')); $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices); $mform->setDefault('autosubscribe', 1); if ($CFG->htmleditor) { - $choices = array( - get_string('texteditor'), - get_string('htmleditor'), - ); + $choices = array(0 => get_string('texteditor'), 1 => get_string('htmleditor')); $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices); $mform->setDefault('htmleditor', 1); + } else { + $mform->addElement('static', 'htmleditor', get_string('textediting'), get_string('texteditor')); } + $mform->setAdvanced('htmleditor'); + + if (empty($CFG->enableajax)) { + $mform->addElement('static', 'ajax', get_string('ajaxuse'), get_string('ajaxno')); + } else { + $choices = array( 0 => get_string('ajaxno'), 1 => get_string('ajaxyes')); + $mform->addElement('select', 'ajax', get_string('ajaxuse'), $choices); + $mform->setDefault('ajax', 1); + } + $mform->setAdvanced('ajax'); $mform->addElement('text', 'city', get_string('city'), 'maxlength="100" size="25"'); $mform->setType('city', PARAM_MULTILANG); $mform->setDefault('city', $templateuser->city); - + $mform->addElement('select', 'country', get_string('selectacountry'), get_list_of_countries()); $mform->setDefault('country', $templateuser->country); + $mform->setAdvanced('country'); $choices = get_list_of_timezones(); $choices['99'] = get_string('serverlocaltime'); $mform->addElement('select', 'timezone', get_string('timezone'), $choices); $mform->setDefault('timezone', $templateuser->timezone); + $mform->setAdvanced('timezone'); $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_list_of_languages()); $mform->setDefault('lang', $templateuser->lang); - + $mform->setAdvanced('lang'); + $mform->addElement('htmleditor', 'description', get_string('userdescription')); $mform->setType('description', PARAM_CLEAN); $mform->setHelpButton('description', array('text', get_string('helptext'))); + $mform->setAdvanced('description'); $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"'); + $mform->setAdvanced('url'); + + $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="64" size="25"'); + $mform->setType('idnumber', PARAM_CLEAN); $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"'); $mform->setType('institution', PARAM_MULTILANG); @@ -97,40 +121,18 @@ class admin_uploaduser_form extends moodleform { $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"'); $mform->setType('phone1', PARAM_CLEAN); + $mform->setAdvanced('phone1'); $mform->addElement('text', 'phone2', get_string('phone'), 'maxlength="20" size="25"'); $mform->setType('phone2', PARAM_CLEAN); + $mform->setAdvanced('phone2'); $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"'); $mform->setType('address', PARAM_MULTILANG); - - $mform->addElement('header', 'settingsheader', get_string('settings')); - - $choices = array( - get_string('infilefield', 'auth'), - get_string('createpasswordifneeded', 'auth'), - ); - $mform->addElement('select', 'createpassword', get_string('passwordhandling', 'auth'), $choices); - - $mform->addElement('selectyesno', 'updateaccounts', get_string('updateaccounts', 'admin')); - $mform->addElement('selectyesno', 'allowrenames', get_string('allowrenames', 'admin')); - - $choices = array( - get_string('addcounter', 'admin'), - get_string('skipuser', 'admin'), - ); - $mform->addElement('select', 'duplicatehandling', get_string('newusernamehandling', 'admin'), $choices); + $mform->setAdvanced('address'); $this->add_action_buttons(false, get_string('uploadusers')); } - function get_userfile_name(){ - if ($this->is_submitted() and $this->is_validated()) { - // return the temporary filename to process - return $this->_upload_manager->files['userfile']['tmp_name']; - }else{ - return NULL; - } - } } ?> diff --git a/lang/en_utf8/admin.php b/lang/en_utf8/admin.php index 78d4311ec3..f2834b780d 100644 --- a/lang/en_utf8/admin.php +++ b/lang/en_utf8/admin.php @@ -291,6 +291,7 @@ $string['enablerecordcache'] = 'Enable Record Cache'; $string['enablerssfeeds'] = 'Enable RSS feeds'; $string['enablestats'] = 'Enable statistics'; $string['enabletrusttext'] = 'Enable Trusted Content'; +$string['encoding'] = 'Encoding'; $string['environment'] = 'Environment'; $string['environmenterrortodo'] = 'You must solve all the environmental problems (errors) found above before proceeding to install this Moodle version!'; $string['environmenterrorupgrade'] = 'Warning: you should solve all the environmental problems (errors) found above before proceeding to upgrade this Moodle version! Upgrading without fixing these requirements could cause problems such as data loss. Are you sure you want to continue with the upgrade?'; -- 2.39.5