Skip to content

Commit

Permalink
MDL-34020: Further work on IMS package importing when using Blackboar…
Browse files Browse the repository at this point in the history
…d packages
  • Loading branch information
tlock committed Dec 4, 2013
1 parent ea04def commit 56dc43f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
6 changes: 3 additions & 3 deletions mod/imscp/backup/moodle1/lib.php
Expand Up @@ -83,7 +83,7 @@ public function process_legacy_resource(array $data, array $raw = null) {
$this->fileman->migrate_directory('moddata/resource/'.$data['id']);

// parse manifest
$structure = $this->parse_structure($this->converter->get_tempdir_path().'/moddata/resource/'.$data['id'].'/imsmanifest.xml');
$structure = $this->parse_structure($this->converter->get_tempdir_path().'/moddata/resource/'.$data['id'].'/imsmanifest.xml', $imscp, $contextid);
$imscp['structure'] = is_array($structure) ? serialize($structure) : null;

// write imscp.xml
Expand Down Expand Up @@ -113,7 +113,7 @@ public function process_legacy_resource(array $data, array $raw = null) {
*
* @param string $manifestfilepath the full path to the manifest file to parse
*/
protected function parse_structure($manifestfilepath) {
protected function parse_structure($manifestfilepath, $imscp, $context) {
global $CFG;

if (!file_exists($manifestfilepath)) {
Expand All @@ -127,6 +127,6 @@ protected function parse_structure($manifestfilepath) {
}

require_once($CFG->dirroot.'/mod/imscp/locallib.php');
return imscp_parse_manifestfile($manifestfilecontents);
return imscp_parse_manifestfile($manifestfilecontents, $imscp, $context);
}
}
44 changes: 42 additions & 2 deletions mod/imscp/locallib.php
Expand Up @@ -95,15 +95,15 @@ function imscp_parse_structure($imscp, $context) {
return null;
}

return imscp_parse_manifestfile($manifestfile->get_content());
return imscp_parse_manifestfile($manifestfile->get_content(), $imscp, $context);
}

/**
* Parse the contents of a IMS package's manifest file
* @param string $manifestfilecontents the contents of the manifest file
* @return array
*/
function imscp_parse_manifestfile($manifestfilecontents) {
function imscp_parse_manifestfile($manifestfilecontents, $imscp, $context) {
$doc = new DOMDocument();
if (!$doc->loadXML($manifestfilecontents, LIBXML_NONET)) {
return null;
Expand Down Expand Up @@ -162,6 +162,9 @@ function imscp_parse_manifestfile($manifestfilecontents) {
foreach ($fileresources as $file) {
$href = $file->getAttribute('href');
}
if (pathinfo($href, PATHINFO_EXTENSION) == 'xml') {
$href = imscp_recursive_href($href, $imscp, $context);
}
if (empty($href)) {
continue;
}
Expand Down Expand Up @@ -189,6 +192,43 @@ function imscp_parse_manifestfile($manifestfilecontents) {
return $items;
}

function imscp_recursive_href($manifestfilename, $imscp, $context) {
$fs = get_file_storage();

$dirname = dirname($manifestfilename);
$filename = basename($manifestfilename);

if ($dirname !== '/') {
$dirname = "/$dirname/";
}

if (!$manifestfile = $fs->get_file($context->id, 'mod_imscp', 'content', $imscp->revision, $dirname, $filename)) {
return null;
}
$doc = new DOMDocument();
if (!$doc->loadXML($manifestfile->get_content(), LIBXML_NONET)) {
return null;
}
$xmlresources = $doc->getElementsByTagName('resource');
foreach ($xmlresources as $res) {
if (!$href = $res->attributes->getNamedItem('href')) {
$fileresources = $res->getElementsByTagName('file');
foreach ($fileresources as $file) {
$href = $file->getAttribute('href');
if (pathinfo($href, PATHINFO_EXTENSION) == 'xml') {
$href = imscp_recursive_href($href, $imscp, $context);
}

if (pathinfo($href, PATHINFO_EXTENSION) == 'htm' || pathinfo($href, PATHINFO_EXTENSION) == 'html') {
return $href;
}
}
}
}

return $href;
}

function imscp_recursive_item($xmlitem, $level, $resources) {
$identifierref = '';
if ($identifierref = $xmlitem->attributes->getNamedItem('identifierref')) {
Expand Down

0 comments on commit 56dc43f

Please sign in to comment.