Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
This files describes API changes in the Moodle Mobile app,
information provided here is intended especially for developers.

=== 2.9 ===

* Most of the functions from $mmaModScormOnline, $mmaModScormOffline, $mmaModScorm, $mmaModScormSync and $mmaModScormHelper now have a new param siteId to determine the site to affect. If you use any of these services please make sure you still pass the right parameters.

=== 2.8 ===

* $mmModuleActionsDelegate has been renamed to $mmContentLinksDelegate to make it more generic. The specification of this delegate has changed, please look at its documentation to adapt your handlers.
Expand Down
7 changes: 4 additions & 3 deletions www/addons/mod_scorm/controllers/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ angular.module('mm.addons.mod_scorm')
$scope.loadingToc = true;

if (scorm.popup) {
// Fix for bug in WS not returning %. If we receive a value <= 100 we'll assume it's a percentage.
// If we receive a value <= 100 we need to assume it's a percentage.
if (scorm.width <= 100) {
scorm.width = scorm.width + '%';
}
Expand Down Expand Up @@ -98,7 +98,8 @@ angular.module('mm.addons.mod_scorm')
promise = $mmaModScormHelper.createOfflineAttempt(scorm, result.attempt, attemptsData.online.length);
} else {
// Last attempt was online, verify that we can create a new online attempt. We ignore cache.
promise = $mmaModScorm.getScormUserData(scorm.id, result.attempt, false, undefined, true).catch(function() {
promise = $mmaModScorm.getScormUserData(scorm.id, result.attempt, false, undefined, undefined, true)
.catch(function() {
// Cannot communicate with the server, create an offline attempt.
offline = true;
return $mmaModScormHelper.createOfflineAttempt(scorm, result.attempt, attemptsData.online.length);
Expand Down Expand Up @@ -245,7 +246,7 @@ angular.module('mm.addons.mod_scorm')
return $mmaModScorm.saveTracks(scoId, attempt, tracks, offline, scorm).then(function() {
if (!offline) {
// New online attempt created, update cached data about online attempts.
$mmaModScorm.getAttemptCount(scorm.id, undefined, false, true);
$mmaModScorm.getAttemptCount(scorm.id, undefined, undefined, false, true);
}
});
}
Expand Down
45 changes: 32 additions & 13 deletions www/addons/mod_scorm/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,41 +64,60 @@ angular.module('mm.addons.mod_scorm', ['mm.core'])
$mmCoursePrefetchDelegateProvider.registerPrefetchHandler('mmaModScorm', 'scorm', '$mmaModScormPrefetchHandler');
})

.run(function($timeout, $mmaModScormSync, $mmApp, $mmEvents, $mmaModScormOnline, $mmaModScormOffline, mmCoreEventLogin,
mmCoreEventLogout) {
.run(function($timeout, $mmaModScormSync, $mmApp, $mmEvents, $mmSite, mmCoreEventLogin) {
var lastExecution = 0,
executing = false;
executing = false,
allSitesCalled = false;

function syncScorms() {
function syncScorms(allSites) {
var now = new Date().getTime();

if (!allSites && !$mmSite.isLoggedIn()) {
return;
}

// Prevent consecutive and simultaneous executions. A sync process shouldn't take more than a few minutes,
// so if it's been more than 5 minutes since the last execution we'll ignore the executing value.
if (now - 5000 > lastExecution && (!executing || now - 300000 > lastExecution)) {
lastExecution = new Date().getTime();
executing = true;

$timeout(function() { // Minor delay just to make sure network is fully established.
$mmaModScormSync.syncAllScorms().finally(function() {
$mmaModScormSync.syncAllScorms(allSites ? undefined : $mmSite.getId()).finally(function() {
executing = false;
});
}, 1000);
}
}

$mmApp.ready().then(function() {
document.addEventListener('online', syncScorms, false); // Cordova event.
window.addEventListener('online', syncScorms, false); // HTML5 event.
document.addEventListener('online', function() {
syncScorms(false);
}, false); // Cordova event.
window.addEventListener('online', function() {
syncScorms(false);
}, false); // HTML5 event.

if (!$mmSite.isLoggedIn()) {
// App was started without any site logged in. Try to sync all sites.
allSitesCalled = true;
if ($mmApp.isOnline()) {
syncScorms(true);
}
}
});

$mmEvents.on(mmCoreEventLogin, function() {
if ($mmApp.isOnline()) {
syncScorms();
var allSites = false;
if (!allSitesCalled) {
// App started with a site logged in. Try to sync all sites.
allSitesCalled = true;
allSites = true;
}
});

$mmEvents.on(mmCoreEventLogout, function() {
$mmaModScormOnline.clearBlockedScorms();
$mmaModScormOffline.clearBlockedScorms();
if ($mmApp.isOnline()) {
syncScorms(allSites);
}
});

});
54 changes: 33 additions & 21 deletions www/addons/mod_scorm/services/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ angular.module('mm.addons.mod_scorm')
* @ngdoc service
* @name $mmCourseHelper
*/
.factory('$mmaModScormHelper', function($mmaModScorm, $mmUtil, $translate, $q, $mmaModScormOffline, $mmaModScormSync) {
.factory('$mmaModScormHelper', function($mmaModScorm, $mmUtil, $translate, $q, $mmaModScormOffline, $mmaModScormSync, $mmSite) {

var self = {},
elementsToIgnore = ['status', 'score_raw', 'total_time', 'session_time', 'student_id', 'student_name', 'credit',
Expand Down Expand Up @@ -54,14 +54,17 @@ angular.module('mm.addons.mod_scorm')
* @name $mmaModScormHelper#convertAttemptToOffline
* @param {Object} scorm SCORM.
* @param {Number} attempt Number of the online attempt.
* @param {String} [siteId] Site ID. If not defined, current site.
* @return {Promise} Promise resolved when the attempt is created.
*/
self.convertAttemptToOffline = function(scorm, attempt) {
self.convertAttemptToOffline = function(scorm, attempt, siteId) {
siteId = siteId || $mmSite.getId();

// Get data from the online attempt.
return $mmaModScorm.getScormUserData(scorm.id, attempt).then(function(onlineData) {
return $mmaModScorm.getScormUserData(scorm.id, attempt, false, siteId).then(function(onlineData) {
// The SCORM API might have written some data to the offline attempt already.
// We don't want to override it with cached online data.
return $mmaModScormOffline.getScormUserData(scorm.id, attempt).catch(function() {
return $mmaModScormOffline.getScormUserData(siteId, scorm.id, attempt).catch(function() {
// Ignore errors.
}).then(function(offlineData) {
var dataToStore = angular.copy(onlineData);
Expand All @@ -85,7 +88,7 @@ angular.module('mm.addons.mod_scorm')
}
});

return $mmaModScormOffline.createNewAttempt(scorm, undefined, attempt, dataToStore, onlineData);
return $mmaModScormOffline.createNewAttempt(siteId, scorm, undefined, attempt, dataToStore, onlineData);
});
}).catch(function() {
// Shouldn't happen.
Expand All @@ -102,11 +105,13 @@ angular.module('mm.addons.mod_scorm')
* @param {Object} scorm SCORM.
* @param {Number} newAttempt Number of the new attempt.
* @param {Number} lastOnline Number of the last online attempt.
* @param {String} [siteId] Site ID. If not defined, current site.
* @return {Promise} Promise resolved when the attempt is created.
*/
self.createOfflineAttempt = function(scorm, newAttempt, lastOnline) {
self.createOfflineAttempt = function(scorm, newAttempt, lastOnline, siteId) {
siteId = siteId || $mmSite.getId();
// Try to get data from online attempts.
return self.searchOnlineAttemptUserData(scorm.id, lastOnline).then(function(userData) {
return self.searchOnlineAttemptUserData(scorm.id, lastOnline, siteId).then(function(userData) {
// We're creating a new attempt, remove all the user data that is not needed for a new attempt.
// We need to get the SCO data from here because WS get_scoes doesn't return sco_data in Moodle 3.0.
angular.forEach(userData, function(sco) {
Expand All @@ -119,7 +124,7 @@ angular.module('mm.addons.mod_scorm')
});
sco.userdata = filtered;
});
return $mmaModScormOffline.createNewAttempt(scorm, undefined, newAttempt, userData);
return $mmaModScormOffline.createNewAttempt(siteId, scorm, undefined, newAttempt, userData);
}).catch(function() {
return $q.reject($translate.instant('mma.mod_scorm.errorcreateofflineattempt'));
});
Expand Down Expand Up @@ -162,9 +167,11 @@ angular.module('mm.addons.mod_scorm')
* @name $mmaModScormHelper#determineAttemptToContinue
* @param {Object} scorm SCORM.
* @param {Object} attempts Result of $mmaModScorm#getAttemptCount.
* @param {String} [siteId] Site ID. If not defined, current site.
* @return {Promise} Promise resolved with an object with 2 properties: 'number' and 'offline'.
*/
self.determineAttemptToContinue = function(scorm, attempts) {
self.determineAttemptToContinue = function(scorm, attempts, siteId) {
siteId = siteId || $mmSite.getId();
var lastOnline,
result = {
number: 0,
Expand All @@ -190,7 +197,7 @@ angular.module('mm.addons.mod_scorm')
if (lastOnline) {
// Check if last online incomplete.
var hasOffline = attempts.offline.indexOf(lastOnline) > -1;
return $mmaModScorm.isAttemptIncomplete(scorm.id, lastOnline, hasOffline).then(function(incomplete) {
return $mmaModScorm.isAttemptIncomplete(scorm.id, lastOnline, hasOffline, false, siteId).then(function(incomplete) {
if (incomplete) {
result.number = lastOnline;
result.offline = hasOffline;
Expand All @@ -212,20 +219,22 @@ angular.module('mm.addons.mod_scorm')
* @module mm.addons.mod_scorm
* @ngdoc method
* @name $mmaModScormHelper#getFirstSco
* @param {String} scormid Scorm ID.
* @param {String} scormId Scorm ID.
* @param {Object[]} [toc] SCORM's TOC.
* @param {String} [organization] Organization to use.
* @param {Number} attempt Attempt number.
* @param {Boolean} offline True if attempt is offline, false otherwise.
* @param {String} [siteId] Site ID. If not defined, current site.
* @return {Promise} Promise resolved with the first SCO.
*/
self.getFirstSco = function(scormid, toc, organization, attempt, offline) {
self.getFirstSco = function(scormId, toc, organization, attempt, offline, siteId) {
siteId = siteId || $mmSite.getId();
var promise;
if (toc && toc.length) {
promise = $q.when(toc);
} else {
// SCORM doesn't have a TOC. Get all the scos.
promise = $mmaModScorm.getScosWithData(scormid, organization, attempt, offline);
promise = $mmaModScorm.getScosWithData(scormId, organization, attempt, offline, false, siteId);
}

return promise.then(function(scos) {
Expand Down Expand Up @@ -312,10 +321,11 @@ angular.module('mm.addons.mod_scorm')
* @ngdoc method
* @name $mmaModScormHelper#getScormReadableSyncTime
* @param {Number} scormId SCORM ID.
* @param {String} [siteId] Site ID. If not defined, current site.
* @return {Promise} Promise resolved with the readable time.
*/
self.getScormReadableSyncTime = function(scormId) {
return $mmaModScormSync.getScormSyncTime(scormId).then(function(time) {
self.getScormReadableSyncTime = function(scormId, siteId) {
return $mmaModScormSync.getScormSyncTime(scormId, siteId).then(function(time) {
if (time == 0) {
return $translate('mm.core.none');
} else {
Expand All @@ -331,15 +341,17 @@ angular.module('mm.addons.mod_scorm')
* @module mm.addons.mod_scorm
* @ngdoc method
* @name $mmaModScormHelper#searchOnlineAttemptUserData
* @param {Number} scormId SCORM ID.
* @param {Number} attempt Online attempt to get the data.
* @return {Promise} Promise resolved with user data.
* @param {Number} scormId SCORM ID.
* @param {Number} attempt Online attempt to get the data.
* @param {String} [siteId] Site ID. If not defined, current site.
* @return {Promise} Promise resolved with user data.
*/
self.searchOnlineAttemptUserData = function(scormId, attempt) {
return $mmaModScorm.getScormUserData(scormId, attempt).catch(function() {
self.searchOnlineAttemptUserData = function(scormId, attempt, siteId) {
siteId = siteId || $mmSite.getId();
return $mmaModScorm.getScormUserData(scormId, attempt, false, siteId).catch(function() {
if (attempt > 0) {
// We couldn't retrieve the data. Try again with the previous online attempt.
return self.searchOnlineAttemptUserData(scormId, attempt - 1);
return self.searchOnlineAttemptUserData(scormId, attempt - 1, siteId);
} else {
// No more attempts to try. Reject
return $q.reject();
Expand Down
Loading