/** * Recursive method saving a testnode 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 _importTests($xml, $courseNewID) { static $savedCourseID = 0; static $count = 0; static $depth = 0; /** * needed to count how many test were imported * in each disctinct course */ if ($savedCourseID != $courseNewID) { $savedCourseID = $courseNewID; $count = 0; } if (self::$_DEBUG) { echo '<pre>' . __METHOD__ . PHP_EOL; } $outArr = array(); $currentElement = $xml; $oldNodeID = (string) $currentElement['id_nodoTestEsportato']; $parentNodeID = (string) $currentElement['id_nodo_parent']; $rootNodeID = (string) $currentElement['id_nodo_radice']; $refNodeID = (string) $currentElement['id_nodo_riferimento']; foreach ($currentElement->children() as $name => $value) { if ($name === 'test') { continue; } else { $temp = (string) $value; if (!empty($temp)) { $outArr[$name] = $temp; } else { $outArr[$name] = null; } } } if (!empty($outArr)) { // make some adjustments to invoke the test datahandler's test_addNode method $this->_logMessage(__METHOD__ . ' Saving test node. course id=' . $courseNewID . ' so far ' . $count . ' nodes have been imported'); $count++; $this->_progressIncrement(); $outArr['id_corso'] = $courseNewID; $outArr['id_posizione'] = (string) $currentElement['id_posizione']; $outArr['id_utente'] = $this->_assignedAuthorID; $outArr['id_istanza'] = (string) $currentElement['id_istanza']; if (isset($this->_testNodeIDMapping[$parentNodeID])) { $outArr['id_nodo_parent'] = $this->_testNodeIDMapping[$parentNodeID]; } if (isset($this->_testNodeIDMapping[$rootNodeID])) { $outArr['id_nodo_radice'] = $this->_testNodeIDMapping[$rootNodeID]; } if (isset($refNodeID) && $refNodeID != '') { if (isset($this->_courseNodeIDMapping[$refNodeID])) { $outArr['id_nodo_riferimento'] = $this->_courseNodeIDMapping[$refNodeID]; } } $outArr['icona'] = !is_null($outArr['icona']) ? str_replace('<root_dir/>', ROOT_DIR, $outArr['icona']) : null; $outArr['icona'] = !is_null($outArr['icona']) ? str_replace('<id_autore/>', $this->_assignedAuthorID, $outArr['icona']) : null; $outArr['testo'] = !is_null($outArr['testo']) ? str_replace('<id_autore/>', $this->_assignedAuthorID, $outArr['testo']) : null; $outArr['testo'] = !is_null($outArr['testo']) ? str_replace('<http_root/>', HTTP_ROOT_DIR, $outArr['testo']) : null; $outArr['testo'] = !is_null($outArr['testo']) ? str_replace('<http_path/>', parse_url(HTTP_ROOT_DIR, PHP_URL_PATH) . '/', $outArr['testo']) : null; $outArr['nome'] = !is_null($outArr['nome']) ? str_replace('<id_autore/>', $this->_assignedAuthorID, $outArr['nome']) : null; $outArr['nome'] = !is_null($outArr['nome']) ? str_replace('<http_root/>', HTTP_ROOT_DIR, $outArr['nome']) : null; $outArr['nome'] = !is_null($outArr['nome']) ? str_replace('<http_path/>', parse_url(HTTP_ROOT_DIR, PHP_URL_PATH) . '/', $outArr['nome']) : null; unset($outArr['data_creazione']); unset($outArr['versione']); unset($outArr['n_contatti']); // 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_nodo_parent']); var_dump($outArr['nome']); var_dump($outArr['tipo']); } } /** * ACTUALLY SAVE THE NODE!! YAHOOOO!!! */ if (!self::$_DEBUG) { $this->_logMessage('Saving test node with a call to test_addNode test data handler, passing:'); $this->_logMessage(print_r($outArr, true)); $newNodeID = $this->_dh->test_addNode($outArr); // if it's an error return it right away, as usual if (AMA_DB::isError($newNodeID)) { $this->_logMessage(__METHOD__ . ' Error saving test node. DB returned the following:'); $this->_logMessage(print_r($newNodeID, true)); return $newNodeID; } else { $this->_logMessage(__METHOD__ . ' Successfully saved test node'); } } else { $newNodeID = 666; } $this->_testNodeIDMapping[$oldNodeID] = $newNodeID; } // recur the children if ($currentElement->test) { for ($i = 0; $i < count($currentElement->test); $i++) { $this->_logMessage(__METHOD__ . ' RECURRING TEST NODES: depth=' . ++$depth . ' This test has ' . count($currentElement->test) . ' kids and is the brother n.' . $i); $this->_importTests($currentElement->test[$i], $courseNewID); } } if (self::$_DEBUG) { echo '</pre>'; } $depth--; return $count; }