Skip to content

Commit

Permalink
MDL-11996 bulk user upload - improvements, bugfixes and cleanup;
Browse files Browse the repository at this point in the history
new csv import library included ;-)
  • Loading branch information
skodak committed Nov 5, 2007
1 parent c8fa018 commit e4e3854
Show file tree
Hide file tree
Showing 10 changed files with 1,348 additions and 550 deletions.
1,274 changes: 801 additions & 473 deletions admin/uploaduser.php

Large diffs are not rendered by default.

215 changes: 174 additions & 41 deletions admin/uploaduser_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@ class admin_uploaduser_form1 extends moodleform {
function definition (){
global $CFG, $USER;

$this->set_upload_manager(new upload_manager('userfile', false, false, null, false, 0, true, true, false));

$mform =& $this->_form;

$this->set_upload_manager(new upload_manager('userfile', false, false, null, false, 0, true, true, false));

$mform->addElement('header', 'settingsheader', get_string('upload'));

$mform->addElement('file', 'userfile', get_string('file'));
$mform->addElement('file', 'userfile', get_string('file'), 'size="40"');
$mform->addRule('userfile', null, 'required');

$choices = array('comma'=>',', 'semicolon'=>';', 'colon'=>':', 'tab'=>'\\t');
if (isset($CFG->CSV_DELIMITER) and !in_array($CFG->CSV_DELIMITER, $choices)) {
$choices['cfg'] = $CFG->CSV_DELIMITER;
}
$mform->addElement('select', 'separator', get_string('csvseparator', 'admin'), $choices);
$choices = csv_import_reader::get_delimiter_list();
$mform->addElement('select', 'delimiter_name', get_string('csvdelimiter', 'admin'), $choices);
if (array_key_exists('cfg', $choices)) {
$mform->setDefault('separator', 'cfg');
$mform->setDefault('delimiter_name', 'cfg');
} else if (get_string('listsep') == ';') {
$mform->setDefault('separator', 'semicolon');
$mform->setDefault('delimiter_name', 'semicolon');
} else {
$mform->setDefault('separator', 'comma');
$mform->setDefault('delimiter_name', 'comma');
}

$textlib = textlib_get_instance();
Expand All @@ -44,35 +41,103 @@ class admin_uploaduser_form2 extends moodleform {
function definition (){
global $CFG, $USER;

$mform =& $this->_form;
//no editors here - we need proper empty fields
$CFG->htmleditor = null;

$mform =& $this->_form;
$columns =& $this->_customdata;

// I am the tamplate user
// I am the template user, why should it be the administrator? we have roles now, other ppl may use this script ;-)
$templateuser = $USER;

// upload settings and file
$mform->addElement('header', 'settingsheader', get_string('settings'));

$choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth'));
$mform->addElement('select', 'createpassword', get_string('passwordhandling', 'auth'), $choices);
$choices = array(UU_ADDNEW => get_string('uuoptype_addnew', 'admin'),
UU_ADDINC => get_string('uuoptype_addinc', 'admin'),
UU_ADD_UPDATE => get_string('uuoptype_addupdate', 'admin'),
UU_UPDATE => get_string('uuoptype_update', 'admin'));
$mform->addElement('select', 'uutype', get_string('uuoptype', 'admin'), $choices);

$mform->addElement('selectyesno', 'updateaccounts', get_string('updateaccounts', 'admin'));
$mform->addElement('selectyesno', 'allowrenames', get_string('allowrenames', 'admin'));
$choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth'));
$mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'admin'), $choices);
$mform->disabledIf('uupasswordnew', 'uutype', 'eq', UU_UPDATE);

$choices = array(0 => get_string('nochanges', 'admin'),
1 => get_string('uuupdatefromfile', 'admin'),
2 => get_string('uuupdateall', 'admin'),
3 => get_string('uuupdatemissing', 'admin'));
$mform->addElement('select', 'uuupdatetype', get_string('uuupdatetype', 'admin'), $choices);
$mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_ADDNEW);
$mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_ADDINC);

