From 5d1cd188d0c33f691498cc54db822e10878a58c1 Mon Sep 17 00:00:00 2001 From: Svein-Tore Griff With Date: Mon, 20 Jul 2015 13:05:41 +0200 Subject: [PATCH] Add duration and scaled to result --- js/h5p-x-api-event.js | 11 ++++++++++- js/h5p-x-api.js | 6 +++++- 2 files changed, 15 insertions(+), 2 deletions(-) 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 *