2015-02-03 20:11:01 +01:00
|
|
|
var H5P = H5P || {};
|
|
|
|
|
|
|
|
H5P.xAPIListener = function(event) {
|
2015-02-11 15:56:35 +01:00
|
|
|
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'];
|
2015-02-03 20:11:01 +01:00
|
|
|
H5P.setFinished(contentId, score, maxScore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
H5P.xAPIEmitter = function (event) {
|
2015-02-11 15:56:35 +01:00
|
|
|
if (event.data.statement !== undefined) {
|
2015-02-03 20:11:01 +01:00
|
|
|
for (var i = 0; i < H5P.xAPIListeners.length; i++) {
|
2015-02-11 15:56:35 +01:00
|
|
|
H5P.xAPIListeners[i](event.data.statement);
|
2015-02-03 20:11:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
H5P.xAPIListeners = [];
|
|
|
|
|
|
|
|
H5P.onXAPI = function(listener) {
|
|
|
|
H5P.xAPIListeners.push(listener);
|
|
|
|
};
|
|
|
|
|
|
|
|
H5P.onXAPI(function(statement) {
|
|
|
|
console.log(statement);
|
|
|
|
});
|
|
|
|
|
|
|
|
H5P.XAPIEvent = function() {
|
2015-02-11 15:56:35 +01:00
|
|
|
H5P.Event.call(this, 'xAPI', {'statement': {}});
|
2015-02-03 20:11:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
H5P.XAPIEvent.prototype = Object.create(H5P.Event.prototype);
|
|
|
|
H5P.XAPIEvent.prototype.constructor = H5P.XAPIEvent;
|
|
|
|
|
|
|
|
H5P.XAPIEvent.prototype.setScoredResult = function(score, maxScore) {
|
2015-02-11 15:56:35 +01:00
|
|
|
this.data.statement.result = {
|
2015-02-03 20:11:01 +01:00
|
|
|
'score': {
|
|
|
|
'min': 0,
|
|
|
|
'max': maxScore,
|
|
|
|
'raw': score
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
H5P.XAPIEvent.prototype.setVerb = function(verb) {
|
|
|
|
if (H5P.jQuery.inArray(verb, H5P.XAPIEvent.allowedXAPIVerbs) !== -1) {
|
2015-02-11 15:56:35 +01:00
|
|
|
this.data.statement.verb = {
|
2015-02-03 20:11:01 +01:00
|
|
|
'id': 'http://adlnet.gov/expapi/verbs/' + verb,
|
|
|
|
'display': {
|
|
|
|
'en-US': verb
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log('illegal verb');
|
|
|
|
}
|
|
|
|
// Else: Fail silently...
|
|
|
|
};
|
|
|
|
|
2015-02-12 15:46:25 +01:00
|
|
|
H5P.XAPIEvent.prototype.getVerb = function(full) {
|
2015-02-11 20:06:40 +01:00
|
|
|
var statement = this.data.statement;
|
|
|
|
if ('verb' in statement) {
|
2015-02-12 15:46:25 +01:00
|
|
|
if (full === true) {
|
|
|
|
return statement.verb;
|
|
|
|
}
|
2015-02-11 20:06:40 +01:00
|
|
|
return statement.verb.id.slice(31);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 20:11:01 +01:00
|
|
|
H5P.XAPIEvent.prototype.setObject = function(instance) {
|
2015-02-11 15:56:35 +01:00
|
|
|
this.data.statement.object = {
|
2015-02-03 20:11:01 +01:00
|
|
|
// TODO: Correct this. contentId might be vid
|
|
|
|
'id': window.location.origin + Drupal.settings.basePath + 'node/' + instance.contentId,
|
|
|
|
'objectType': 'Activity',
|
|
|
|
'extensions': {
|
|
|
|
'http://h5p.org/x-api/h5p-local-content-id': instance.contentId
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
H5P.XAPIEvent.prototype.setActor = function() {
|
2015-02-11 15:56:35 +01:00
|
|
|
this.data.statement.actor = H5P.getActor();
|
2015-02-03 20:11:01 +01:00
|
|
|
};
|
|
|
|
|
2015-02-05 10:46:55 +01:00
|
|
|
H5P.XAPIEvent.prototype.getMaxScore = function() {
|
|
|
|
return this.getVerifiedStatementValue(['result', 'score', 'max']);
|
|
|
|
};
|
|
|
|
|
|
|
|
H5P.XAPIEvent.prototype.getScore = function() {
|
|
|
|
return this.getVerifiedStatementValue(['result', 'score', 'raw']);
|
|
|
|
};
|
|
|
|
|
|
|
|
H5P.XAPIEvent.prototype.getVerifiedStatementValue = function(keys) {
|
2015-02-11 15:56:35 +01:00
|
|
|
var val = this.data.statement;
|
2015-02-05 10:46:55 +01:00
|
|
|
for (var i in keys) {
|
|
|
|
if (val[keys[i]] === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
val = val[keys[i]];
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
H5P.XAPIEvent.allowedXAPIVerbs = [
|
|
|
|
'answered',
|
|
|
|
'asked',
|
|
|
|
'attempted',
|
|
|
|
'attended',
|
|
|
|
'commented',
|
|
|
|
'completed',
|
|
|
|
'exited',
|
|
|
|
'experienced',
|
|
|
|
'failed',
|
|
|
|
'imported',
|
|
|
|
'initialized',
|
|
|
|
'interacted',
|
|
|
|
'launched',
|
|
|
|
'mastered',
|
|
|
|
'passed',
|
|
|
|
'preferred',
|
|
|
|
'progressed',
|
|
|
|
'registered',
|
|
|
|
'responded',
|
|
|
|
'resumed',
|
|
|
|
'scored',
|
|
|
|
'shared',
|
|
|
|
'suspended',
|
|
|
|
'terminated',
|
|
|
|
'voided'
|
|
|
|
];
|
|
|
|
|
2015-02-03 20:11:01 +01:00
|
|
|
H5P.EventDispatcher.prototype.triggerXAPI = function(verb, extra) {
|
2015-02-11 15:56:35 +01:00
|
|
|
this.trigger(this.createXAPIEventTemplate(verb, extra));
|
2015-02-03 20:11:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
2015-02-11 15:56:35 +01:00
|
|
|
event.data.statement[i] = extra[i];
|
2015-02-03 20:11:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!('object' in event)) {
|
|
|
|
event.setObject(this);
|
|
|
|
}
|
|
|
|
return event;
|
|
|
|
};
|
|
|
|
|
|
|
|
H5P.EventDispatcher.prototype.triggerXAPICompleted = function(score, maxScore) {
|
|
|
|
var event = this.createXAPIEventTemplate('completed');
|
|
|
|
event.setScoredResult(score, maxScore);
|
2015-02-11 15:56:35 +01:00
|
|
|
this.trigger(event);
|
2015-02-03 20:11:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
H5P.getActor = function() {
|
|
|
|
var user = H5PIntegration.getUser();
|
|
|
|
return {
|
|
|
|
'name': user.name,
|
|
|
|
'mbox': 'mailto:' + user.mail,
|
|
|
|
'objectType': 'Agent'
|
|
|
|
};
|
2015-02-05 10:46:55 +01:00
|
|
|
};
|