diff --git a/js/h5p-x-api-event.js b/js/h5p-x-api-event.js index acaed22..98b287a 100644 --- a/js/h5p-x-api-event.js +++ b/js/h5p-x-api-event.js @@ -19,7 +19,7 @@ H5P.XAPIEvent.prototype.constructor = H5P.XAPIEvent; * @param {number} score * @param {number} maxScore */ -H5P.XAPIEvent.prototype.setScoredResult = function (score, maxScore) { +H5P.XAPIEvent.prototype.setScoredResult = function (score, maxScore, instance) { this.data.statement.result = { 'score': { 'min': 0, @@ -27,6 +27,15 @@ H5P.XAPIEvent.prototype.setScoredResult = function (score, maxScore) { 'raw': score } }; + if (maxScore > 0) { + this.data.statement.result.score.scaled = Math.round(score / maxScore * 10000) / 10000; + } + if (instance && instance.activityStartTime) { + var duration = Math.round((Date.now() - instance.activityStartTime ) / 10) / 100; + // xAPI spec allows a precision of 0.01 seconds + + this.data.statement.result.duration = 'PT' + duration + 'S'; + } }; /** diff --git a/js/h5p-x-api.js b/js/h5p-x-api.js index ea5ba79..e287c1a 100644 --- a/js/h5p-x-api.js +++ b/js/h5p-x-api.js @@ -82,10 +82,14 @@ H5P.EventDispatcher.prototype.triggerXAPICompleted = function (score, maxScore) */ H5P.EventDispatcher.prototype.triggerXAPIScored = function (score, maxScore, verb) { var event = this.createXAPIEventTemplate(verb); - event.setScoredResult(score, maxScore); + event.setScoredResult(score, maxScore, this); this.trigger(event); }; +H5P.EventDispatcher.prototype.setActivityStarted = function() { + this.activityStartTime = Date.now(); +}; + /** * Internal H5P function listening for xAPI completed events and stores scores *