2015-02-03 20:11:01 +01:00
|
|
|
var H5P = H5P || {};
|
|
|
|
|
2015-02-16 15:30:49 +01:00
|
|
|
/**
|
2015-05-05 13:13:57 +02:00
|
|
|
* Used for xAPI events.
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-02-16 15:30:49 +01:00
|
|
|
* @class
|
2015-05-05 13:13:57 +02:00
|
|
|
* @extends H5P.Event
|
2015-02-16 15:30:49 +01:00
|
|
|
*/
|
2015-05-05 13:13:57 +02:00
|
|
|
H5P.XAPIEvent = function () {
|
2015-04-07 19:33:21 +02:00
|
|
|
H5P.Event.call(this, 'xAPI', {'statement': {}}, {bubbles: true, external: 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
|
|
|
/**
|
2015-05-05 13:13:57 +02:00
|
|
|
* Set scored result statements.
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-05-05 13:13:57 +02:00
|
|
|
* @param {number} score
|
|
|
|
* @param {number} maxScore
|
2015-10-20 12:13:08 +02:00
|
|
|
* @param {object} instance
|
|
|
|
* @param {boolean} completion
|
|
|
|
* @param {boolean} success
|
2015-02-16 15:30:49 +01:00
|
|
|
*/
|
2015-10-20 12:13:08 +02:00
|
|
|
H5P.XAPIEvent.prototype.setScoredResult = function (score, maxScore, instance, completion, success) {
|
|
|
|
this.data.statement.result = {};
|
2016-06-20 14:57:14 +02:00
|
|
|
|
2015-10-20 12:13:08 +02:00
|
|
|
if (typeof score !== 'undefined') {
|
|
|
|
if (typeof maxScore === 'undefined') {
|
|
|
|
this.data.statement.result.score = {'raw': score};
|
2015-02-03 20:11:01 +01:00
|
|
|
}
|
2015-10-20 12:13:08 +02:00
|
|
|
else {
|
|
|
|
this.data.statement.result.score = {
|
|
|
|
'min': 0,
|
|
|
|
'max': maxScore,
|
|
|
|
'raw': score
|
|
|
|
};
|
|
|
|
if (maxScore > 0) {
|
|
|
|
this.data.statement.result.score.scaled = Math.round(score / maxScore * 10000) / 10000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 14:57:14 +02:00
|
|
|
|
2015-10-20 12:13:08 +02:00
|
|
|
if (typeof completion === 'undefined') {
|
2015-11-01 00:19:56 +01:00
|
|
|
this.data.statement.result.completion = (this.getVerb() === 'completed' || this.getVerb() === 'answered');
|
2015-10-20 12:13:08 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.data.statement.result.completion = completion;
|
|
|
|
}
|
2016-06-20 14:57:14 +02:00
|
|
|
|
2015-10-20 12:13:08 +02:00
|
|
|
if (typeof success !== 'undefined') {
|
|
|
|
this.data.statement.result.success = success;
|
2015-07-20 13:05:41 +02:00
|
|
|
}
|
2016-06-20 14:57:14 +02:00
|
|
|
|
2015-07-20 13:05:41 +02:00
|
|
|
if (instance && instance.activityStartTime) {
|
|
|
|
var duration = Math.round((Date.now() - instance.activityStartTime ) / 10) / 100;
|
|
|
|
// xAPI spec allows a precision of 0.01 seconds
|
2016-06-20 14:57:14 +02:00
|
|
|
|
2015-07-20 13:05:41 +02:00
|
|
|
this.data.statement.result.duration = 'PT' + duration + 'S';
|
|
|
|
}
|
2015-02-03 20:11:01 +01:00
|
|
|
};
|
|
|
|
|
2015-02-16 15:30:49 +01:00
|
|
|
/**
|
2015-05-05 13:13:57 +02:00
|
|
|
* Set a verb.
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-02-16 15:30:49 +01:00
|
|
|
* @param {string} verb
|
2015-05-05 13:13:57 +02:00
|
|
|
* Verb in short form, one of the verbs defined at
|
|
|
|
* {@link http://adlnet.gov/expapi/verbs/|ADL xAPI Vocabulary}
|
|
|
|
*
|
2015-02-16 15:30:49 +01:00
|
|
|
*/
|
2015-05-05 13:13:57 +02:00
|
|
|
H5P.XAPIEvent.prototype.setVerb = function (verb) {
|
2015-02-03 20:11:01 +01:00
|
|
|
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
|
|
|
/**
|
2015-05-05 13:13:57 +02:00
|
|
|
* Get the statements verb id.
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-02-16 15:30:49 +01:00
|
|
|
* @param {boolean} full
|
2015-05-05 13:13:57 +02:00
|
|
|
* 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-16 15:30:49 +01:00
|
|
|
*/
|
2015-05-05 13:13:57 +02: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
|
|
|
/**
|
2015-05-05 13:13:57 +02:00
|
|
|
* 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-05-05 13:13:57 +02:00
|
|
|
* @param {Object} instance
|
|
|
|
* The H5P instance
|
2015-02-16 15:30:49 +01:00
|
|
|
*/
|
2015-05-05 13:13:57 +02: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-04-07 19:33:21 +02:00
|
|
|
if (instance.subContentId) {
|
|
|
|
this.data.statement.object.definition.extensions['http://h5p.org/x-api/h5p-subContentId'] = instance.subContentId;
|
2015-03-22 15:14:28 +01:00
|
|
|
// Don't set titles on main content, title should come from publishing platform
|
2015-05-12 16:09:36 +02:00
|
|
|
if (typeof instance.getTitle === 'function') {
|
2015-03-22 15:14:28 +01:00
|
|
|
this.data.statement.object.definition.name = {
|
2015-05-12 16:09:36 +02:00
|
|
|
"en-US": instance.getTitle()
|
2015-03-22 15:14:28 +01:00
|
|
|
};
|
|
|
|
}
|
2015-03-21 16:45:38 +01:00
|
|
|
}
|
2015-03-22 15:14:28 +01:00
|
|
|
else {
|
2015-03-24 18:24:07 +01:00
|
|
|
if (H5PIntegration && H5PIntegration.contents && H5PIntegration.contents['cid-' + instance.contentId].title) {
|
2015-03-22 15:14:28 +01:00
|
|
|
this.data.statement.object.definition.name = {
|
2015-04-07 19:52:18 +02:00
|
|
|
"en-US": H5P.createTitle(H5PIntegration.contents['cid-' + instance.contentId].title)
|
2015-03-22 15:14:28 +01:00
|
|
|
};
|
|
|
|
}
|
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
|
|
|
/**
|
2015-05-05 13:13:57 +02:00
|
|
|
* Set the context part of the statement.
|
2015-03-21 16:45:38 +01:00
|
|
|
*
|
2015-05-05 13:13:57 +02:00
|
|
|
* @param {Object} instance
|
|
|
|
* The H5P instance
|
2015-03-21 16:45:38 +01:00
|
|
|
*/
|
2015-05-05 13:13:57 +02:00
|
|
|
H5P.XAPIEvent.prototype.setContext = function (instance) {
|
2015-04-07 19:33:21 +02:00
|
|
|
if (instance.parent && (instance.parent.contentId || instance.parent.subContentId)) {
|
|
|
|
var parentId = instance.parent.subContentId === undefined ? instance.parent.contentId : instance.parent.subContentId;
|
2015-03-21 16:45:38 +01:00
|
|
|
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-07-10 12:58:02 +02:00
|
|
|
if (instance.libraryInfo) {
|
|
|
|
if (this.data.statement.context === undefined) {
|
|
|
|
this.data.statement.context = {"contextActivities":{}};
|
|
|
|
}
|
|
|
|
this.data.statement.context.contextActivities.category = [
|
|
|
|
{
|
|
|
|
"id": "http://h5p.org/libraries/" + instance.libraryInfo.versionedNameNoSpaces,
|
|
|
|
"objectType": "Activity"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
2015-03-21 16:45:38 +01:00
|
|
|
};
|
|
|
|
|
2015-02-16 15:30:49 +01:00
|
|
|
/**
|
2015-05-05 13:13:57 +02:00
|
|
|
* Set the actor. Email and name will be added automatically.
|
2015-02-16 15:30:49 +01:00
|
|
|
*/
|
2015-05-05 13:13:57 +02: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;
|
2016-06-20 14:57:14 +02:00
|
|
|
try {
|
|
|
|
if (localStorage.H5PUserUUID) {
|
|
|
|
uuid = localStorage.H5PUserUUID;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
uuid = H5P.createUUID();
|
|
|
|
localStorage.H5PUserUUID = uuid;
|
|
|
|
}
|
2015-03-02 15:49:27 +01:00
|
|
|
}
|
2016-06-20 14:57:14 +02:00
|
|
|
catch (err) {
|
|
|
|
// LocalStorage and Cookies are probably disabled. Do not track the user.
|
|
|
|
uuid = 'not-trackable-' + H5P.createUUID();
|
2015-03-02 15:49:27 +01:00
|
|
|
}
|
|
|
|
this.data.statement.actor = {
|
|
|
|
'account': {
|
|
|
|
'name': uuid,
|
2015-03-23 09:45:02 +01:00
|
|
|
'homePage': H5PIntegration.siteUrl
|
2015-03-02 15:49:27 +01:00
|
|
|
},
|
|
|
|
'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-05-05 13:13:57 +02:00
|
|
|
* @returns {number}
|
|
|
|
* The max score, or null if not defined
|
2015-02-16 15:30:49 +01:00
|
|
|
*/
|
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-05-05 13:13:57 +02:00
|
|
|
* @returns {number}
|
|
|
|
* The score, or null if not defined
|
2015-02-16 15:30:49 +01:00
|
|
|
*/
|
2015-02-05 10:46:55 +01:00
|
|
|
H5P.XAPIEvent.prototype.getScore = function() {
|
|
|
|
return this.getVerifiedStatementValue(['result', 'score', 'raw']);
|
|
|
|
};
|
|
|
|
|
2015-05-05 13:13:57 +02:00
|
|
|
/**
|
|
|
|
* Get content xAPI ID.
|
|
|
|
*
|
|
|
|
* @param {Object} instance
|
|
|
|
* The H5P instance
|
|
|
|
*/
|
2015-03-21 16:45:38 +01:00
|
|
|
H5P.XAPIEvent.prototype.getContentXAPIId = function (instance) {
|
|
|
|
var xAPIId;
|
2015-03-24 18:24:07 +01:00
|
|
|
if (instance.contentId && H5PIntegration && H5PIntegration.contents) {
|
2015-03-21 16:45:38 +01:00
|
|
|
xAPIId = H5PIntegration.contents['cid-' + instance.contentId].url;
|
2015-04-07 19:33:21 +02:00
|
|
|
if (instance.subContentId) {
|
|
|
|
xAPIId += '?subContentId=' + instance.subContentId;
|
2015-03-21 16:45:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return xAPIId;
|
2015-05-05 13:13:57 +02:00
|
|
|
};
|
2015-03-21 16:45:38 +01:00
|
|
|
|
2017-01-04 16:16:28 +01:00
|
|
|
/**
|
|
|
|
* Check if this event is sent from a child (i.e not from grandchild)
|
|
|
|
*
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
H5P.XAPIEvent.prototype.isFromChild = function () {
|
|
|
|
var parentId = this.getVerifiedStatementValue(['context', 'contextActivities', 'parent', 0, 'id']);
|
|
|
|
return !parentId || parentId.indexOf('subContentId') === -1;
|
|
|
|
}
|
|
|
|
|
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-05-05 13:13:57 +02:00
|
|
|
* @param {string[]} 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-16 15:30:49 +01:00
|
|
|
*/
|
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
|
|
|
/**
|
2015-05-05 13:13:57 +02:00
|
|
|
* List of verbs defined at {@link http://adlnet.gov/expapi/verbs/|ADL xAPI Vocabulary}
|
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',
|
2017-01-31 13:35:23 +01:00
|
|
|
'voided',
|
2017-11-16 01:41:48 +01:00
|
|
|
|
|
|
|
// Custom verbs for video events
|
|
|
|
'played',
|
|
|
|
'paused',
|
|
|
|
'seeked',
|
2017-01-31 13:35:23 +01:00
|
|
|
|
|
|
|
// Custom verbs used for action toolbar below content
|
|
|
|
'downloaded',
|
|
|
|
'accessed-embed',
|
|
|
|
'accessed-copyright'
|
2015-02-20 10:26:33 +01:00
|
|
|
];
|