/** * Implements deleteLibrary */ public function deleteLibrary($library) { global $DB; // Delete library files \H5PCore::deleteFileTree($this->getH5pPath() . '/libraries/' . $library->name . '-' . $library->major_version . '.' . $library->minor_version); // Remove library data from database $DB->delete('hvp_libraries_libraries', array('library_id' => $library->id)); $DB->delete('hvp_libraries_languages', array('library_id' => $library->id)); $DB->delete('hvp_libraries', array('id' => $library->id)); }
/** * Return path to h5p package. * * Creates package if not already created * * @param array $content * @return string */ 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'; // Temp dir to put the h5p files in @mkdir($tempPath, 0777, TRUE); @mkdir($h5pDir . 'exports', 0777, TRUE); // Create content folder if ($this->h5pC->copyFileTree($h5pDir . 'content' . DIRECTORY_SEPARATOR . $content['id'], $tempPath . DIRECTORY_SEPARATOR . 'content') === FALSE) { return FALSE; } file_put_contents($tempPath . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR . 'content.json', $content['params']); // Make embedTypes into an array $embedTypes = explode(', ', $content['embedType']); // Won't content always be embedded in one way? // Build h5p.json $h5pJson = array('title' => $content['title'], 'language' => isset($content['language']) && strlen(trim($content['language'])) !== 0 ? $content['language'] : 'und', 'mainLibrary' => $content['library']['name'], 'embedTypes' => $embedTypes); // Add dependencies to h5p foreach ($content['dependencies'] as $dependency) { $library = $dependency['library']; // Copy library to h5p $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['type'] === 'editor') { continue; } $h5pJson[$dependency['type'] . 'Dependencies'][] = array('machineName' => $library['machineName'], 'majorVersion' => $library['majorVersion'], 'minorVersion' => $library['minorVersion']); } // 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 | ZIPARCHIVE::OVERWRITE); // 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(); H5PCore::deleteFileTree($tempPath); }
/** * Remove content folder. * * @param int $id * Content identifier */ public function deleteContent($id) { \H5PCore::deleteFileTree("{$this->path}/content/{$id}"); }
/** * Return path to h5p package. * * Creates package if not already created * * @param array $content * @return string */ public function createExportFile($content) { $h5pDir = $this->h5pC->path . DIRECTORY_SEPARATOR; $tempPath = $h5pDir . 'temp' . DIRECTORY_SEPARATOR . $content['id']; $zipPath = $h5pDir . 'exports' . DIRECTORY_SEPARATOR . $content['id'] . '.h5p'; // Temp dir to put the h5p files in @mkdir($tempPath, 0777, TRUE); @mkdir($h5pDir . 'exports', 0777, TRUE); // Create content folder if ($this->h5pC->copyFileTree($h5pDir . 'content' . DIRECTORY_SEPARATOR . $content['id'], $tempPath . DIRECTORY_SEPARATOR . 'content') === FALSE) { return FALSE; } file_put_contents($tempPath . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR . 'content.json', $content['params']); // Make embedTypes into an array $embedTypes = explode(', ', $content['embedType']); // Won't content always be embedded in one way? // Build h5p.json $h5pJson = array('title' => $content['title'], 'language' => isset($content['language']) && strlen(trim($content['language'])) !== 0 ? $content['language'] : 'und', 'mainLibrary' => $content['library']['name'], 'embedTypes' => $embedTypes); // Add dependencies to h5p foreach ($content['dependencies'] as $dependency) { $library = $dependency['library']; // Copy library to h5p $source = $h5pDir . (isset($library['path']) ? $library['path'] : '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['type'] === 'editor') { continue; } $h5pJson[$dependency['type'] . 'Dependencies'][] = array('machineName' => $library['machineName'], 'majorVersion' => $library['majorVersion'], 'minorVersion' => $library['minorVersion']); } // Save h5p.json $results = print_r(json_encode($h5pJson), true); file_put_contents($tempPath . DIRECTORY_SEPARATOR . 'h5p.json', $results); // Get a complete file list from our tmp dir $files = array(); self::populateFileList($tempPath, $files); // Create new zip instance. $zip = new ZipArchive(); $zip->open($zipPath, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE); // Add all the files from the tmp dir. foreach ($files as $file) { // Please note that the zip format has no concept of folders, we must // use forward slashes to separate our directories. $zip->addFile($file->absolutePath, $file->relativePath); } // Close zip and remove temp dir $zip->close(); H5PCore::deleteFileTree($tempPath); }
/** * Return path to h5p package. * * Creates package if not already created * * @param array $content * @return string */ public function createExportFile($content) { // Get path to temporary folder, where export will be contained $tmpPath = $this->h5pC->fs->getTmpPath(); mkdir($tmpPath, 0777, true); try { // Create content folder and populate with files $this->h5pC->fs->exportContent($content['id'], "{$tmpPath}/content"); } catch (Exception $e) { $this->h5pF->setErrorMessage($this->h5pF->t($e->getMessage())); H5PCore::deleteFileTree($tmpPath); return FALSE; } // Update content.json with content from database file_put_contents("{$tmpPath}/content/content.json", $content['params']); // Make embedType into an array $embedTypes = explode(', ', $content['embedType']); // Build h5p.json $h5pJson = array('title' => $content['title'], 'language' => isset($content['language']) && strlen(trim($content['language'])) !== 0 ? $content['language'] : 'und', 'mainLibrary' => $content['library']['name'], 'embedTypes' => $embedTypes); // Add dependencies to h5p foreach ($content['dependencies'] as $dependency) { $library = $dependency['library']; try { $exportFolder = NULL; // Determine path of export library if (isset($this->h5pC) && isset($this->h5pC->h5pD)) { // Tries to find library in development folder $isDevLibrary = $this->h5pC->h5pD->getLibrary($library['machineName'], $library['majorVersion'], $library['minorVersion']); if ($isDevLibrary !== NULL) { $exportFolder = "/" . $library['path']; } } // Export required libraries $this->h5pC->fs->exportLibrary($library, $tmpPath, $exportFolder); } catch (Exception $e) { $this->h5pF->setErrorMessage($this->h5pF->t($e->getMessage())); H5PCore::deleteFileTree($tmpPath); return FALSE; } // Do not add editor dependencies to h5p json. if ($dependency['type'] === 'editor') { continue; } // Add to h5p.json dependencies $h5pJson[$dependency['type'] . 'Dependencies'][] = array('machineName' => $library['machineName'], 'majorVersion' => $library['majorVersion'], 'minorVersion' => $library['minorVersion']); } // Save h5p.json $results = print_r(json_encode($h5pJson), true); file_put_contents("{$tmpPath}/h5p.json", $results); // Get a complete file list from our tmp dir $files = array(); self::populateFileList($tmpPath, $files); // Get path to temporary export target file $tmpFile = $this->h5pC->fs->getTmpPath(); // Create new zip instance. $zip = new ZipArchive(); $zip->open($tmpFile, ZipArchive::CREATE | ZipArchive::OVERWRITE); // Add all the files from the tmp dir. foreach ($files as $file) { // Please note that the zip format has no concept of folders, we must // use forward slashes to separate our directories. $zip->addFile($file->absolutePath, $file->relativePath); } // Close zip and remove tmp dir $zip->close(); H5PCore::deleteFileTree($tmpPath); try { // Save export $this->h5pC->fs->saveExport($tmpFile, $content['slug'] . '-' . $content['id'] . '.h5p'); } catch (Exception $e) { $this->h5pF->setErrorMessage($this->h5pF->t($e->getMessage())); } unlink($tmpFile); return true; }