From 0a5dffccd707a2ec68ba7a4ac942cf3511aa41ff Mon Sep 17 00:00:00 2001 From: poltawski Date: Wed, 2 May 2007 09:33:56 +0000 Subject: [PATCH] MDL-9627 - moving uploaduser to formslib --- admin/uploaduser.php | 67 ++++++++++----------------------------- admin/uploaduser_form.php | 34 ++++++++++++++++++++ 2 files changed, 51 insertions(+), 50 deletions(-) create mode 100644 admin/uploaduser_form.php diff --git a/admin/uploaduser.php b/admin/uploaduser.php index 73b7b6d818..5b67a79348 100755 --- a/admin/uploaduser.php +++ b/admin/uploaduser.php @@ -4,15 +4,11 @@ /// Returns list of users with their user ids require_once('../config.php'); -require_once($CFG->libdir.'/uploadlib.php'); require_once($CFG->libdir.'/adminlib.php'); +require_once('uploaduser_form.php'); admin_externalpage_setup('uploadusers'); -$createpassword = optional_param('createpassword', 0, PARAM_BOOL); -$updateaccounts = optional_param('updateaccounts', 0, PARAM_BOOL); -$allowrenames = optional_param('allowrenames', 0, PARAM_BOOL); - require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM)); if (! $site = get_site()) { @@ -23,12 +19,8 @@ if (!$adminuser = get_admin()) { error("Could not find site admin"); } -$strfile = get_string('file'); $struser = get_string('user'); $strusersnew = get_string('usersnew'); -$strusersupdated = get_string('usersupdated'); -$struploadusers = get_string('uploadusers'); -$straddnewuser = get_string('importuser'); $csv_encode = '/\&\#44/'; if (isset($CFG->CSV_DELIMITER)) { @@ -47,15 +39,18 @@ if (isset($CFG->CSV_DELIMITER)) { admin_externalpage_print_header(); +$mform = new admin_uploaduser_form(); /// If a file has been uploaded, then process it +if ( $formdata = $mform->get_data() ) { -$um = new upload_manager('userfile',false,false,null,false,0); - -if ($um->preprocess_files() && confirm_sesskey()) { - $filename = $um->files['userfile']['tmp_name']; + $createpassword = $formdata->createpassword; + $updateaccounts = $formdata->updateaccounts; + $allowrenames = $formdata->allowrenames; + $filename = $mform->get_userfile_name(); + // Large files are likely to take their time and memory. Let PHP know // that we'll take longer, and that the process should be recycled soon // to free up memory. @@ -274,12 +269,12 @@ if ($um->preprocess_files() && confirm_sesskey()) { $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)); + 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)); } } else { // Record not added -- possibly some other error @@ -369,36 +364,9 @@ if ($um->preprocess_files() && confirm_sesskey()) { } /// Print the form -print_heading_with_help($struploadusers, 'uploadusers'); - -$noyesoptions = array( get_string('no'), get_string('yes') ); - -$maxuploadsize = get_max_upload_file_size(); -echo '
'; -echo '
'. -$strfile.' '. -''. -''; -print_heading(get_string('settings')); -echo ''; -echo ''; - -echo ''; - -echo ''; -echo '
' . get_string('passwordhandling', 'auth') . ''; -$passwordopts = array( 0 => get_string('infilefield', 'auth'), - 1 => get_string('createpasswordifneeded', 'auth'), - ); -choose_from_menu($passwordopts, 'createpassword', $createpassword); -echo '
' . get_string('updateaccounts', 'admin') . ''; -choose_from_menu($noyesoptions, 'updateaccounts', $updateaccounts); -echo '
' . get_string('allowrenames', 'admin') . ''; -choose_from_menu($noyesoptions, 'allowrenames', $allowrenames); -echo '

'; -echo ''; -echo '

'; -echo '
'; +print_heading_with_help(get_string('uploadusers'), 'uploadusers'); + +$mform->display(); admin_externalpage_print_footer(); @@ -419,4 +387,3 @@ function my_file_get_contents($filename, $use_include_path = 0) { } ?> - diff --git a/admin/uploaduser_form.php b/admin/uploaduser_form.php new file mode 100644 index 0000000000..2778c5a5d0 --- /dev/null +++ b/admin/uploaduser_form.php @@ -0,0 +1,34 @@ +libdir.'/formslib.php'; + +class admin_uploaduser_form extends moodleform { + function definition (){ + $mform =& $this->_form; + + $mform->addElement('file', 'userfile', get_string('file')); + $mform->addRule('userfile', null, 'required'); + + $mform->addElement('header', 'settingsheader', get_string('settings')); + + $passwordopts = array( 0 => get_string('infilefield', 'auth'), + 1 => get_string('createpasswordifneeded', 'auth'), + ); + + $mform->addElement('select', 'createpassword', get_string('passwordhandling', 'auth'), $passwordopts); + + $mform->addElement('selectyesno', 'updateaccounts', get_string('updateaccounts', 'admin')); + $mform->addElement('selectyesno', 'allowrenames', get_string('allowrenames', 'admin')); + + $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; + } + } +} +?> -- 2.39.5