@@ -23,11 +23,14 @@ angular.module('mm.core.course')
2323 */
2424. controller ( 'mmCourseSectionsCtrl' , function ( $mmCourse , $mmUtil , $scope , $stateParams , $translate , $mmCourseHelper , $mmEvents ,
2525 $mmSite , $mmCoursePrefetchDelegate , $mmCourses , $q , $ionicHistory , $ionicPlatform , mmCoreCourseAllSectionsId ,
26- mmCoreEventSectionStatusChanged , $mmConfig , mmCoreSettingsDownloadSection ) {
27- var courseid = $stateParams . courseid ,
26+ mmCoreEventSectionStatusChanged , $mmConfig , mmCoreSettingsDownloadSection , $state , $timeout ) {
27+ var courseId = $stateParams . courseid ,
28+ sectionId = $stateParams . sid ,
29+ moduleId = $stateParams . moduleid ,
2830 downloadSectionsEnabled ;
2931
30- $scope . courseid = courseid ;
32+ $scope . courseId = courseId ;
33+ $scope . sectionToLoad = 2 ; // Load "General" section by default.
3134
3235 function checkDownloadSectionsEnabled ( ) {
3336 return $mmConfig . get ( mmCoreSettingsDownloadSection , true ) . then ( function ( enabled ) {
@@ -40,10 +43,10 @@ angular.module('mm.core.course')
4043
4144 function loadSections ( refresh ) {
4245 // Get full course data. If not refreshing we'll try to get it from cache to speed up the response.
43- return $mmCourses . getUserCourse ( courseid ) . then ( function ( course ) {
46+ return $mmCourses . getUserCourse ( courseId ) . then ( function ( course ) {
4447 $scope . fullname = course . fullname ;
4548 // Get the sections.
46- return $mmCourse . getSections ( courseid ) . then ( function ( sections ) {
49+ return $mmCourse . getSections ( courseId ) . then ( function ( sections ) {
4750 // Add a fake first section (all sections).
4851 return $translate ( 'mm.course.allsections' ) . then ( function ( str ) {
4952 // Adding fake first section.
@@ -56,7 +59,7 @@ angular.module('mm.core.course')
5659
5760 if ( downloadSectionsEnabled ) {
5861 // Calculate status of the sections.
59- return $mmCourseHelper . calculateSectionsStatus ( result , courseid , true , refresh ) . catch ( function ( ) {
62+ return $mmCourseHelper . calculateSectionsStatus ( result , courseId , true , refresh ) . catch ( function ( ) {
6063 // Ignore errors (shouldn't happen).
6164 } ) . then ( function ( downloadpromises ) {
6265 // If we restored any download we'll recalculate the status once all of them have finished.
@@ -68,7 +71,7 @@ angular.module('mm.core.course')
6871 } ) . finally ( function ( ) {
6972 if ( ! $scope . $$destroyed ) {
7073 // Recalculate the status.
71- $mmCourseHelper . calculateSectionsStatus ( $scope . sections , courseid , false ) ;
74+ $mmCourseHelper . calculateSectionsStatus ( $scope . sections , courseId , false ) ;
7275 }
7376 } ) ;
7477 }
@@ -88,7 +91,7 @@ angular.module('mm.core.course')
8891 // Prefetch a section. The second parameter indicates if the prefetch was started manually (true)
8992 // or it was automatically started because all modules are being downloaded (false).
9093 function prefetch ( section , manual ) {
91- $mmCourseHelper . prefetch ( section , courseid , $scope . sections ) . catch ( function ( ) {
94+ $mmCourseHelper . prefetch ( section , courseId , $scope . sections ) . catch ( function ( ) {
9295 // Don't show error message if scope is destroyed or it's an automatic download but we aren't in this state.
9396 if ( $scope . $$destroyed ) {
9497 return ;
@@ -105,15 +108,41 @@ angular.module('mm.core.course')
105108 } ) . finally ( function ( ) {
106109 if ( ! $scope . $$destroyed ) {
107110 // Recalculate the status.
108- $mmCourseHelper . calculateSectionsStatus ( $scope . sections , courseid , false ) ;
111+ $mmCourseHelper . calculateSectionsStatus ( $scope . sections , courseId , false ) ;
109112 }
110113 } ) ;
111114 }
112115
116+ // Convenience function to autoload a section if sectionId param is set.
117+ function autoloadSection ( ) {
118+ if ( sectionId ) {
119+ if ( $ionicPlatform . isTablet ( ) ) {
120+ // Search the position of the section to load.
121+ angular . forEach ( $scope . sections , function ( section , index ) {
122+ if ( section . id == sectionId ) {
123+ $scope . sectionToLoad = index + 1 ;
124+ }
125+ } ) ;
126+ // Set moduleId to pass it to the new state when the section is autoloaded. We unset it after this
127+ // to prevent autoloading the module when the user manually loads a section.
128+ $scope . moduleId = moduleId ;
129+ $timeout ( function ( ) {
130+ $scope . moduleId = null ; // Unset moduleId when
131+ } , 500 ) ;
132+ } else {
133+ $state . go ( 'site.mm_course-section' , {
134+ sectionid : sectionId ,
135+ cid : courseId ,
136+ mid : moduleId
137+ } ) ;
138+ }
139+ }
140+ }
141+
113142 $scope . doRefresh = function ( ) {
114143 var promises = [ ] ;
115144 promises . push ( $mmCourses . invalidateUserCourses ( ) ) ;
116- promises . push ( $mmCourse . invalidateSections ( courseid ) ) ;
145+ promises . push ( $mmCourse . invalidateSections ( courseId ) ) ;
117146
118147 $q . all ( promises ) . finally ( function ( ) {
119148 loadSections ( true ) . finally ( function ( ) {
@@ -126,13 +155,14 @@ angular.module('mm.core.course')
126155 e . preventDefault ( ) ;
127156 e . stopPropagation ( ) ;
128157
129- $mmCourseHelper . confirmDownloadSize ( courseid , section , $scope . sections ) . then ( function ( ) {
158+ $mmCourseHelper . confirmDownloadSize ( courseId , section , $scope . sections ) . then ( function ( ) {
130159 prefetch ( section , true ) ;
131160 } ) ;
132161 } ;
133162
134163 checkDownloadSectionsEnabled ( ) . then ( function ( ) {
135164 loadSections ( ) . finally ( function ( ) {
165+ autoloadSection ( ) ;
136166 $scope . sectionsLoaded = true ;
137167 } ) ;
138168 } ) ;
@@ -148,7 +178,7 @@ angular.module('mm.core.course')
148178 }
149179
150180 // Recalculate the status.
151- $mmCourseHelper . calculateSectionsStatus ( $scope . sections , courseid , false ) . then ( function ( ) {
181+ $mmCourseHelper . calculateSectionsStatus ( $scope . sections , courseId , false ) . then ( function ( ) {
152182 var section ;
153183 angular . forEach ( $scope . sections , function ( s ) {
154184 if ( s . id === data . sectionid ) {
0 commit comments