Revert "Improved results and contentuserdata saving by queueing requests"

This reverts commit 3849f3a054.
pull/17/head
Frode Petterson 2016-02-22 11:56:52 +01:00
parent 3849f3a054
commit 256bf1b6d2
1 changed files with 32 additions and 129 deletions

161
js/h5p.js
View File

@ -1580,97 +1580,46 @@ H5P.shuffleArray = function (array) {
return array; return array;
}; };
(function ($) { /**
var token; * Post finished results for user.
var queue = []; *
* @deprecated
/** * Do not use this function directly, trigger the finish event instead.
* Return unix timestamp for the given JS Date. * Will be removed march 2016
* * @param {number} contentId
* @private * Identifies the content
* @param {Date} date * @param {number} score
* @returns {Number} * Achieved score/points
*/ * @param {number} maxScore
var toUnix = function (date) { * The maximum score/points that can be achieved
return Math.round(date.getTime() / 1000); * @param {number} [time]
}; * Reported time consumption/usage
*/
/** H5P.setFinished = function (contentId, score, maxScore, time) {
* Post results to server if (H5PIntegration.postUserStatistics === true) {
* /**
* @param Object result * Return unix timestamp for the given JS Date.
*/ *
var post = function (result) { * @private
// Add token to post * @param {Date} date
result.token = token; * @returns {Number}
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', result).fail(function() { H5P.jQuery.post(H5PIntegration.ajaxPath + 'setFinished', {
// 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) {
@ -1779,8 +1728,6 @@ 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
@ -1802,16 +1749,6 @@ 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),
@ -1847,44 +1784,10 @@ 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;
}
}
runAjaxRequest(options); $.ajax(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.
* *