From c33028235173a81483c64444c25f69082c48f70a Mon Sep 17 00:00:00 2001 From: Svein-Tore Griff With Date: Mon, 16 Feb 2015 16:47:04 +0100 Subject: [PATCH] Refactor xAPI code --- h5p.classes.php | 5 +- ...-dispatcher.js => h5p-event-dispatcher.js} | 0 js/{x-api.js => h5p-x-api-event.js} | 112 ++---------------- js/h5p-x-api.js | 71 +++++++++++ js/h5p.js | 4 +- 5 files changed, 83 insertions(+), 109 deletions(-) rename js/{event-dispatcher.js => h5p-event-dispatcher.js} (100%) rename js/{x-api.js => h5p-x-api-event.js} (59%) create mode 100644 js/h5p-x-api.js diff --git a/h5p.classes.php b/h5p.classes.php index c54a92c..f85b0dc 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -1590,8 +1590,9 @@ class H5PCore { public static $scripts = array( 'js/jquery.js', 'js/h5p.js', - 'js/event-dispatcher.js', - 'js/x-api.js', + 'js/h5p-event-dispatcher.js', + 'js/h5p-x-api-event.js', + 'js/h5p-x-api.js', ); public static $adminScripts = array( 'js/jquery.js', diff --git a/js/event-dispatcher.js b/js/h5p-event-dispatcher.js similarity index 100% rename from js/event-dispatcher.js rename to js/h5p-event-dispatcher.js diff --git a/js/x-api.js b/js/h5p-x-api-event.js similarity index 59% rename from js/x-api.js rename to js/h5p-x-api-event.js index a2fedee..2d8e24e 100644 --- a/js/x-api.js +++ b/js/h5p-x-api-event.js @@ -1,46 +1,5 @@ var H5P = H5P || {}; -/** - * Internal H5P function listening for xAPI completed events and stores scores - * - * @param {function} event - xAPI event - */ -H5P.xAPIListener = function(event) { - var statement = event.data.statement; - if ('verb' in statement) { - if (statement.verb.id === 'http://adlnet.gov/expapi/verbs/completed') { - var score = statement.result.score.raw; - var maxScore = statement.result.score.max; - var contentId = statement.object.extensions['http://h5p.org/x-api/h5p-local-content-id']; - H5P.setFinished(contentId, score, maxScore); - } - } -}; - -/** - * Trigger xAPI events on all registered listeners - * - * @param {Function} event - xAPI event - */ -H5P.xAPIEmitter = function (event) { - if (event.data.statement !== undefined) { - for (var i = 0; i < H5P.xAPIListeners.length; i++) { - H5P.xAPIListeners[i](event.data.statement); - } - } -}; - -H5P.xAPIListeners = []; - -/** - * API function used to register for xAPI events - * - * @param {Function} listener - */ -H5P.onXAPI = function(listener) { - H5P.xAPIListeners.push(listener); -}; - /** * Constructor for xAPI events * @@ -134,7 +93,12 @@ H5P.XAPIEvent.prototype.setObject = function(instance) { * Helper function to set the actor, email and name will be added automatically */ H5P.XAPIEvent.prototype.setActor = function() { - this.data.statement.actor = H5P.getActor(); + var user = H5PIntegration.getUser(); + this.data.statement.actor = { + 'name': user.name, + 'mbox': 'mailto:' + user.mail, + 'objectType': 'Agent' + }; }; /** @@ -205,66 +169,4 @@ H5P.XAPIEvent.allowedXAPIVerbs = [ 'suspended', 'terminated', 'voided' -]; - -/** - * Helper function for triggering xAPI added to the EventDispatcher - * - * @param {string} verb - the short id of the verb we want to trigger - * @param {oject} extra - extra properties for the xAPI statement - */ -H5P.EventDispatcher.prototype.triggerXAPI = function(verb, extra) { - this.trigger(this.createXAPIEventTemplate(verb, extra)); -}; - -/** - * Helper function to create event templates added to the EventDispatcher - * - * Will in the future be used to add representations of the questions to the - * statements. - * - * @param {string} verb - verb id in short form - * @param {object} extra - Extra values to be added to the statement - * @returns {Function} - XAPIEvent object - */ -H5P.EventDispatcher.prototype.createXAPIEventTemplate = function(verb, extra) { - var event = new H5P.XAPIEvent(); - - event.setActor(); - event.setVerb(verb); - if (extra !== undefined) { - for (var i in extra) { - event.data.statement[i] = extra[i]; - } - } - if (!('object' in event)) { - event.setObject(this); - } - return event; -}; - -/** - * Helper function to create xAPI completed events - * - * @param {int} score - will be set as the 'raw' value of the score object - * @param {int} maxScore - will be set as the "max" value of the score object - */ -H5P.EventDispatcher.prototype.triggerXAPICompleted = function(score, maxScore) { - var event = this.createXAPIEventTemplate('completed'); - event.setScoredResult(score, maxScore); - this.trigger(event); -} - -/** - * Helps get the data for the actor part of the xAPI statement - * - * @returns {object} - the actor object for the xAPI statement - */ -H5P.getActor = function() { - var user = H5PIntegration.getUser(); - return { - 'name': user.name, - 'mbox': 'mailto:' + user.mail, - 'objectType': 'Agent' - }; -}; \ No newline at end of file +]; \ No newline at end of file diff --git a/js/h5p-x-api.js b/js/h5p-x-api.js new file mode 100644 index 0000000..230497c --- /dev/null +++ b/js/h5p-x-api.js @@ -0,0 +1,71 @@ +var H5P = H5P || {}; + +// Create object where external code may register and listen for H5P Events +H5P.xAPIExternal = new H5P.EventDispatcher(); + +// EventDispatcher extensions + +/** + * Helper function for triggering xAPI added to the EventDispatcher + * + * @param {string} verb - the short id of the verb we want to trigger + * @param {oject} extra - extra properties for the xAPI statement + */ +H5P.EventDispatcher.prototype.triggerXAPI = function(verb, extra) { + this.trigger(this.createXAPIEventTemplate(verb, extra)); +}; + +/** + * Helper function to create event templates added to the EventDispatcher + * + * Will in the future be used to add representations of the questions to the + * statements. + * + * @param {string} verb - verb id in short form + * @param {object} extra - Extra values to be added to the statement + * @returns {Function} - XAPIEvent object + */ +H5P.EventDispatcher.prototype.createXAPIEventTemplate = function(verb, extra) { + var event = new H5P.XAPIEvent(); + + event.setActor(); + event.setVerb(verb); + if (extra !== undefined) { + for (var i in extra) { + event.data.statement[i] = extra[i]; + } + } + if (!('object' in event)) { + event.setObject(this); + } + return event; +}; + +/** + * Helper function to create xAPI completed events + * + * @param {int} score - will be set as the 'raw' value of the score object + * @param {int} maxScore - will be set as the "max" value of the score object + */ +H5P.EventDispatcher.prototype.triggerXAPICompleted = function(score, maxScore) { + var event = this.createXAPIEventTemplate('completed'); + event.setScoredResult(score, maxScore); + this.trigger(event); +} + +/** + * Internal H5P function listening for xAPI completed events and stores scores + * + * @param {function} event - xAPI event + */ +H5P.xAPICompletedListener = function(event) { + var statement = event.data.statement; + if ('verb' in statement) { + if (statement.verb.id === 'http://adlnet.gov/expapi/verbs/completed') { + var score = statement.result.score.raw; + var maxScore = statement.result.score.max; + var contentId = statement.object.extensions['http://h5p.org/x-api/h5p-local-content-id']; + H5P.setFinished(contentId, score, maxScore); + } + } +}; \ No newline at end of file diff --git a/js/h5p.js b/js/h5p.js index 5df3f2b..4f11a32 100644 --- a/js/h5p.js +++ b/js/h5p.js @@ -128,8 +128,8 @@ H5P.init = function () { H5P.instances.push(instance); } - H5P.on(instance, 'xAPI', H5P.xAPIListener); - H5P.on(instance, 'xAPI', H5P.xAPIEmitter); + H5P.on(instance, 'xAPI', H5P.xAPICompletedListener); + H5P.on(instance, 'xAPI', H5P.xAPIExternal.trigger); // Resize everything when window is resized. $window.resize(function () {