From 31780324ff2cb506c8112ef50183e1d7a2145081 Mon Sep 17 00:00:00 2001 From: Thomas Horn Sivertsen Date: Tue, 27 Mar 2018 16:20:35 +0200 Subject: [PATCH 1/2] Added/modified calculation of max score --- presave.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 presave.js diff --git a/presave.js b/presave.js new file mode 100644 index 0000000..dca68e5 --- /dev/null +++ b/presave.js @@ -0,0 +1,33 @@ +var H5PPresave = H5PPresave || {}; + +H5PPresave['H5P.QuestionSet'] = function (content, finished) { + var presave = H5PEditor.Presave; + + if (isContentInValid()) { + throw new presave.exceptions.InvalidContentSemanticsException('Invalid Question Set Error') + } + + var score = content.questions + .filter(function (action) { + return action.hasOwnProperty('library') && action.hasOwnProperty('params'); + }) + .map(function (action) { + return (new presave).process(action.library, action.params).maxScore; + }) + .reduce(function (currentScore, scoreToAdd) { + if (presave.isInt(scoreToAdd)) { + currentScore += scoreToAdd; + } + return currentScore; + }, 0); + + presave.validateScore(score); + + if (finished) { + finished({maxScore: score}); + } + + function isContentInValid() { + return !presave.checkNestedRequirements(content, 'content.questions') || !Array.isArray(content.questions); + } +}; From 4a2d83086f9cc9b44b8f53c1daddddc58b3bf9f0 Mon Sep 17 00:00:00 2001 From: Thomas Horn Sivertsen Date: Wed, 28 Mar 2018 11:44:54 +0200 Subject: [PATCH 2/2] Added JSDoc --- presave.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/presave.js b/presave.js index dca68e5..6727b1e 100644 --- a/presave.js +++ b/presave.js @@ -1,5 +1,12 @@ var H5PPresave = H5PPresave || {}; +/** + * Resolve the presave logic for the content type Question Set + * + * @param {object} content + * @param finished + * @constructor + */ H5PPresave['H5P.QuestionSet'] = function (content, finished) { var presave = H5PEditor.Presave; @@ -27,6 +34,10 @@ H5PPresave['H5P.QuestionSet'] = function (content, finished) { finished({maxScore: score}); } + /** + * Check if required parameters is present + * @return {boolean} + */ function isContentInValid() { return !presave.checkNestedRequirements(content, 'content.questions') || !Array.isArray(content.questions); }