From d47628a8167fe3553ae11378055317bb484033a8 Mon Sep 17 00:00:00 2001 From: Oliver Tacke Date: Thu, 7 Oct 2021 13:18:58 +0200 Subject: [PATCH] Add background score computation for better getScore support --- scripts/essay.js | 26 ++++++++++++++++++++++---- scripts/inputfield.js | 2 +- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/scripts/essay.js b/scripts/essay.js index 317a6db..883dcd3 100644 --- a/scripts/essay.js +++ b/scripts/essay.js @@ -166,8 +166,8 @@ H5P.Essay = function ($, Question) { previousState: this.previousState, statusBar: statusBar }, { - onInteracted: (function () { - that.handleInteracted(); + onInteracted: (function (params) { + that.handleInteracted(params); }), onInput: (function () { that.handleInput(); @@ -252,10 +252,19 @@ H5P.Essay = function ($, Question) { /** * Handle user interacted. + * @param {object} params Parameters. + * @param {boolean} [params.updateScore] If true, will trigger score computation. */ - Essay.prototype.handleInteracted = function () { + Essay.prototype.handleInteracted = function (params) { + params = params || {}; + // Deliberately keeping the state once answered this.isAnswered = this.isAnswered || this.inputField.getText().length > 0; + if (params.updateScore) { + // Only triggered when explicitly requested due to potential complexity + this.updateScore(); + } + this.triggerXAPI('interacted'); }; @@ -362,6 +371,15 @@ H5P.Essay = function ($, Question) { return (this.params.behaviour.ignoreScoring || this.getScore() >= this.scorePassing); }; + /** + * Update score. + * @param {object} results Results. + */ + Essay.prototype.updateScore = function (results) { + results = results || this.computeResults(); + this.score = Math.min(this.computeScore(results), this.getMaxScore()); + }; + /** * Handle the evaluation. */ @@ -377,7 +395,7 @@ H5P.Essay = function ($, Question) { } // Not all keyword groups might be necessary for mastering - this.score = Math.min(this.computeScore(results), this.getMaxScore()); + this.updateScore(results); const textScore = H5P.Question .determineOverallFeedback(this.params.overallFeedback, this.getScore() / this.getMaxScore()) .replace('@score', this.getScore()) diff --git a/scripts/inputfield.js b/scripts/inputfield.js index e54ad8c..35e870d 100644 --- a/scripts/inputfield.js +++ b/scripts/inputfield.js @@ -59,7 +59,7 @@ var H5P = H5P || {}; // Interacted listener this.inputField.addEventListener('blur', function () { if (that.oldValue !== that.getText()) { - that.callbacks.onInteracted(); + that.callbacks.onInteracted({ updateScore: true }); } that.oldValue = that.getText();