Skip to content

Commit 5b3bf52

Browse files
authored
Merge pull request #936 from dpalou/MOBILE-1987
Mobile 1987
2 parents ac6a267 + 010c26d commit 5b3bf52

File tree

4 files changed

+91
-15
lines changed

4 files changed

+91
-15
lines changed

upgrade.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ information provided here is intended especially for developers.
2323
* A new field has been added to config.json: privacypolicy.
2424
* String identifiers mm.core.elementseparator and mm.core.fieldvalueseparator have been renamed to:
2525
mm.core.listsep and mm.core.labelsep
26+
* Filepool now also throws file events (getFileEventNameByUrl) when files are deleted, outdated or start downloading. It passes an 'action' property to identify the exact action.
2627

2728
=== 3.2 ===
2829

www/core/components/courses/controllers/viewresult.js

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ angular.module('mm.core.courses')
2222
* @name mmCoursesViewResultCtrl
2323
*/
2424
.controller('mmCoursesViewResultCtrl', function($scope, $stateParams, $mmCourses, $mmCoursesDelegate, $mmUtil, $translate, $q,
25-
$ionicModal, $mmEvents, $mmSite, mmCoursesSearchComponent, mmCoursesEnrolInvalidKey, mmCoursesEventMyCoursesUpdated) {
25+
$ionicModal, $mmEvents, $mmSite, mmCoursesSearchComponent, mmCoursesEnrolInvalidKey, mmCoursesEventMyCoursesUpdated,
26+
$timeout) {
2627

27-
var course = $stateParams.course || {},
28+
var course = angular.copy($stateParams.course || {}), // Copy the object to prevent modifying the one from the previous view.
2829
selfEnrolWSAvailable = $mmCourses.isSelfEnrolmentEnabled(),
2930
guestWSAvailable = $mmCourses.isGuestWSAvailable(),
3031
isGuestEnabled = false,
3132
guestInstanceId,
32-
enrollmentMethods;
33+
enrollmentMethods,
34+
waitStart = 0;
3335

3436
$scope.course = course;
3537
$scope.component = mmCoursesSearchComponent;
@@ -82,12 +84,12 @@ angular.module('mm.core.courses')
8284
// Success retrieving the course, we can assume the user has permissions to view it.
8385
course.fullname = c.fullname || course.fullname;
8486
course.summary = c.summary || course.summary;
85-
return loadCourseNavHandlers(refresh);
87+
return loadCourseNavHandlers(refresh, false);
8688
}).catch(function() {
8789
// The user is not an admin/manager. Check if we can provide guest access to the course.
8890
return canAccessAsGuest().then(function(passwordRequired) {
8991
if (!passwordRequired) {
90-
return loadCourseNavHandlers(refresh);
92+
return loadCourseNavHandlers(refresh, true);
9193
} else {
9294
course._handlers = [];
9395
$scope.handlersShouldBeShown = false;
@@ -97,6 +99,8 @@ angular.module('mm.core.courses')
9799
$scope.handlersShouldBeShown = false;
98100
});
99101
});
102+
}).finally(function() {
103+
$scope.courseLoaded = true;
100104
});
101105
}
102106

@@ -126,7 +130,7 @@ angular.module('mm.core.courses')
126130
}
127131

128132
// Load course nav handlers.
129-
function loadCourseNavHandlers(refresh) {
133+
function loadCourseNavHandlers(refresh, guest) {
130134
var promises = [],
131135
navOptions,
132136
admOptions;
@@ -147,8 +151,9 @@ angular.module('mm.core.courses')
147151
}));
148152

149153
return $q.all(promises).then(function() {
150-
course._handlers = $mmCoursesDelegate.getNavHandlersFor(
151-
course.id, refresh, navOptions[course.id], admOptions[course.id]);
154+
var getHandlersFn = guest ? $mmCoursesDelegate.getNavHandlersForGuest : $mmCoursesDelegate.getNavHandlersFor;
155+
course._handlers = getHandlersFn(course.id, refresh, navOptions[course.id], admOptions[course.id]);
156+
$scope.handlersShouldBeShown = true;
152157
});
153158

154159
}
@@ -172,9 +177,7 @@ angular.module('mm.core.courses')
172177
});
173178
}
174179

175-
getCourse().finally(function() {
176-
$scope.courseLoaded = true;
177-
});
180+
getCourse();
178181

179182
$scope.doRefresh = function() {
180183
refreshData().finally(function() {
@@ -217,8 +220,13 @@ angular.module('mm.core.courses')
217220
$mmCourses.selfEnrol(course.id, password, instanceId).then(function() {
218221
// Close modal and refresh data.
219222
$scope.isEnrolled = true;
223+
$scope.courseLoaded = false;
224+
220225
// Don't refresh until modal is closed. See https://github.com/driftyco/ionic/issues/9069
221226
$scope.closeModal().then(function() {
227+
// Sometimes the list of enrolled courses takes a while to be updated. Wait for it.
228+
return waitForEnrolled(true);
229+
}).then(function() {
222230
refreshData().finally(function() {
223231
// My courses have been updated, trigger event.
224232
$mmEvents.trigger(mmCoursesEventMyCoursesUpdated, $mmSite.getId());
@@ -245,5 +253,28 @@ angular.module('mm.core.courses')
245253
});
246254
});
247255
};
256+
257+
function waitForEnrolled(init) {
258+
if (init) {
259+
waitStart = Date.now();
260+
}
261+
262+
// Check if user is enrolled in the course.
263+
return $mmCourses.invalidateUserCourses().catch(function() {
264+
// Ignore errors.
265+
}).then(function() {
266+
return $mmCourses.getUserCourse(course.id);
267+
}).catch(function() {
268+
// Not enrolled, wait a bit and try again.
269+
if ($scope.$$destroyed || (Date.now() - waitStart > 60000)) {
270+
// Max time reached or the user left the view, stop.
271+
return;
272+
}
273+
274+
return $timeout(function() {
275+
return waitForEnrolled();
276+
}, 5000);
277+
});
278+
}
248279
}
249280
});

www/core/directives/file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ angular.module('mm.core')
204204
$mmFilepool.getFileEventNameByUrl(siteId, fileUrl).then(function(eventName) {
205205
observer = $mmEvents.on(eventName, function(data) {
206206
getState(scope, siteId, fileUrl, timeModified, alwaysDownload);
207-
if (!data.success) {
207+
if (data.action == 'download' && !data.success) {
208208
$mmUtil.showErrorModal('mm.core.errordownloading', true);
209209
}
210210
});

www/core/lib/filepool.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ angular.module('mm.core')
431431
}).then(function() {
432432
// Check if the queue is running.
433433
self.checkQueueProcessing();
434+
self._notifyFileDownloading(siteId, fileId);
434435
return self._getQueuePromise(siteId, fileId);
435436
});
436437
}
@@ -803,6 +804,8 @@ angular.module('mm.core')
803804
addExtension = typeof filePath == "undefined",
804805
pathPromise = filePath ? filePath : self._getFilePath(siteId, fileId, extension);
805806

807+
self._notifyFileDownloading(siteId, fileId);
808+
806809
return $q.when(pathPromise).then(function(filePath) {
807810
if (poolFileObject && poolFileObject.fileId !== fileId) {
808811
$log.error('Invalid object to update passed');
@@ -2055,6 +2058,19 @@ angular.module('mm.core')
20552058
return fileObject.stale || revision > fileObject.revision || timemodified > fileObject.timemodified;
20562059
};
20572060

2061+
/**
2062+
* Notify a file has been deleted.
2063+
*
2064+
* @module mm.core
2065+
* @ngdoc method
2066+
* @name $mmFilepool#_notifyFileDeleted
2067+
* @param {String} siteId The site ID.
2068+
* @param {String} fileId The file ID.
2069+
*/
2070+
self._notifyFileDeleted = function(siteId, fileId) {
2071+
$mmEvents.trigger(self._getFileEventName(siteId, fileId), {action: 'deleted'});
2072+
};
2073+
20582074
/**
20592075
* Notify a file has been downloaded.
20602076
*
@@ -2065,7 +2081,7 @@ angular.module('mm.core')
20652081
* @param {String} fileId The file ID.
20662082
*/
20672083
self._notifyFileDownloaded = function(siteId, fileId) {
2068-
$mmEvents.trigger(self._getFileEventName(siteId, fileId), {success: true});
2084+
$mmEvents.trigger(self._getFileEventName(siteId, fileId), {action: 'download', success: true});
20692085
};
20702086

20712087
/**
@@ -2078,7 +2094,33 @@ angular.module('mm.core')
20782094
* @param {String} fileId The file ID.
20792095
*/
20802096
self._notifyFileDownloadError = function(siteId, fileId) {
2081-
$mmEvents.trigger(self._getFileEventName(siteId, fileId), {success: false});
2097+
$mmEvents.trigger(self._getFileEventName(siteId, fileId), {action: 'download', success: false});
2098+
};
2099+
2100+
/**
2101+
* Notify a file starts being downloaded or added to queue.
2102+
*
2103+
* @module mm.core
2104+
* @ngdoc method
2105+
* @name $mmFilepool#_notifyFileDownloading
2106+
* @param {String} siteId The site ID.
2107+
* @param {String} fileId The file ID.
2108+
*/
2109+
self._notifyFileDownloading = function(siteId, fileId) {
2110+
$mmEvents.trigger(self._getFileEventName(siteId, fileId), {action: 'downloading'});
2111+
};
2112+
2113+
/**
2114+
* Notify a file has been outdated.
2115+
*
2116+
* @module mm.core
2117+
* @ngdoc method
2118+
* @name $mmFilepool#_notifyFileOutdated
2119+
* @param {String} siteId The site ID.
2120+
* @param {String} fileId The file ID.
2121+
*/
2122+
self._notifyFileOutdated = function(siteId, fileId) {
2123+
$mmEvents.trigger(self._getFileEventName(siteId, fileId), {action: 'outdated'});
20822124
};
20832125

20842126
/**
@@ -2352,7 +2394,9 @@ angular.module('mm.core')
23522394
}));
23532395
}
23542396

2355-
return $q.all(promises);
2397+
return $q.all(promises).then(function() {
2398+
self._notifyFileDeleted(siteId, fileId);
2399+
});
23562400
});
23572401
});
23582402
};

0 commit comments

Comments
 (0)