/** * Imports an extracted SCORM 2004 module from ilias-data dir into database * * @access public * @return string title of package */ public function il_import($packageFolder, $packageId, $ilias, $validate, $reimport = false) { global $ilDB, $ilLog, $ilErr; if ($reimport === true) { $this->packageId = $packageId; $this->dbRemoveAll(); } $this->packageFolder = $packageFolder; $this->packageId = $packageId; $this->imsmanifestFile = $this->packageFolder . '/' . 'imsmanifest.xml'; //step 1 - parse Manifest-File and validate $this->imsmanifest = new DOMDocument(); $this->imsmanifest->async = false; if (!@$this->imsmanifest->load($this->imsmanifestFile)) { $this->diagnostic[] = 'XML not wellformed'; return false; } //step 2 tranform $this->manifest = $this->transform($this->imsmanifest, self::DB_ENCODE_XSL); if (!$this->manifest) { $this->diagnostic[] = 'Cannot transform into normalized manifest'; return false; } //setp 2.5 if only a single item, make sure the scormType of it's linked resource is SCO $path = new DOMXpath($this->manifest); $path->registerNamespace("scorm", "http://www.openpalms.net/scorm/scorm13"); $items = $path->query("//scorm:item"); if ($items->length == 1) { $n = $items->item(0); $resource = $path->query("//scorm:resource"); //[&id='"+$n->getAttribute("resourceId")+"']"); foreach ($resource as $res) { if ($res->getAttribute('id') == $n->getAttribute("resourceId")) { $res->setAttribute('scormType', 'sco'); } } } //$this->manifest->save("C:\Users\gratat\after.xml"); //step 3 validation -just for normalized XML if ($validate == "y") { if (!$this->validate($this->manifest, self::VALIDATE_XSD)) { $ilErr->raiseError("<b>The uploaded SCORM 1.2 / SCORM 2004 is not valid. You can try to import the package without the validation option checked on your own risk. </b><br><br>Validation Error(s):</b><br> Normalized XML is not conform to " . self::VALIDATE_XSD, $ilErr->MESSAGE); } } $this->dbImport($this->manifest); if (file_exists($this->packageFolder . '/' . 'index.xml')) { $doc = simplexml_load_file($this->packageFolder . '/' . 'index.xml'); $l = $doc->xpath("/ContentObject/MetaData"); if ($l[0]) { include_once 'Services/MetaData/classes/class.ilMDXMLCopier.php'; $mdxml =& new ilMDXMLCopier($l[0]->asXML(), $packageId, $packageId, ilObject::_lookupType($packageId)); $mdxml->startParsing(); $mdxml->getMDObject()->update(); } } //step 5 $x = simplexml_load_string($this->manifest->saveXML()); $x['persistPreviousAttempts'] = $this->packageData['persistprevattempts']; $x['online'] = $this->packageData['c_online']; $x['defaultLessonMode'] = $this->packageData['default_lesson_mode']; $x['credit'] = $this->packageData['credit']; $x['autoReview'] = $this->packageData['auto_review']; $j = array(); // first read resources into flat array to resolve item/identifierref later $r = array(); foreach ($x->resource as $xe) { $r[strval($xe['id'])] = $xe; unset($xe); } // iterate through items and set href and scoType as activity attributes foreach ($x->xpath('//*[local-name()="item"]') as $xe) { // get reference to resource and set href accordingly if ($b = $r[strval($xe['resourceId'])]) { $xe['href'] = strval($b['base']) . strval($b['href']); unset($xe['resourceId']); if (strval($b['scormType']) == 'sco') { $xe['sco'] = true; } } } // iterate recursivly through activities and build up simple php object // with items and associated sequencings // top node is the default organization which is handled as an item self::jsonNode($x->organization, $j['item']); foreach ($x->sequencing as $s) { self::jsonNode($s, $j['sequencing'][]); } // combined manifest+resources xml:base is set as organization base $j['item']['base'] = strval($x['base']); // package folder is base to whole playing process $j['base'] = $packageFolder . '/'; $j['foreignId'] = floatval($x['foreignId']); // manifest cp_node_id for associating global (package wide) objectives $j['id'] = strval($x['id']); // manifest id for associating global (package wide) objectives //last step - build ADL Activity tree $act = new SeqTreeBuilder(); $adl_tree = $act->buildNodeSeqTree($this->imsmanifestFile); $ilDB->update('cp_package', array('xmldata' => array('clob', $x->asXML()), 'jsdata' => array('clob', json_encode($j)), 'activitytree' => array('clob', json_encode($adl_tree['tree'])), 'global_to_system' => array('integer', (int) $adl_tree['global']), 'shared_data_global_to_system' => array('integer', (int) $adl_tree['dataglobal'])), array('obj_id' => array('integer', (int) $this->packageId))); return $j['item']['title']; }
//start directory processing - one level only $dirs = array(); $d = dir($search_path); while (false !== ($entry = $d->read())) { if ($entry != '.' && $entry != '..' && is_dir($search_path . "/" . $entry)) { array_push($dirs, $entry); } } $d->close(); //iterate over array for ($i = 0; $i < count($dirs); $i++) { //check for imsmanifest $toparse = $search_path . "/" . $dirs[$i] . "/imsmanifest.xml"; if (is_file($toparse)) { //check for DB entry //get id $id = $webdir = str_replace("lm_", "", $dirs[$i]); $result = mysql_query("SELECT * FROM cp_package WHERE(obj_id={$id})"); $row = mysql_fetch_array($result, MYSQL_ASSOC); if (count($row) > 0 && strlen($row['jsdata']) > 10) { //create new parser $builder = new SeqTreeBuilder(); $ret = $builder->buildNodeSeqTree($toparse); $global = $ret['global']; $adltree = mysql_escape_string(json_encode($ret['tree'])); $result_update = mysql_query("UPDATE cp_package SET activitytree='{$adltree}',global_to_system='{$global}' WHERE(obj_id={$id})") or die(mysql_error()); echo "Updated activitytree for: " . $dirs[$i] . " Global:" . $global . " \n"; } } } mysql_close($dbh);
<?php require_once "SeqTreeBuilder.php"; $builder = new SeqTreeBuilder(); $ret = $builder->buildNodeSeqTree("imsmanifest.xml"); echo json_encode($ret);