Commit 24ccf0a0 authored by thomasmars's avatar thomasmars

Updated submodules and applied max-score patch.

parent 6a68d0ab
......@@ -451,6 +451,19 @@ class H5PDefaultStorage implements \H5PFileStorage {
return self::dirReady($this->path);
}
/**
* Check if the file presave.js exists in the root of the library
*
* @param string $libraryFolder
* @param string $developmentPath
* @return bool
*/
public function hasPresave($libraryFolder, $developmentPath = null) {
$path = is_null($developmentPath) ? 'libraries' . DIRECTORY_SEPARATOR . $libraryFolder : $developmentPath;
$filePath = realpath($this->path . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . 'presave.js');
return file_exists($filePath);
}
/**
* Recursive function for copying directories.
*
......
......@@ -190,4 +190,13 @@ interface H5PFileStorage {
* @return bool True if server has the proper write access
*/
public function hasWriteAccess();
/**
* Check if the library has a presave.js in the root folder
*
* @param string $libraryName
* @param string $developmentPath
* @return bool
*/
public function hasPresave($libraryName, $developmentPath = null);
}
......@@ -137,6 +137,11 @@ function h5peditor_form_h5p_content_node_form_alter(&$form, &$form_state) {
'#default_value' => $library,
);
$form['h5p_max_score'] = array(
'#type' => 'hidden',
'#default_value' => null,
);
$form['#submit'][] = 'h5peditor_h5p_content_node_form_submit';
for ($i = 0, $s = count($form['#validate']); $i < $s; $i++) {
if ($form['#validate'][$i] == 'h5p_form_validate') {
......
......@@ -36,6 +36,7 @@ class H5peditor {
'scripts/h5peditor-dimensions.js',
'scripts/h5peditor-coordinates.js',
'scripts/h5peditor-none.js',
'scripts/h5peditor-pre-save.js',
'ckeditor/ckeditor.js',
);
private $h5p, $storage;
......@@ -353,6 +354,11 @@ class H5peditor {
// Get list of JS and CSS files that belongs to the dependencies
$files = $this->h5p->getDependenciesFiles($libraries, $prefix);
$libraryName = H5PCore::libraryToString(compact('machineName', 'majorVersion', 'minorVersion'), true);
if( $this->hasPresave($libraryName) === true ){
$library = $this->h5p->loadLibrary($machineName, $majorVersion, $minorVersion);
$this->addPresaveFile($files, $library, $prefix);
}
$this->storage->alterLibraryFiles($files, $libraries);
// Restore asset aggregation setting
......@@ -640,4 +646,49 @@ class H5peditor {
}
}
}
/**
* Determine if a library has a presave.js file in the root folder
*
* @param string $libraryName
* @return bool
*/
public function hasPresave($libraryName){
if( isset($this->h5p->h5pD) ){
$parsedLibrary = H5PCore::libraryFromString($libraryName);
if($parsedLibrary !== false){
$machineName = $parsedLibrary['machineName'];
$majorVersion = $parsedLibrary['majorVersion'];
$minorVersion = $parsedLibrary['minorVersion'];
$library = $this->h5p->h5pD->getLibrary($machineName, $majorVersion, $minorVersion);
if( !is_null($library)){
return $this->h5p->fs->hasPresave($libraryName, $library['path']);
}
}
}
return $this->h5p->fs->hasPresave($libraryName);
}
/**
* Adds the path to the presave.js file to the list of dependency assets for the library
*
* @param array $assets
* @param array $library
* @param string $prefix
*/
public function addPresaveFile(&$assets, $library, $prefix = ''){
$path = 'libraries' . DIRECTORY_SEPARATOR . H5PCore::libraryToString($library, true);
if( array_key_exists('path', $library)){
$path = $library['path'];
}
$version = "?ver={$library['majorVersion']}.{$library['minorVersion']}.{$library['patchVersion']}";
if( array_key_exists('version', $library) ){
$version = $library['version'];
}
$assets['scripts'][] = (object) array(
'path' => $prefix . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . 'presave.js',
'version' => $version,
);
}
}
This diff is collapsed.
......@@ -157,5 +157,8 @@ H5PEditor.language.core = {
contentTypeCacheOutdated: 'Content type list outdated',
contentTypeCacheOutdatedDesc: 'Your site is having difficulties connecting to H5P.org to check for content type updates. You may not be able to update or install new content types.',
tryAgain: 'Try again',
getHelp: 'Get help'
getHelp: 'Get help',
errorHeader: 'An error occured',
errorCalculatingMaxScore: 'Could not calculate the max score for this content. The max score is assumed to be 0. Contact your administrator if this isn’t correct.',
maxScoreSemanticsMissing: 'Could not find the expected semantics in the content.'
};
......@@ -158,5 +158,8 @@ H5PEditor.language.core = {
contentTypeCacheOutdated: 'Content type list outdated',
contentTypeCacheOutdatedDesc: 'Your site is having difficulties connecting to H5P.org to check for content type updates. You may not be able to update or install new content types.',
tryAgain: 'Try again',
getHelp: 'Get help'
getHelp: 'Get help',
errorHeader: 'An error occured',
errorCalculatingMaxScore: 'Could not calculate the max score for this content. The max score is assumed to be 0. Contact your administrator if this isn’t correct.',
maxScoreSemanticsMissing: 'Could not find the expected semantics in the content.'
};
......@@ -159,5 +159,8 @@ H5PEditor.language.core = {
contentTypeCacheOutdated: 'Content type list outdated',
contentTypeCacheOutdatedDesc: 'Your site is having difficulties connecting to H5P.org to check for content type updates. You may not be able to update or install new content types.',
tryAgain: 'Try again',
getHelp: 'Get help'
getHelp: 'Get help',
errorHeader: 'An error occured',
errorCalculatingMaxScore: 'Could not calculate the max score for this content. The max score is assumed to be 0. Contact your administrator if this isn’t correct.',
maxScoreSemanticsMissing: 'Could not find the expected semantics in the content.'
};
......@@ -159,5 +159,8 @@ H5PEditor.language.core = {
contentTypeCacheOutdated: 'Content type list outdated',
contentTypeCacheOutdatedDesc: 'Your site is having difficulties connecting to H5P.org to check for content type updates. You may not be able to update or install new content types.',
tryAgain: 'Try again',
getHelp: 'Get help'
getHelp: 'Get help',
errorHeader: 'An error occured',
errorCalculatingMaxScore: 'Could not calculate the max score for this content. The max score is assumed to be 0. Contact your administrator if this isn’t correct.',
maxScoreSemanticsMissing: 'Could not find the expected semantics in the content.'
};
This diff is collapsed.
......@@ -159,5 +159,8 @@ H5PEditor.language.core = {
contentTypeCacheOutdated: 'Content type list outdated',
contentTypeCacheOutdatedDesc: 'Your site is having difficulties connecting to H5P.org to check for content type updates. You may not be able to update or install new content types.',
tryAgain: 'Try again',
getHelp: 'Get help'
getHelp: 'Get help',
errorHeader: 'An error occured',
errorCalculatingMaxScore: 'Could not calculate the max score for this content. The max score is assumed to be 0. Contact your administrator if this isn’t correct.',
maxScoreSemanticsMissing: 'Could not find the expected semantics in the content.'
};
......@@ -158,5 +158,8 @@ H5PEditor.language.core = {
contentTypeCacheOutdated: 'Innholdstype-listen er utdatert',
contentTypeCacheOutdatedDesc: 'Nettstedet ditt har problemer med å koble til H5P.org for å se etter innholdstype-oppdateringer. Det kan hende at du ikke får oppdatert eller installert nye innholdstyper.',
tryAgain: 'Prøv på nytt',
getHelp: 'Get help'
getHelp: 'Get help',
errorHeader: 'En feil har oppstått',
errorCalculatingMaxScore: 'Kunne ikke kalkulerer maks poengsum for denne innholdstypen. Maks poengsum er antatt til å være 0. Kontakt administrator dersom dette ikke er korrekt.',
maxScoreSemanticsMissing: 'Kunne ikke finne den forventede semantikken i den valgte innholdstypen.'
};
......@@ -159,5 +159,8 @@ H5PEditor.language.core = {
contentTypeCacheOutdated: 'Content type list outdated',
contentTypeCacheOutdatedDesc: 'Your site is having difficulties connecting to H5P.org to check for content type updates. You may not be able to update or install new content types.',
tryAgain: 'Try again',
getHelp: 'Get help'
getHelp: 'Get help',
errorHeader: 'An error occured',
errorCalculatingMaxScore: 'Could not calculate the max score for this content. The max score is assumed to be 0. Contact your administrator if this isn’t correct.',
maxScoreSemanticsMissing: 'Could not find the expected semantics in the content.'
};
......@@ -160,5 +160,8 @@ H5PEditor.language.core = {
contentTypeCacheOutdated: 'Innhaldstype-lista er utdatert',
contentTypeCacheOutdatedDesc: 'Nettstedet ditt har problemer med å koble til H5P.org for å se etter innhaldstype-oppdateringer. Det kan hende at du ikke får oppdatert eller installert nye innhaldstyper.',
tryAgain: 'Prøv på nytt',
getHelp: 'Get help'
getHelp: 'Get help',
errorHeader: 'An error occured',
errorCalculatingMaxScore: 'Could not calculate the max score for this content. The max score is assumed to be 0. Contact your administrator if this isn’t correct.',
maxScoreSemanticsMissing: 'Could not find the expected semantics in the content.'
};
......@@ -159,5 +159,8 @@ H5PEditor.language.core = {
contentTypeCacheOutdated: 'Content type list outdated',
contentTypeCacheOutdatedDesc: 'Your site is having difficulties connecting to H5P.org to check for content type updates. You may not be able to update or install new content types.',
tryAgain: 'Try again',
getHelp: 'Get help'
getHelp: 'Get help',
errorHeader: 'An error occured',
errorCalculatingMaxScore: 'Could not calculate the max score for this content. The max score is assumed to be 0. Contact your administrator if this isn’t correct.',
maxScoreSemanticsMissing: 'Could not find the expected semantics in the content.'
};
......@@ -159,5 +159,8 @@ H5PEditor.language.core = {
contentTypeCacheOutdated: 'Content type list outdated',
contentTypeCacheOutdatedDesc: 'Your site is having difficulties connecting to H5P.org to check for content type updates. You may not be able to update or install new content types.',
tryAgain: 'Try again',
getHelp: 'Get help'
getHelp: 'Get help',
errorHeader: 'An error occured',
errorCalculatingMaxScore: 'Could not calculate the max score for this content. The max score is assumed to be 0. Contact your administrator if this isn’t correct.',
maxScoreSemanticsMissing: 'Could not find the expected semantics in the content.'
};
......@@ -255,6 +255,16 @@ ns.Editor.prototype.getParams = function (notFormSubmit) {
}
};
/**
*
* @alias H5PEditor.Editor#presave
* @param content
* @return {H5PEditor.Presave}
*/
ns.Editor.prototype.presave = function (content) {
return this.selector.presave(content, this.getLibrary());
};
/**
* Editor translations index by library name or "core".
*
......
(function ($, ns) {
H5PEditor.init = function ($form, $type, $upload, $create, $editor, $library, $params) {
H5PEditor.init = function ($form, $type, $upload, $create, $editor, $library, $params, $maxScore) {
H5PEditor.$ = H5P.jQuery;
H5PEditor.basePath = H5PIntegration.editor.libraryUrl;
H5PEditor.fileIcon = H5PIntegration.editor.fileIcon;
......@@ -51,6 +51,13 @@
if (params !== undefined) {
$library.val(h5peditor.getLibrary());
$params.val(JSON.stringify(params));
try{
var presave = h5peditor.presave(params);
$maxScore.val(presave.maxScore);
} catch (err) {
alert(err.message); //This halts processing. Swap with H5P.Dialog? And perhaps stop probagation?
$maxScore.val(0);
}
}
}
});
......
......@@ -199,3 +199,13 @@ ns.LibrarySelector.prototype.getParams = function () {
//return valid ? this.form.params : false;
return this.form.params; // TODO: Switch to the line above when we are able to tell the user where the validation fails
};
/**
*
* @param content
* @param library
* @returns {H5PEditor.Presave} Result after processing library and content
*/
ns.LibrarySelector.prototype.presave = function (content, library) {
return (new ns.Presave).process(library, content);
};
\ No newline at end of file
var H5PEditor = H5PEditor || {};
var H5PPresave = H5PPresave || {};
H5PEditor.Presave = (function (Editor) {
"use strict";
/**
* Presave structure
*
* @class
*/
function Presave() {
this.maxScore = 0;
}
/**
* Process the given library and calculate the max score
*
* @public
* @param {string} library
* @param {object} content
* @returns {H5PEditor.Presave}
*/
Presave.prototype.process = function (library, content) {
var self = this;
library = Presave.sanitizeLibrary(library);
if (Presave.libraryExists(library) === true) {
H5PPresave[library](content, function (serverSideData) {
if (typeof serverSideData !== 'object') {
return;
}
if (serverSideData.hasOwnProperty('maxScore') && Presave.isInt(serverSideData.maxScore)) {
self.maxScore += serverSideData.maxScore;
}
});
}
return this;
};
/**
* Check if the score is valid or throw exception if not
*
* @static
* @param score
* @returns {boolean}
* @throws {Presave.exceptions.InvalidMaxScoreException} If score is not valid
*/
Presave.validateScore = function (score) {
if (!Presave.isInt(score) || score < 0) {
throw new this.exceptions.InvalidMaxScoreException();
}
return true;
};
/**
* Check if a object has the given properties.
*
* @static
* @param {object} content
* @param {string|[]} requirements
* @returns {boolean}
*/
Presave.checkNestedRequirements = function (content, requirements) {
if (typeof content === 'undefined') {
return false;
}
if (typeof requirements === 'string') {
requirements = requirements.split('.');
}
for (var i = 1; i < requirements.length; i++) {
if (!content.hasOwnProperty(requirements[i])) {
return false;
}
content = content[requirements[i]];
}
return true;
};
/**
* Check if value is a integer
*
* @static
* @param {*} value
* @returns {boolean}
*/
Presave.isInt = function (value) {
return !isNaN(value) && (function (x) {
return (x | 0) === x;
})(parseFloat(value));
};
/**
* Checks if given library exists as a presave function
*
* @static
* @param {string} library
* @returns {boolean}
*/
Presave.libraryExists = function (library) {
return typeof H5PPresave[library] !== 'undefined';
};
/**
* Remove potential version number from library
*
* @param {string} library
* @returns {*}
*/
Presave.sanitizeLibrary = function (library) {
return Editor.libraryFromString(library).machineName || library;
};
/**
* Collection of common exceptions related to the logic handled in this file
*
* @type {{InvalidMaxScoreException: H5PEditor.Presave.exceptions.InvalidMaxScoreException, InvalidContentSemanticsException: H5PEditor.Presave.exceptions.InvalidContentSemanticsException}}
*/
Presave.exceptions = {
InvalidMaxScoreException: function (message) {
this.message = typeof message === 'string' ? message : Editor.t('core', 'errorCalculatingMaxScore');
this.name = 'InvalidMaxScoreError';
this.code = 'H5P-P400';
},
InvalidContentSemanticsException: function (name, message) {
this.message = typeof message === 'string' ? message : Editor.t('core', 'semanticsError', {':error': Editor.t('core', 'maxScoreSemanticsMissing')});
this.name = typeof name === 'string' ? name : 'Invalid Content Semantics Error';
this.code = 'H5P-P500';
}
};
/**
* C
* @constructor
* @type {Presave}
*/
Presave.prototype.constructor = Presave;
return Presave;
})(H5PEditor);
......@@ -161,12 +161,14 @@ ns.loadLibrary = function (libraryName, callback) {
libraryData.semantics = semantics;
ns.libraryCache[libraryName] = libraryData;
ns.libraryRequested(libraryName, callback);
ns.libraryRequested(libraryName, function (semen) {
callback(semen);
// Run queue.
for (var i = 0; i < ns.loadedCallbacks[libraryName].length; i++) {
ns.loadedCallbacks[libraryName][i](libraryData.semantics);
}
// Run queue.
for (var i = 0; i < ns.loadedCallbacks[libraryName].length; i++) {
ns.loadedCallbacks[libraryName][i](semen);
}
});
},
error: function(jqXHR, textStatus, errorThrown) {
if (window['console'] !== undefined) {
......
......@@ -9,6 +9,7 @@ var ns = H5PEditor;
var $type = $('input[name="h5p_type"]');
var $params = $('input[name="json_content"]');
var $library = $('input[name="h5p_library"]');
var $maxScore = $('input[name="h5p_max_score"]');
var library = $library.val();
ns.$ = H5P.jQuery;
......@@ -60,6 +61,14 @@ var ns = H5PEditor;
if (params !== undefined) {
$library.val(h5peditor.getLibrary());
$params.val(JSON.stringify(params));
try{
debugger;
var presave = h5peditor.presave(params);
$maxScore.val(presave.maxScore);
} catch (err) {
// alert(err.message); //This halts processing. Swap with H5P.Dialog? And perhaps stop probagation?
$maxScore.val(0);
}
}
}
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment