Skip to content

Commit

Permalink
Add background score computation for better getScore support
Browse files Browse the repository at this point in the history
  • Loading branch information
otacke committed Oct 7, 2021
1 parent e17cd9a commit d47628a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
26 changes: 22 additions & 4 deletions scripts/essay.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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');
};

Expand Down Expand Up @@ -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.
*/
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion scripts/inputfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit d47628a

Please sign in to comment.