@@ -21,8 +21,10 @@ angular.module('mm.addons.mod_book')
2121 * @ngdoc service
2222 * @name $mmaModBookCourseContentHandler
2323 */
24- . factory ( '$mmaModBookCourseContentHandler' , function ( $mmCourse , $mmaModBook , $mmFilepool , $mmEvents , $state , $mmSite , $mmUtil ,
25- mmCoreEventQueueEmpty ) {
24+ . factory ( '$mmaModBookCourseContentHandler' , function ( $mmCourse , $mmaModBook , $mmEvents , $state , $mmSite , $mmUtil , $mmFilepool ,
25+ $mmCoursePrefetchDelegate , mmCoreDownloading , mmCoreNotDownloaded , mmCoreOutdated , mmCoreDownloaded ,
26+ mmCoreEventPackageStatusChanged , mmaModBookComponent ) {
27+
2628 var self = { } ;
2729
2830 /**
@@ -34,9 +36,7 @@ angular.module('mm.addons.mod_book')
3436 * @return {Boolean }
3537 */
3638 self . isEnabled = function ( ) {
37- var version = $mmSite . getInfo ( ) . version ;
38- // Require Moodle 2.9.
39- return version && ( parseInt ( version ) >= 2015051100 ) && $mmSite . canDownloadFiles ( ) ;
39+ return $mmaModBook . isPluginEnabled ( ) ;
4040 } ;
4141
4242 /**
@@ -53,107 +53,18 @@ angular.module('mm.addons.mod_book')
5353 return function ( $scope ) {
5454 var downloadBtn ,
5555 refreshBtn ,
56- observers = { } ,
57- queueObserver ,
58- previousState ,
59- siteid = $mmSite . getId ( ) ,
60- revision = $mmCourse . getRevisionFromContents ( module . contents ) ,
61- timemodified = $mmCourse . getTimemodifiedFromContents ( module . contents ) ;
62-
63- // Add queue observer to clear observers when filepool queue is empty. Needed because sometimes when "restoring"
64- // downloading the spinner was shown forever, probably because a file download finished before observer was set.
65- function addQueueObserver ( ) {
66- queueObserver = $mmEvents . on ( mmCoreEventQueueEmpty , function ( ) {
67- // Queue is empty. Clear observers.
68- if ( queueObserver ) {
69- queueObserver . off ( ) ;
70- }
71- if ( Object . keys ( observers ) . length ) {
72- clearObservers ( ) ;
73- setDownloaded ( ) ;
74- }
75- delete queueObserver ;
76- } ) ;
77- }
78-
79- // Add observers to monitor file downloads.
80- function addObservers ( eventNames , isOpeningModule ) {
81- angular . forEach ( eventNames , function ( e ) {
82- if ( typeof observers [ e ] == 'undefined' ) {
83- observers [ e ] = $mmEvents . on ( e , function ( data ) {
84- if ( data . success ) {
85- // Download success. Disable this observer and check if all files have been downloaded.
86- if ( typeof observers [ e ] !== 'undefined' ) {
87- observers [ e ] . off ( ) ;
88- delete observers [ e ] ;
89- }
90- if ( Object . keys ( observers ) . length < 1 ) {
91- setDownloaded ( ) ;
92- }
93- } else if ( data . success === false ) {
94- // A download failed. Clear observers, show error message and set previous state.
95- clearObservers ( ) ;
96- $scope . spinner = false ;
97- $mmCourse . storeModuleStatus ( siteid , module . id , previousState , revision , timemodified ) ;
98- if ( previousState === $mmFilepool . FILENOTDOWNLOADED ) {
99- downloadBtn . hidden = false ;
100- } else {
101- refreshBtn . hidden = false ;
102- }
103- // Don't show error message if state left or the module is being opened.
104- if ( ! $scope . $$destroyed && ! isOpeningModule ) {
105- $mmUtil . showErrorModal ( 'mm.core.errordownloading' , true ) ;
106- }
107- }
108-
109- } ) ;
110- }
111- } ) ;
112- }
113-
114- // Disable file download observers.
115- function clearObservers ( ) {
116- angular . forEach ( observers , function ( observer ) {
117- observer . off ( ) ;
118- } ) ;
119- observers = { } ;
120- }
121-
122- // Set module as 'downloaded', hiding icons and storing its state.
123- function setDownloaded ( ) {
124- $scope . spinner = false ;
125- downloadBtn . hidden = true ;
126- refreshBtn . hidden = false ; // Always show refresh button because revision and timemodified are not reliable.
127- // Store module as downloaded.
128- $mmCourse . storeModuleStatus ( siteid , module . id , $mmFilepool . FILEDOWNLOADED , revision , timemodified ) ;
129- }
130-
131- // Show downloading spinner and hide other icons.
132- function showDownloading ( ) {
133- downloadBtn . hidden = true ;
134- refreshBtn . hidden = true ;
135- $scope . spinner = true ;
136- }
56+ revision = $mmFilepool . getRevisionFromFileList ( module . contents ) ,
57+ timemodified = $mmFilepool . getTimemodifiedFromFileList ( module . contents ) ;
13758
13859 downloadBtn = {
13960 hidden : true ,
14061 icon : 'ion-ios-cloud-download' ,
14162 label : 'mm.core.download' ,
14263 action : function ( e ) {
143- var eventNames ;
144-
14564 e . preventDefault ( ) ;
14665 e . stopPropagation ( ) ;
147-
148- showDownloading ( ) ;
149-
150- $mmaModBook . getFileEventNames ( module ) . then ( function ( eventNames ) {
151- previousState = $mmFilepool . FILENOTDOWNLOADED ;
152- addObservers ( eventNames , false ) ;
153- $mmaModBook . prefetchContent ( module ) ;
154- // Store module as dowloading.
155- $mmCourse . storeModuleStatus ( siteid , module . id , $mmFilepool . FILEDOWNLOADING , revision , timemodified ) ;
156- addQueueObserver ( ) ;
66+ $mmaModBook . prefetchContent ( module ) . catch ( function ( ) {
67+ $mmUtil . showErrorModal ( 'mm.core.errordownloading' , true ) ;
15768 } ) ;
15869 }
15970 } ;
@@ -166,16 +77,9 @@ angular.module('mm.addons.mod_book')
16677 e . preventDefault ( ) ;
16778 e . stopPropagation ( ) ;
16879
169- showDownloading ( ) ;
170-
171- $mmaModBook . invalidateContent ( module . id ) . then ( function ( ) {
172- $mmaModBook . getFileEventNames ( module ) . then ( function ( eventNames ) {
173- previousState = $mmFilepool . FILEOUTDATED ;
174- addObservers ( eventNames , false ) ;
175- $mmaModBook . prefetchContent ( module ) ;
176- // Store module as dowloading.
177- $mmCourse . storeModuleStatus ( siteid , module . id , $mmFilepool . FILEDOWNLOADING , revision , timemodified ) ;
178- addQueueObserver ( ) ;
80+ $mmaModBook . invalidateContent ( module . id ) . finally ( function ( ) {
81+ $mmaModBook . prefetchContent ( module ) . catch ( function ( ) {
82+ $mmUtil . showErrorModal ( 'mm.core.errordownloading' , true ) ;
17983 } ) ;
18084 } ) ;
18185 }
@@ -187,46 +91,33 @@ angular.module('mm.addons.mod_book')
18791 $scope . spinner = false ;
18892
18993 $scope . action = function ( e ) {
190- // Refresh or download icon shown. Let's add observers to monitor download.
191- previousState = downloadBtn . hidden ? $mmFilepool . FILEOUTDATED : $mmFilepool . FILENOTDOWNLOADED ;
192- $mmaModBook . getFileEventNames ( module ) . then ( function ( eventNames ) {
193- addObservers ( eventNames , true ) ;
194- } ) ;
195- $mmCourse . storeModuleStatus ( siteid , module . id , $mmFilepool . FILEDOWNLOADING , revision , timemodified ) ;
196- showDownloading ( ) ;
94+ e . preventDefault ( ) ;
95+ e . stopPropagation ( ) ;
19796 $state . go ( 'site.mod_book' , { module : module , courseid : courseid } ) ;
19897 } ;
19998
200- // Check current status to decide which icon should be shown.
201- $mmCourse . getModuleStatus ( siteid , module . id , revision , timemodified ) . then ( function ( status ) {
202- if ( status == $mmFilepool . FILENOTDOWNLOADED ) {
203- downloadBtn . hidden = false ;
204- } else if ( status == $mmFilepool . FILEDOWNLOADING ) {
205- $scope . spinner = true ;
206- $mmaModBook . getDownloadingFilesEventNames ( module ) . then ( function ( eventNames ) {
207- if ( eventNames . length ) {
208- $mmCourse . getModulePreviousStatus ( siteid , module . id ) . then ( function ( previous ) {
209- previousState = previous ;
210- } ) ;
211- addObservers ( eventNames , false ) ;
212- addQueueObserver ( ) ;
213- } else {
214- // Weird case, state downloading but no files being downloaded. Set state to previousState.
215- $mmCourse . getModulePreviousStatus ( siteid , module . id ) . then ( function ( previous ) {
216- $scope . spinner = false ;
217- if ( previous === $mmFilepool . FILENOTDOWNLOADED ) {
218- downloadBtn . hidden = false ;
219- } else {
220- refreshBtn . hidden = false ;
221- }
222- $mmCourse . storeModuleStatus ( siteid , module . id , previous , revision , timemodified ) ;
223- } ) ;
224- }
225- } ) ;
226- } else {
227- // Show refresh button also if FILEDOWNLOADED because revision and timemodified are not reliable.
228- refreshBtn . hidden = false ;
99+ // Show buttons according to module status.
100+ function showStatus ( status ) {
101+ if ( status ) {
102+ $scope . spinner = status === mmCoreDownloading ;
103+ downloadBtn . hidden = status !== mmCoreNotDownloaded ;
104+ // Always show refresh button if a book is downloaded because revision and timemodified aren't reliable.
105+ refreshBtn . hidden = status !== mmCoreOutdated && status !== mmCoreDownloaded ;
229106 }
107+ }
108+
109+ // Listen for changes on this module status.
110+ var statusObserver = $mmEvents . on ( mmCoreEventPackageStatusChanged , function ( data ) {
111+ if ( data . siteid === $mmSite . getId ( ) && data . componentId === module . id && data . component === mmaModBookComponent ) {
112+ showStatus ( data . status ) ;
113+ }
114+ } ) ;
115+
116+ // Get current status to decide which icon should be shown.
117+ $mmCoursePrefetchDelegate . getModuleStatus ( module , revision , timemodified ) . then ( showStatus ) ;
118+
119+ $scope . $on ( '$destroy' , function ( ) {
120+ statusObserver && statusObserver . off && statusObserver . off ( ) ;
230121 } ) ;
231122 } ;
232123 } ;
0 commit comments