Improved results and contentuserdata saving by queueing requests

pull/17/head
Frode Petterson 2016-02-19 12:57:53 +01:00
parent dc8b70748f
commit 3849f3a054
1 changed files with 129 additions and 32 deletions

161
js/h5p.js
View File

@ -1580,46 +1580,97 @@ H5P.shuffleArray = function (array) {
return array; return array;
}; };
/** (function ($) {
* Post finished results for user. var token;
* var queue = [];
* @deprecated
* Do not use this function directly, trigger the finish event instead. /**
* Will be removed march 2016 * Return unix timestamp for the given JS Date.
* @param {number} contentId *
* Identifies the content * @private
* @param {number} score * @param {Date} date
* Achieved score/points * @returns {Number}
* @param {number} maxScore */
* The maximum score/points that can be achieved var toUnix = function (date) {
* @param {number} [time] return Math.round(date.getTime() / 1000);
* Reported time consumption/usage };
*/
H5P.setFinished = function (contentId, score, maxScore, time) { /**
if (H5PIntegration.postUserStatistics === true) { * Post results to server
/** *
* Return unix timestamp for the given JS Date. * @param Object result
* */
* @private var post = function (result) {
* @param {Date} date // Add token to post
* @returns {Number} result.token = token;
*/ token = null;
var toUnix = function (date) {
return Math.round(date.getTime() / 1000);
};
// Post the results // Post the results
// TODO: Should we use a variable with the complete path? // TODO: Should we use a variable with the complete path?
H5P.jQuery.post(H5PIntegration.ajaxPath + 'setFinished', { H5P.jQuery.post(H5PIntegration.ajaxPath + 'setFinished', result).fail(function() {
// Reuse token
token = result.token;
}).done(function(data) {
token = data.token;
}).always(function() {
// Check for more requests to run
if (queue[0] !== undefined) {
post(queue.splice(0, 1));
}
});
};
/**
* Creates the finished result object for the user and schedules it for
* posting to the server.
*
* @deprecated
* Do not use this function directly, trigger the finish event instead.
* Will be removed march 2016
* @param {number} contentId
* Identifies the content
* @param {number} score
* Achieved score/points
* @param {number} maxScore
* The maximum score/points that can be achieved
* @param {number} [time]
* Reported time consumption/usage
*/
H5P.setFinished = function (contentId, score, maxScore, time) {
if (H5PIntegration.postUserStatistics !== true) {
return;
}
var result = {
contentId: contentId, contentId: contentId,
score: score, score: score,
maxScore: maxScore, maxScore: maxScore,
opened: toUnix(H5P.opened[contentId]), opened: toUnix(H5P.opened[contentId]),
finished: toUnix(new Date()), finished: toUnix(new Date()),
time: time time: time
}); };
}
}; if (token === undefined) {
if (H5PIntegration.tokens === undefined ||
H5PIntegration.tokens.result === undefined) {
token = 'canHasDummyToken';
}
else {
token = H5PIntegration.tokens.result;
}
}
if (token === null) {
// Already in progress, add to queue
queue.push(result);
}
else {
post(result);
}
};
})(H5P.jQuery);
// Add indexOf to browsers that lack them. (IEs) // Add indexOf to browsers that lack them. (IEs)
if (!Array.prototype.indexOf) { if (!Array.prototype.indexOf) {
@ -1728,6 +1779,8 @@ H5P.createTitle = function (rawTitle, maxLength) {
// Wrap in privates // Wrap in privates
(function ($) { (function ($) {
var token;
var queue = [];
/** /**
* Creates ajax requests for inserting, updateing and deleteing * Creates ajax requests for inserting, updateing and deleteing
@ -1749,6 +1802,16 @@ H5P.createTitle = function (rawTitle, maxLength) {
done('Not signed in.'); done('Not signed in.');
return; return;
} }
if (token === undefined) {
// Load initial token
if (H5PIntegration.tokens === undefined ||
H5PIntegration.tokens.contentUserData === undefined) {
token = 'canHasDummyToken';
}
else {
token = H5PIntegration.tokens.contentUserData;
}
}
var options = { var options = {
url: H5PIntegration.ajax.contentUserData.replace(':contentId', contentId).replace(':dataType', dataType).replace(':subContentId', subContentId ? subContentId : 0), url: H5PIntegration.ajax.contentUserData.replace(':contentId', contentId).replace(':dataType', dataType).replace(':subContentId', subContentId ? subContentId : 0),
@ -1784,10 +1847,44 @@ H5P.createTitle = function (rawTitle, maxLength) {
done(undefined, response.data); done(undefined, response.data);
}; };
} }
if (options.type === 'POST') {
if (token === null) {
// We must queue and wait for a new token
queue.push(options);
}
else {
// Use token
options.data.token = token;
token = null;
}
}
$.ajax(options); runAjaxRequest(options);
} }
/**
*
*
* @param Object options Details for request
*/
var runAjaxRequest = function (options) {
var $req = $.ajax(options);
if (options.type === 'POST') {
$req.fail(function() {
// Reuse token
token = options.data.token;
}).done(function(data) {
// Set new token
token = data.token;
}).always(function() {
// Check for more requests to run
if (queue[0] !== undefined) {
runAjaxRequest(queue.splice(0, 1));
}
});
}
};
/** /**
* Get user data for given content. * Get user data for given content.
* *