Skip to content

Commit ddfb8d7

Browse files
committed
MOBILE-1391 scorm: Add siteId param to SCORM functions
1 parent 10fd2f5 commit ddfb8d7

File tree

8 files changed

+977
-784
lines changed

8 files changed

+977
-784
lines changed

upgrade.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
This files describes API changes in the Moodle Mobile app,
22
information provided here is intended especially for developers.
33

4+
=== 2.9 ===
5+
6+
* 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.
7+
48
=== 2.8 ===
59

610
* $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.

www/addons/mod_scorm/controllers/player.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ angular.module('mm.addons.mod_scorm')
4040
$scope.loadingToc = true;
4141

4242
if (scorm.popup) {
43-
// Fix for bug in WS not returning %. If we receive a value <= 100 we'll assume it's a percentage.
43+
// If we receive a value <= 100 we need to assume it's a percentage.
4444
if (scorm.width <= 100) {
4545
scorm.width = scorm.width + '%';
4646
}
@@ -98,7 +98,8 @@ angular.module('mm.addons.mod_scorm')
9898
promise = $mmaModScormHelper.createOfflineAttempt(scorm, result.attempt, attemptsData.online.length);
9999
} else {
100100
// Last attempt was online, verify that we can create a new online attempt. We ignore cache.
101-
promise = $mmaModScorm.getScormUserData(scorm.id, result.attempt, false, undefined, true).catch(function() {
101+
promise = $mmaModScorm.getScormUserData(scorm.id, result.attempt, false, undefined, undefined, true)
102+
.catch(function() {
102103
// Cannot communicate with the server, create an offline attempt.
103104
offline = true;
104105
return $mmaModScormHelper.createOfflineAttempt(scorm, result.attempt, attemptsData.online.length);
@@ -245,7 +246,7 @@ angular.module('mm.addons.mod_scorm')
245246
return $mmaModScorm.saveTracks(scoId, attempt, tracks, offline, scorm).then(function() {
246247
if (!offline) {
247248
// New online attempt created, update cached data about online attempts.
248-
$mmaModScorm.getAttemptCount(scorm.id, undefined, false, true);
249+
$mmaModScorm.getAttemptCount(scorm.id, undefined, undefined, false, true);
249250
}
250251
});
251252
}

www/addons/mod_scorm/main.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,41 +64,60 @@ angular.module('mm.addons.mod_scorm', ['mm.core'])
6464
$mmCoursePrefetchDelegateProvider.registerPrefetchHandler('mmaModScorm', 'scorm', '$mmaModScormPrefetchHandler');
6565
})
6666

67-
.run(function($timeout, $mmaModScormSync, $mmApp, $mmEvents, $mmaModScormOnline, $mmaModScormOffline, mmCoreEventLogin,
68-
mmCoreEventLogout) {
67+
.run(function($timeout, $mmaModScormSync, $mmApp, $mmEvents, $mmSite, mmCoreEventLogin) {
6968
var lastExecution = 0,
70-
executing = false;
69+
executing = false,
70+
allSitesCalled = false;
7171

72-
function syncScorms() {
72+
function syncScorms(allSites) {
7373
var now = new Date().getTime();
74+
75+
if (!allSites && !$mmSite.isLoggedIn()) {
76+
return;
77+
}
78+
7479
// Prevent consecutive and simultaneous executions. A sync process shouldn't take more than a few minutes,
7580
// so if it's been more than 5 minutes since the last execution we'll ignore the executing value.
7681
if (now - 5000 > lastExecution && (!executing || now - 300000 > lastExecution)) {
7782
lastExecution = new Date().getTime();
7883
executing = true;
7984

8085
$timeout(function() { // Minor delay just to make sure network is fully established.
81-
$mmaModScormSync.syncAllScorms().finally(function() {
86+
$mmaModScormSync.syncAllScorms(allSites ? undefined : $mmSite.getId()).finally(function() {
8287
executing = false;
8388
});
8489
}, 1000);
8590
}
8691
}
8792

8893
$mmApp.ready().then(function() {
89-
document.addEventListener('online', syncScorms, false); // Cordova event.
90-
window.addEventListener('online', syncScorms, false); // HTML5 event.
94+
document.addEventListener('online', function() {
95+
syncScorms(false);
96+
}, false); // Cordova event.
97+
window.addEventListener('online', function() {
98+
syncScorms(false);
99+
}, false); // HTML5 event.
100+
101+
if (!$mmSite.isLoggedIn()) {
102+
// App was started without any site logged in. Try to sync all sites.
103+
allSitesCalled = true;
104+
if ($mmApp.isOnline()) {
105+
syncScorms(true);
106+
}
107+
}
91108
});
92109

93110
$mmEvents.on(mmCoreEventLogin, function() {
94-
if ($mmApp.isOnline()) {
95-
syncScorms();
111+
var allSites = false;
112+
if (!allSitesCalled) {
113+
// App started with a site logged in. Try to sync all sites.
114+
allSitesCalled = true;
115+
allSites = true;
96116
}
97-
});
98117

99-
$mmEvents.on(mmCoreEventLogout, function() {
100-
$mmaModScormOnline.clearBlockedScorms();
101-
$mmaModScormOffline.clearBlockedScorms();
118+
if ($mmApp.isOnline()) {
119+
syncScorms(allSites);
120+
}
102121
});
103122

104123
});

www/addons/mod_scorm/services/helper.js

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ angular.module('mm.addons.mod_scorm')
2121
* @ngdoc service
2222
* @name $mmCourseHelper
2323
*/
24-
.factory('$mmaModScormHelper', function($mmaModScorm, $mmUtil, $translate, $q, $mmaModScormOffline, $mmaModScormSync) {
24+
.factory('$mmaModScormHelper', function($mmaModScorm, $mmUtil, $translate, $q, $mmaModScormOffline, $mmaModScormSync, $mmSite) {
2525

2626
var self = {},
2727
elementsToIgnore = ['status', 'score_raw', 'total_time', 'session_time', 'student_id', 'student_name', 'credit',
@@ -54,14 +54,17 @@ angular.module('mm.addons.mod_scorm')
5454
* @name $mmaModScormHelper#convertAttemptToOffline
5555
* @param {Object} scorm SCORM.
5656
* @param {Number} attempt Number of the online attempt.
57+
* @param {String} [siteId] Site ID. If not defined, current site.
5758
* @return {Promise} Promise resolved when the attempt is created.
5859
*/
59-
self.convertAttemptToOffline = function(scorm, attempt) {
60+
self.convertAttemptToOffline = function(scorm, attempt, siteId) {
61+
siteId = siteId || $mmSite.getId();
62+
6063
// Get data from the online attempt.
61-
return $mmaModScorm.getScormUserData(scorm.id, attempt).then(function(onlineData) {
64+
return $mmaModScorm.getScormUserData(scorm.id, attempt, false, siteId).then(function(onlineData) {
6265
// The SCORM API might have written some data to the offline attempt already.
6366
// We don't want to override it with cached online data.
64-
return $mmaModScormOffline.getScormUserData(scorm.id, attempt).catch(function() {
67+
return $mmaModScormOffline.getScormUserData(siteId, scorm.id, attempt).catch(function() {
6568
// Ignore errors.
6669
}).then(function(offlineData) {
6770
var dataToStore = angular.copy(onlineData);
@@ -85,7 +88,7 @@ angular.module('mm.addons.mod_scorm')
8588
}
8689
});
8790

88-
return $mmaModScormOffline.createNewAttempt(scorm, undefined, attempt, dataToStore, onlineData);
91+
return $mmaModScormOffline.createNewAttempt(siteId, scorm, undefined, attempt, dataToStore, onlineData);
8992
});
9093
}).catch(function() {
9194
// Shouldn't happen.
@@ -102,11 +105,13 @@ angular.module('mm.addons.mod_scorm')
102105
* @param {Object} scorm SCORM.
103106
* @param {Number} newAttempt Number of the new attempt.
104107
* @param {Number} lastOnline Number of the last online attempt.
108+
* @param {String} [siteId] Site ID. If not defined, current site.
105109
* @return {Promise} Promise resolved when the attempt is created.
106110
*/
107-
self.createOfflineAttempt = function(scorm, newAttempt, lastOnline) {
111+
self.createOfflineAttempt = function(scorm, newAttempt, lastOnline, siteId) {
112+
siteId = siteId || $mmSite.getId();
108113
// Try to get data from online attempts.
109-
return self.searchOnlineAttemptUserData(scorm.id, lastOnline).then(function(userData) {
114+
return self.searchOnlineAttemptUserData(scorm.id, lastOnline, siteId).then(function(userData) {
110115
// We're creating a new attempt, remove all the user data that is not needed for a new attempt.
111116
// We need to get the SCO data from here because WS get_scoes doesn't return sco_data in Moodle 3.0.
112117
angular.forEach(userData, function(sco) {
@@ -119,7 +124,7 @@ angular.module('mm.addons.mod_scorm')
119124
});
120125
sco.userdata = filtered;
121126
});
122-
return $mmaModScormOffline.createNewAttempt(scorm, undefined, newAttempt, userData);
127+
return $mmaModScormOffline.createNewAttempt(siteId, scorm, undefined, newAttempt, userData);
123128
}).catch(function() {
124129
return $q.reject($translate.instant('mma.mod_scorm.errorcreateofflineattempt'));
125130
});
@@ -162,9 +167,11 @@ angular.module('mm.addons.mod_scorm')
162167
* @name $mmaModScormHelper#determineAttemptToContinue
163168
* @param {Object} scorm SCORM.
164169
* @param {Object} attempts Result of $mmaModScorm#getAttemptCount.
170+
* @param {String} [siteId] Site ID. If not defined, current site.
165171
* @return {Promise} Promise resolved with an object with 2 properties: 'number' and 'offline'.
166172
*/
167-
self.determineAttemptToContinue = function(scorm, attempts) {
173+
self.determineAttemptToContinue = function(scorm, attempts, siteId) {
174+
siteId = siteId || $mmSite.getId();
168175
var lastOnline,
169176
result = {
170177
number: 0,
@@ -190,7 +197,7 @@ angular.module('mm.addons.mod_scorm')
190197
if (lastOnline) {
191198
// Check if last online incomplete.
192199
var hasOffline = attempts.offline.indexOf(lastOnline) > -1;
193-
return $mmaModScorm.isAttemptIncomplete(scorm.id, lastOnline, hasOffline).then(function(incomplete) {
200+
return $mmaModScorm.isAttemptIncomplete(scorm.id, lastOnline, hasOffline, false, siteId).then(function(incomplete) {
194201
if (incomplete) {
195202
result.number = lastOnline;
196203
result.offline = hasOffline;
@@ -212,20 +219,22 @@ angular.module('mm.addons.mod_scorm')
212219
* @module mm.addons.mod_scorm
213220
* @ngdoc method
214221
* @name $mmaModScormHelper#getFirstSco
215-
* @param {String} scormid Scorm ID.
222+
* @param {String} scormId Scorm ID.
216223
* @param {Object[]} [toc] SCORM's TOC.
217224
* @param {String} [organization] Organization to use.
218225
* @param {Number} attempt Attempt number.
219226
* @param {Boolean} offline True if attempt is offline, false otherwise.
227+
* @param {String} [siteId] Site ID. If not defined, current site.
220228
* @return {Promise} Promise resolved with the first SCO.
221229
*/
222-
self.getFirstSco = function(scormid, toc, organization, attempt, offline) {
230+
self.getFirstSco = function(scormId, toc, organization, attempt, offline, siteId) {
231+
siteId = siteId || $mmSite.getId();
223232
var promise;
224233
if (toc && toc.length) {
225234
promise = $q.when(toc);
226235
} else {
227236
// SCORM doesn't have a TOC. Get all the scos.
228-
promise = $mmaModScorm.getScosWithData(scormid, organization, attempt, offline);
237+
promise = $mmaModScorm.getScosWithData(scormId, organization, attempt, offline, false, siteId);
229238
}
230239

231240
return promise.then(function(scos) {
@@ -312,10 +321,11 @@ angular.module('mm.addons.mod_scorm')
312321
* @ngdoc method
313322
* @name $mmaModScormHelper#getScormReadableSyncTime
314323
* @param {Number} scormId SCORM ID.
324+
* @param {String} [siteId] Site ID. If not defined, current site.
315325
* @return {Promise} Promise resolved with the readable time.
316326
*/
317-
self.getScormReadableSyncTime = function(scormId) {
318-
return $mmaModScormSync.getScormSyncTime(scormId).then(function(time) {
327+
self.getScormReadableSyncTime = function(scormId, siteId) {
328+
return $mmaModScormSync.getScormSyncTime(scormId, siteId).then(function(time) {
319329
if (time == 0) {
320330
return $translate('mm.core.none');
321331
} else {
@@ -331,15 +341,17 @@ angular.module('mm.addons.mod_scorm')
331341
* @module mm.addons.mod_scorm
332342
* @ngdoc method
333343
* @name $mmaModScormHelper#searchOnlineAttemptUserData
334-
* @param {Number} scormId SCORM ID.
335-
* @param {Number} attempt Online attempt to get the data.
336-
* @return {Promise} Promise resolved with user data.
344+
* @param {Number} scormId SCORM ID.
345+
* @param {Number} attempt Online attempt to get the data.
346+
* @param {String} [siteId] Site ID. If not defined, current site.
347+
* @return {Promise} Promise resolved with user data.
337348
*/
338-
self.searchOnlineAttemptUserData = function(scormId, attempt) {
339-
return $mmaModScorm.getScormUserData(scormId, attempt).catch(function() {
349+
self.searchOnlineAttemptUserData = function(scormId, attempt, siteId) {
350+
siteId = siteId || $mmSite.getId();
351+
return $mmaModScorm.getScormUserData(scormId, attempt, false, siteId).catch(function() {
340352
if (attempt > 0) {
341353
// We couldn't retrieve the data. Try again with the previous online attempt.
342-
return self.searchOnlineAttemptUserData(scormId, attempt - 1);
354+
return self.searchOnlineAttemptUserData(scormId, attempt - 1, siteId);
343355
} else {
344356
// No more attempts to try. Reject
345357
return $q.reject();

0 commit comments

Comments
 (0)