Refactor xAPI code

d6
Svein-Tore Griff With 2015-02-16 16:47:04 +01:00
parent b575426f83
commit c330282351
5 changed files with 83 additions and 109 deletions

View File

@ -1590,8 +1590,9 @@ class H5PCore {
public static $scripts = array( public static $scripts = array(
'js/jquery.js', 'js/jquery.js',
'js/h5p.js', 'js/h5p.js',
'js/event-dispatcher.js', 'js/h5p-event-dispatcher.js',
'js/x-api.js', 'js/h5p-x-api-event.js',
'js/h5p-x-api.js',
); );
public static $adminScripts = array( public static $adminScripts = array(
'js/jquery.js', 'js/jquery.js',

View File

@ -1,46 +1,5 @@
var H5P = H5P || {}; 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 * 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 * Helper function to set the actor, email and name will be added automatically
*/ */
H5P.XAPIEvent.prototype.setActor = function() { 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', 'suspended',
'terminated', 'terminated',
'voided' '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'
};
};

71
js/h5p-x-api.js Normal file
View File

@ -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);
}
}
};

View File

@ -128,8 +128,8 @@ H5P.init = function () {
H5P.instances.push(instance); H5P.instances.push(instance);
} }
H5P.on(instance, 'xAPI', H5P.xAPIListener); H5P.on(instance, 'xAPI', H5P.xAPICompletedListener);
H5P.on(instance, 'xAPI', H5P.xAPIEmitter); H5P.on(instance, 'xAPI', H5P.xAPIExternal.trigger);
// Resize everything when window is resized. // Resize everything when window is resized.
$window.resize(function () { $window.resize(function () {