Refactoring xAPI to use inheritence and event classes

namespaces
Svein-Tore Griff With 2014-11-01 21:54:42 +01:00
parent b3da2c5b32
commit 9391b60a03
2 changed files with 114 additions and 104 deletions

View File

@ -2163,7 +2163,7 @@ class H5PCore {
*/ */
public function getLibrariesMetadata() { public function getLibrariesMetadata() {
// Fetch from cache: // Fetch from cache:
$metadata = $this->h5pF->cacheGet('libraries','metadata'); // $metadata = $this->h5pF->cacheGet('libraries','metadata');
// If not available in cache, or older than a week => refetch! // If not available in cache, or older than a week => refetch!
if ($metadata === NULL || $metadata->lastTimeFetched < (time() - self::SECONDS_IN_WEEK)) { if ($metadata === NULL || $metadata->lastTimeFetched < (time() - self::SECONDS_IN_WEEK)) {
@ -2174,7 +2174,7 @@ class H5PCore {
$metadata->json = ($json === FALSE ? NULL : json_decode($json)); $metadata->json = ($json === FALSE ? NULL : json_decode($json));
$metadata->lastTimeFetched = time(); $metadata->lastTimeFetched = time();
$this->h5pF->cacheSet('libraries','metadata', $metadata); // $this->h5pF->cacheSet('libraries','metadata', $metadata);
} }
return $metadata->json; return $metadata->json;

180
js/h5p.js
View File

@ -95,13 +95,6 @@ H5P.init = function () {
// Keep track of when we started // Keep track of when we started
H5P.opened[contentId] = new Date(); H5P.opened[contentId] = new Date();
// Handle events when the user finishes the content. Useful for logging exercise results.
instance.$.on('finish', function (event) {
if (event.data !== undefined) {
H5P.setFinished(contentId, event.data.score, event.data.maxScore, event.data.time);
}
});
if (H5P.isFramed) { if (H5P.isFramed) {
// Make it possible to resize the iframe when the content changes size. This way we get no scrollbars. // Make it possible to resize the iframe when the content changes size. This way we get no scrollbars.
var iframe = window.parent.document.getElementById('h5p-iframe-' + contentId); var iframe = window.parent.document.getElementById('h5p-iframe-' + contentId);
@ -162,12 +155,15 @@ H5P.init = function () {
}; };
H5P.xAPIListener = function(event) { H5P.xAPIListener = function(event) {
if (event.verb.id === 'http://adlnet.gov/expapi/verbs/completed') { console.log(event);
var points = event.result.score.raw; if ('verb' in event.statement) {
var maxPoints = event.result.score.max; if (event.statement.verb.id === 'http://adlnet.gov/expapi/verbs/completed') {
var contentId = event.object.contentId; var points = event.statement.result.score.raw;
var maxPoints = event.statement.result.score.max;
var contentId = event.statement.object.contentId;
H5P.setFinished(contentId, points, maxPoints); H5P.setFinished(contentId, points, maxPoints);
} }
}
} }
/** /**
@ -403,8 +399,6 @@ H5P.newRunnable = function (library, contentId, $attachTo, skipResize) {
var instance = new constructor(library.params, contentId); var instance = new constructor(library.params, contentId);
H5P.addContentTypeFeatures(instance);
// Make xAPI events bubble // Make xAPI events bubble
// if (parent !== null && parent.trigger !== undefined) { // if (parent !== null && parent.trigger !== undefined) {
// instance.on('xAPI', parent.trigger); // instance.on('xAPI', parent.trigger);
@ -426,64 +420,113 @@ H5P.newRunnable = function (library, contentId, $attachTo, skipResize) {
return instance; return instance;
}; };
/** H5P.EventEnabled = function() {
* Add features like event handling to an H5P content type library this.listeners = {};
* };
* @param instance
* An H5P content type instance H5P.EventEnabled.prototype.on = function(type, listener) {
*/
H5P.addContentTypeFeatures = function(instance) {
if (instance.listeners === undefined) {
instance.listeners = {};
}
if (instance.on === undefined) {
instance.on = function(type, listener) {
if (typeof listener === 'function') { if (typeof listener === 'function') {
if (this.listeners[type] === undefined) { if (this.listeners[type] === undefined) {
this.listeners[type] = []; this.listeners[type] = [];
} }
this.listeners[type].push(listener); this.listeners[type].push(listener);
} }
} }
}
if (instance.off === undefined) { H5P.EventEnabled.prototype.off = function (type, listener) {
instance.off = function (type, listener) {
if (this.listeners[type] !== undefined) { if (this.listeners[type] !== undefined) {
var removeIndex = listeners[type].indexOf(listener); var removeIndex = listeners[type].indexOf(listener);
if (removeIndex) { if (removeIndex) {
listeners[type].splice(removeIndex, 1); listeners[type].splice(removeIndex, 1);
} }
} }
}
H5P.EventEnabled.prototype.trigger = function (type, event) {
if (event === null) {
event = new H5P.Event();
} }
}
if (instance.trigger === undefined) {
instance.trigger = function (type, event) {
if (this.listeners[type] !== undefined) { if (this.listeners[type] !== undefined) {
for (var i = 0; i < this.listeners[type].length; i++) { for (var i = 0; i < this.listeners[type].length; i++) {
this.listeners[type][i](event); this.listeners[type][i](event);
} }
} }
} }
}
if (instance.triggerXAPI === undefined) { H5P.Event = function() {
instance.triggerXAPI = function(verb, extra) { // We're going to add bubbling, propagation and other features here later
var event = { }
'actor': H5P.getActor(),
'verb': H5P.getxAPIVerb(verb) H5P.XAPIEvent = function() {
} H5P.Event.call(this);
if (extra !== undefined) { this.statement = {};
for (var i in extra) { }
event[i] = extra[i];
} H5P.XAPIEvent.prototype = Object.create(H5P.Event.prototype);
} H5P.XAPIEvent.prototype.constructor = H5P.XAPIEvent;
if (!('object' in event)) {
event.object = H5P.getxAPIObject(this); H5P.XAPIEvent.prototype.setScoredResult = function(score, maxScore) {
} this.statement.result = {
this.trigger('xAPI', event); 'score': {
'min': 0,
'max': maxScore,
'raw': score
} }
} }
} }
H5P.XAPIEvent.prototype.setVerb = function(verb) {
if (H5P.jQuery.inArray(verb, H5P.XAPIEvent.allowedXAPIVerbs) !== -1) {
this.statement.verb = {
'id': 'http://adlnet.gov/expapi/verbs/' + verb,
'display': {
'en-US': verb
}
}
}
else {
console.log('illegal verb');
}
// Else: Fail silently...
}
H5P.XAPIEvent.prototype.setObject = function(instance) {
// TODO: Implement the correct id(url)
// Return dummy data...
this.statement.object = {
'id': 'http://mysite.com/path-to-main-activity#sub-activity',
'contentId': instance.contentId,
'reference': instance
}
}
H5P.XAPIEvent.prototype.setActor = function() {
this.statement.actor = H5P.getActor();
}
H5P.EventEnabled.prototype.triggerXAPI = function(verb, extra) {
var event = this.createXAPIEventTemplate(verb, extra);
this.trigger('xAPI', event);
}
H5P.EventEnabled.prototype.createXAPIEventTemplate = function(verb, extra) {
var event = new H5P.XAPIEvent();
event.setActor();
event.setVerb(verb);
if (extra !== undefined) {
for (var i in extra) {
event.statement[i] = extra[i];
}
}
if (!('object' in event)) {
event.setObject(this);
}
return event;
}
H5P.getActor = function() { H5P.getActor = function() {
// TODO: Implement or remove? // TODO: Implement or remove?
@ -494,7 +537,7 @@ H5P.getActor = function() {
} }
} }
H5P.allowedxAPIVerbs = [ H5P.XAPIEvent.allowedXAPIVerbs = [
'answered', 'answered',
'asked', 'asked',
'attempted', 'attempted',
@ -522,39 +565,6 @@ H5P.allowedxAPIVerbs = [
'voided' 'voided'
] ]
H5P.getxAPIVerb = function(verb) {
if (H5P.jQuery.inArray(verb, H5P.allowedxAPIVerbs) !== -1) {
return {
'id': 'http://adlnet.gov/expapi/verbs/' + verb,
'display': {
'en-US': verb
}
}
}
// Else: Fail silently...
}
H5P.getxAPIObject = function(instance) {
// TODO: Implement or remove?
// Return dummy data...
return {
'id': 'http://mysite.com/path-to-main-activity#sub-activity',
'contentId': instance.contentId,
'reference': instance
}
}
H5P.getxAPIScoredResult = function(score, maxScore) {
return {
'score': {
'min': 0,
'max': maxScore,
'raw': score
}
}
}
/** /**
* Used to print useful error messages. * Used to print useful error messages.
* *
@ -1212,8 +1222,8 @@ H5P.setFinished = function (contentId, score, maxScore, time) {
// TODO: Should we use a variable with the complete path? // TODO: Should we use a variable with the complete path?
H5P.jQuery.post(H5P.ajaxPath + 'setFinished', { H5P.jQuery.post(H5P.ajaxPath + 'setFinished', {
contentId: contentId, contentId: contentId,
score: score, points: score,
maxScore: maxScore, maxPoints: maxScore,
opened: toUnix(H5P.opened[contentId]), opened: toUnix(H5P.opened[contentId]),
finished: toUnix(new Date()), finished: toUnix(new Date()),
time: time time: time