From 05d9982070d3324de8d38ddf11b9d23de1fd0e77 Mon Sep 17 00:00:00 2001 From: Svein-Tore Griff With Date: Sun, 2 Nov 2014 21:37:30 +0100 Subject: [PATCH] Add code to make it easy for others to register for xAPI events --- js/h5p.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/js/h5p.js b/js/h5p.js index 7cf7388..08f4f84 100644 --- a/js/h5p.js +++ b/js/h5p.js @@ -129,6 +129,7 @@ H5P.init = function () { } instance.on('xAPI', H5P.xAPIListener); + instance.on('xAPI', H5P.xAPIEmitter); // Resize everything when window is resized. $window.resize(function () { @@ -155,7 +156,6 @@ H5P.init = function () { }; H5P.xAPIListener = function(event) { - console.log(event); if ('verb' in event.statement) { if (event.statement.verb.id === 'http://adlnet.gov/expapi/verbs/completed') { var score = event.statement.result.score.raw; @@ -166,6 +166,24 @@ H5P.xAPIListener = function(event) { } } +H5P.xAPIEmitter = function (event) { + if (event.statement !== undefined) { + for (var i = 0; i < H5P.xAPIListeners.length; i++) { + H5P.xAPIListeners[i](event.statement) + } + } +} + +H5P.xAPIListeners = []; + +H5P.onXAPI = function(listener) { + H5P.xAPIListeners.push(listener); +} + +H5P.onXAPI(function(statement) { + console.log(statement); +}); + /** * Enable full screen for the given h5p. *