Issue #2120227 by gafolini, Ellinokon and falcon: Add export feature to H5P
parent
1d46204ac0
commit
5ca6a5f39a
116
h5p.classes.php
116
h5p.classes.php
|
@ -219,6 +219,122 @@ interface H5PFrameworkInterface {
|
||||||
* Library Id
|
* Library Id
|
||||||
*/
|
*/
|
||||||
public function deleteLibraryDependencies($libraryId);
|
public function deleteLibraryDependencies($libraryId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the paths we need to export
|
||||||
|
*
|
||||||
|
* @param int $contentId
|
||||||
|
* @param string $title
|
||||||
|
* @param string $language
|
||||||
|
* @return export object
|
||||||
|
*/
|
||||||
|
public function getExportData($contentId, $title, $language);
|
||||||
|
/**
|
||||||
|
* Check if export is enabled.
|
||||||
|
*/
|
||||||
|
public function exportEnabled();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is used for exporting zips
|
||||||
|
*/
|
||||||
|
Class H5PExport {
|
||||||
|
public $h5pF;
|
||||||
|
public $h5pC;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for the H5PStorage
|
||||||
|
*
|
||||||
|
* @param object $H5PFramework
|
||||||
|
* The frameworks implementation of the H5PFrameworkInterface
|
||||||
|
*/
|
||||||
|
public function __construct($H5PFramework, $H5PCore) {
|
||||||
|
$this->h5pF = $H5PFramework;
|
||||||
|
$this->h5pC = $H5PCore;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Create the h5p package
|
||||||
|
*
|
||||||
|
* @param object $exports
|
||||||
|
* The data to be exported.
|
||||||
|
* @return h5p package.
|
||||||
|
*/
|
||||||
|
public function exportToZip($exports) {
|
||||||
|
$h5pDir = $this->h5pF->getH5pPath() . DIRECTORY_SEPARATOR;
|
||||||
|
$tempPath = $h5pDir . 'temp' . DIRECTORY_SEPARATOR . $exports['contentId'];
|
||||||
|
$zipPath = $h5pDir . 'exports' . DIRECTORY_SEPARATOR . $exports['contentId'] . '.h5p';
|
||||||
|
// Check if h5p-package already exists.
|
||||||
|
if (!file_exists($zipPath) == true) {
|
||||||
|
// Temp dir to put the h5p files in
|
||||||
|
@mkdir($tempPath);
|
||||||
|
$this->h5pC->copyTree($h5pDir . 'content' . DIRECTORY_SEPARATOR . $exports['contentId'], $tempPath . DIRECTORY_SEPARATOR . 'content');
|
||||||
|
// Copies libraries to temp dir and create mention in h5p.json
|
||||||
|
foreach($exports['libraries'] as $library) {
|
||||||
|
$source = $h5pDir . 'libraries' . DIRECTORY_SEPARATOR . $library['machine_name'] . '-' . $library['major_version'] . '.' . $library['minor_version'];
|
||||||
|
$destination = $tempPath . DIRECTORY_SEPARATOR . $library['machine_name'];
|
||||||
|
$this->h5pC->copyTree($source, $destination);
|
||||||
|
|
||||||
|
// Set preloaded and dynamic dependencies
|
||||||
|
if ($library['preloaded']) {
|
||||||
|
$preloadedDependencies[] = array(
|
||||||
|
'machineName' => $library['machine_name'],
|
||||||
|
'majorVersion' => $library['major_version'],
|
||||||
|
'minorVersion' => $library['minor_version'],
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$dynamicDependencies[] = array(
|
||||||
|
'machineName' => $library['machine_name'],
|
||||||
|
'majorVersion' => $library['major_version'],
|
||||||
|
'minorVersion' => $library['minor_version'],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Make embedTypes into an array
|
||||||
|
$embedTypes = explode(', ', $exports['embed_type']);
|
||||||
|
|
||||||
|
// Build h5p.json
|
||||||
|
$h5pJson = array (
|
||||||
|
'title' => $exports['title'],
|
||||||
|
'language' => $exports['language'],
|
||||||
|
'mainLibrary' => $exports['mainLibrary'],
|
||||||
|
'embedTypes' => $embedTypes,
|
||||||
|
);
|
||||||
|
// Add preloaded and dynamic dependencies if they exist
|
||||||
|
if ($preloadedDependencies) { $h5pJson['preloadedDependencies'] = $preloadedDependencies; }
|
||||||
|
if ($dynamicDependencies) { $h5pJson['dynamicDependencies'] = $dynamicDependencies; }
|
||||||
|
|
||||||
|
// Save h5p.json
|
||||||
|
$results = print_r(json_encode($h5pJson), true);
|
||||||
|
file_put_contents($tempPath . DIRECTORY_SEPARATOR . 'h5p.json', $results);
|
||||||
|
|
||||||
|
// Create new zip instance.
|
||||||
|
$zip = new ZipArchive();
|
||||||
|
$zip->open($zipPath, ZIPARCHIVE::CREATE);
|
||||||
|
|
||||||
|
// Get all files and folders in $tempPath
|
||||||
|
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tempPath . DIRECTORY_SEPARATOR));
|
||||||
|
// Add files to zip
|
||||||
|
foreach ($iterator as $key=>$value) {
|
||||||
|
$test = '.';
|
||||||
|
// Do not add the folders '.' and '..' to the zip. This will make zip invalid.
|
||||||
|
if (substr_compare($key, $test, -strlen($test), strlen($test)) !== 0) {
|
||||||
|
// Get files path in $tempPath
|
||||||
|
$filePath = explode($tempPath . DIRECTORY_SEPARATOR, $key);
|
||||||
|
// Add files to the zip with the intended file-structure
|
||||||
|
$zip->addFile($key, $filePath[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Close zip and remove temp dir
|
||||||
|
$zip->close();
|
||||||
|
@rmdir($tempPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set headers for automagic download!!
|
||||||
|
header('Content-Description: File Transfer');
|
||||||
|
header('Content-Type: application/zip');
|
||||||
|
header('Content-Disposition: attachment; filename=' . $exports['title'] . '.h5p');
|
||||||
|
readfile ($zipPath);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -62,6 +62,10 @@ H5P.init = function () {
|
||||||
H5P.fullScreen($el, obj);
|
H5P.fullScreen($el, obj);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
if (H5PIntegration.getExport()) {
|
||||||
|
H5P.jQuery('<div class="h5p-export"><a href="' + window.location + '/h5p-export">Last ned H5P</a></div>').insertAfter($el).children();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -435,4 +439,4 @@ if (H5P.jQuery) {
|
||||||
H5P.init();
|
H5P.init();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
Loading…
Reference in New Issue