@@ -232,48 +232,27 @@ angular.module('mm.addons.mod_book')
232232 * @module mm.addons.mod_book
233233 * @ngdoc method
234234 * @name $mmaModBook#getChapterContent
235- * @param {Object } contents The module contents .
235+ * @param {Object } contentsMap Contents map returned by $mmaModBook#getContentsMap .
236236 * @param {String } chapterId Chapter to retrieve.
237237 * @param {Integer } moduleId The module ID.
238238 * @return {Promise }
239239 */
240- self . getChapterContent = function ( contents , chapterId , moduleId ) {
241- var indexUrl ,
242- paths = { } ,
240+ self . getChapterContent = function ( contentsMap , chapterId , moduleId ) {
241+ var indexUrl = contentsMap [ chapterId ] ? contentsMap [ chapterId ] . indexUrl : undefined ,
243242 promise ;
244243
245- // Extract the information about paths from the module contents.
246- angular . forEach ( contents , function ( content ) {
247- if ( self . isFileDownloadable ( content ) ) {
248- var key ,
249- url = content . fileurl ;
250-
251- if ( ! indexUrl && content . filename == 'index.html' ) {
252- // First chapter, we don't have a chapter id.
253- if ( content . filepath == "/" + chapterId + "/" ) {
254- indexUrl = url ;
255- }
256- } else {
257- key = content . filename ;
258- paths [ key ] = url ;
259- }
260- }
261- } ) ;
244+ if ( ! indexUrl ) {
245+ // If ever that happens.
246+ $log . debug ( 'Could not locate the index chapter' ) ;
247+ return $q . reject ( ) ;
248+ }
262249
263- // Promise handling when we are in a browser.
264- promise = ( function ( ) {
265- if ( ! indexUrl ) {
266- // If ever that happens.
267- $log . debug ( 'Could not locate the index chapter' ) ;
268- return $q . reject ( ) ;
269- } else if ( $mmFS . isAvailable ( ) ) {
270- // The file system is available.
271- return $mmFilepool . downloadUrl ( $mmSite . getId ( ) , indexUrl , false , mmaModBookComponent , moduleId ) ;
272- } else {
273- // We return the live URL.
274- return $q . when ( $mmSite . fixPluginfileURL ( indexUrl ) ) ;
275- }
276- } ) ( ) ;
250+ if ( $mmFS . isAvailable ( ) ) {
251+ promise = $mmFilepool . downloadUrl ( $mmSite . getId ( ) , indexUrl , false , mmaModBookComponent , moduleId ) ;
252+ } else {
253+ // We return the live URL.
254+ return $q . when ( $mmSite . fixPluginfileURL ( indexUrl ) ) ;
255+ }
277256
278257 return promise . then ( function ( url ) {
279258 // Fetch the URL content.
@@ -283,12 +262,62 @@ angular.module('mm.addons.mod_book')
283262 } else {
284263 // Now that we have the content, we update the SRC to point back to
285264 // the external resource. That will be caught by mm-format-text.
286- return $mmUtil . restoreSourcesInHtml ( response . data , paths ) ;
265+ return $mmUtil . restoreSourcesInHtml ( response . data , contentsMap [ chapterId ] . paths ) ;
287266 }
288267 } ) ;
289268 } ) ;
290269 } ;
291270
271+ /**
272+ * Convert an array of book contents into an object where contents are organized in chapters.
273+ * Each chapter has an indexUrl and the list of contents in that chapter.
274+ *
275+ * @module mm.addons.mod_book
276+ * @ngdoc method
277+ * @name $mmaModBook#getContentsMap
278+ * @param {Object } contents The module contents.
279+ * @return {Object } Contents map.
280+ */
281+ self . getContentsMap = function ( contents ) {
282+ var map = { } ;
283+
284+ angular . forEach ( contents , function ( content ) {
285+ if ( self . isFileDownloadable ( content ) ) {
286+ var chapter ,
287+ matches ,
288+ split ,
289+ filepathIsChapter ;
290+
291+ // Search the chapter number in the filepath.
292+ matches = content . filepath . match ( / \/ ( \d + ) \/ / ) ;
293+ if ( matches && matches [ 1 ] ) {
294+ chapter = matches [ 1 ] ;
295+ filepathIsChapter = content . filepath == '/' + chapter + '/' ;
296+
297+ // Init the chapter if it's not defined yet.
298+ map [ chapter ] = map [ chapter ] || { paths : { } } ;
299+
300+ if ( content . filename == 'index.html' && filepathIsChapter ) {
301+ map [ chapter ] . indexUrl = content . fileurl ;
302+ } else {
303+ if ( filepathIsChapter ) {
304+ // It's a file in the root folder OR the WS isn't returning the filepath as it should (MDL-53671).
305+ // Try to get the path to the file from the URL.
306+ split = content . fileurl . split ( 'mod_book/chapter' + content . filepath ) ;
307+ key = split [ 1 ] || content . filename ; // Use filename if we couldn't find the path.
308+ } else {
309+ // Remove the chapter folder from the path and add the filename.
310+ key = content . filepath . replace ( '/' + chapter + '/' , '' ) + content . filename ;
311+ }
312+ map [ chapter ] . paths [ key ] = content . fileurl ;
313+ }
314+ }
315+ }
316+ } ) ;
317+
318+ return map ;
319+ } ;
320+
292321 /**
293322 * Invalidate the prefetched content.
294323 *
0 commit comments