Skip to content
This repository has been archived by the owner on Jun 2, 2020. It is now read-only.

Commit

Permalink
MOBILE-557 Add support for offline images (profile and manage accounts)
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Jun 21, 2014
1 parent e70bcab commit b891459
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 41 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ <h1>
<% _.each(sites, function(site) { %>
<div class="accounts">
<a class="account-details" onclick="MM.loadSite('<%= site.id %>')">
<img src="<%= site.userpictureurl %>">
<img src="<%= MM.util.getMoodleFilePath(site.userpictureurl, 1, site.id) %>">
<div>
<span class="account-fullname"><%= MM.util.formatText(site.fullname) %></span>
<span class="account-sitename"><%= MM.util.formatText(site.sitename) %></span>
Expand Down
8 changes: 7 additions & 1 deletion lib/mm.cordova.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ MM.cordova = {
errorCallback();
} else {
fs.stat(filePath, function (err, stats) {
if (stats.File()) {
if (typeof(stats.File) == "function" && stats.File()) {
var entry = new FileEntry(path, filePath);
successCallback(entry);
return;
Expand Down Expand Up @@ -563,13 +563,19 @@ MM.cordova = {
},
removeRecursively: function(successCallback, errorCallback) {
successCallback();
},
getFile: function(f, o, successCallback, errorCallback) {
errorCallback();
}
},
toNativeURL: function () {
return 'emulator/index.html#/fake_base_path';
},
toURL: function () {
return 'emulator/index.html#/fake_base_path';
},
getFile: function(f, o, successCallback, errorCallback) {
errorCallback();
}
};
successCallback(entry);
Expand Down
9 changes: 6 additions & 3 deletions lib/mm.js
Original file line number Diff line number Diff line change
Expand Up @@ -1836,7 +1836,12 @@ var MM = {
*
* @param {string} url The url to be fixed.
*/
fixPluginfile: function(url) {
fixPluginfile: function(url, token) {

if (!token) {
token = MM.config.current_token;
}

// First check if we need to fix this url or is already fixed.
if (url.indexOf('token=') != -1) {
return url;
Expand All @@ -1847,8 +1852,6 @@ var MM = {
return url;
}

var token = MM.config.current_token;

// In which way the server is serving the files? Are we using slash parameters?
if (url.indexOf('?file=') != -1) {
url += '&';
Expand Down
88 changes: 53 additions & 35 deletions lib/mm.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,58 +220,76 @@ MM.util = {
return filename.substr(filename.lastIndexOf(".") + 1);
},

replaceFile: function(oldSrc, newSrc) {
var oldFile = $("img[src$='"+ oldSrc +"']");
if (oldFile) {
oldFile.attr("src", newSrc);
MM.log("Replaced " + oldSrc + " by cached " + newSrc);
}
},

/**
* This function gets the path of a Moodle file looking online or offline
* if the Mobile is not connected to the Internet
* This function downloads a file from Moodle if
*
* @param {string} file The file path (usually a url)
* @return {string} A local or URL path
*/
getMoodleFilePath: function (fileurl, courseId) {
getMoodleFilePath: function (fileurl, courseId, siteId, token) {

if (!courseId) {
courseId = 1;
}

var downloadURL = MM.fixPluginfile(fileurl);
if (!siteId) {
siteId = MM.config.current_site.id;
}

if (!token) {
var site = MM.db.get('sites', siteId);
token = site.get('token');
}

var downloadURL = MM.fixPluginfile(fileurl, token);
var extension = "." + fileurl.split('.').pop();
if (extension.indexOf(".php") === 0) {
extension = "";
}

var filename = hex_md5(fileurl);
var filename = hex_md5(fileurl) + extension;

// TODO:
// Object for the sake of it?
// Either split into two variables (recommended)
// Or make a new class type and pass that around.
var path = {
directory: MM.config.current_site.id + "/" + courseId,
file: MM.config.current_site.id + "/" + courseId + "/" + filename
directory: siteId + "/" + courseId,
file: siteId + "/" + courseId + "/" + filename
};

// We download the file asynchronously
// We download the file asynchronously because this function must to be sync.
MM.fs.init(function() {
MM.log("Starting download of Moodle file: " + downloadURL);
// All the functions are asynchronous, like createDir.
MM.fs.createDir(path.directory, function() {
MM.log("Downloading Moodle file to " + path.file + " from URL: " + downloadURL);

MM.moodleDownloadFile(downloadURL, path.file,
function(fullpath) {
MM.log("Download of content finished " + fullpath + " URL: " + downloadURL);
},
function(fullpath) {
MM.log("Error downloading " + fullpath + " URL: " + downloadURL);
}
);
});
MM.fs.fileExists(path.file,
function() {
MM.util.replaceFile(downloadURL, path.file);
},
function() {});

if (MM.deviceConnected()) {
MM.log("Starting download of Moodle file: " + downloadURL);
// All the functions are asynchronous, like createDir.
MM.fs.createDir(path.directory, function() {
MM.log("Downloading Moodle file to " + path.file + " from URL: " + downloadURL);

MM.moodleDownloadFile(downloadURL, path.file,
function(fullpath) {
MM.util.replaceFile(downloadURL, fullpath);
MM.log("Download of content finished " + fullpath + " URL: " + downloadURL);
},
function(fullpath) {
MM.log("Error downloading " + fullpath + " URL: " + downloadURL);
}
);
});
}
});

var moodleFilePath = "";
if (MM.deviceConnected()) {
moodleFilePath = MM.fixPluginfile(fileurl);
} else if (MM.fs.loaded()) {
var root = MM.fs.getRoot();
moodleFilePath = root + "/" + path.file;
}

return moodleFilePath;
return downloadURL;
},

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"window": {
"title": "Moodle Mobile for desktop",
"icon": "icon.png",
"toolbar": false,
"toolbar": true,
"frame": true,
"width": 1024,
"height": 768,
Expand Down

0 comments on commit b891459

Please sign in to comment.