$choices = array(0 => get_string('nochanges', 'admin'), 1 => get_string('update'));
$mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'admin'), $choices);
$mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_ADDNEW);
$mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_ADDINC);
$mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 0);
$mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 3);

$mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'admin'));
$mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_ADDNEW);
$mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_ADDINC);

$mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'admin'));
$mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_ADDNEW);
$mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_ADDINC);

$choices = array(0 => get_string('no'),
1 => get_string('uubulknew', 'admin'),
2 => get_string('uubulkupdated', 'admin'),
3 => get_string('uubulkall', 'admin'));
$mform->addElement('select', 'uubulk', get_string('uubulk', 'admin'), $choices);

// roles selection
$showroles = false;
foreach ($columns as $column) {
if (preg_match('/^type\d+$/', $column)) {
$showroles = true;
break;
}
}
if ($showroles) {
$mform->addElement('header', 'rolesheader', get_string('roles'));

$choices = uu_allowed_roles(true);

$choices[0] = get_string('uucoursedefaultrole', 'admin');
$mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'admin'), $choices);
$mform->setDefault('uulegacy1', 0);
unset($choices[0]);

$mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'admin'), $choices);
if ($editteacherroles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW)) {
$editteacherrole = array_shift($editteacherroles); /// Take the first one
$mform->setDefault('uulegacy2', $editteacherrole->id);
unset($editteacherroles);
} else {
$mform->setDefault('uulegacy2', $CFG->defaultcourseroleid);
}

$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
$mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'admin'), $choices);
if ($teacherroles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW)) {
$teacherrole = array_shift($teacherroles); /// Take the first one
$mform->setDefault('uulegacy3', $teacherrole->id);
unset($teacherroles);
} else {
$mform->setDefault('uulegacy3', $CFG->defaultcourseroleid);
}
}

// default values
$mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'admin'));

$mform->addElement('text', 'username', get_string('username'), 'size="20"');
$mform->addRule('username', get_string('requiredtemplate', 'admin'), 'required', null, 'client');

// only enabled plugins
$aplugins = get_enabled_auth_plugins();
$auth_options = array();
foreach ($aplugins as $module) {
$auth_options[$module] = get_string('auth_'.$module.'title', 'auth');
}
$mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
// only enabled and known to work plugins
$choices = uu_allowed_auths();
$mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices);
$mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default
$mform->setHelpButton('auth', array('authchange', get_string('chooseauthmethod','auth')));
$mform->setAdvanced('auth');
Expand Down Expand Up @@ -162,31 +227,99 @@ function definition (){
$mform->setType('address', PARAM_MULTILANG);
$mform->setAdvanced('address');

// hidden fields
$mform->addElement('hidden', 'uplid');
$mform->setType('uplid', PARAM_FILE);
/// Next the profile defaults
profile_definition($mform);

$mform->addElement('hidden', 'separator');
$mform->setType('separator', PARAM_ALPHA);
// hidden fields
$mform->addElement('hidden', 'iid');
$mform->setType('iid', PARAM_INT);

$mform->addElement('hidden', 'previewrows');
$mform->setType('previewrows', PARAM_ALPHA);
$mform->setType('previewrows', PARAM_INT);

$mform->addElement('hidden', 'readcount');
$mform->setType('readcount', PARAM_INT);

$this->add_action_buttons(true, get_string('uploadusers'));
}

/**
* Form tweaks that depend on current data.
*/
function definition_after_data() {
$mform =& $this->_form;
$mform =& $this->_form;
$columns =& $this->_customdata;

foreach ($columns as $column) {
if ($mform->elementExists($column)) {
$mform->removeElement($column);
}
}
}

