diff --git a/h5p.classes.php b/h5p.classes.php index 44e9aba..ba1b59a 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -300,6 +300,13 @@ interface H5PFrameworkInterface { * Get content without cache. */ public function getNotCached(); + + /** + * Get number of contents using library as main library. + * + * @param int $library_id + */ + public function getNumContent($library_id); } /** @@ -1719,6 +1726,35 @@ class H5PCore { 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 + */ + public function get_upgrades($library, $versions) { + $upgrades = array(); + + foreach ($versions as $upgrade) { + if ($upgrade->major_version > $library->major_version || $upgrade->major_version === $library->major_version && $upgrade->minor_version > $library->minor_version) { + $upgrades[$upgrade->id] = H5PCore::libraryVersion($upgrade); + } + } + + return $upgrades; + } } /** diff --git a/images/throbber.gif b/images/throbber.gif new file mode 100644 index 0000000..0177983 Binary files /dev/null and b/images/throbber.gif differ diff --git a/js/h5p-content-upgrade.js b/js/h5p-content-upgrade.js new file mode 100644 index 0000000..0d7315b --- /dev/null +++ b/js/h5p-content-upgrade.js @@ -0,0 +1,147 @@ +(function ($) { + var info, outData, $container, $throbber, throbberText, majorVersion, minorVersion; + + /** + * Generate html for version select. + * + * @param {Object} versions + * @returns {String} + */ + var getVersionSelect = function (versions) { + var html = ''; + for (var id in versions) { + html += ''; + } + if (html !== '') { + html = ''; + return html; + } + }; + + /** + * Process the current batch of parameters. + * + * @param {Object} params + */ + var processParameters = function (inData) { + var upgraded = {}; + + var i = 0; + for (var id in inData.params) { + if (!inData.params.hasOwnProperty(id)) { + continue; + } + + var param = JSON.parse(inData.params[id]); + for (var major in H5PUpgrades) { + if (!H5PUpgrades.hasOwnProperty(major) || major < info.majorVersion || major > majorVersion) { + continue; + } + + for (var minor in H5PUpgrades[major]) { + if (!H5PUpgrades[major].hasOwnProperty(major) || minor <= info.minorVersion || minor > minorVersion) { + continue; + } + + param = H5PUpgrades[major][minor](param); + } + } + upgraded[id] = JSON.stringify(param); + + i++; + $throbber.text(throbberText + Math.round((info.total - inData.left + i) / (info.total / 100)) + ' %') ; + } + + outData.params = JSON.stringify(upgraded); + outData.token = inData.token; + + // Get next round of data to process. + getParameters(); + }; + + /** + * Handles errors while processing parameters. + * + * @param {Object} params + */ + var process = function (inData) { + // Script is loaded. Start processing. + try { + processParameters(inData); + } + catch (err) { + $container.html('An error occurred while processing parameters: ' + err); + } + }; + + /** + * Get the next batch of parameters. + */ + var getParameters = function () { + $.post(info.url, outData, function (inData) { + if (!(inData instanceof Object)) { + // Print errors from backend + $container.html(inData); + return; + } + if (inData.left === '0') { + $container.html(info.done); + return; + } + + if (inData.script !== undefined) { + $.ajax({ + dataType: 'script', + cache: true, + url: inData.script + }).done(function () { + // Start processing + process(inData); + }).fail(function () { + $container.html('Error: Could not load upgrade script.'); + }); + return; + } + + // Continue processing + process(inData); + }); + }; + + // 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 + $('', { + class: 'h5p-admin-upgrade-button', + text: info.buttonLabel, + click: function () { + outData = { + libraryId: $version.val(), + token: info.token + }; + + // Get version + var version = info.versions[outData.libraryId]; + var versionLevels = version.split('.', 3); + majorVersion = versionLevels[0]; + minorVersion = versionLevels[1]; + + throbberText = 'Upgrading to ' + version + '...'; + $throbber = H5PUtils.throbber(throbberText); + $container.html('').append($throbber); + + // Start upgrade progress + getParameters(); + } + }).appendTo($container); + }); +})(H5P.jQuery); \ No newline at end of file diff --git a/js/h5p-library-details.js b/js/h5p-library-details.js index 108db88..e274d22 100644 --- a/js/h5p-library-details.js +++ b/js/h5p-library-details.js @@ -41,7 +41,7 @@ var H5PLibraryDetails= H5PLibraryDetails || {}; var count; if (H5PLibraryDetails.library.notCached !== undefined) { - count = -1; + count = H5PIntegration.i18n.H5P.NA; } else if (H5PLibraryDetails.library.content === undefined) { count = 0; diff --git a/js/h5p-library-list.js b/js/h5p-library-list.js index 9673765..79bc400 100644 --- a/js/h5p-library-list.js +++ b/js/h5p-library-list.js @@ -33,25 +33,38 @@ var H5PLibraryList= H5PLibraryList || {}; $table.addClass('libraries'); // Add libraries + var t = H5PIntegration.i18n.H5P; $.each (libraries.listData, function (index, library) { var $libraryRow = H5PUtils.createTableRow([ - library.name, - library.machineName, - library.contentCount, - library.libraryDependencyCount, - ' ' + library.title, + library.numContent, + library.numContentDependencies === -1 ? t.NA : library.numContentDependencies, + library.numLibraryDependencies, + ' ' ]); + if (library.upgradeUrl !== null && library.numContent !== '0') { + $('.h5p-admin-upgrade-library', $libraryRow).attr('title', t.upgradeLibrary).click(function () { + window.location.href = library.upgradeUrl; + }); + } + else { + $('.h5p-admin-upgrade-library', $libraryRow).attr('disabled', true); + } + // 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 (library.contentCount !== 0 || library.libraryDependencyCount !== 0) { + if (library.numContent !== '0' || library.numContentDependencies !== '0' || library.numLibraryDependencies !== '0') { // Disabled delete if content. - $deleteButton.attr('disabled', true); //.addClass('disabled'); + $deleteButton.attr('disabled', true).attr('title', t.deleteLibrary); } else { // Go to delete page om click. diff --git a/js/h5p-utils.js b/js/h5p-utils.js index 5cda763..d8faf68 100644 --- a/js/h5p-utils.js +++ b/js/h5p-utils.js @@ -66,6 +66,19 @@ var H5PUtils = H5PUtils || {}; return template; }; + /** + * Get throbber with given text. + * + * @param {String} text + * @returns {$} + */ + H5PUtils.throbber = function (text) { + return $('', { + class: 'h5p-throbber', + text: text + }); + }; + /** * Makes it possbile to rebuild all content caches from admin UI. * @param {Object} notCached diff --git a/styles/h5p-admin.css b/styles/h5p-admin.css index 664ddab..6d63c9e 100644 --- a/styles/h5p-admin.css +++ b/styles/h5p-admin.css @@ -47,6 +47,10 @@ text-indent: -0.125em; margin: 0.125em 0.125em 0 0.125em; } +.h5p-admin-upgrade-library:before { + font-family: 'H5P'; + content: "\2191"; +} .h5p-admin-view-library:before { font-family: 'H5P'; content: "\e888"; @@ -56,19 +60,22 @@ content: "\e88e"; } -.h5p-admin-table.libraries button:hover, -.h5p-admin-table.libraries .h5p-admin-delete-library:hover { +.h5p-admin-table.libraries button:hover { background-color: #d0d0d0; } - -.h5p-admin-table.libraries .h5p-admin-delete-library:disabled:hover { +.h5p-admin-table.libraries button:disabled:hover { background-color: #e0e0e0; + cursor: default; } -.h5p-admin-table.libraries .h5p-admin-delete-library { +.h5p-admin-upgrade-library { + color: #0f01f9; +} +.h5p-admin-delete-library { color: #f9010f; } -.h5p-admin-table.libraries .h5p-admin-delete-library:disabled { +.h5p-admin-delete-library:disabled, +.h5p-admin-upgrade-library:disabled { cursor: default; color: #c0c0c0; } diff --git a/styles/h5p.css b/styles/h5p.css index 812aeda..529c4c9 100644 --- a/styles/h5p.css +++ b/styles/h5p.css @@ -313,3 +313,9 @@ div.h5p-fullscreen { margin-left: 0.25em; padding-left: 0.25em; } +.h5p-throbber { + background: url('../images/throbber.gif') left center no-repeat; + padding-left: 38px; + min-height: 30px; + line-height: 30px; +} \ No newline at end of file