Recreate .h5p when content cache is rebuilt. Avoid loading content dependencies multiple times.

namespaces
Frode Petterson 2014-05-27 14:05:20 +02:00
parent 43d239c6f1
commit 2053b43024
1 changed files with 78 additions and 79 deletions

View File

@ -1152,22 +1152,14 @@ Class H5PExport {
*
* Creates package if not already created
*
* @param int/string $contentId
* Identifier for this H5P
* @param String $title
* Title of H5P
* @param string $language
* Language code for H5P
* @param array $content
* @return string
* Path to .h5p file
*/
public function getExportPath($content) {
public function createExportFile($content) {
$h5pDir = $this->h5pF->getH5pPath() . DIRECTORY_SEPARATOR;
$tempPath = $h5pDir . 'temp' . DIRECTORY_SEPARATOR . $content['id'];
$zipPath = $h5pDir . 'exports' . DIRECTORY_SEPARATOR . $content['id'] . '.h5p';
// Check if h5p-package already exists.
if (!file_exists($zipPath)) {
// Temp dir to put the h5p files in
@mkdir($tempPath, 0777, TRUE);
@mkdir($h5pDir . 'exports', 0777, TRUE);
@ -1190,22 +1182,23 @@ Class H5PExport {
);
// Add dependencies to h5p
$dependencies = $this->h5pC->loadContentDependencies($content['id']);
foreach ($dependencies as $dependency) {
foreach ($content['dependencies'] as $dependency) {
$library = $dependency['library'];
// Copy library to h5p
$source = isset($dependency['path']) ? $dependency['path'] : $h5pDir . 'libraries' . DIRECTORY_SEPARATOR . H5PCore::libraryToString($dependency, TRUE);
$destination = $tempPath . DIRECTORY_SEPARATOR . $dependency['machineName'];
$source = isset($library['path']) ? $library['path'] : $h5pDir . 'libraries' . DIRECTORY_SEPARATOR . H5PCore::libraryToString($library, TRUE);
$destination = $tempPath . DIRECTORY_SEPARATOR . $library['machineName'];
$this->h5pC->copyFileTree($source, $destination);
// Do not add editor dependencies to h5p json.
if ($dependency['dependencyType'] === 'editor') {
if ($dependency['type'] === 'editor') {
continue;
}
$h5pJson[$dependency['dependencyType'] . 'Dependencies'][] = array(
'machineName' => $dependency['machineName'],
'majorVersion' => $dependency['majorVersion'],
'minorVersion' => $dependency['minorVersion']
$h5pJson[$dependency['type'] . 'Dependencies'][] = array(
'machineName' => $library['machineName'],
'majorVersion' => $library['majorVersion'],
'minorVersion' => $library['minorVersion']
);
}
@ -1235,9 +1228,6 @@ Class H5PExport {
H5PCore::deleteFileTree($tempPath);
}
return str_replace(DIRECTORY_SEPARATOR, '/', $zipPath);
}
/**
* Delete .h5p file
*
@ -1297,6 +1287,7 @@ class H5PCore {
public static $defaultLibraryWhitelistExtras = 'js css';
public $librariesJsonData, $contentJsonData, $mainJsonData, $h5pF, $path, $development_mode, $h5pD;
private $exportEnabled;
/**
* Constructor for the H5PCore
@ -1305,13 +1296,15 @@ class H5PCore {
* The frameworks implementation of the H5PFrameworkInterface
* @param string $path H5P file storage directory.
* @param string $language code. Defaults to english.
* @param boolean $export enabled?
* @param int $development_mode mode.
*/
public function __construct($H5PFramework, $path, $language = 'en', $development_mode = H5PDevelopment::MODE_NONE) {
public function __construct($H5PFramework, $path, $language = 'en', $export = FALSE, $development_mode = H5PDevelopment::MODE_NONE) {
$this->h5pF = $H5PFramework;
$this->h5pF = $H5PFramework;
$this->path = $path;
$this->exportEnabled = $export;
$this->development_mode = $development_mode;
if ($development_mode & H5PDevelopment::MODE_LIBRARY) {
@ -1398,11 +1391,17 @@ class H5PCore {
$params = json_encode($params->params);
// Update content dependencies.
$dependencies = $validator->getDependencies();
$content['dependencies'] = $validator->getDependencies();
$this->h5pF->deleteLibraryUsage($content['id']);
$this->h5pF->saveLibraryUsage($content['id'], $dependencies);
$this->h5pF->saveLibraryUsage($content['id'], $content['dependencies']);
// TODO: Create new export if enabled.
if ($this->exportEnabled) {
// Recreate export file
$exporter = new H5PExport($this->h5pF, $this);
$exporter->createExportFile($content);
// TODO: Should we rather create the file once first accessed, like imagecache?
}
// Cache.
$this->h5pF->cacheSet('parameters', $content['id'], $params);