2018-05-03 10:27:06 +02:00
|
|
|
var H5P = window.H5P = window.H5P || {};
|
2015-02-16 16:47:04 +01:00
|
|
|
|
2015-05-05 13:13:57 +02:00
|
|
|
/**
|
|
|
|
* The external event dispatcher. Others, outside of H5P may register and
|
|
|
|
* listen for H5P Events here.
|
|
|
|
*
|
|
|
|
* @type {H5P.EventDispatcher}
|
|
|
|
*/
|
2015-02-17 10:57:21 +01:00
|
|
|
H5P.externalDispatcher = new H5P.EventDispatcher();
|
2015-02-16 16:47:04 +01:00
|
|
|
|
|
|
|
// EventDispatcher extensions
|
|
|
|
|
|
|
|
/**
|
2015-05-05 13:13:57 +02:00
|
|
|
* Helper function for triggering xAPI added to the EventDispatcher.
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-05-05 13:13:57 +02:00
|
|
|
* @param {string} verb
|
|
|
|
* The short id of the verb we want to trigger
|
|
|
|
* @param {Oject} [extra]
|
|
|
|
* Extra properties for the xAPI statement
|
2015-02-16 16:47:04 +01:00
|
|
|
*/
|
2015-05-05 13:13:57 +02:00
|
|
|
H5P.EventDispatcher.prototype.triggerXAPI = function (verb, extra) {
|
2015-02-16 16:47:04 +01:00
|
|
|
this.trigger(this.createXAPIEventTemplate(verb, extra));
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-05-05 13:13:57 +02:00
|
|
|
* Helper function to create event templates added to the EventDispatcher.
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-02-16 16:47:04 +01:00
|
|
|
* Will in the future be used to add representations of the questions to the
|
|
|
|
* statements.
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-05-05 13:13:57 +02:00
|
|
|
* @param {string} verb
|
|
|
|
* Verb id in short form
|
|
|
|
* @param {Object} [extra]
|
|
|
|
* Extra values to be added to the statement
|
|
|
|
* @returns {H5P.XAPIEvent}
|
|
|
|
* Instance
|
2015-02-16 16:47:04 +01:00
|
|
|
*/
|
2015-05-05 13:13:57 +02:00
|
|
|
H5P.EventDispatcher.prototype.createXAPIEventTemplate = function (verb, extra) {
|
2015-02-16 16:47:04 +01:00
|
|
|
var event = new H5P.XAPIEvent();
|
|
|
|
|
|
|
|
event.setActor();
|
|
|
|
event.setVerb(verb);
|
|
|
|
if (extra !== undefined) {
|
|
|
|
for (var i in extra) {
|
|
|
|
event.data.statement[i] = extra[i];
|
|
|
|
}
|
|
|
|
}
|
2015-03-22 12:43:07 +01:00
|
|
|
if (!('object' in event.data.statement)) {
|
2015-02-16 16:47:04 +01:00
|
|
|
event.setObject(this);
|
2015-03-22 12:43:07 +01:00
|
|
|
}
|
|
|
|
if (!('context' in event.data.statement)) {
|
|
|
|
event.setContext(this);
|
2015-02-16 16:47:04 +01:00
|
|
|
}
|
|
|
|
return event;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to create xAPI completed events
|
|
|
|
*
|
2015-04-09 19:42:05 +02:00
|
|
|
* DEPRECATED - USE triggerXAPIScored instead
|
|
|
|
*
|
2015-05-05 13:13:57 +02:00
|
|
|
* @deprecated
|
|
|
|
* since 1.5, use triggerXAPIScored instead.
|
|
|
|
* @param {number} score
|
|
|
|
* Will be set as the 'raw' value of the score object
|
|
|
|
* @param {number} maxScore
|
|
|
|
* will be set as the "max" value of the score object
|
2015-10-20 12:29:01 +02:00
|
|
|
* @param {boolean} success
|
|
|
|
* will be set as the "success" value of the result object
|
2015-02-16 16:47:04 +01:00
|
|
|
*/
|
2015-10-20 12:29:01 +02:00
|
|
|
H5P.EventDispatcher.prototype.triggerXAPICompleted = function (score, maxScore, success) {
|
2015-11-01 00:23:10 +01:00
|
|
|
this.triggerXAPIScored(score, maxScore, 'completed', true, success);
|
2015-04-09 19:42:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to create scored xAPI events
|
|
|
|
*
|
2015-05-05 13:13:57 +02:00
|
|
|
* @param {number} score
|
|
|
|
* Will be set as the 'raw' value of the score object
|
|
|
|
* @param {number} maxScore
|
|
|
|
* Will be set as the "max" value of the score object
|
|
|
|
* @param {string} verb
|
|
|
|
* Short form of adl verb
|
2015-11-01 00:23:10 +01:00
|
|
|
* @param {boolean} completion
|
|
|
|
* Is this a statement from a completed activity?
|
|
|
|
* @param {boolean} success
|
|
|
|
* Is this a statement from an activity that was done successfully?
|
2015-04-09 19:42:05 +02:00
|
|
|
*/
|
2015-10-20 12:29:01 +02:00
|
|
|
H5P.EventDispatcher.prototype.triggerXAPIScored = function (score, maxScore, verb, completion, success) {
|
2015-04-09 19:42:05 +02:00
|
|
|
var event = this.createXAPIEventTemplate(verb);
|
2015-10-20 12:29:01 +02:00
|
|
|
event.setScoredResult(score, maxScore, this, completion, success);
|
2015-02-16 16:47:04 +01:00
|
|
|
this.trigger(event);
|
2015-02-20 10:26:33 +01:00
|
|
|
};
|
2015-02-16 16:47:04 +01:00
|
|
|
|
2018-10-23 11:25:46 +02:00
|
|
|
H5P.EventDispatcher.prototype.setActivityStarted = function () {
|
2015-11-27 20:49:49 +01:00
|
|
|
if (this.activityStartTime === undefined) {
|
|
|
|
// Don't trigger xAPI events in the editor
|
2015-12-18 10:26:59 +01:00
|
|
|
if (this.contentId !== undefined &&
|
|
|
|
H5PIntegration.contents !== undefined &&
|
2015-12-10 14:49:30 +01:00
|
|
|
H5PIntegration.contents['cid-' + this.contentId] !== undefined) {
|
2015-11-27 20:49:49 +01:00
|
|
|
this.triggerXAPI('attempted');
|
|
|
|
}
|
|
|
|
this.activityStartTime = Date.now();
|
|
|
|
}
|
2015-07-20 13:05:41 +02:00
|
|
|
};
|
|
|
|
|
2015-02-16 16:47:04 +01:00
|
|
|
/**
|
|
|
|
* Internal H5P function listening for xAPI completed events and stores scores
|
2015-02-20 10:26:33 +01:00
|
|
|
*
|
2015-05-05 13:13:57 +02:00
|
|
|
* @param {H5P.XAPIEvent} event
|
2015-02-16 16:47:04 +01:00
|
|
|
*/
|
2015-05-05 13:13:57 +02:00
|
|
|
H5P.xAPICompletedListener = function (event) {
|
2015-11-01 00:30:45 +01:00
|
|
|
if ((event.getVerb() === 'completed' || event.getVerb() === 'answered') && !event.getVerifiedStatementValue(['context', 'contextActivities', 'parent'])) {
|
2015-03-22 20:38:57 +01:00
|
|
|
var score = event.getScore();
|
|
|
|
var maxScore = event.getMaxScore();
|
|
|
|
var contentId = event.getVerifiedStatementValue(['object', 'definition', 'extensions', 'http://h5p.org/x-api/h5p-local-content-id']);
|
|
|
|
H5P.setFinished(contentId, score, maxScore);
|
2015-02-16 16:47:04 +01:00
|
|
|
}
|
2015-12-10 14:49:30 +01:00
|
|
|
};
|