コード例 #1
0
 /**
  * runs the actual import
  *
  * @return Ambigous AMA_Error on error |array recpArray on success
  *
  * @access public
  */
 public function runImport()
 {
     $count = 0;
     $zipFileName = ADA_UPLOAD_PATH . $this->_importFile;
     $zip = new ZipArchive();
     if ($zip->open($zipFileName)) {
         $XMLfile = $zip->getFromName(XML_EXPORT_FILENAME);
         $XMLObj = new SimpleXMLElement($XMLfile);
         $this->_progressResetValues(substr_count($XMLfile, '</nodo>') + substr_count($XMLfile, '<survey ') + substr_count($XMLfile, '</test>'));
         foreach ($XMLObj as $objName => $course) {
             // first level object must be 'modello_corso'
             if ($objName === 'modello_corso') {
                 $count++;
                 // get the attributes as local vars
                 // e.g. attributed exportedId=107 becomes
                 // a local var named $exportedId, initialized to 107 as a string
                 foreach ($course->attributes() as $name => $val) {
                     ${$name} = (string) $val;
                 }
                 // as a result of this foreach we have a php var for any XML object attribute
                 // var_dump ($exportedId); should neither raise an error nor dump a null value.
                 $this->_courseOldID = $exportedId;
                 /**
                  * sets the log file name that will be used from now on!
                  */
                 $this->_logFile = MODULES_IMPEXPORT_LOGDIR . "import-" . $this->_courseOldID . "_" . date('d-m-Y_His') . ".log";
                 $this->_logMessage('**** IMPORT STARTED at ' . date('d/m/Y H:i:s') . '(timestamp: ' . $this->_dh->date_to_ts('now') . ') ****');
                 $this->_progressSetTitle((string) $course->titolo);
                 /**
                  * ADDS THE COURSE TO THE APPROPIATE TABLES
                  */
                 if (!self::$_DEBUG) {
                     if (is_null($this->_selectedCourseID)) {
                         $courseNewID = $this->_add_course($course);
                         /**
                          * this is a new course you want, first node
                          * is going to be the root of the course (i.e. zero)
                          * Setting selectedNodeID will make the running code
                          * do the trick!
                          */
                         $this->_selectedNodeID = null;
                     } else {
                         $courseNewID = $this->_selectedCourseID;
                     }
                     if (AMA_DB::isError($courseNewID)) {
                         return $courseNewID;
                     }
                 } else {
                     $courseNewID = 123 * $count;
                 }
                 /**
                  * NOW ADD  NODES, TESTS AND SURVEYS
                  */
                 foreach ($this->_specialNodes as $groupName) {
                     $method = '_import' . ucfirst(strtolower($groupName));
                     $this->_logMessage(__METHOD__ . ' Saving ' . $groupName . ' by calling method: ' . $method);
                     if ($groupName === 'tests' || $groupName === 'surveys') {
                         // prepares the mapping array by emptying it
                         if ($groupName === 'tests') {
                             if (isset($this->_testNodeIDMapping)) {
                                 unset($this->_testNodeIDMapping);
                             }
                             $this->_testNodeIDMapping = array();
                         }
                         // prepares the test data handler
                         $this->_dh->disconnect();
                         $this->_dh = AMATestDataHandler::instance(MultiPort::getDSN($this->_selectedTester));
                     }
                     /**
                      * calls a method named _import<groupName> foreach special node.
                      * e.g. for nodes it will call _importNodi, for tests _importTests....
                      */
                     if (method_exists($this, $method) && !empty($course->{$groupName})) {
                         $specialVal = $this->{$method}($course->{$groupName}, $courseNewID);
                         // if it's an error return it right away
                         if (AMA_DB::isError($specialVal)) {
                             $this->_logMessage(__METHOD__ . ' Error saving ' . $groupName . '. DB returned the following:');
                             $this->_logMessage(print_r($specialVal, true));
                             return $specialVal;
                         } else {
                             $this->_logMessage(__METHOD__ . ' Saving ' . $groupName . ' successfully ended');
                             $this->_recapArray[$courseNewID][$groupName] = $specialVal;
                         }
                     }
                     if ($groupName === 'nodi') {
                         // save all the links and clean the array
                         $this->_saveLinks($courseNewID);
                         // after links have been saved, update inernal links pseudo html in proper nodes
                         $this->_updateInternalLinksInNodes($courseNewID);
                     } else {
                         if ($groupName === 'tests' || $groupName === 'surveys') {
                             // restores the import/export data handler
                             $this->_dh->disconnect();
                             $this->_dh = AMAImpExportDataHandler::instance(MultiPort::getDSN($this->_selectedTester));
                             if ($groupName === 'tests') {
                                 $this->_updateTestLinksInNodes($courseNewID);
                             }
                         }
                     }
                 }
                 // 					$this->_updateTestLinksInNodes ( $courseNewID );
             }
             // if ($objName === 'modello_corso')
             $this->_logMessage('**** IMPORT ENDED at ' . date('d/m/Y H:i:s') . '(timestamp: ' . $this->_dh->date_to_ts('now') . ') ****');
             $this->_logMessage('If there\'s no zip log below, this is a multi course import: pls find unzip log at the end of the last course log');
         }
         // foreach ($XMLObj as $objName=>$course)
         // extract the zip files to the appropriate media dir
         $this->_unzipToMedia($zip);
         $zip->close();
         if (!self::$_DEBUG) {
             unlink($zipFileName);
         }
     }
     $this->_progressDestroy();
     return $this->_recapArray;
 }