This is the new routine that I’m going to use to resolve the resource dependencies in the package manifest file.
function resolveIMSManifestDependencies($identifier) {
global $resourceData;
$files = $resourceData[$identifier]['files'];
$dependencies = $resourceData[$identifier]['dependencies'];
if (is_array($dependencies)) {
foreach ($dependencies as $d => $dependencyidentifier) {
$files = array_merge($files,resolveIMSManifestDependencies($dependencyidentifier));
unset($resourceData[$identifier]['dependencies'][$d]);
}
$files = array_unique($files);
}
return $files;
}
Here’s what I’m doing – line-by-line:
- Line 1: The function takes a resource identifier as its argument.
- Line 3: I’m going to process the data that I’ve stored in the $resourceData array, so I make it a global variable. I’ll have to do the same in the readIMSManifestFile() function, but more of that later.
- Line 5: I read the existing list of files for this resource (see line 1) into an array. I’ll be updating this as I go through the function.
- Line 7: I read the dependency information for this resource into an array called $dependencies.
- Lines 8 through 14: I loop through each of the dependencies for this resource.
- Line 10: This is the line that does the hard work. What I do is to merge together my existing ‘files’ array with a new ‘files’ array that I create by recursively calling the resolveIMSManifestDependencies() for each dependency (and any of its dependencies).
- Line 11: Since I’ve resolved this dependency (and any of its dependencies), I unset the dependency data in the $resourceData array so that I don’t process it again.
- Line 13: I eliminate any duplicate entries in the ‘files’ array.
- Line 16: I return the ‘files’ array to the calling program.
Now I have to modify the readIMSManifestFile() function so that it looks like this.
function readIMSManifestFile($manifestfile) {
// PREPARATIONS
// central array for resource data
global $resourceData;
// load the imsmanifest.xml file
$xmlfile = new DomDocument;
$xmlfile->preserveWhiteSpace = FALSE;
$xmlfile->load($manifestfile);
// adlcp namespace
$manifest = $xmlfile->getElementsByTagName('manifest');
$adlcp = $manifest->item(0)->getAttribute('xmlns:adlcp');
// READ THE RESOURCES LIST
// array to store the results
$resourceData = array();
// get the list of resource element
$resourceList = $xmlfile->getElementsByTagName('resource');
$r = 0;
foreach ($resourceList as $rtemp) {
// decode the resource attributes
$identifier = $resourceList->item($r)->getAttribute('identifier');
$resourceData[$identifier]['type'] = $resourceList->item($r)->getAttribute('type');
$resourceData[$identifier]['scormtype'] = $resourceList->item($r)->getAttribute('adlcp:scormtype');
$resourceData[$identifier]['href'] = $resourceList->item($r)->getAttribute('href');
// list of files
$fileList = $resourceList->item($r)->getElementsByTagName('file');
$f = 0;
foreach ($fileList as $ftemp) {
$resourceData[$identifier]['files'][$f] = $fileList->item($f)->getAttribute('href');
$f++;
}
// list of dependencies
$dependencyList = $resourceList->item($r)->getElementsByTagName('dependency');
$d = 0;
foreach ($dependencyList as $dtemp) {
$resourceData[$identifier]['dependencies'][$d] = $dependencyList->item($d)->getAttribute('identifierref');
$d++;
}
$r++;
}
// resolve resource dependencies to create the file lists for each resource
foreach ($resourceData as $identifier => $resource) {
$resourceData[$identifier]['files'] = resolveIMSManifestDependencies($identifier);
}
// READ THE ITEMS LIST
// PROCESS THE ITEMS LIST TO FIND SCOS
// RETURN RESULTS
}
The changes are:
- Line 6: I make the $resourceData array – a working array where I store all of the information as I process it – global so that it’s accessible to the resolveIMSManifestDependencies() function.
- Lines 44 to 49: I create a list of dependencies for each resource, and save it into the $resourceData array.
- Lines 56 to 59: After I’ve read all of the data for the resources (the loop finishes on line 54) I loop through the list of resources again and resolve all of the dependencies by calling resolveIMSManifestDependencies() as I described above.
Now that I’ve read all of the resources data, I’m going to read the items data. That will be my next post.