From 39ea23b8616e7d38ed972d4e7ad9c5cefc689b63 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Fri, 26 Sep 2014 13:29:07 +0200 Subject: [PATCH 01/19] Text align --- styles/h5p-admin.css | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/styles/h5p-admin.css b/styles/h5p-admin.css index 44310b0..219309a 100644 --- a/styles/h5p-admin.css +++ b/styles/h5p-admin.css @@ -244,4 +244,7 @@ button.h5p-admin.disabled:hover { padding: 0 0.5em; font-size: 1.5em; font-weight: bold; -} \ No newline at end of file +} +#h5p-admin-container .h5p-admin-center { + text-align: center; +} From a38a0f1253e646a05bfe3835ebbf36ebecc8bf67 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Fri, 26 Sep 2014 15:56:42 +0200 Subject: [PATCH 02/19] Removed old caching system. Use content variable for filtered parameters. Added getOption/setOption. --- h5p.classes.php | 373 ++++++++++++++++++++++++------------------------ 1 file changed, 183 insertions(+), 190 deletions(-) diff --git a/h5p.classes.php b/h5p.classes.php index 3cc31ac..b246771 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -55,31 +55,31 @@ interface H5PFrameworkInterface { * @return string Path to the last uploaded h5p */ public function getUploadedH5pPath(); - + /** * Get the list of the current installed libraries - * + * * @return array Associative array containg one item per machine name. This item contains an array of libraries. */ public function loadLibraries(); - + /** * Saving the unsupported library list - * + * * @param array A list of unsupported libraries */ public function setUnsupportedLibraries($libraries); - + /** * Returns unsupported libraries - * + * * @return array A list of the unsupported libraries */ public function getUnsupportedLibraries(); - + /** * Returns the URL to the library admin page - * + * * @return string URL to admin page */ public function getAdminUrl(); @@ -108,7 +108,7 @@ interface H5PFrameworkInterface { * @param string $defaultLibraryWhitelist */ public function getWhitelist($isLibrary, $defaultContentWhitelist, $defaultLibraryWhitelist); - + /** * Is the library a patched version of an existing library? * @@ -147,23 +147,23 @@ interface H5PFrameworkInterface { * Object holding the information that is to be stored */ public function saveLibraryData(&$libraryData, $new = TRUE); - + /** * Insert new content. - * + * * @param object $content * @param int $contentMainId */ public function insertContent($content, $contentMainId = NULL); - + /** * Update old content. - * + * * @param object $content * @param int $contentMainId */ public function updateContent($content, $contentMainId = NULL); - + /** * Save what libraries a library is dependending on * @@ -219,11 +219,11 @@ interface H5PFrameworkInterface { public function saveLibraryUsage($contentId, $librariesInUse); /** - * Get number of content/nodes using a library, and the number of + * Get number of content/nodes using a library, and the number of * dependencies to other libraries - * + * * @param int $library_id - * @return array The array contains two elements, keyed by 'content' and 'libraries'. + * @return array The array contains two elements, keyed by 'content' and 'libraries'. * Each element contains a number */ public function getLibraryUsage($libraryId); @@ -249,7 +249,7 @@ interface H5PFrameworkInterface { * @return string semantics. */ public function loadLibrarySemantics($name, $majorVersion, $minorVersion); - + /** * Makes it possible to alter the semantics, adding custom fields, etc. * @@ -267,21 +267,21 @@ interface H5PFrameworkInterface { * Library Id */ public function deleteLibraryDependencies($libraryId); - + /** * Delete a library from database and file system - * + * * @param mixed $library Library */ public function deleteLibrary($library); - + /** * Load content. * * @return object Content, null if not found. */ public function loadContent($id); - + /** * Load dependencies for the given content of the given type. * @@ -290,49 +290,55 @@ interface H5PFrameworkInterface { * @return array */ public function loadContentDependencies($id, $type = NULL); - + /** - * Get data from cache. - * - * @param string $group - * @param string $key + * Get stored setting. + * + * @param string $name Identifier + * @param string $default Optional + * @return mixed data */ - public function cacheGet($group, $key); - + public function getOption($name, $default = NULL); + /** - * Store data in cache. - * - * @param string $group - * @param string $key - * @param mixed $data + * Stores the given setting. + * For example when did we last check h5p.org for updates to our libraries. + * + * @param string $name Identifier + * @param mixed $value Data */ - public function cacheSet($group, $key, $data); - + public function setOption($name, $value); + /** - * Delete data from cache. - * - * @param string $group - * @param string $key + * This will set the filtered parameters for the given content. + * + * @param int $content_id + * @param string $parameters filtered */ - public function cacheDel($group, $key = NULL); - + public function setFilteredParameters($content_id, $parameters = ''); + /** - * Will invalidate the cache for the content that uses the specified library. - * This means that the content dependencies has to be rebuilt, and the parameters refiltered. - * + * Will clear filtered params for all the content that uses the specified + * library. This means that the content dependencies will have to be rebuilt, + * and the parameters refiltered. + * * @param int $library_id */ - public function invalidateContentCache($library_id); - + public function clearFilteredParameters($library_id); + /** - * Get content without cache. + * Get number of contents that has to get their content dependencies rebuilt + * and parameters refiltered. + * + * @return int */ - public function getNotCached(); - + public function getNumNotFiltered(); + /** * Get number of contents using library as main library. - * + * * @param int $library_id + * @return int */ public function getNumContent($library_id); } @@ -447,14 +453,14 @@ class H5PValidator { // Extract and then remove the package file. $zip = new ZipArchive; - + // Only allow files with the .h5p extension: if (strtolower(substr($tmpPath, -3)) !== 'h5p') { $this->h5pF->setErrorMessage($this->h5pF->t('The file you uploaded is not a valid HTML5 Package (It does not have the .h5p file extension)')); H5PCore::deleteFileTree($tmpDir); return; } - + if ($zip->open($tmpPath) === true) { $zip->extractTo($tmpDir); $zip->close(); @@ -482,7 +488,7 @@ class H5PValidator { if ($skipContent === TRUE) { continue; } - + $mainH5pData = $this->getJsonData($filePath); if ($mainH5pData === FALSE) { $valid = FALSE; @@ -540,7 +546,7 @@ class H5PValidator { } $libraryH5PData = $this->getLibraryData($file, $filePath, $tmpDir); - + if ($libraryH5PData !== FALSE) { // Library's directory name must be: // - @@ -576,7 +582,7 @@ class H5PValidator { } if ($valid) { if ($upgradeOnly) { - // When upgrading, we opnly add allready installed libraries, + // When upgrading, we opnly add allready installed libraries, // and new dependent libraries $upgrades = array(); foreach ($libraries as &$library) { @@ -594,25 +600,25 @@ class H5PValidator { } } } - + $libraries = $upgrades; } - + $this->h5pC->librariesJsonData = $libraries; - + if ($skipContent === FALSE) { $this->h5pC->mainJsonData = $mainH5pData; $this->h5pC->contentJsonData = $contentJsonData; $libraries['mainH5pData'] = $mainH5pData; // Check for the dependencies in h5p.json as well as in the libraries } - + $missingLibraries = $this->getMissingLibraries($libraries); foreach ($missingLibraries as $missing) { if ($this->h5pF->getLibraryId($missing['machineName'], $missing['majorVersion'], $missing['minorVersion'])) { unset($missingLibraries[H5PCore::libraryToString($missing)]); } } - + if (!empty($missingLibraries)) { foreach ($missingLibraries as $library) { $this->h5pF->setErrorMessage($this->h5pF->t('Missing required library @library', array('@library' => H5PCore::libraryToString($library)))); @@ -1021,7 +1027,7 @@ class H5PStorage { public $h5pF; public $h5pC; - + public $contentId = NULL; // Quick fix so WP can get ID of new content. /** @@ -1052,11 +1058,11 @@ class H5PStorage { $library_saved = FALSE; $upgradedLibsCount = 0; $mayUpdateLibraries = $this->h5pF->mayUpdateLibraries(); - + foreach ($this->h5pC->librariesJsonData as &$library) { $libraryId = $this->h5pF->getLibraryId($library['machineName'], $library['majorVersion'], $library['minorVersion']); $library['saveDependencies'] = TRUE; - + if (!$libraryId) { $new = TRUE; } @@ -1101,21 +1107,21 @@ class H5PStorage { if (isset($library['editorDependencies'])) { $this->h5pF->saveLibraryDependencies($library['libraryId'], $library['editorDependencies'], 'editor'); } - + // Make sure libraries dependencies, parameter filtering and export files gets regenerated for all content who uses this library. - $this->h5pF->invalidateContentCache($library['libraryId']); - + $this->h5pF->clearFilteredParameters($library['libraryId']); + $upgradedLibsCount++; } } - + if (!$skipContent) { $current_path = $this->h5pF->getUploadedH5pFolderPath() . DIRECTORY_SEPARATOR . 'content'; - + // Find out which libraries are used by this package/content $librariesInUse = array(); $this->h5pC->findLibraryDependencies($librariesInUse, $this->h5pC->mainJsonData); - + // Save content if ($content === NULL) { $content = array(); @@ -1132,19 +1138,19 @@ class H5PStorage { if (!is_dir($contents_path)) { mkdir($contents_path, 0777, true); } - + // Move the content folder $destination_path = $contents_path . DIRECTORY_SEPARATOR . $contentId; @rename($current_path, $destination_path); - + // Save the content library dependencies $this->h5pF->saveLibraryUsage($contentId, $librariesInUse); H5PCore::deleteFileTree($this->h5pF->getUploadedH5pFolderPath()); } - + // Update supported library list if neccessary: $this->h5pC->validateLibrarySupport(TRUE); - + if ($upgradeOnly) { // TODO - support translation $this->h5pF->setInfoMessage($this->h5pF->t('@num libraries were upgraded!', array('@num' => $upgradedLibsCount))); @@ -1152,7 +1158,7 @@ class H5PStorage { return $library_saved; } - + /** * Delete an H5P package * @@ -1221,7 +1227,7 @@ Class H5PExport { $this->h5pF = $H5PFramework; $this->h5pC = $H5PCore; } - + /** * Return path to h5p package. * @@ -1234,7 +1240,7 @@ Class H5PExport { $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); @@ -1251,8 +1257,8 @@ Class H5PExport { // Build h5p.json $h5pJson = array ( 'title' => $content['title'], - // TODO - stop using 'und', this is not the preferred way. - // Either remove language from the json if not existing, or use "language": null + // TODO - stop using 'und', this is not the preferred way. + // Either remove language from the json if not existing, or use "language": null 'language' => isset($content['language']) ? $content['language'] : 'und', 'mainLibrary' => $content['library']['name'], 'embedTypes' => $embedTypes, @@ -1261,7 +1267,7 @@ Class H5PExport { // 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']; @@ -1269,7 +1275,7 @@ Class H5PExport { // Do not add editor dependencies to h5p json. if ($dependency['type'] === 'editor') { - continue; + continue; } $h5pJson[$dependency['type'] . 'Dependencies'][] = array( @@ -1333,7 +1339,7 @@ Class H5PExport { */ private function addEditorLibraries($libraries, $editorLibraries) { foreach ($editorLibraries as $editorLibrary) { - $libraries[$editorLibrary['machineName']] = $editorLibrary; + $libraries[$editorLibrary['machineName']] = $editorLibrary; } return $libraries; } @@ -1378,12 +1384,12 @@ class H5PCore { */ 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) { $this->h5pD = new H5PDevelopment($this->h5pF, $path, $language); } @@ -1391,7 +1397,7 @@ class H5PCore { /** * Save content and clear cache. - * + * * @param array $content * @return int Content ID */ @@ -1400,17 +1406,12 @@ class H5PCore { $this->h5pF->updateContent($content, $contentMainId); } else { - $content['id'] = $this->h5pF->insertContent($content, $contentMainId); + $content['id'] = $this->h5pF->insertContent($content, $contentMainId); } - - if (!isset($content['filtered'])) { - // TODO: Add filtered to all impl. and remove - $this->h5pF->cacheDel('parameters', $content['id']); - } - + return $content['id']; } - + /** * Load content. * @@ -1419,7 +1420,7 @@ class H5PCore { */ public function loadContent($id) { $content = $this->h5pF->loadContent($id); - + if ($content !== NULL) { $content['library'] = array( 'id' => $content['libraryId'], @@ -1430,7 +1431,7 @@ class H5PCore { 'fullscreen' => $content['libraryFullscreen'], ); unset($content['libraryId'], $content['libraryName'], $content['libraryEmbedTypes'], $content['libraryFullscreen']); - + // // TODO: Move to filterParameters? // if ($this->development_mode & H5PDevelopment::MODE_CONTENT) { // // TODO: Remove Drupal specific stuff @@ -1444,29 +1445,21 @@ class H5PCore { // } // } } - + return $content; } - + /** * Filter content run parameters, rebuild content dependecy cache and export file. - * + * * @param Object $content * @return Object NULL on failure. */ public function filterParameters($content) { - if (isset($content['filtered'])) { - $params = ($content['filtered'] === '' ? NULL : $content['filtered']); - } - else { - // TODO: Add filtered to all impl. and remove - $params = $this->h5pF->cacheGet('parameters', $content['id']); + if (isset($content['filtered']) && $content['filtered'] !== '') { + return $content['filtered']; } - if ($params !== NULL) { - return $params; - } - // Validate and filter against main library semantics. $validator = new H5PContentValidator($this->h5pF, $this); $params = (object) array( @@ -1476,25 +1469,25 @@ class H5PCore { $validator->validateLibrary($params, (object) array('options' => array($params->library))); $params = json_encode($params->params); - + // Update content dependencies. $content['dependencies'] = $validator->getDependencies(); $this->h5pF->deleteLibraryUsage($content['id']); $this->h5pF->saveLibraryUsage($content['id'], $content['dependencies']); - + 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); + $this->h5pF->setFilteredParameters($content['id'], $params); return $params; } - + /** * Find the files required for this content to work. * @@ -1503,10 +1496,10 @@ class H5PCore { */ public function loadContentDependencies($id, $type = NULL) { $dependencies = $this->h5pF->loadContentDependencies($id, $type); - + if ($this->development_mode & H5PDevelopment::MODE_LIBRARY) { $developmentLibraries = $this->h5pD->getLibraries(); - + foreach ($dependencies as $key => $dependency) { $libraryString = H5PCore::libraryToString($dependency); if (isset($developmentLibraries[$libraryString])) { @@ -1515,13 +1508,13 @@ class H5PCore { } } } - + return $dependencies; } - + /** * Get all dependency assets of the given type - * + * * @param array $dependency * @param string $type * @param array $assets @@ -1531,12 +1524,12 @@ class H5PCore { if (empty($dependency[$type]) || $dependency[$type][0] === '') { return; } - + // Check if we should skip CSS. if ($type === 'preloadedCss' && (isset($dependency['dropCss']) && $dependency['dropCss'] === '1')) { return; } - + foreach ($dependency[$type] as $file) { $assets[] = (object) array( 'path' => $dependency['path'] . '/' . trim(is_array($file) ? $file['path'] : $file), @@ -1544,23 +1537,23 @@ class H5PCore { ); } } - + /** * Combines path with cache buster / version. - * + * * @param array $assets * @return array */ public function getAssetsUrls($assets) { $urls = array(); - + foreach ($assets as $asset) { $urls[] = $asset->path . $asset->version; } - + return $urls; } - + /** * Return file paths for all dependecies files. * @@ -1578,14 +1571,14 @@ class H5PCore { $dependency['preloadedJs'] = explode(',', $dependency['preloadedJs']); $dependency['preloadedCss'] = explode(',', $dependency['preloadedCss']); } - + $dependency['version'] = "?ver={$dependency['majorVersion']}.{$dependency['minorVersion']}.{$dependency['patchVersion']}"; $this->getDependencyAssets($dependency, 'preloadedJs', $files['scripts']); $this->getDependencyAssets($dependency, 'preloadedCss', $files['styles']); } return $files; } - + /** * Load library semantics. * @@ -1597,20 +1590,20 @@ class H5PCore { // Try to load from dev lib $semantics = $this->h5pD->getSemantics($name, $majorVersion, $minorVersion); } - + if ($semantics === NULL) { // Try to load from DB. $semantics = $this->h5pF->loadLibrarySemantics($name, $majorVersion, $minorVersion); } - + if ($semantics !== NULL) { $semantics = json_decode($semantics); $this->h5pF->alterLibrarySemantics($semantics, $name, $majorVersion, $minorVersion); } - + return $semantics; } - + /** * Load library. * @@ -1625,31 +1618,31 @@ class H5PCore { $library['semantics'] = $this->h5pD->getSemantics($name, $majorVersion, $minorVersion); } } - + if ($library === NULL) { // Try to load from DB. $library = $this->h5pF->loadLibrary($name, $majorVersion, $minorVersion); } - + return $library; } - + /** * Deletes a library - * + * * @param unknown $libraryId */ public function deleteLibrary($libraryId) { $this->h5pF->deleteLibrary($libraryId); - + // Force update of unsupported libraries list: $this->validateLibrarySupport(TRUE); } - + /** - * Recursive. Goes through the dependency tree for the given library and + * Recursive. Goes through the dependency tree for the given library and * adds all the dependencies to the given array in a flat format. - * + * * @param array $librariesUsed Flat list of all dependencies. * @param array $library To find all dependencies for. * @param bool $editor Used interally to force all preloaded sub dependencies of an editor dependecy to be editor dependencies. @@ -1660,18 +1653,18 @@ class H5PCore { if (!isset($library[$property])) { continue; // Skip, no such dependencies. } - + if ($type === 'preloaded' && $editor === TRUE) { // All preloaded dependencies of an editor library is set to editor. $type = 'editor'; } - + foreach ($library[$property] as $dependency) { $dependencyKey = $type . '-' . $dependency['machineName']; if (isset($dependencies[$dependencyKey]) === TRUE) { continue; // Skip, already have this. } - + $dependencyLibrary = $this->loadLibrary($dependency['machineName'], $dependency['majorVersion'], $dependency['minorVersion']); if ($dependencyLibrary) { $dependencies[$dependencyKey] = array( @@ -1800,7 +1793,7 @@ class H5PCore { } return FALSE; } - + /** * Determine the correct embed type to use. * @@ -1809,32 +1802,32 @@ class H5PCore { public static function determineEmbedType($contentEmbedType, $libraryEmbedTypes) { // Detect content embed type $embedType = strpos(strtolower($contentEmbedType), 'div') !== FALSE ? 'div' : 'iframe'; - + if ($libraryEmbedTypes !== NULL && $libraryEmbedTypes !== '') { // Check that embed type is available for library $embedTypes = strtolower($libraryEmbedTypes); if (strpos($embedTypes, $embedType) === FALSE) { // Not available, pick default. $embedType = strpos($embedTypes, 'div') !== FALSE ? 'div' : 'iframe'; - } + } } - + return $embedType; } - + /** * Get the absolute version for the library as a human readable string. - * + * * @param object $library * @return string */ public static function libraryVersion($library) { return $library->major_version . '.' . $library->minor_version . '.' . $library->patch_version; } - + /** * Detemine which versions content with the given library can be upgraded to. - * + * * @param object $library * @param array $versions * @return array @@ -1850,13 +1843,13 @@ class H5PCore { return $upgrades; } - + /** * Converts all the properties of the given object or array from * snake_case to camelCase. Useful after fetching data from the database. - * + * * Note that some databases does not support camelCase. - * + * * @param mixed $arr input * @param boolean $obj return object * @return mixed object or array @@ -1875,36 +1868,36 @@ class H5PCore { return $obj ? (object) $newArr : $newArr; } - + /** * Check if currently installed H5P libraries are supported by * the current versjon of core. Which versions of which libraries are supported is * defined in the library-support.json file. * - * @param boolean If TRUE, unsupported libraries list are rebuilt. If FALSE, list is + * @param boolean If TRUE, unsupported libraries list are rebuilt. If FALSE, list is * rebuilt only if non-existing */ public function validateLibrarySupport($force = false) { if (!($this->h5pF->getUnsupportedLibraries() === NULL || $force)) { return; } - + $minVersions = $this->getMinimumVersionsSupported(realpath(dirname(__FILE__)) . '/library-support.json'); if ($minVersions === NULL) { return; } - + // Get all libraries installed, check if any of them is not supported: $libraries = $this->h5pF->loadLibraries(); $unsupportedLibraries = array(); - + // Iterate over all installed libraries foreach ($libraries as $library_name => $versions) { if (!isset($minVersions[$library_name])) { continue; } $min = $minVersions[$library_name]; - + // For each version of this library, check if it is supported foreach ($versions as $library) { if (!$this->isLibraryVersionSupported($library, $min->versions)) { @@ -1920,23 +1913,23 @@ class H5PCore { ); } } - + $this->h5pF->setUnsupportedLibraries(empty($unsupportedLibraries) ? NULL : $unsupportedLibraries); } } - + /** * Returns a list of the minimum version of libraries that are supported. * This is needed because some old libraries are no longer supported by core. - * + * * TODO: Make it possible for the systems to cache this list between requests. - * + * * @param string $path to json file * @return array indexed using library names */ public function getMinimumVersionsSupported($path) { $minSupported = array(); - + // Get list of minimum version for libraries. Some old libraries are no longer supported. $libraries = file_get_contents($path); if ($libraries !== FALSE) { @@ -1950,13 +1943,13 @@ class H5PCore { } } } - + return empty($minSupported) ? NULL : $minSupported; } - + /** * Check if a specific version of a library is supported - * + * * @param object library * @param array An array containing versions * @return boolean TRUE if supported, otherwise FALSE @@ -1969,37 +1962,37 @@ class H5PCore { // --- minor is higher than any minimumversion for a given major // --- major and minor equals and patch is >= supported $major_supported |= ($library->major_version > $minimumVersion->major); - + if ($library->major_version == $minimumVersion->major) { $minor_supported |= ($library->minor_version > $minimumVersion->minor); } - + if ($library->major_version == $minimumVersion->major && $library->minor_version == $minimumVersion->minor) { $patch_supported |= ($library->patch_version >= $minimumVersion->patch); } } - + return ($patch_supported || $minor_supported || $major_supported); } - + /** - * Helper function for creating markup for the unsupported libraries list - * + * Helper function for creating markup for the unsupported libraries list + * * TODO: Make help text translatable - * + * * @return string Html * */ public function createMarkupForUnsupportedLibraryList($libraries) { $html = '
The following versions of H5P libraries are not supported anymore:
    '; - + foreach ($libraries as $library) { $downloadUrl = $library['downloadUrl']; $libraryName = $library['name']; $currentVersion = $library['currentVersion']['major'] . '.' . $library['currentVersion']['minor'] .'.' . $library['currentVersion']['patch']; $html .= "
  • $libraryName ($currentVersion)
  • "; } - + $html .= '

