/** * Show the import course page. */ function WPCW_showPage_ImportExport_importUsers() { $page = new PageBuilder(true); $page->showPageHeader(__('Import Users from CSV File', 'wp_courseware'), '75%', WPCW_icon_getPageIconURL()); // Show selection menu for import/export to save pages WPCW_showPage_ImportExport_menu('import_users'); // Show form to import some XML $form = new FormBuilder('wpcw_import_users'); $form->setSubmitLabel(__('Import Users', 'wp_courseware')); // Course upload for XML file $formElem = new FormElement('import_course_csv', __('User Import CSV File', 'wp_courseware'), true); $formElem->setTypeAsUploadFile(); $form->addFormElement($formElem); if ($form->formSubmitted()) { // Do the full export if ($form->formValid()) { // Handle the importing/uploading WPCW_users_importUsersFromFile($page); } else { $page->showListOfErrors($form->getListOfErrors(), __('Unfortunately, there were some errors trying to import the XML file.', 'wp_courseware')); } } // Workout maximum upload size $max_upload = (int) ini_get('upload_max_filesize'); $max_post = (int) ini_get('post_max_size'); $memory_limit = (int) ini_get('memory_limit'); $upload_mb = min($max_upload, $max_post, $memory_limit); printf('<p class="wpcw_doc_quick">'); printf(__('You can import a CSV file of users using the form below.', 'wp_courseware') . ' ' . __('The <b>maximum upload file size</b> for your server is <b>%d MB</b>.', 'wp_courseware'), $upload_mb); printf('</p>'); echo $form->toString(); printf('<br/><br/><div class="wpcw_docs_wrapper">'); printf('<b>%s</b>', __('Some tips for importing users via a CSV file:', 'wp_courseware')); printf('<ul>'); printf('<li>' . __('If a user email address already exists, then just the courses are updated for that user.', 'wp_courseware')); printf('<li>' . __('User names are generated from the first and last name information. If a user name already exists, then a unique username is generated.', 'wp_courseware')); printf('<li>' . __('To add a user to many courses, just separate those course IDs with a comma in the <code>courses_to_add_to</code> column.', 'wp_courseware')); printf('<li>' . __('If a user is created, any courses set to be automatically assigned will be done first, and then the courses added in the <code>courses_to_add_to</code> column.', 'wp_courseware')); printf('<li>' . __('You can download an <a href="%s">example CSV file here</a>.', 'wp_courseware') . '</li>', admin_url('?wpcw_export=csv_import_user_sample')); printf('<li>' . __('The IDs for the training courses can be found on the <a href="%s">course summary page</a>.', 'wp_courseware') . '</li>', admin_url('admin.php?page=WPCW_wp_courseware')); printf('</ul>'); printf('</div>'); $page->showPageFooter(); }
/** * Convert an array of details into a form element. * @param String $fieldName The name of the form element * @param Array $fieldDetails The list of details for the form element. */ protected function createElementObject($fieldName, $fieldDetails) { // Extract fields $label = $this->formObj->getArrayValue($fieldDetails, 'label'); $type = $this->formObj->getArrayValue($fieldDetails, 'type'); // Required 'true' or true are valid $required = $this->formObj->getArrayValue($fieldDetails, 'required'); $required = $required == 'true' || $required == '1'; // Start creating form element if anything other than a break. $elem = false; if ($type != 'break') { $elem = new FormElement($fieldName, $label, $required); } // Handle specific types switch ($type) { // Text Area case 'textarea': $rows = $this->formObj->getArrayValue($fieldDetails, 'rows') + 0; if ($rows == 0) { $rows = 5; } $elem->setTypeAsTextArea($rows, 70); break; // Select/Dropdown // Select/Dropdown case 'select': $options = false; if (isset($fieldDetails['data']) && is_array($fieldDetails['data'])) { $options = $fieldDetails['data']; } $elem->setTypeAsComboBox($options); break; // Radio Buttons // Radio Buttons case 'radio': $options = false; if (isset($fieldDetails['data']) && is_array($fieldDetails['data'])) { $options = $fieldDetails['data']; } $elem->setTypeAsRadioButtons($options); break; // Checkbox // Checkbox case 'checkbox': $label = false; if (isset($fieldDetails['extralabel'])) { $label = $fieldDetails['extralabel']; } $elem->setTypeAsCheckbox($label); break; // Checkbox List // Checkbox List case 'checkboxlist': $options = false; if (isset($fieldDetails['data']) && is_array($fieldDetails['data'])) { $options = $fieldDetails['data']; } $elem->setTypeAsCheckboxList($options); break; // Merged Fields - process each sub element // Merged Fields - process each sub element case 'merge': $elementList = array(); if (!empty($fieldDetails['merge'])) { foreach ($fieldDetails['merge'] as $fieldName => $fieldDetails) { $elementList[] = $this->createElementObject($fieldName, $fieldDetails); } } $elem->setTypeAsMergedElements($elementList); break; // File upload // File upload case 'uploadfile': $elem->setTypeAsUploadFile($this->formObj->getArrayValue($fieldDetails, 'show_existing'), $this->formObj->getArrayValue($fieldDetails, 'valid_if_value')); break; // Custom HTML // Custom HTML case 'custom': $elem->setTypeAsCustom($this->formObj->getArrayValue($fieldDetails, 'html')); break; // Hidden field // Hidden field case 'hidden': $elem->setTypeAsHidden(); break; // Section break // Section break case 'break': $this->formObj->addBreak($fieldName, $this->formObj->getArrayValue($fieldDetails, 'html')); break; // Text box // Text box default: break; } // Add optional fields if ($type != 'break') { // Element description if ($desc = $this->formObj->getArrayValue($fieldDetails, 'desc')) { $elem->description = $desc; } // Extra CSS if ($cssclass = $this->formObj->getArrayValue($fieldDetails, 'cssclass')) { $elem->cssclass = $cssclass; } // Add extra HTML if provided. $extraHTML = $this->formObj->getArrayValue($fieldDetails, 'extrahtml'); if ($extraHTML) { $elem->afterFormElementHTML = $extraHTML; } // Add custom error message if there is one $elem->errorMessage = $this->formObj->getArrayValue($fieldDetails, 'errormsg'); // Validation rules if (isset($fieldDetails['validate']) && is_array($fieldDetails['validate'])) { // Is it a custom function? If so, get the function name. if ($this->formObj->getArrayValue($fieldDetails['validate'], 'type') == 'function') { $elem->validationFn = $this->formObj->getArrayValue($fieldDetails['validate'], 'fname'); $elem->errorMessage = $this->formObj->getArrayValue($fieldDetails['validate'], 'error'); } else { $elem->setValidationRules($fieldDetails['validate']); } } // See if there are any suffix items? If so, parse them. // See wiki for documentation on how to structure this. if (!empty($fieldDetails['suffix_subitems'])) { $suffixItems = array(); // suffix_subitems contains a list of 'position' => array(fieldName => fieldDetails) foreach ($fieldDetails['suffix_subitems'] as $position => $elementDetails) { $suffixItems[$position] = array(); // Need to process each field now we know the position. foreach ($elementDetails as $fieldName => $fieldDetails) { // Add the position of this item (in order), with the object for this element. $suffixItems[$position][] = $this->createElementObject($fieldName, $fieldDetails); } } // Update the form element with the subitems $elem->setSuffixItems($suffixItems); } } return $elem; }