Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
This files describes API changes in the Moodle Mobile app,
information provided here is intended especially for developers.

=== 2.10 ===

* The function $mmaModBook#getChapterContent now requires to receive the result of $mmaModBook#getContentsMap instead of module.contents.

=== 2.9 ===

* 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.
Expand Down
5 changes: 3 additions & 2 deletions www/addons/mod_book/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ angular.module('mm.addons.mod_book')

var module = $stateParams.module || {},
courseid = $stateParams.courseid,
currentChapter;
currentChapter,
contentsMap = $mmaModBook.getContentsMap(module.contents);

$scope.title = module.name;
$scope.description = module.description;
Expand All @@ -43,7 +44,7 @@ angular.module('mm.addons.mod_book')
function loadChapter(chapterId) {
currentChapter = chapterId;
$ionicScrollDelegate.scrollTop();
return $mmaModBook.getChapterContent(module.contents, chapterId, module.id).then(function(content) {
return $mmaModBook.getChapterContent(contentsMap, chapterId, module.id).then(function(content) {
$scope.content = content;
$scope.previousChapter = $mmaModBook.getPreviousChapter(chapters, chapterId);
$scope.nextChapter = $mmaModBook.getNextChapter(chapters, chapterId);
Expand Down
101 changes: 65 additions & 36 deletions www/addons/mod_book/services/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,48 +232,27 @@ angular.module('mm.addons.mod_book')
* @module mm.addons.mod_book
* @ngdoc method
* @name $mmaModBook#getChapterContent
* @param {Object} contents The module contents.
* @param {Object} contentsMap Contents map returned by $mmaModBook#getContentsMap.
* @param {String} chapterId Chapter to retrieve.
* @param {Integer} moduleId The module ID.
* @return {Promise}
*/
self.getChapterContent = function(contents, chapterId, moduleId) {
var indexUrl,
paths = {},
self.getChapterContent = function(contentsMap, chapterId, moduleId) {
var indexUrl = contentsMap[chapterId] ? contentsMap[chapterId].indexUrl : undefined,
promise;

// Extract the information about paths from the module contents.
angular.forEach(contents, function(content) {
if (self.isFileDownloadable(content)) {
var key,
url = content.fileurl;

if (!indexUrl && content.filename == 'index.html') {
// First chapter, we don't have a chapter id.
if (content.filepath == "/" + chapterId + "/") {
indexUrl = url;
}
} else {
key = content.filename;
paths[key] = url;
}
}
});
if (!indexUrl) {
// If ever that happens.
$log.debug('Could not locate the index chapter');
return $q.reject();
}

// Promise handling when we are in a browser.
promise = (function() {
if (!indexUrl) {
// If ever that happens.
$log.debug('Could not locate the index chapter');
return $q.reject();
} else if ($mmFS.isAvailable()) {
// The file system is available.
return $mmFilepool.downloadUrl($mmSite.getId(), indexUrl, false, mmaModBookComponent, moduleId);
} else {
// We return the live URL.
return $q.when($mmSite.fixPluginfileURL(indexUrl));
}
})();
if ($mmFS.isAvailable()) {
promise = $mmFilepool.downloadUrl($mmSite.getId(), indexUrl, false, mmaModBookComponent, moduleId);
} else {
// We return the live URL.
return $q.when($mmSite.fixPluginfileURL(indexUrl));
}

return promise.then(function(url) {
// Fetch the URL content.
Expand All @@ -283,12 +262,62 @@ angular.module('mm.addons.mod_book')
} else {
// Now that we have the content, we update the SRC to point back to
// the external resource. That will be caught by mm-format-text.
return $mmUtil.restoreSourcesInHtml(response.data, paths);
return $mmUtil.restoreSourcesInHtml(response.data, contentsMap[chapterId].paths);
}
});
});
};

/**
* Convert an array of book contents into an object where contents are organized in chapters.
* Each chapter has an indexUrl and the list of contents in that chapter.
*
* @module mm.addons.mod_book
* @ngdoc method
* @name $mmaModBook#getContentsMap
* @param {Object} contents The module contents.
* @return {Object} Contents map.
*/
self.getContentsMap = function(contents) {
var map = {};

angular.forEach(contents, function(content) {
if (self.isFileDownloadable(content)) {
var chapter,
matches,
split,
filepathIsChapter;

// Search the chapter number in the filepath.
matches = content.filepath.match(/\/(\d+)\//);
if (matches && matches[1]) {
chapter = matches[1];
filepathIsChapter = content.filepath == '/' + chapter + '/';

// Init the chapter if it's not defined yet.
map[chapter] = map[chapter] || { paths: {} };

if (content.filename == 'index.html' && filepathIsChapter) {
map[chapter].indexUrl = content.fileurl;
} else {
if (filepathIsChapter) {
// It's a file in the root folder OR the WS isn't returning the filepath as it should (MDL-53671).
// Try to get the path to the file from the URL.
split = content.fileurl.split('mod_book/chapter' + content.filepath);
key = split[1] || content.filename; // Use filename if we couldn't find the path.
} else {
// Remove the chapter folder from the path and add the filename.
key = content.filepath.replace('/' + chapter + '/', '') + content.filename;
}
map[chapter].paths[key] = content.fileurl;
}
}
}
});

return map;
};

/**
* Invalidate the prefetched content.
*
Expand Down