Refactor xAPI code
parent
b575426f83
commit
c330282351
|
@ -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',
|
||||
|
|
|
@ -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'
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -206,65 +170,3 @@ H5P.XAPIEvent.allowedXAPIVerbs = [
|
|||
'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'
|
||||
};
|
||||
};
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
};
|
|
@ -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 () {
|
||||
|
|
Loading…
Reference in New Issue