Skip to content

Commit 23b0c4c

Browse files
committed
MOBILE-1497 book: Fix files in folders and bug with same filename
1 parent 2f56a49 commit 23b0c4c

File tree

3 files changed

+72
-38
lines changed

3 files changed

+72
-38
lines changed

upgrade.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
This files describes API changes in the Moodle Mobile app,
22
information provided here is intended especially for developers.
33

4+
=== 2.10 ===
5+
6+
* The function $mmaModBook#getChapterContent now requires to receive the result of $mmaModBook#getContentsMap instead of module.contents.
7+
48
=== 2.9 ===
59

610
* Most of the functions from $mmaModScormOnline, $mmaModScormOffline, $mmaModScorm, $mmaModScormSync and $mmaModScormHelper now have a new param siteId to determine the site to affect. If you use any of these services please make sure you still pass the right parameters.

www/addons/mod_book/controllers/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ angular.module('mm.addons.mod_book')
2727

2828
var module = $stateParams.module || {},
2929
courseid = $stateParams.courseid,
30-
currentChapter;
30+
currentChapter,
31+
contentsMap = $mmaModBook.getContentsMap(module.contents);
3132

3233
$scope.title = module.name;
3334
$scope.description = module.description;
@@ -43,7 +44,7 @@ angular.module('mm.addons.mod_book')
4344
function loadChapter(chapterId) {
4445
currentChapter = chapterId;
4546
$ionicScrollDelegate.scrollTop();
46-
return $mmaModBook.getChapterContent(module.contents, chapterId, module.id).then(function(content) {
47+
return $mmaModBook.getChapterContent(contentsMap, chapterId, module.id).then(function(content) {
4748
$scope.content = content;
4849
$scope.previousChapter = $mmaModBook.getPreviousChapter(chapters, chapterId);
4950
$scope.nextChapter = $mmaModBook.getNextChapter(chapters, chapterId);

www/addons/mod_book/services/book.js

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)