Renaming functions

namespaces
Svein-Tore Griff With 2013-11-14 23:42:10 +01:00
parent f98cce49a6
commit 9dd710a8fb
2 changed files with 104 additions and 104 deletions

View File

@ -246,109 +246,7 @@ interface H5PFrameworkInterface {
/**
* 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['machineName'] . '-' . $library['majorVersion'] . '.' . $library['minorVersion'];
$destination = $tempPath . DIRECTORY_SEPARATOR . $library['machineName'];
$this->h5pC->copyTree($source, $destination);
// Set preloaded and dynamic dependencies
if ($library['preloaded']) {
$preloadedDependencies[] = array(
'machineName' => $library['machineName'],
'majorVersion' => $library['majorVersion'],
'minorVersion' => $library['minorVersion'],
);
} else {
$dynamicDependencies[] = array(
'machineName' => $library['machineName'],
'majorVersion' => $library['majorVersion'],
'minorVersion' => $library['minorVersion'],
);
}
}
// Make embedTypes into an array
$embedTypes = explode(', ', $exports['embedType']);
// 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);
}
public function isExportEnabled();
}
/**
@ -1133,6 +1031,108 @@ class H5PStorage {
}
}
/**
* 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['machineName'] . '-' . $library['majorVersion'] . '.' . $library['minorVersion'];
$destination = $tempPath . DIRECTORY_SEPARATOR . $library['machineName'];
$this->h5pC->copyTree($source, $destination);
// Set preloaded and dynamic dependencies
if ($library['preloaded']) {
$preloadedDependencies[] = array(
'machineName' => $library['machineName'],
'majorVersion' => $library['majorVersion'],
'minorVersion' => $library['minorVersion'],
);
} else {
$dynamicDependencies[] = array(
'machineName' => $library['machineName'],
'majorVersion' => $library['majorVersion'],
'minorVersion' => $library['minorVersion'],
);
}
}
// Make embedTypes into an array
$embedTypes = explode(', ', $exports['embedType']);
// 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);
}
}
/**
* Functions and storage shared by the other H5P classes
*/

View File

@ -64,7 +64,7 @@ H5P.init = function () {
});
};
if (H5PIntegration.getExport()) {
if (H5PIntegration.isExportEnabled()) {
H5P.jQuery('<div class="h5p-export"><a href="' + window.location + '/h5p-export">Last ned H5P</a></div>').insertAfter($el).children();
}
});