@@ -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} ) ;
0 commit comments