From 8ed63d4710aea334138141af1690f7d5d10ffdab Mon Sep 17 00:00:00 2001 From: Tim Lock Date: Tue, 26 Jun 2012 11:43:33 +0930 Subject: [PATCH] MDL-34020: Further work on IMS package importing when using Blackboard packages --- mod/imscp/backup/moodle1/lib.php | 6 +++--- mod/imscp/locallib.php | 44 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/mod/imscp/backup/moodle1/lib.php b/mod/imscp/backup/moodle1/lib.php index 1977312e42b..2a74c31c1bb 100644 --- a/mod/imscp/backup/moodle1/lib.php +++ b/mod/imscp/backup/moodle1/lib.php @@ -83,7 +83,7 @@ class moodle1_mod_imscp_handler extends moodle1_resource_successor_handler { $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 @@ -113,7 +113,7 @@ class moodle1_mod_imscp_handler extends moodle1_resource_successor_handler { * * @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)) { @@ -127,6 +127,6 @@ class moodle1_mod_imscp_handler extends moodle1_resource_successor_handler { } require_once($CFG->dirroot.'/mod/imscp/locallib.php'); - return imscp_parse_manifestfile($manifestfilecontents); + return imscp_parse_manifestfile($manifestfilecontents, $imscp, $context); } } diff --git a/mod/imscp/locallib.php b/mod/imscp/locallib.php index d51ecf7a690..b3dbafbf9a8 100644 --- a/mod/imscp/locallib.php +++ b/mod/imscp/locallib.php @@ -95,7 +95,7 @@ function imscp_parse_structure($imscp, $context) { return null; } - return imscp_parse_manifestfile($manifestfile->get_content()); + return imscp_parse_manifestfile($manifestfile->get_content(), $imscp, $context); } /** @@ -103,7 +103,7 @@ function imscp_parse_structure($imscp, $context) { * @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; @@ -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; } @@ -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')) { -- 2.11.4.GIT