2015-02-03 20:11:01 +01:00
|
|
|
var H5P = H5P || {};
|
|
|
|
|
2015-02-16 15:30:49 +01:00
|
|
|
/**
|
|
|
|
* Constructor for xAPI events
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-02-16 15:30:49 +01:00
|
|
|
* @class
|
|
|
|
*/
|
2015-02-03 20:11:01 +01:00
|
|
|
H5P.XAPIEvent = function() {
|
2015-03-22 20:37:28 +01:00
|
|
|
H5P.Event.call(this, 'xAPI', {'statement': {}}, {bubbles: true});
|
2015-02-03 20:11:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
H5P.XAPIEvent.prototype = Object.create(H5P.Event.prototype);
|
|
|
|
H5P.XAPIEvent.prototype.constructor = H5P.XAPIEvent;
|
|
|
|
|
2015-02-16 15:30:49 +01:00
|
|
|
/**
|
|
|
|
* Helperfunction to set scored result statements
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-02-16 15:30:49 +01:00
|
|
|
* @param {int} score
|
|
|
|
* @param {int} maxScore
|
|
|
|
*/
|
2015-02-03 20:11:01 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-02-16 15:30:49 +01:00
|
|
|
/**
|
|
|
|
* Helperfunction to set a verb.
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-02-16 15:30:49 +01:00
|
|
|
* @param {string} verb
|
|
|
|
* Verb in short form, one of the verbs defined at
|
|
|
|
* http://adlnet.gov/expapi/verbs/
|
|
|
|
*/
|
2015-02-03 20:11:01 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2015-03-22 20:37:48 +01:00
|
|
|
else if (verb.id !== undefined) {
|
|
|
|
this.data.statement.verb = verb;
|
2015-02-03 20:11:01 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-16 15:30:49 +01:00
|
|
|
/**
|
|
|
|
* Helperfunction to get the statements verb id
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-02-16 15:30:49 +01:00
|
|
|
* @param {boolean} full
|
|
|
|
* if true the full verb id prefixed by http://adlnet.gov/expapi/verbs/ will be returned
|
|
|
|
* @returns {string} - Verb or null if no verb with an id has been defined
|
|
|
|
*/
|
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-20 10:26:33 +01:00
|
|
|
};
|
2015-02-11 20:06:40 +01:00
|
|
|
|
2015-02-16 15:30:49 +01:00
|
|
|
/**
|
|
|
|
* Helperfunction to set the object part of the statement.
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-02-16 15:30:49 +01:00
|
|
|
* The id is found automatically (the url to the content)
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-02-16 15:30:49 +01:00
|
|
|
* @param {object} instance - the H5P instance
|
|
|
|
*/
|
2015-02-03 20:11:01 +01:00
|
|
|
H5P.XAPIEvent.prototype.setObject = function(instance) {
|
2015-02-18 09:07:19 +01:00
|
|
|
if (instance.contentId) {
|
|
|
|
this.data.statement.object = {
|
2015-03-21 16:45:38 +01:00
|
|
|
'id': this.getContentXAPIId(instance),
|
2015-02-18 09:07:19 +01:00
|
|
|
'objectType': 'Activity',
|
2015-03-22 12:43:07 +01:00
|
|
|
'definition': {
|
|
|
|
'extensions': {
|
|
|
|
'http://h5p.org/x-api/h5p-local-content-id': instance.contentId
|
|
|
|
}
|
2015-02-18 09:07:19 +01:00
|
|
|
}
|
|
|
|
};
|
2015-03-22 15:14:28 +01:00
|
|
|
if (instance.uuid) {
|
|
|
|
this.data.statement.object.definition.extensions['http://h5p.org/x-api/h5p-uuid'] = instance.uuid;
|
|
|
|
// Don't set titles on main content, title should come from publishing platform
|
|
|
|
if (typeof instance.getH5PTitle === 'function') {
|
|
|
|
this.data.statement.object.definition.name = {
|
|
|
|
"en-US": instance.getH5PTitle()
|
|
|
|
};
|
|
|
|
}
|
2015-03-21 16:45:38 +01:00
|
|
|
}
|
2015-03-22 15:14:28 +01:00
|
|
|
else {
|
|
|
|
if (H5PIntegration.contents['cid-' + instance.contentId].title) {
|
|
|
|
this.data.statement.object.definition.name = {
|
|
|
|
"en-US": H5P.createH5PTitle(H5PIntegration.contents['cid-' + instance.contentId].title)
|
|
|
|
};
|
|
|
|
}
|
2015-03-21 16:45:38 +01:00
|
|
|
}
|
2015-02-18 09:07:19 +01:00
|
|
|
}
|
2015-02-03 20:11:01 +01:00
|
|
|
};
|
|
|
|
|
2015-03-21 16:45:38 +01:00
|
|
|
/**
|
|
|
|
* Helperfunction to set the context part of the statement.
|
|
|
|
*
|
|
|
|
* @param {object} instance - the H5P instance
|
|
|
|
*/
|
|
|
|
H5P.XAPIEvent.prototype.setContext = function(instance) {
|
2015-03-22 20:38:17 +01:00
|
|
|
if (instance.parent && (instance.parent.contentId || instance.parent.uuid)) {
|
2015-03-21 16:45:38 +01:00
|
|
|
var parentId = instance.parent.uuid === undefined ? instance.parent.contentId : instance.parent.uuid;
|
|
|
|
this.data.statement.context = {
|
2015-03-22 12:43:07 +01:00
|
|
|
"contextActivities": {
|
|
|
|
"parent": [
|
|
|
|
{
|
|
|
|
"id": this.getContentXAPIId(instance.parent),
|
|
|
|
"objectType": "Activity"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2015-03-21 16:45:38 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-16 15:30:49 +01:00
|
|
|
/**
|
|
|
|
* Helper function to set the actor, email and name will be added automatically
|
|
|
|
*/
|
2015-02-03 20:11:01 +01:00
|
|
|
H5P.XAPIEvent.prototype.setActor = function() {
|
2015-03-02 15:49:27 +01:00
|
|
|
if (H5PIntegration.user !== undefined) {
|
|
|
|
this.data.statement.actor = {
|
|
|
|
'name': H5PIntegration.user.name,
|
|
|
|
'mbox': 'mailto:' + H5PIntegration.user.mail,
|
|
|
|
'objectType': 'Agent'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var uuid;
|
|
|
|
if (localStorage.H5PUserUUID) {
|
|
|
|
uuid = localStorage.H5PUserUUID;
|
|
|
|
}
|
|
|
|
else {
|
2015-03-21 14:16:31 +01:00
|
|
|
uuid = H5P.createUUID();
|
2015-03-02 15:49:27 +01:00
|
|
|
localStorage.H5PUserUUID = uuid;
|
|
|
|
}
|
|
|
|
this.data.statement.actor = {
|
|
|
|
'account': {
|
|
|
|
'name': uuid,
|
|
|
|
'homePage': window.location.origin + H5PIntegration.basePath
|
|
|
|
},
|
|
|
|
'objectType': 'Agent'
|
|
|
|
};
|
|
|
|
}
|
2015-02-03 20:11:01 +01:00
|
|
|
};
|
|
|
|
|
2015-02-16 15:30:49 +01:00
|
|
|
/**
|
|
|
|
* Get the max value of the result - score part of the statement
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-02-16 15:30:49 +01:00
|
|
|
* @returns {int} the max score, or null if not defined
|
|
|
|
*/
|
2015-02-05 10:46:55 +01:00
|
|
|
H5P.XAPIEvent.prototype.getMaxScore = function() {
|
|
|
|
return this.getVerifiedStatementValue(['result', 'score', 'max']);
|
|
|
|
};
|
|
|
|
|
2015-02-16 15:30:49 +01:00
|
|
|
/**
|
|
|
|
* Get the raw value of the result - score part of the statement
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-02-16 15:30:49 +01:00
|
|
|
* @returns {int} the max score, or null if not defined
|
|
|
|
*/
|
2015-02-05 10:46:55 +01:00
|
|
|
H5P.XAPIEvent.prototype.getScore = function() {
|
|
|
|
return this.getVerifiedStatementValue(['result', 'score', 'raw']);
|
|
|
|
};
|
|
|
|
|
2015-03-21 16:45:38 +01:00
|
|
|
H5P.XAPIEvent.prototype.getContentXAPIId = function (instance) {
|
|
|
|
var xAPIId;
|
|
|
|
if (instance.contentId) {
|
|
|
|
xAPIId = H5PIntegration.contents['cid-' + instance.contentId].url;
|
2015-03-22 12:44:55 +01:00
|
|
|
if (instance.uuid) {
|
|
|
|
xAPIId += '?uuid=' + instance.uuid;
|
2015-03-21 16:45:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return xAPIId;
|
|
|
|
}
|
|
|
|
|
2015-02-16 15:30:49 +01:00
|
|
|
/**
|
|
|
|
* Figure out if a property exists in the statement and return it
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-02-16 15:30:49 +01:00
|
|
|
* @param {array} keys
|
|
|
|
* List describing the property we're looking for. For instance
|
|
|
|
* ['result', 'score', 'raw'] for result.score.raw
|
|
|
|
* @returns the value of the property if it is set, null otherwise
|
|
|
|
*/
|
2015-02-05 10:46:55 +01:00
|
|
|
H5P.XAPIEvent.prototype.getVerifiedStatementValue = function(keys) {
|
2015-02-11 15:56:35 +01:00
|
|
|
var val = this.data.statement;
|
2015-02-16 15:30:49 +01:00
|
|
|
for (var i = 0; i < keys.length; i++) {
|
2015-02-05 10:46:55 +01:00
|
|
|
if (val[keys[i]] === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
val = val[keys[i]];
|
|
|
|
}
|
|
|
|
return val;
|
2015-02-16 15:30:49 +01:00
|
|
|
};
|
2015-02-05 10:46:55 +01:00
|
|
|
|
2015-02-16 15:30:49 +01:00
|
|
|
/**
|
|
|
|
* List of verbs defined at http://adlnet.gov/expapi/verbs/
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-02-16 15:30:49 +01:00
|
|
|
* @type Array
|
|
|
|
*/
|
2015-02-05 10:46:55 +01:00
|
|
|
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-20 10:26:33 +01:00
|
|
|
];
|