コード例 #1
0
 /**
  * Recursive method saving a node in the DB and then recurring over all of its children
  *
  * @param SimpleXMLElement $xml the element from which the recursion starts (i.e. root node)
  * @param int $courseNewID the generated ID of the imported course
  *
  * @return boolean on debug |AMA_Error on error |int number of imported nodes on success
  *
  * @access private
  */
 private function _importNodi($xml, $courseNewID)
 {
     static $savedCourseID = 0;
     static $count = 0;
     static $depth = 0;
     /**
      * needed to count how many nodes were imported
      * in each disctinct course
      */
     if ($savedCourseID != $courseNewID) {
         $savedCourseID = $courseNewID;
         $count = 0;
     }
     if (self::$_DEBUG) {
         echo '<pre>' . __METHOD__ . PHP_EOL;
     }
     $outArr = array();
     $resourcesArr = array(0 => 'unused');
     $currentElement = $xml;
     $outArr['id'] = (string) $currentElement['id'];
     $outArr['id_parent'] = (string) $currentElement['parent_id'];
     foreach ($currentElement->children() as $name => $value) {
         if ($name === 'posizione') {
             $outArr['pos_x0'] = (int) $value['x0'];
             $outArr['pos_y0'] = (int) $value['y0'];
             $outArr['pos_x1'] = (int) $value['x1'];
             $outArr['pos_y1'] = (int) $value['y1'];
         } else {
             if ($name === 'resource') {
                 // must do an array push because the method that saves the resources expects
                 // the array to start at index 1
                 array_push($resourcesArr, $this->_buildResourceArray($value, $courseNewID . self::$courseSeparator . $outArr['id'], $courseNewID));
                 // NOTE: the files will be copied later on, together with the others
             } else {
                 if ($name === 'link') {
                     // this array is saved in the _saveLinks method
                     $this->_linksArray[] = $this->_buildLinkArray($value, $courseNewID);
                 } else {
                     if ($name === 'extended') {
                         // it's enough to merge the extended array to the outArr and then add_node saves 'em all
                         $outArr = array_merge($outArr, $this->_buildExtendedArray($value, $courseNewID));
                     } else {
                         if ($name === 'nodo') {
                             continue;
                         } else {
                             $outArr[$name] = (string) $value;
                         }
                     }
                 }
             }
         }
     }
     if ($outArr['id'] != '') {
         $this->_logMessage(__METHOD__ . ' Saving course node. course id=' . $courseNewID . ' so far ' . $count . ' nodes have been imported');
         // add the node to the counted elements
         $count++;
         $this->_progressIncrement();
         // make some adjustments to invoke the datahandler's add_node method
         if (!is_null($outArr['id_parent']) && strtolower($outArr['id_parent']) != 'null' && $outArr['id_parent'] != '') {
             $oldNodeID = $this->_courseOldID . self::$courseSeparator . $outArr['id_parent'];
             if (isset($this->_courseNodeIDMapping[$oldNodeID])) {
                 $outArr['parent_id'] = $this->_courseNodeIDMapping[$oldNodeID];
             } else {
                 $outArr['parent_id'] = $courseNewID . self::$courseSeparator . $outArr['id_parent'];
             }
         }
         // 			else
         // 			{
         // 				$outArr['parent_id'] = null;
         // 			}
         unset($outArr['id_parent']);
         $outArr['id_course'] = $courseNewID;
         $outArr['creation_date'] = 'now';
         $outArr['id_node_author'] = $this->_assignedAuthorID;
         $outArr['version'] = 0;
         $outArr['contacts'] = 0;
         $outArr['icon'] = str_replace('<root_dir/>', ROOT_DIR, $outArr['icon']);
         $outArr['icon'] = str_replace('<id_autore/>', $this->_assignedAuthorID, $outArr['icon']);
         $outArr['icon'] = str_replace('<http_path/>', parse_url(HTTP_ROOT_DIR, PHP_URL_PATH), $outArr['icon']);
         $outArr['text'] = str_replace('<id_autore/>', $this->_assignedAuthorID, $outArr['text']);
         $outArr['text'] = str_replace('<http_root/>', HTTP_ROOT_DIR, $outArr['text']);
         $outArr['text'] = str_replace('<http_path/>', parse_url(HTTP_ROOT_DIR, PHP_URL_PATH), $outArr['text']);
         // oldID is needed below, for creating the array that maps the old node id
         // to the new node id. This must be done AFTER node is saved.
         $oldID = $outArr['id'];
         unset($outArr['id']);
         // when a generated id will be used and comment below
         // set array of resources to be saved together with the node
         // for some unbelievable reason the _add_media method called by add_node
         // expects the resources array to start at index 1, so let's make it happy.
         unset($resourcesArr[0]);
         $outArr['resources_ar'] = $resourcesArr;
         // sets the parent if an exported root node is made child in import
         if (is_null($this->_selectedNodeID) && $count == 1) {
             $outArr['parent_id'] = null;
         } else {
             if ((!isset($outArr['parent_id']) || $count == 1) && !is_null($this->_selectedNodeID)) {
                 $outArr['parent_id'] = $this->_selectedNodeID;
             }
         }
         // prints out some basic info if in debug mode
         if (self::$_DEBUG) {
             echo "count=" . $count . PHP_EOL;
             if ($count == 1) {
                 //				if ($outArr['id']==$courseNewID.self::$courseSeparator.'1') {
                 print_r($outArr);
                 echo "<hr/>";
             } else {
                 var_dump($outArr['id']);
                 var_dump($outArr['parent_id']);
                 var_dump($outArr['name']);
             }
         }
         /**
          * ACTUALLY SAVE THE NODE!! YAHOOOO!!!
          */
         if (!self::$_DEBUG) {
             // $outArr['id'] = $courseNewID.self::$courseSeparator.$outArr['id'];
             $this->_logMessage('Saving course node, passing:');
             $this->_logMessage(print_r($outArr, true));
             $addResult = $this->_dh->add_node($outArr);
             // if it's an error return it right away, as usual
             if (AMA_DB::isError($addResult)) {
                 $this->_logMessage(__METHOD__ . ' Error saving course node. DB returned the following:');
                 $this->_logMessage(print_r($addResult, true));
                 return $addResult;
             } else {
                 // add node to the course node mapping array,
                 // keys are exported node ids, values are imported ones
                 $this->_courseNodeIDMapping[$this->_courseOldID . self::$courseSeparator . $oldID] = $addResult;
                 $this->_logMessage(__METHOD__ . ' Successfully saved course node');
             }
         }
     }
     // recur the children
     if ($currentElement->nodo) {
         for ($i = 0; $i < count($currentElement->nodo); $i++) {
             $this->_logMessage(__METHOD__ . ' RECURRING COURSE NODES: depth=' . ++$depth . ' This node has ' . count($currentElement->test) . ' kids and is the brother n.' . $i);
             $this->_importNodi($currentElement->nodo[$i], $courseNewID);
         }
     }
     if (self::$_DEBUG) {
         echo "</pre>";
     }
     $depth--;
     return $count;
 }