/**
* Server side validation.
*/
function validation($data) {
$errors = array();
$columns =& $this->_customdata;
$optype = $data['uutype'];

// detect if password column needed in file
if (!in_array('password', $columns)) {
switch ($optype) {
case UU_UPDATE:
if (!empty($data['uupasswordold'])) {
$errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
}
break;

case UU_ADD_UPDATE:
if (empty($data['uupasswordnew'])) {
$errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
}
if (!empty($data['uupasswordold'])) {
$errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
}
break;

case UU_ADDNEW:
case UU_ADDINC:
if (empty($data['uupasswordnew'])) {
$errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
}
break;
}
}

$separator = $mform->getElementValue('separator');
$uplid = $mform->getElementValue('uplid');

if ($headers = get_uf_headers($uplid, $separator)) {
foreach ($headers as $header) {
if ($mform->elementExists($header)) {
$mform->removeElement($header);
// look for other required data
if ($optype != UU_UPDATE) {
if (!in_array('firstname', $columns)) {
$errors['uutype'] = get_string('missingfield', 'error', 'firstname');
}

if (!in_array('lastname', $columns)) {
if (isset($errors['uutype'])) {
$errors['uutype'] = '';
} else {
$errors['uutype'] = ' ';
}
$errors['uutype'] .= get_string('missingfield', 'error', 'lastname');
}

if (!in_array('email', $columns) and empty($data['email'])) {
$errors['email'] = get_string('requiredtemplate', 'admin');
}

if (!in_array('city', $columns) and empty($data['city'])) {
$errors['city'] = get_string('required');
}
}

if (0 == count($errors)){
return true;
} else {
return $errors;
}
}
}
Expand Down
34 changes: 29 additions & 5 deletions lang/en_utf8/admin.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
$string['accessdenied'] = 'Access denied';
$string['accounts'] = 'Accounts';
$string['addcounter'] = 'Append counter';
$string['adminseesall'] = 'Admins See All';
$string['adminseesallevents'] = 'Administrators see all events';
$string['adminseesownevents'] = 'Administrators are just like other users';
$string['allowcategorythemes'] = 'Allow category themes';
$string['allowcoursethemes'] = 'Allow course themes';
$string['allowdeletes'] = 'Allow deletes';
$string['allowemailaddresses'] = 'Allowed email domains';
$string['allowobjectembed'] = 'Allow EMBED and OBJECT tags';
$string['allowrenames'] = 'Allow renames';
Expand Down Expand Up @@ -231,7 +231,7 @@
$string['cronerrorpassword'] = 'Sorry, you have not provided a valid password to access this page';
$string['cronremotepassword'] = 'Cron password for remote access';
$string['cronwarning'] = 'The <a href=\"cron.php\">cron.php maintenance script</a> has not been run for at least 24 hours.';
$string['csvseparator'] = 'CSV separator';
$string['csvdelimiter'] = 'CSV delimiter';
$string['curlrecommended'] = 'Installing the optional Curl library is highly recommended in order to enable Moodle Networking functionality.';
$string['customcheck'] = 'Other Checks';
$string['datarootsecuritywarning'] = 'Your site configuration might not be secure. Please make sure that your dataroot directory ($a) is not directly accessible via web.';
Expand Down Expand Up @@ -434,8 +434,8 @@
$string['mymoodleredirect'] = 'Force users to use My Moodle';
$string['mysql416bypassed'] = 'However, if your site is using iso-8859-1 (latin) languages ONLY, you may continue using your currently installed MySQL 4.1.12 (or higher).';
$string['mysql416required'] = 'MySQL 4.1.16 is the minimum version required for Moodle 1.6 in order to guarantee that all data can be converted to UTF-8 in the future.';
$string['newusernamehandling'] = 'New username duplicate handling';
$string['nobookmarksforuser'] = 'You do not have any bookmarks.';
$string['nochanges'] = 'No changes';
$string['nodefaultuserrolelists'] = 'Don\'t return all default role users';
$string['nolangupdateneeded'] = 'All your language packs are up to date, no update is needed';
$string['nomissingstrings'] = 'No missing strings';
Expand Down Expand Up @@ -536,6 +536,7 @@
$string['releasenoteslink'] = 'For information about this version of Moodle, please see the online <a target=\"_blank\" href=\"$a\">Release Notes</a>';
$string['remotelangnotavailable'] = 'Because Moodle can not connect to download.moodle.org, we are unable to do language pack installation automatically. Please download the appropriate zip file(s) from the list below, copy them to your $a directory and unzip them manually.';
$string['renameerrors'] = 'Rename errors';
$string['requiredtemplate'] = 'Required. You may use template syntax here (%%l = lastname, %%f = firstname, %%u = username). See help for details and examples.';
$string['restrictbydefault'] = 'Restrict modules by default';
$string['restrictmodulesfor'] = 'Restrict modules for';
$string['riskconfig'] = 'Users could change site configuration and behaviour';
Expand Down Expand Up @@ -576,7 +577,6 @@
$string['sitepolicies'] = 'Site policies';
$string['sitepolicy'] = 'Site policy URL';
$string['sitesectionhelp'] = 'If selected, a topic section will be displayed on the site\'s front page.';
$string['skipuser'] = 'Skip user';
$string['slasharguments'] = 'Use slash arguments';
$string['smartpix'] ='Smart pix search';
$string['smtphosts'] = 'SMTP hosts';
Expand Down Expand Up @@ -633,6 +633,8 @@
Are you sure you want to upgrade this server to this version?';
$string['upgradingdata'] = 'Upgrading data';
$string['upgradinglogs'] = 'Upgrading logs';
$string['uploaduserspreview'] = 'Upload users preview';
$string['uploadusersresult'] = 'Upload users results';
$string['upwards'] = 'upwards';
$string['usehtmleditor'] = 'Use HTML editor';
$string['useraccountupdated'] = 'User updated';
Expand All @@ -643,10 +645,32 @@
$string['userrenamed'] = 'User renamed';
$string['users'] = 'Users';
$string['userscreated'] = 'Users created';
$string['usersrenamed'] = 'Users renamed';
$string['usersdeleted'] = 'Users deleted';
$string['usersrenamed'] = 'Users renamed';
$string['usersskipped'] = 'Users skipped';
$string['usersupdated'] = 'Users updated';
$string['usetags'] = 'Enable tags functionality';
$string['uubulk'] = 'Select for bulk operations';
$string['uubulkall'] = 'All users';
$string['uubulknew'] = 'New users';
$string['uubulkupdated'] = 'Updated users';
$string['uucsvline'] = 'CSV line';
$string['uucoursedefaultrole'] = 'Default course role';
$string['uulegacy1role'] = '(Original Student) typeN=1';
$string['uulegacy2role'] = '(Original Teacher) typeN=2';
$string['uulegacy3role'] = '(Original Non-editing teacher) typeN=3';
$string['uuoptype_addinc'] = 'Add all, append counter to usernames if needed';
$string['uuoptype_addnew'] = 'Add new only, skip existing users';
$string['uuoptype_addupdate'] = 'Add new and update existing users';
$string['uuoptype_update'] = 'Update existing users only';
$string['uuoptype'] = 'Upload type';
$string['uupasswordnew'] = 'New user password';
$string['uupasswordold'] = 'Existing user password';
$string['uupreprocessedcount'] = 'Number of preprocessed records: $a';
$string['uuupdateall'] = 'Override with file and defaults';
$string['uuupdatefromfile'] = 'Override with file';
$string['uuupdatemissing'] = 'Fill in missing from file and defaults';
$string['uuupdatetype'] = 'Existing user details';
$string['validateerror'] = 'This value was not valid:';
$string['xmlstrictheaders'] = 'XML strict headers';

Expand Down
Loading

0 comments on commit e4e3854

Please sign in to comment.