@@ -21,73 +21,10 @@ angular.module('mm.addons.mod_book')
2121 * @ngdoc service
2222 * @name $mmaModBook
2323 */
24- . factory ( '$mmaModBook' , function ( $mmFilepool , $mmSite , $mmFS , $http , $log , $q , $mmCourse , mmaModBookComponent ,
25- mmCoreDownloading , mmCoreDownloaded ) {
24+ . factory ( '$mmaModBook' , function ( $mmFilepool , $mmSite , $mmFS , $http , $log , $q , mmaModBookComponent ) {
2625 $log = $log . getInstance ( '$mmaModBook' ) ;
2726
28- var self = { } ,
29- downloadPromises = { } ; // To handle downloads.
30-
31- /**
32- * Downloads or prefetches all the content.
33- *
34- * @param {Object } module The module object.
35- * @param {Boolean } prefetch True if prefetching, false otherwise.
36- * @return {Promise } Promise resolved when all content is downloaded. Data returned is not reliable.
37- */
38- function downloadOrPrefetch ( module , prefetch ) {
39-
40- var siteid = $mmSite . getId ( ) ;
41- if ( downloadPromises [ siteid ] && downloadPromises [ siteid ] [ module . id ] ) {
42- // There's already a download ongoing for this module, return the promise.
43- return downloadPromises [ siteid ] [ module . id ] ;
44- } else if ( ! downloadPromises [ siteid ] ) {
45- downloadPromises [ siteid ] = { } ;
46- }
47-
48- var revision = $mmCourse . getRevisionFromContents ( module . contents ) ,
49- timemod = $mmCourse . getTimemodifiedFromContents ( module . contents ) ,
50- dwnPromise ,
51- deleted = false ;
52-
53- // Set module as downloading.
54- dwnPromise = $mmCourse . storeModuleStatus ( siteid , module . id , mmCoreDownloading , revision , timemod ) . then ( function ( ) {
55- var promises = [ ] ;
56-
57- angular . forEach ( module . contents , function ( content ) {
58- var url = content . fileurl ,
59- filetimemodified = content . timemodified ;
60- if ( ! self . isFileDownloadable ( content ) ) {
61- return ;
62- }
63-
64- if ( prefetch ) {
65- promises . push ( $mmFilepool . addToQueueByUrl ( siteid , url , mmaModBookComponent , module . id , filetimemodified ) ) ;
66- } else {
67- promises . push ( $mmFilepool . downloadUrl ( siteid , url , false , mmaModBookComponent , module . id , filetimemodified ) ) ;
68- }
69- } ) ;
70-
71- return $q . all ( promises ) . then ( function ( ) {
72- // Success prefetching, store module as downloaded.
73- return $mmCourse . storeModuleStatus ( siteid , module . id , mmCoreDownloaded , revision , timemod ) ;
74- } ) . catch ( function ( ) {
75- // Error downloading, go back to previous status and reject the promise.
76- return $mmCourse . setModulePreviousStatus ( siteid , module . id ) . then ( function ( ) {
77- return $q . reject ( ) ;
78- } ) ;
79- } ) ;
80- } ) . finally ( function ( ) {
81- // Download finished, delete the promise.
82- delete downloadPromises [ siteid ] [ module . id ] ;
83- deleted = true ;
84- } ) ;
85-
86- if ( ! deleted ) { // In case promise was finished immediately.
87- downloadPromises [ siteid ] [ module . id ] = dwnPromise ;
88- }
89- return dwnPromise ;
90- }
27+ var self = { } ;
9128
9229 /**
9330 * Download all the content.
@@ -99,7 +36,8 @@ angular.module('mm.addons.mod_book')
9936 * @return {Promise } Promise resolved when all content is downloaded. Data returned is not reliable.
10037 */
10138 self . downloadAllContent = function ( module ) {
102- return downloadOrPrefetch ( module , false ) ;
39+ var files = self . getDownloadableFiles ( module ) ;
40+ return $mmFilepool . downloadPackage ( $mmSite . getId ( ) , files , mmaModBookComponent , module . id ) ;
10341 } ;
10442
10543 /**
@@ -135,22 +73,6 @@ angular.module('mm.addons.mod_book')
13573 } ) ;
13674 } ;
13775
138- /**
139- * Get a download promise. If the promise is not set, return undefined.
140- *
141- * @module mm.addons.mod_book
142- * @ngdoc method
143- * @name $mmaModBook#getDownloadPromise
144- * @param {String } siteId Site ID.
145- * @param {Number } moduleId Module ID.
146- * @return {Promise } Download promise or undefined.
147- */
148- self . getDownloadPromise = function ( siteId , moduleId ) {
149- if ( downloadPromises [ siteId ] && downloadPromises [ siteId ] [ moduleId ] ) {
150- return downloadPromises [ siteId ] [ moduleId ] ;
151- }
152- } ;
153-
15476 /**
15577 * Returns a list of file event names.
15678 *
@@ -174,6 +96,26 @@ angular.module('mm.addons.mod_book')
17496 } ) ;
17597 } ;
17698
99+ /**
100+ * Returns a list of files that can be downloaded.
101+ *
102+ * @module mm.addons.mod_book
103+ * @ngdoc method
104+ * @name $mmaModBook#getDownloadableFiles
105+ * @param {Object } module The module object returned by WS.
106+ * @return {Object[] } List of files.
107+ */
108+ self . getDownloadableFiles = function ( module ) {
109+ var files = [ ] ;
110+
111+ angular . forEach ( module . contents , function ( content ) {
112+ if ( self . isFileDownloadable ( content ) ) {
113+ files . push ( content ) ;
114+ }
115+ } ) ;
116+
117+ return files ;
118+ } ;
177119
178120 /**
179121 * Get the book toc as an array.
@@ -427,7 +369,8 @@ angular.module('mm.addons.mod_book')
427369 * @return {Promise } Promise resolved when all content is downloaded. Data returned is not reliable.
428370 */
429371 self . prefetchContent = function ( module ) {
430- return downloadOrPrefetch ( module , true ) ;
372+ var files = self . getDownloadableFiles ( module ) ;
373+ return $mmFilepool . prefetchPackage ( $mmSite . getId ( ) , files , mmaModBookComponent , module . id ) ;
431374 } ;
432375
433376 return self ;
0 commit comments