예제 #1
0
 /**
  * Import a course using the specified XML filename. Returns no errors if it worked correctly.
  * 
  * @param String $xmlFileName The name of the file to import.
  * @return Array An array containing 'errors' with a list of errors, and 'course_id' of the newly created course ID.
  */
 public static function importTrainingCourseFromXML($xmlFileName)
 {
     $errorList = array();
     libxml_use_internal_errors(true);
     $xml = simplexml_load_file($xmlFileName);
     // Replaced with actual ID of newly created course.
     $newCourseID = false;
     // Error loading XML file, store errors and return them.
     if (!$xml) {
         foreach (libxml_get_errors() as $error) {
             $errorList[] = sprintf(__('Line %d, Column %d, Error: %s', 'wp_courseware'), $error->line, $error->column, $error->message);
         }
     } else {
         $import = new WPCW_Import($xml);
         // At some point, might pass back errors from here.
         $errorList = $import->importCourseIntoDatabase();
         if (!$errorList) {
             $newCourseID = $import->getNewCourseID();
         }
     }
     // Return false if no errors, for easier error checking
     if (count($errorList) == 0) {
         $errorList = false;
     }
     // Return detials back to code for processing
     return array('errors' => $errorList, 'course_id' => $newCourseID);
 }
/**
 * Handles the upload and import of the course file.
 * @param Object $page The page object to show messages.
 */
function WPCW_courses_importCourseFromFile($page)
{
    if (isset($_FILES['import_course_xml']['name'])) {
        // See what type of file we're tring to upload
        $type = strtolower($_FILES['import_course_xml']['type']);
        $fileTypes = array('text/xml', 'application/xml');
        if (!in_array($type, $fileTypes)) {
            $page->showMessage(__('Unfortunately, you tried to upload a file that isn\'t XML.', 'wp_courseware'), true);
            return false;
        }
        // Filetype is fine, carry on
        $errornum = $_FILES['import_course_xml']['error'] + 0;
        $tempfile = $_FILES['import_course_xml']['tmp_name'];
        // File uploaded successfully?
        if ($errornum == 0) {
            // Try the import, return error/success here
            $importResults = WPCW_Import::importTrainingCourseFromXML($tempfile);
            if ($importResults['errors']) {
                $page->showListOfErrors($importResults['errors'], __('Unfortunately, there were some errors trying to import the XML file.', 'wp_courseware'));
            } else {
                $message = __('The course was successfully imported.', 'wp_courseware') . '<br/><br/>';
                $message .= sprintf(__('You can now <a href="%s">edit the Course Settings</a> or <a href="%s">edit the Unit &amp; Module Ordering</a>.', 'wp_courseware'), admin_url('admin.php?page=WPCW_showPage_ModifyCourse&course_id=' . $importResults['course_id']), admin_url('admin.php?page=WPCW_showPage_CourseOrdering&course_id=' . $importResults['course_id']));
                $page->showMessage($message);
            }
        } else {
            switch ($errornum) {
                case UPLOAD_ERR_FORM_SIZE:
                case UPLOAD_ERR_INI_SIZE:
                    $page->showMessage(__("Unfortunately the file you've uploaded is too large for the system.", 'wp_courseware'), true);
                    break;
                case UPLOAD_ERR_PARTIAL:
                case UPLOAD_ERR_NO_FILE:
                    $page->showMessage(__("For some reason, the file you've uploaded didn't transfer correctly to the server. Please try again.", 'wp_courseware'), true);
                    break;
                case UPLOAD_ERR_NO_TMP_DIR:
                case UPLOAD_ERR_CANT_WRITE:
                    $page->showMessage(__("There appears to be an issue with your server, as the import file could not be stored in the temporary directory.", 'wp_courseware'), true);
                    break;
                case UPLOAD_ERR_EXTENSION:
                    $page->showMessage(__('Unfortunately, you tried to upload a file that isn\'t XML.', 'wp_courseware'), true);
                    break;
            }
        }
    }
}