These libraries may cause problems on this site. See here for more info
'; return $html; } @@ -2038,24 +2031,24 @@ class H5PContentValidator { 'select' => 'validateSelect', 'library' => 'validateLibrary', ); - + // Keep track of the libraries we load to avoid loading it multiple times. $this->libraries = array(); // TODO: Should this possible be done in core's loadLibrary? This might be done multiple places. - + // Keep track of all dependencies for the given content. $this->dependencies = array(); } /** * Get the flat dependecy tree. - * + * * @return array */ public function getDependencies() { return $this->dependencies; } - + /** * Validate given text value against text semantics. */ @@ -2280,19 +2273,19 @@ class H5PContentValidator { $validkeys = array_merge($validkeys, $semantics->extraAttributes); // TODO: Validate extraAttributes } $this->filterParams($file, $validkeys); - + if (isset($file->width)) { $file->width = intval($file->width); } - + if (isset($file->height)) { $file->height = intval($file->height); } - + if (isset($file->codecs)) { $file->codecs = htmlspecialchars($file->codecs, ENT_QUOTES, 'UTF-8', FALSE); } - + if (isset($file->quality)) { if (!is_object($file->quality) || !isset($file->quality->level) || !isset($file->quality->label)) { unset($file->quality); @@ -2303,7 +2296,7 @@ class H5PContentValidator { $file->quality->label = htmlspecialchars($file->quality->label, ENT_QUOTES, 'UTF-8', FALSE); } } - + if (isset($file->copyright)) { $this->validateGroup($file->copyright, H5PContentValidator::getCopyrightSemantics()); } @@ -2406,7 +2399,7 @@ class H5PContentValidator { $value = new stdClass(); return; } - + if (!isset($this->libraries[$value->library])) { $libspec = H5PCore::libraryFromString($value->library); $library = $this->h5pC->loadLibrary($libspec['machineName'], $libspec['majorVersion'], $libspec['minorVersion']); @@ -2700,7 +2693,7 @@ class H5PContentValidator { /** * Processes an HTML attribute value and strips dangerous protocols from URLs. - * + * * @param $string * The string with the attribute value. * @param $decode @@ -2773,10 +2766,10 @@ class H5PContentValidator { return $uri; } - + public static function getCopyrightSemantics() { static $semantics; - + if ($semantics === NULL) { $semantics = (object) array( 'name' => 'copyright', @@ -2874,7 +2867,7 @@ class H5PContentValidator { ) ); } - + return $semantics; } } From 2011e856c04101073dfb9f3ab46cf8659012cd35 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Mon, 29 Sep 2014 17:13:47 +0200 Subject: [PATCH 03/19] Improved error handling when upgrading content. --- js/h5p-content-upgrade.js | 161 ++++++++++++++++++++------------------ 1 file changed, 86 insertions(+), 75 deletions(-) diff --git a/js/h5p-content-upgrade.js b/js/h5p-content-upgrade.js index bbea037..5be3428 100644 --- a/js/h5p-content-upgrade.js +++ b/js/h5p-content-upgrade.js @@ -2,18 +2,18 @@ var H5PUpgrades = H5PUpgrades || {}; (function ($) { var info, $container, librariesCache = {}; - + // Initialize $(document).ready(function () { // Get library info info = H5PIntegration.getLibraryInfo(); - + // Get and reset container $container = $('#h5p-admin-container').html('

' + info.message + '

'); - + // Make it possible to select version var $version = $(getVersionSelect(info.versions)).appendTo($container); - + // Add "go" button $(''); H5PLibraryDetails.$next = $(''); - + H5PLibraryDetails.$previous.on('click', function () { if(H5PLibraryDetails.$previous.hasClass('disabled')) { return; } - + H5PLibraryDetails.currentPage--; H5PLibraryDetails.updatePager(); H5PLibraryDetails.createContentTable(); }); - + H5PLibraryDetails.$next.on('click', function () { if(H5PLibraryDetails.$next.hasClass('disabled')) { return; } - + H5PLibraryDetails.currentPage++; H5PLibraryDetails.updatePager(); H5PLibraryDetails.createContentTable(); }); - + // This is the Page x of y widget: H5PLibraryDetails.$pagerInfo = $(''); - + H5PLibraryDetails.$pager = $('
').append(H5PLibraryDetails.$previous, H5PLibraryDetails.$pagerInfo, H5PLibraryDetails.$next); H5PLibraryDetails.$content.append(H5PLibraryDetails.$pager); - + H5PLibraryDetails.$pagerInfo.on('click', function () { var width = H5PLibraryDetails.$pagerInfo.innerWidth(); H5PLibraryDetails.$pagerInfo.hide(); @@ -134,24 +134,24 @@ var H5PLibraryDetails= H5PLibraryDetails || {}; var pageNumerUpdated = function() { var newPageNum = $gotoInput.val()-1; var intRegex = /^\d+$/; - + $goto.remove(); H5PLibraryDetails.$pagerInfo.css({display: 'inline-block'}); - + // Check if input value is valid, and that it has actually changed if(!(intRegex.test(newPageNum) && newPageNum >= 0 && newPageNum < H5PLibraryDetails.getNumPages() && newPageNum != H5PLibraryDetails.currentPage)) { return; } - + H5PLibraryDetails.currentPage = newPageNum; H5PLibraryDetails.updatePager(); H5PLibraryDetails.createContentTable(); }; - + // We create an input box where the user may type in the page number // he wants to be displayed. // Reson for doing this is when user has ten-thousands of elements in list, - // this is the easiest way of getting to a specified page + // this is the easiest way of getting to a specified page var $gotoInput = $('', { type: 'number', min : 1, @@ -166,30 +166,30 @@ var H5PLibraryDetails= H5PLibraryDetails || {}; } } }).css({width: width}); - var $goto = $('', { + var $goto = $('', { 'class': 'h5p-pager-goto' }).css({width: width}).append($gotoInput).insertAfter(H5PLibraryDetails.$pagerInfo); - + $gotoInput.focus(); }); - + H5PLibraryDetails.updatePager(); } }; - + /** * Calculates number of pages */ H5PLibraryDetails.getNumPages = function () { return Math.ceil(H5PLibraryDetails.currentContent.length / H5PLibraryDetails.PAGER_SIZE); }; - + /** * Update the pager text, and enables/disables the next and previous buttons as needed */ H5PLibraryDetails.updatePager = function () { H5PLibraryDetails.$pagerInfo.css({display: 'inline-block'}); - + if(H5PLibraryDetails.getNumPages() > 0) { var message = H5PUtils.translateReplace(H5PLibraryDetails.library.translations.pageXOfY, { '$x': (H5PLibraryDetails.currentPage+1), @@ -200,26 +200,26 @@ var H5PLibraryDetails= H5PLibraryDetails || {}; else { H5PLibraryDetails.$pagerInfo.html(''); } - + H5PLibraryDetails.$previous.toggleClass('disabled', H5PLibraryDetails.currentPage <= 0); H5PLibraryDetails.$next.toggleClass('disabled', H5PLibraryDetails.currentContent.length < (H5PLibraryDetails.currentPage+1)*H5PLibraryDetails.PAGER_SIZE); }; - + /** * Creates the search element */ H5PLibraryDetails.createSearchElement = function () { - + H5PLibraryDetails.$search = $(''); - + var performSeach = function () { var searchString = $('.h5p-content-search > input').val(); - + // If search string same as previous, just do nothing if(H5PLibraryDetails.currentFilter === searchString) { return; } - + if (searchString.trim().length === 0) { // If empty search, use the complete list H5PLibraryDetails.currentContent = H5PLibraryDetails.library.content; @@ -230,7 +230,7 @@ var H5PLibraryDetails= H5PLibraryDetails || {}; } else { var listToFilter = H5PLibraryDetails.library.content; - + // Check if we can filter the already filtered results (for performance) if(searchString.length > 1 && H5PLibraryDetails.currentFilter === searchString.substr(0, H5PLibraryDetails.currentFilter.length)) { listToFilter = H5PLibraryDetails.currentContent; @@ -239,47 +239,47 @@ var H5PLibraryDetails= H5PLibraryDetails || {}; return content.title && content.title.match(new RegExp(searchString, 'i')); }); } - + H5PLibraryDetails.currentFilter = searchString; // Cache the current result H5PLibraryDetails.filterCache[searchString] = H5PLibraryDetails.currentContent; H5PLibraryDetails.currentPage = 0; H5PLibraryDetails.createContentTable(); - + // Display search results: if (H5PLibraryDetails.$searchResults) { H5PLibraryDetails.$searchResults.remove(); } if (searchString.trim().length > 0) { - H5PLibraryDetails.$searchResults = $('' + H5PLibraryDetails.currentContent.length + ' hits on ' + H5PLibraryDetails.currentFilter + ''); + H5PLibraryDetails.$searchResults = $('' + H5PLibraryDetails.currentContent.length + ' hits on ' + H5PLibraryDetails.currentFilter + ''); H5PLibraryDetails.$search.append(H5PLibraryDetails.$searchResults); } H5PLibraryDetails.updatePager(); }; - - var inputTimer = undefined; + + var inputTimer; $('input', H5PLibraryDetails.$search).on('change keypress paste input', function () { // Here we start the filtering // We wait at least 500 ms after last input to perform search if(inputTimer) { clearTimeout(inputTimer); } - + inputTimer = setTimeout( function () { performSeach(); }, 500); }); - + H5PLibraryDetails.$content.append(H5PLibraryDetails.$search); }; - + /** * Creates the page size selector */ H5PLibraryDetails.createPageSizeSelector = function () { H5PLibraryDetails.$search.append('
' + H5PLibraryDetails.library.translations.pageSizeSelectorLabel + ':102050100200
'); - - // Listen to clicks on the page size selector: + + // Listen to clicks on the page size selector: $('.h5p-admin-pager-size-selector > span', H5PLibraryDetails.$search).on('click', function () { H5PLibraryDetails.PAGER_SIZE = $(this).data('page-size'); $('.h5p-admin-pager-size-selector > span', H5PLibraryDetails.$search).removeClass('selected'); @@ -289,7 +289,7 @@ var H5PLibraryDetails= H5PLibraryDetails || {}; H5PLibraryDetails.updatePager(); }); }; - + // Initialize me: $(document).ready(function () { if (!H5PLibraryDetails.initialized) { @@ -297,5 +297,5 @@ var H5PLibraryDetails= H5PLibraryDetails || {}; H5PLibraryDetails.init(); } }); - -})(H5P.jQuery); \ No newline at end of file + +})(H5P.jQuery); diff --git a/js/h5p-library-list.js b/js/h5p-library-list.js index a1e517f..fa37d3a 100644 --- a/js/h5p-library-list.js +++ b/js/h5p-library-list.js @@ -1,3 +1,4 @@ +/*jshint multistr: true */ var H5PLibraryList= H5PLibraryList || {}; (function ($) { @@ -7,19 +8,19 @@ var H5PLibraryList= H5PLibraryList || {}; */ H5PLibraryList.init = function () { var $adminContainer = H5PIntegration.getAdminContainer(); - + var libraryList = H5PIntegration.getLibraryList(); if (libraryList.notCached) { $adminContainer.append(H5PUtils.getRebuildCache(libraryList.notCached)); } - + // Create library list $adminContainer.append(H5PLibraryList.createLibraryList(H5PIntegration.getLibraryList())); }; - + /** * Create the library list - * + * * @param {object} libraries List of libraries and headers */ H5PLibraryList.createLibraryList = function (libraries) { @@ -27,11 +28,11 @@ var H5PLibraryList= H5PLibraryList || {}; if(libraries.listData === undefined || libraries.listData.length === 0) { return $('
' + t.NA + '
'); } - + // Create table var $table = H5PUtils.createTable(libraries.listHeaders); $table.addClass('libraries'); - + // Add libraries $.each (libraries.listData, function (index, library) { var $libraryRow = H5PUtils.createTableRow([ @@ -54,7 +55,7 @@ var H5PLibraryList= H5PLibraryList || {}; \ ' ]); - + var hasContent = !(library.numContent === '' || library.numContent === 0); if (library.upgradeUrl === null) { $('.h5p-admin-upgrade-library', $libraryRow).remove(); @@ -67,12 +68,12 @@ var H5PLibraryList= H5PLibraryList || {}; window.location.href = library.upgradeUrl; }); } - + // Open details view when clicked $('.h5p-admin-view-library', $libraryRow).on('click', function () { window.location.href = library.detailsUrl; }); - + var $deleteButton = $('.h5p-admin-delete-library', $libraryRow); if (libraries.notCached !== undefined || hasContent || (library.numContentDependencies !== '' && library.numContentDependencies !== 0) || (library.numLibraryDependencies !== '' && library.numLibraryDependencies !== 0)) { // Disabled delete if content. @@ -87,11 +88,11 @@ var H5PLibraryList= H5PLibraryList || {}; $table.append($libraryRow); }); - + return $table; }; - - + + // Initialize me: $(document).ready(function () { if (!H5PLibraryList.initialized) { @@ -99,5 +100,5 @@ var H5PLibraryList= H5PLibraryList || {}; H5PLibraryList.init(); } }); - + })(H5P.jQuery); diff --git a/js/h5p-utils.js b/js/h5p-utils.js index 26ffa1d..0db73a7 100644 --- a/js/h5p-utils.js +++ b/js/h5p-utils.js @@ -3,7 +3,7 @@ var H5PUtils = H5PUtils || {}; (function ($) { /** * Generic function for creating a table including the headers - * + * * @param {array} headers List of headers */ H5PUtils.createTable = function (headers) { @@ -12,62 +12,62 @@ var H5PUtils = H5PUtils || {}; if(headers) { var $thead = $(''); var $tr = $(''); - + $.each(headers, function (index, value) { if (!(value instanceof Object)) { value = { html: value }; } - + $('', value).appendTo($tr); }); - + $table.append($thead.append($tr)); } - + return $table; }; - + /** * Generic function for creating a table row - * + * * @param {array} rows Value list. Object name is used as class name in */ H5PUtils.createTableRow = function (rows) { var $tr = $(''); - + $.each(rows, function (index, value) { if (!(value instanceof Object)) { value = { html: value }; } - + $('', value).appendTo($tr); }); - + return $tr; }; - + /** * Generic function for creating a field containing label and value - * - * @param {string} label The label displayed in front of the value + * + * @param {string} label The label displayed in front of the value * @param {string} value The value */ H5PUtils.createLabeledField = function (label, value) { var $field = $('
'); - + $field.append('
' + label + '
'); $field.append('
' + value + '
'); - + return $field; }; /** * Replaces placeholder fields in translation strings - * + * * @param {string} template The translation template string in the following format: "$name is a $sex" * @param {array} replacors An js object with key and values. Eg: {'$name': 'Frode', '$sex': 'male'} */ @@ -77,10 +77,10 @@ var H5PUtils = H5PUtils || {}; }); return template; }; - + /** * Get throbber with given text. - * + * * @param {String} text * @returns {$} */ @@ -90,7 +90,7 @@ var H5PUtils = H5PUtils || {}; text: text }); }; - + /** * Makes it possbile to rebuild all content caches from admin UI. * @param {Object} notCached @@ -107,7 +107,7 @@ var H5PUtils = H5PUtils || {}; current++; if (current === parts.length) current = 0; }, 100); - + var $counter = $container.find('.progress'); var build = function () { $.post(notCached.url, function (left) { @@ -126,8 +126,8 @@ var H5PUtils = H5PUtils || {}; }; build(); }); - + return $container; }; - -})(H5P.jQuery); \ No newline at end of file + +})(H5P.jQuery); diff --git a/js/h5p.js b/js/h5p.js index 3b5db69..c858652 100644 --- a/js/h5p.js +++ b/js/h5p.js @@ -1,3 +1,4 @@ +/*jshint multistr: true */ // TODO: Should we split up the generic parts needed by the editor(and others), and the parts needed to "run" H5Ps? var H5P = H5P || {}; @@ -11,12 +12,12 @@ H5P.$window = H5P.jQuery(window); if (document.documentElement.requestFullScreen) { H5P.fullScreenBrowserPrefix = ''; } -else if (document.documentElement.webkitRequestFullScreen - && navigator.userAgent.indexOf('Android') === -1 // Skip Android +else if (document.documentElement.webkitRequestFullScreen && + navigator.userAgent.indexOf('Android') === -1 // Skip Android ) { H5P.safariBrowser = navigator.userAgent.match(/Version\/(\d)/); H5P.safariBrowser = (H5P.safariBrowser === null ? 0 : parseInt(H5P.safariBrowser[1])); - + // Do not allow fullscreen for safari < 7. if (H5P.safariBrowser === 0 || H5P.safariBrowser > 6) { H5P.fullScreenBrowserPrefix = 'webkit'; @@ -56,14 +57,14 @@ H5P.init = function () { // Create new instance. var instance = H5P.newRunnable(library, contentId, $container, true); - + // Check if we should add and display a fullscreen button for this H5P. if (contentData.fullScreen == 1) { H5P.jQuery('
').prependTo($container).children().click(function () { H5P.fullScreen($container, instance); }); - }; - + } + var $actions = H5P.jQuery('
    '); if (contentData.exportUrl !== '') { // Display export button @@ -87,7 +88,7 @@ H5P.init = function () { H5P.jQuery('
  • ').appendTo($actions); } $actions.insertAfter($container); - + if (H5P.isFramed) { // Make it possible to resize the iframe when the content changes size. This way we get no scrollbars. var iframe = window.parent.document.getElementById('h5p-iframe-' + contentId); @@ -95,10 +96,10 @@ H5P.init = function () { if (window.parent.H5P.isFullscreen) { return; // Skip if full screen. } - + // Retain parent size to avoid jumping/scrolling var parentHeight = iframe.parentElement.style.height; - iframe.parentElement.style.height = iframe.parentElement.clientHeight + 'px'; + iframe.parentElement.style.height = iframe.parentElement.clientHeight + 'px'; // Reset iframe height, in case content has shrinked. iframe.style.height = '1px'; @@ -109,7 +110,7 @@ H5P.init = function () { // Free parent iframe.parentElement.style.height = parentHeight; }; - + var resizeDelay; instance.$.on('resize', function () { // Use a delay to make sure iframe is resized to the correct size. @@ -119,7 +120,7 @@ H5P.init = function () { }, 1); }); } - + // Resize everything when window is resized. $window.resize(function () { if (window.parent.H5P.isFullscreen) { @@ -130,7 +131,7 @@ H5P.init = function () { instance.$.trigger('resize'); } }); - + // Resize content. instance.$.trigger('resize'); }); @@ -159,7 +160,7 @@ H5P.fullScreen = function ($element, instance, exitCallback, body) { window.parent.H5P.fullScreen($element, instance, exitCallback, H5P.$body.get()); return; } - + var $container = $element; var $classes, $iframe; if (body === undefined) { @@ -173,23 +174,23 @@ H5P.fullScreen = function ($element, instance, exitCallback, body) { $iframe = H5P.jQuery(iframeSelector); $element = $iframe.parent(); // Put iframe wrapper in fullscreen, not container. } - + $classes = $element.add(H5P.$body).add($classes); - + /** * Prepare for resize by setting the correct styles. - * + * * @param {String} classes CSS */ var before = function (classes) { $classes.addClass(classes); - + if ($iframe !== undefined) { // Set iframe to its default size(100%). $iframe.css('height', ''); } }; - + /** * Gets called when fullscreen mode has been entered. * Resizes and sets focus on content. @@ -199,17 +200,17 @@ H5P.fullScreen = function ($element, instance, exitCallback, body) { instance.$.trigger('resize'); instance.$.trigger('focus'); }; - + /** * Gets called when fullscreen mode has been exited. * Resizes and sets focus on content. - * + * * @param {String} classes CSS */ var done = function (classes) { H5P.isFullscreen = false; $classes.removeClass(classes); - + // Do not rely on window resize events. instance.$.trigger('resize'); instance.$.trigger('focus'); @@ -222,11 +223,11 @@ H5P.fullScreen = function ($element, instance, exitCallback, body) { H5P.isFullscreen = true; if (H5P.fullScreenBrowserPrefix === undefined) { // Create semi fullscreen. - + before('h5p-semi-fullscreen'); var $disable = H5P.jQuery('
    ').appendTo($container.find('.h5p-content-controls')); var keyup, disableSemiFullscreen = function () { - $disable.remove(); + $disable.remove(); $body.unbind('keyup', keyup); done('h5p-semi-fullscreen'); }; @@ -241,7 +242,7 @@ H5P.fullScreen = function ($element, instance, exitCallback, body) { } else { // Create real fullscreen. - + before('h5p-fullscreen'); var first, eventName = (H5P.fullScreenBrowserPrefix === 'ms' ? 'MSFullscreenChange' : H5P.fullScreenBrowserPrefix + 'fullscreenchange'); document.addEventListener(eventName, function () { @@ -251,7 +252,7 @@ H5P.fullScreen = function ($element, instance, exitCallback, body) { entered(); return; } - + // We are exiting fullscreen done('h5p-fullscreen'); document.removeEventListener(eventName, arguments.callee, false); @@ -282,26 +283,26 @@ H5P.getPath = function (path, contentId) { var hasProtocol = function (path) { return path.match(/^[a-z0-9]+:\/\//i); }; - + if (hasProtocol(path)) { return path; } - + if (contentId !== undefined) { prefix = H5PIntegration.getContentPath(contentId); } - else if (window['H5PEditor'] !== undefined) { + else if (window.H5PEditor !== undefined) { prefix = H5PEditor.filesPath; } else { return; } - + if (!hasProtocol(prefix)) { prefix = window.parent.location.protocol + "//" + window.parent.location.host + prefix; } - - return prefix + '/' + path; + + return prefix + '/' + path; }; /** @@ -318,7 +319,7 @@ H5P.getContentPath = function (contentId) { /** * Get library class constructor from H5P by classname. - * Note that this class will only work for resolve "H5P.NameWithoutDot". + * Note that this class will only work for resolve "H5P.NameWithoutDot". * Also check out: H5P.newRunnable * * Used from libraries to construct instances of other libraries' objects by name. @@ -336,34 +337,36 @@ H5P.classFromName = function (name) { * * TODO: Should we check if version matches the library? * TODO: Dynamically try to load libraries currently not loaded? That will require a callback. - * + * * @param {Object} library Library/action object form params. - * @param {Number} contentId + * @param {Number} contentId * @param {jQuery} $attachTo An optional element to attach the instance to. * @param {Boolean} skipResize Optionally skip triggering of the resize event after attaching. * @return {Object} Instance. */ H5P.newRunnable = function (library, contentId, $attachTo, skipResize) { + var nameSplit, versionSplit; try { - var nameSplit = library.library.split(' ', 2); - var versionSplit = nameSplit[1].split('.', 2); + nameSplit = library.library.split(' ', 2); + versionSplit = nameSplit[1].split('.', 2); } catch (err) { return H5P.error('Invalid library string: ' + library.library); } - + if ((library.params instanceof Object) !== true || (library.params instanceof Array) === true) { H5P.error('Invalid library params for: ' + library.library); return H5P.error(library.params); } - + // Find constructor function + var constructor; try { nameSplit = nameSplit[0].split('.'); - var constructor = window; - for (var i = 0; i < nameSplit.length; i++) { + constructor = window; + for (var i = 0; i < nameSplit.length; i++) { constructor = constructor[nameSplit[i]]; - }; + } if (typeof constructor !== 'function') { throw null; } @@ -371,16 +374,16 @@ H5P.newRunnable = function (library, contentId, $attachTo, skipResize) { catch (err) { return H5P.error('Unable to find constructor for: ' + library.library); } - + var instance = new constructor(library.params, contentId); - + if (instance.$ === undefined) { instance.$ = H5P.jQuery(instance); } - + if ($attachTo !== undefined) { instance.attach($attachTo); - + if (skipResize === undefined || !skipResize) { // Resize content. instance.$.trigger('resize'); @@ -417,18 +420,18 @@ H5P.t = function (key, vars, ns) { if (H5PIntegration.i18n[ns] === undefined) { return '[Missing translation namespace "' + ns + '"]'; } - + if (H5PIntegration.i18n[ns][key] === undefined) { return '[Missing translation "' + key + '" in "' + ns + '"]'; } var translation = H5PIntegration.i18n[ns][key]; - + if (vars !== undefined) { // Replace placeholder with variables. for (var placeholder in vars) { translation = translation.replace(placeholder, vars[placeholder]); - } + } } return translation; @@ -457,7 +460,7 @@ H5P.Dialog = function (name, title, content, $element) { }) .end() .end(); - + this.open = function () { setTimeout(function () { $dialog.addClass('h5p-open'); // Fade in @@ -465,7 +468,7 @@ H5P.Dialog = function (name, title, content, $element) { H5P.jQuery(self).trigger('dialog-opened', [$dialog]); }, 1); }; - + this.close = function () { $dialog.removeClass('h5p-open'); // Fade out setTimeout(function () { @@ -489,7 +492,7 @@ H5P.openCopyrightsDialog = function ($element, instance) { if (copyrights === undefined || copyrights === '') { copyrights = H5P.t('noCopyrights'); } - + var dialog = new H5P.Dialog('copyrights', H5P.t('copyrightInformation'), copyrights, $element); dialog.open(); }; @@ -503,12 +506,12 @@ H5P.openCopyrightsDialog = function ($element, instance) { */ H5P.openEmbedDialog = function ($element, embedCode) { var dialog = new H5P.Dialog('embed', H5P.t('embed'), '', $element); - + // Selecting embed code when dialog is opened H5P.jQuery(dialog).on('dialog-opened', function (event, $dialog) { $dialog.find('.h5p-embed-code-container').select(); }); - + dialog.open(); }; @@ -519,7 +522,7 @@ H5P.ContentCopyrights = function () { var label; var media = []; var content = []; - + /** * Public. Set label. * @@ -528,7 +531,7 @@ H5P.ContentCopyrights = function () { this.setLabel = function (newLabel) { label = newLabel; }; - + /** * Public. Add sub content. * @@ -539,7 +542,7 @@ H5P.ContentCopyrights = function () { media.push(newMedia); } }; - + /** * Public. Add sub content. * @@ -550,7 +553,7 @@ H5P.ContentCopyrights = function () { content.push(newContent); } }; - + /** * Public. Print content copyright. * @@ -558,28 +561,28 @@ H5P.ContentCopyrights = function () { */ this.toString = function () { var html = ''; - + // Add media rights for (var i = 0; i < media.length; i++) { html += media[i]; } - + // Add sub content rights - for (var i = 0; i < content.length; i++) { + for (i = 0; i < content.length; i++) { html += content[i]; } - - + + if (html !== '') { // Add a label to this info if (label !== undefined) { html = '

    ' + label + '

    ' + html; } - + // Add wrapper html = '
    ' + html + '
    '; } - + return html; }; }; @@ -595,35 +598,35 @@ H5P.ContentCopyrights = function () { H5P.MediaCopyright = function (copyright, labels, order, extraFields) { var thumbnail; var list = new H5P.DefinitionList(); - + /** * Private. Get translated label for field. * * @param {String} fieldName - * @return {String} + * @return {String} */ var getLabel = function (fieldName) { if (labels === undefined || labels[fieldName] === undefined) { return H5P.t(fieldName); } - + return labels[fieldName]; }; - + /** * Private. Get humanized value for field. * * @param {String} fieldName - * @return {String} + * @return {String} */ var humanizeValue = function (fieldName, value) { if (fieldName === 'license') { return H5P.copyrightLicenses[value]; } - + return value; }; - + if (copyright !== undefined) { // Add the extra fields for (var field in extraFields) { @@ -631,12 +634,12 @@ H5P.MediaCopyright = function (copyright, labels, order, extraFields) { copyright[field] = extraFields[field]; } } - + if (order === undefined) { // Set default order order = ['title', 'author', 'year', 'source', 'license']; } - + for (var i = 0; i < order.length; i++) { var fieldName = order[i]; if (copyright[fieldName] !== undefined) { @@ -644,7 +647,7 @@ H5P.MediaCopyright = function (copyright, labels, order, extraFields) { } } } - + /** * Public. Set thumbnail. * @@ -653,7 +656,7 @@ H5P.MediaCopyright = function (copyright, labels, order, extraFields) { this.setThumbnail = function (newThumbnail) { thumbnail = newThumbnail; }; - + /** * Public. Checks if this copyright is undisclosed. * I.e. only has the license attribute set, and it's undisclosed. @@ -669,7 +672,7 @@ H5P.MediaCopyright = function (copyright, labels, order, extraFields) { } return false; }; - + /** * Public. Print media copyright. * @@ -677,20 +680,20 @@ H5P.MediaCopyright = function (copyright, labels, order, extraFields) { */ this.toString = function () { var html = ''; - + if (this.undisclosed()) { return html; // No need to print a copyright with a single undisclosed license. } - + if (thumbnail !== undefined) { html += thumbnail; } html += list; - + if (html !== '') { html = ''; } - + return html; }; }; @@ -742,16 +745,16 @@ H5P.Field = function (label, value) { * Public. Get field label. * * @returns {String} - */ + */ this.getLabel = function () { return label; }; - + /** * Public. Get field value. * * @returns {String} - */ + */ this.getValue = function () { return value; }; @@ -762,7 +765,7 @@ H5P.Field = function (label, value) { */ H5P.DefinitionList = function () { var fields = []; - + /** * Public. Add field to list. * @@ -771,7 +774,7 @@ H5P.DefinitionList = function () { this.add = function (field) { fields.push(field); }; - + /** * Public. Get Number of fields. * @@ -780,7 +783,7 @@ H5P.DefinitionList = function () { this.size = function () { return fields.length; }; - + /** * Public. Get field at given index. * @@ -790,7 +793,7 @@ H5P.DefinitionList = function () { this.get = function (index) { return fields[index]; }; - + /** * Public. Print definition list. * @@ -879,7 +882,7 @@ H5P.getLibraryPath = function (library) { /** * Recursivly clone the given object. - * TODO: Consider if this needs to be in core. Doesn't $.extend do the same? + * TODO: Consider if this needs to be in core. Doesn't $.extend do the same? * * @param {object} object Object to clone. * @param {type} recursive From 43188bb7e0b3fe6ded3a3cd67ff9f45435d96bc7 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Tue, 7 Oct 2014 14:46:35 +0200 Subject: [PATCH 05/19] Specifying language code is now required when getting library translations. --- h5p-development.class.php | 57 +++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/h5p-development.class.php b/h5p-development.class.php index d599ca1..75e9daf 100644 --- a/h5p-development.class.php +++ b/h5p-development.class.php @@ -30,7 +30,7 @@ class H5PDevelopment { $this->findLibraries($filesPath . '/development'); } } - + /** * Get contents of file. * @@ -41,15 +41,15 @@ class H5PDevelopment { if (file_exists($file) === FALSE) { return NULL; } - + $contents = file_get_contents($file); if ($contents === FALSE) { return NULL; } - + return $contents; } - + /** * Scans development directory and find all libraries. * @@ -57,39 +57,39 @@ class H5PDevelopment { */ private function findLibraries($path) { $this->libraries = array(); - + if (is_dir($path) === FALSE) { - return; + return; } - + $contents = scandir($path); - + for ($i = 0, $s = count($contents); $i < $s; $i++) { if ($contents[$i]{0} === '.') { continue; // Skip hidden stuff. } - + $libraryPath = $path . '/' . $contents[$i]; $libraryJSON = $this->getFileContents($libraryPath . '/library.json'); if ($libraryJSON === NULL) { continue; // No JSON file, skip. } - + $library = json_decode($libraryJSON, TRUE); if ($library === FALSE) { continue; // Invalid JSON. } - + // TODO: Validate props? Not really needed, is it? this is a dev site. - + // Save/update library. $library['libraryId'] = $this->h5pF->getLibraryId($library['machineName'], $library['majorVersion'], $library['minorVersion']); $this->h5pF->saveLibraryData($library, $library['libraryId'] === FALSE); - + $library['path'] = $libraryPath; $this->libraries[H5PDevelopment::libraryToString($library['machineName'], $library['majorVersion'], $library['minorVersion'])] = $library; } - + // TODO: Should we remove libraries without files? Not really needed, but must be cleaned up some time, right? // Go trough libraries and insert dependencies. Missing deps. will just be ignored and not available. (I guess?!) @@ -106,17 +106,17 @@ class H5PDevelopment { } // TODO: Deps must be inserted into h5p_nodes_libraries as well... ? But only if they are used?! } - + /** * @return array Libraris in development folder. */ public function getLibraries() { return $this->libraries; } - + /** * Get library - * + * * @param string $name of the library. * @param int $majorVersion of the library. * @param int $minorVersion of the library. @@ -126,10 +126,10 @@ class H5PDevelopment { $library = H5PDevelopment::libraryToString($name, $majorVersion, $minorVersion); return isset($this->libraries[$library]) === TRUE ? $this->libraries[$library] : NULL; } - + /** * Get semantics for the given library. - * + * * @param string $name of the library. * @param int $majorVersion of the library. * @param int $minorVersion of the library. @@ -137,32 +137,32 @@ class H5PDevelopment { */ public function getSemantics($name, $majorVersion, $minorVersion) { $library = H5PDevelopment::libraryToString($name, $majorVersion, $minorVersion); - + if (isset($this->libraries[$library]) === FALSE) { return NULL; } - + return $this->getFileContents($this->libraries[$library]['path'] . '/semantics.json'); } - + /** * Get translations for the given library. - * + * * @param string $name of the library. * @param int $majorVersion of the library. * @param int $minorVersion of the library. * @return string Translation */ - public function getLanguage($name, $majorVersion, $minorVersion) { + public function getLanguage($name, $majorVersion, $minorVersion, $language) { $library = H5PDevelopment::libraryToString($name, $majorVersion, $minorVersion); - + if (isset($this->libraries[$library]) === FALSE) { return NULL; } - - return $this->getFileContents($this->libraries[$library]['path'] . '/language/' . $this->language . '.json'); + + return $this->getFileContents($this->libraries[$library]['path'] . '/language/' . $language . '.json'); } - + /** * Writes library as string on the form "name majorVersion.minorVersion" * @@ -175,4 +175,3 @@ class H5PDevelopment { return $name . ' ' . $majorVersion . '.' . $minorVersion; } } - From a8e6854324f57fdc5afda94b1f55f049656f2f03 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Tue, 14 Oct 2014 14:06:49 +0200 Subject: [PATCH 06/19] Use correct semantics. --- h5p.classes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/h5p.classes.php b/h5p.classes.php index b246771..a6de0d4 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -2403,7 +2403,7 @@ class H5PContentValidator { if (!isset($this->libraries[$value->library])) { $libspec = H5PCore::libraryFromString($value->library); $library = $this->h5pC->loadLibrary($libspec['machineName'], $libspec['majorVersion'], $libspec['minorVersion']); - $library['semantics'] = json_decode($library['semantics']); + $library['semantics'] = $this->h5pC->loadLibrarySemantics($libspec['machineName'], $libspec['majorVersion'], $libspec['minorVersion']); $this->libraries[$value->library] = $library; // Find all dependencies for this library From 86c18476cd473910b0067c50b4ac730ea4e21a12 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Wed, 15 Oct 2014 11:38:29 +0200 Subject: [PATCH 07/19] Created a new table data view from library management and library details that is more generic. Added support for finish event from H5Ps. JS now also log to results when the H5P was opened and finished. --- js/h5p-data-view.js | 126 ++++++++++++++++++++++++ js/h5p-utils.js | 222 ++++++++++++++++++++++++++++++++++++++++++- js/h5p.js | 45 ++++++++- styles/h5p-admin.css | 31 +++--- 4 files changed, 406 insertions(+), 18 deletions(-) create mode 100644 js/h5p-data-view.js diff --git a/js/h5p-data-view.js b/js/h5p-data-view.js new file mode 100644 index 0000000..e38ecd3 --- /dev/null +++ b/js/h5p-data-view.js @@ -0,0 +1,126 @@ +var H5PDataView = (function ($) { + + /** + * Initialize a new H5P data view. + * + * @param {Object} container + * @param {String} source URL for data + * @param {Array} headers for data + * @param {Object} l10n translations + */ + function H5PDataView(container, source, headers, l10n, classes) { + var self = this; + + self.$container = $(container).addClass('h5p-data-view').html(''); + H5PUtils.throbber(l10n.loading).appendTo(self.$container); + + self.source = source; + self.headers = headers; + self.l10n = l10n; + self.classes = (classes === undefined ? {} : classes); + self.limit = 20; + + self.loadData(); + } + + /** + * Load data for view. + * + * @param {Number} offset data collection offset + */ + H5PDataView.prototype.loadData = function (offset) { + var self = this; + + // Create URL + var url = self.source; + if (offset !== undefined) { + url += (url.indexOf('?') === -1 ? '?' : '&') + 'offset=' + offset + '&limit=' + self.limit; + } + + // Fire ajax request + $.ajax({ + dataType: 'json', + cache: true, + url: url + }).fail(function () { + // Error handling + self.setMessage(self.l10n.ajaxFailed); + }).done(function (data) { + if (!data.rows.length) { + self.setMessage(self.l10n.noData); + } + else { + // Update table data + self.updateTable(data.rows); + } + + // Update pagination widget + self.updatePagination(data.num); + }); + }; + + /** + * Display the given message to the user. + * + * @param {String} message + */ + H5PDataView.prototype.setMessage = function (message) { + var self = this; + + var $message = $('

    ', { + text: message + }); + if (self.table === undefined) { + self.$container.children().replaceWith($message); + } + else { + self.table.setBody($('

    ', {text: message})); + } + }; + + /** + * Update table data. + * + * @param {Array} rows + */ + H5PDataView.prototype.updateTable = function (rows) { + var self = this; + + if (self.table === undefined) { + // Create new table + self.table = new H5PUtils.Table(self.classes, self.headers); + self.table.appendTo(self.$container.html('')); + } + + // Add/update rows + self.table.setRows(rows); + }; + + /** + * Update pagination widget. + * + * @param {Number} num size of data collection + */ + H5PDataView.prototype.updatePagination = function (num) { + var self = this; + + if (self.pagination === undefined) { + // Create new widget + var $pagerContainer = $('

    ', {'class': 'h5p-pagination'}); + self.pagination = new H5PUtils.Pagination(num, self.limit, function (offset) { + // Handle page changes in pagination widget + self.table.setBody(H5PUtils.throbber(self.l10n.loading)); + self.loadData(offset); + }, self.l10n); + + self.pagination.appendTo($pagerContainer); + self.table.setFoot($pagerContainer); + } + else { + // Update existing widget + self.pagination.update(num, self.limit); + } + }; + + return H5PDataView; +})(H5P.jQuery); diff --git a/js/h5p-utils.js b/js/h5p-utils.js index 0db73a7..6ef61ad 100644 --- a/js/h5p-utils.js +++ b/js/h5p-utils.js @@ -64,7 +64,7 @@ var H5PUtils = H5PUtils || {}; return $field; }; - + /** * Replaces placeholder fields in translation strings * @@ -130,4 +130,224 @@ var H5PUtils = H5PUtils || {}; return $container; }; + /** + * Generic table class with useful helpers. + * + * @param {Object} classes to use for styling + * @param {Array} cols headers + */ + H5PUtils.Table = function (classes, cols) { + // Create basic table + var tableOptions = {}; + if (classes.table !== undefined) { + tableOptions['class'] = classes.table; + } + var $table = $('', tableOptions); + var $thead = $('').appendTo($table); + var $tfoot = $('').appendTo($table); + var $tbody = $('').appendTo($table); + + // Set cols - create header + var $tr = $('').appendTo($thead); + for (var i = 0; i < cols.length; i++) { + $(''); + + for (var i = 0; i < rows.length; i++) { + var $tr = $('').appendTo($newTbody); + + for (var j = 0; j < rows[i].length; j++) { + $(''); + var $tr = $('').appendTo($newTbody); + $(''); + var $tr = $('').appendTo($newTfoot); + $('
    ', { + html: cols[i] + }).appendTo($tr); + } + + /** + * Public. + * + * @param {Array} rows with cols + */ + this.setRows = function (rows) { + var $newTbody = $('
    ', { + html: rows[i][j] + }).appendTo($tr); + } + } + + $tbody.replaceWith($newTbody); + $tbody = $newTbody; + }; + + /** + * Public. + * + * @param {jQuery} $content custom + */ + this.setBody = function ($content) { + var $newTbody = $('
    ', { + colspan: cols.length + }).append($content).appendTo($tr); + $tbody.replaceWith($newTbody); + $tbody = $newTbody; + }; + + /** + * Public. + * + * @param {jQuery} $content custom + */ + this.setFoot = function ($content) { + var $newTfoot = $('
    ', { + colspan: cols.length + }).append($content).appendTo($tr); + $tfoot.replaceWith($newTfoot); + }; + + + /** + * Public. + * + * @param {jQuery} $container + */ + this.appendTo = function ($container) { + $table.appendTo($container); + }; + }; + + /** + * Generic pagination class. + * + * @param {Number} num total items + * @param {Number} limit items per page + * @param {Function} goneTo page callback + */ + H5PUtils.Pagination = function (num, limit, goneTo, l10n) { + var current = 0; + var pages = Math.ceil(num / limit); + + // Create components + + // Previous button + var $left = $('