Skip to content

Commit a207fee

Browse files
authored
Merge pull request #903 from crazyserver/MOBILE-1966
Mobile 1966
2 parents 8717484 + 242f0e3 commit a207fee

File tree

10 files changed

+387
-56
lines changed

10 files changed

+387
-56
lines changed

www/addons/mod/glossary/controllers/edit.js

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ angular.module('mm.addons.mod_glossary')
2222
* @name mmaModGlossaryEditCtrl
2323
*/
2424
.controller('mmaModGlossaryEditCtrl', function($stateParams, $scope, mmaModGlossaryComponent, $mmUtil, $q, $mmaModGlossary, $mmText,
25-
$translate, $ionicHistory, $mmEvents, mmaModGlossaryAddEntryEvent, $mmaModGlossaryOffline) {
25+
$translate, $ionicHistory, $mmEvents, mmaModGlossaryAddEntryEvent, $mmaModGlossaryOffline, $mmaModGlossaryHelper) {
2626

2727
var module = $stateParams.module,
2828
courseId = $stateParams.courseid,
@@ -46,6 +46,7 @@ angular.module('mm.addons.mod_glossary')
4646
casesensitive: false,
4747
fullmatch: false
4848
};
49+
$scope.attachments = [];
4950

5051
if (entry) {
5152
$scope.entry.concept = entry.concept || '';
@@ -54,40 +55,70 @@ angular.module('mm.addons.mod_glossary')
5455
$scope.options.categories = entry.options.categories || null;
5556
$scope.options.aliases = entry.options.aliases || "";
5657
$scope.options.usedynalink = !!entry.options.usedynalink || glossary.usedynalink;
57-
$scope.options.casesensitive = !!entry.options.casesensitive
58+
$scope.options.casesensitive = !!entry.options.casesensitive;
5859
$scope.options.fullmatch = !!entry.options.fullmatch;
5960
}
61+
62+
// Treat offline attachments if any.
63+
if (entry.attachments && entry.attachments.offline) {
64+
$mmaModGlossaryHelper.getStoredFiles(glossaryId, entry.concept).then(function(files) {
65+
$scope.attachments = files;
66+
});
67+
}
6068
}
6169

6270
// Block leaving the view, we want to show a confirm to the user if there's unsaved data.
6371
$mmUtil.blockLeaveView($scope, cancel);
6472

6573
// Fetch Glossary data.
66-
function fetchGlossaryData(refresh) {
74+
function fetchGlossaryData() {
6775
return $mmaModGlossary.getAllCategories(glossaryId).then(function(categories) {
6876
$scope.categories = categories;
77+
78+
if ($scope.options.categories) {
79+
var cats = $scope.options.categories.split(",");
80+
angular.forEach(cats, function(catId) {
81+
angular.forEach($scope.categories, function(category) {
82+
if (category.id == catId) {
83+
category.selected = true;
84+
}
85+
});
86+
});
87+
}
6988
});
7089
}
7190

7291
// Just ask to confirm the lost of data.
7392
function cancel() {
74-
if (!$scope.entry.text && !$scope.entry.concept) {
75-
return $q.when();
93+
var promise;
94+
95+
if (!$mmaModGlossaryHelper.hasEntryDataChanged($scope.entry, $scope.attachments)) {
96+
promise = $q.when();
7697
} else {
7798
// Show confirmation if some data has been modified.
78-
return $mmUtil.showConfirm($translate('mm.core.confirmcanceledit'));
99+
promise = $mmUtil.showConfirm($translate('mm.core.confirmcanceledit'));
79100
}
101+
102+
return promise.then(function() {
103+
// Delete the local files from the tmp folder.
104+
$mmaModGlossaryHelper.clearTmpFiles($scope.attachments);
105+
});
80106
}
81107

82108
$scope.save = function() {
83109
var concept = $scope.entry.concept,
84-
definition = $scope.entry.text;
110+
definition = $scope.entry.text,
111+
modal,
112+
attachments,
113+
saveOffline = false;
85114

86115
if (!concept || !definition) {
87116
$mmUtil.showErrorModal('mma.mod_glossary.fillfields', true);
88117
return;
89118
}
90119

120+
modal = $mmUtil.showModalLoading('mm.core.sending', true);
121+
91122
// Check if rich text editor is enabled or not.
92123
$mmUtil.isRichTextEditorEnabled().then(function(enabled) {
93124
if (!enabled) {
@@ -101,7 +132,19 @@ angular.module('mm.addons.mod_glossary')
101132
}
102133
return $q.when();
103134

104-
}).then(function(entryId) {
135+
}).then(function() {
136+
attachments = $scope.attachments;
137+
138+
// Upload attachments first if any.
139+
if (attachments.length) {
140+
return $mmaModGlossaryHelper.uploadOrStoreFiles(glossaryId, concept, attachments, false)
141+
.catch(function() {
142+
// Cannot upload them in online, save them in offline.
143+
saveOffline = true;
144+
return $mmaModGlossaryHelper.uploadOrStoreFiles(glossaryId, concept, attachments, true);
145+
});
146+
}
147+
}).then(function(attach) {
105148
var cats = $scope.categories.filter(function(category) {
106149
return category.selected;
107150
}).map(function(category) {
@@ -120,24 +163,43 @@ angular.module('mm.addons.mod_glossary')
120163
options.fullmatch = $scope.options.fullmatch ? 1 : 0;
121164
}
122165
}
123-
return $mmaModGlossary.addEntry(glossaryId, concept, courseId, definition, options);
166+
167+
if (saveOffline) {
168+
// Save entry in offline.
169+
return $mmaModGlossaryOffline.saveAddEntry(glossaryId, concept, definition, courseId, options, attach).then(function() {
170+
// Don't return anything.
171+
});
172+
} else {
173+
// Try to send it to server.
174+
// Don't allow offline if there are attachments since they were uploaded fine.
175+
return $mmaModGlossary.addEntry(glossaryId, concept, definition, courseId, options, attach, undefined,
176+
!attachments.length);
177+
}
124178
}).then(function(entryId) {
179+
if (entryId) {
180+
$scope.entry.id = entryId;
181+
// Data sent to server, delete stored files (if any).
182+
$mmaModGlossaryHelper.deleteStoredFiles(glossaryId, concept);
183+
}
125184
$scope.entry.glossaryid = glossaryId;
126-
$scope.entry.id = entryId;
127185
$scope.entry.definition = definition;
128186
return returnToEntryList();
129187
}).catch(function(error) {
130188
$mmUtil.showErrorModalDefault(error, 'mma.mod_glossary.cannoteditentry', true);
189+
}).finally(function() {
190+
modal.dismiss();
131191
});
132192
};
133193

134-
function returnToEntryList(entryId) {
194+
function returnToEntryList() {
135195
var data = {
136196
glossaryid: glossaryId,
137197
cmid: cmid,
138198
entry: $scope.entry
139199
};
140200

201+
$mmaModGlossaryHelper.clearTmpFiles($scope.attachments);
202+
141203
$mmEvents.trigger(mmaModGlossaryAddEntryEvent, data);
142204

143205
// Go back to discussions list.

www/addons/mod/glossary/lang/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"addentry": "Add a new entry",
33
"aliases": "Keyword(s)",
4+
"attachment": "Attachment",
45
"browsemode": "Browse entries",
56
"byalphabet": "Alphabetically",
67
"byauthor": "Group by author",

www/addons/mod/glossary/services/glossary.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -630,41 +630,44 @@ angular.module('mm.addons.mod_glossary')
630630
* @module mm.addons.mod_glossary
631631
* @ngdoc method
632632
* @name $mmaModGlossary#addEntry
633-
* @param {Number} glossaryId Glossary ID.
634-
* @param {String} concept Glossary entry concept.
635-
* @param {String} definition Glossary entry concept definition.
636-
* @param {Number} courseId Course ID of the glossary.
637-
* @param {Array} [options] Array of options for the entry.
638-
* @param {String} [siteId] Site ID. If not defined, current site.
633+
* @param {Number} glossaryId Glossary ID.
634+
* @param {String} concept Glossary entry concept.
635+
* @param {String} definition Glossary entry concept definition.
636+
* @param {Number} courseId Course ID of the glossary.
637+
* @param {Array} [options] Array of options for the entry.
638+
* @param {Mixed} [attach] Attachments ID if sending online, result of $mmFileUploader#storeFilesToUpload otherwise.
639+
* @param {String} [siteId] Site ID. If not defined, current site.
640+
* @param {Boolean} allowOffline True if it can be stored in offline, false otherwise.
639641
* @return {Promise} Promise resolved with entry ID if entry was created in server, false if stored in device.
640642
*/
641-
self.addEntry = function(glossaryId, concept, definition, courseId, options, siteId) {
643+
self.addEntry = function(glossaryId, concept, definition, courseId, options, attach, siteId, allowOffline) {
642644
siteId = siteId || $mmSite.getId();
643645

644-
if (!$mmApp.isOnline()) {
646+
if (!$mmApp.isOnline() && allowOffline) {
645647
// App is offline, store the action.
646648
return storeOffline();
647649
}
648650

649651
// Discard stored content for this entry. If it exists it means the user is editing it.
650652
return $mmaModGlossaryOffline.deleteAddEntry(glossaryId, concept, siteId).then(function() {
651653
// Try to add it in online.
652-
return self.addEntryOnline(glossaryId, concept, definition, options, siteId).then(function(entryId) {
654+
return self.addEntryOnline(glossaryId, concept, definition, options, attach, siteId).then(function(entryId) {
653655
return entryId;
654656
}).catch(function(error) {
655-
if (error && error.wserror) {
656-
// The WebService has thrown an error, this means that responses cannot be deleted.
657-
return $q.reject(error.error);
658-
} else {
657+
if (allowOffline && error && !error.wserror) {
659658
// Couldn't connect to server, store in offline.
660659
return storeOffline();
660+
} else {
661+
// The WebService has thrown an error or offline not supported, reject.
662+
return $q.reject(error.error);
661663
}
662664
});
663665
});
664666

665667
// Convenience function to store a new page to be synchronized later.
666668
function storeOffline() {
667-
return $mmaModGlossaryOffline.saveAddEntry(glossaryId, concept, definition, courseId, options, siteId).then(function() {
669+
return $mmaModGlossaryOffline.saveAddEntry(glossaryId, concept, definition, courseId, options, attach,
670+
siteId).then(function() {
668671
return false;
669672
});
670673
}
@@ -680,12 +683,13 @@ angular.module('mm.addons.mod_glossary')
680683
* @param {String} concept Glossary entry concept.
681684
* @param {String} definition Glossary entry concept definition.
682685
* @param {Array} [options] Array of options for the entry.
686+
* @param {Number} [attachId] Attachments ID (if any attachment).
683687
* @param {String} [siteId] Site ID. If not defined, current site.
684688
* @return {Promise} Promise resolved if created, rejected otherwise. Reject param is an object with:
685689
* - error: The error message.
686690
* - wserror: True if it's an error returned by the WebService, false otherwise.
687691
*/
688-
self.addEntryOnline = function(glossaryId, concept, definition, options, siteId) {
692+
self.addEntryOnline = function(glossaryId, concept, definition, options, attachId, siteId) {
689693
return $mmSitesManager.getSite(siteId).then(function(site) {
690694
var params = {
691695
glossaryid: glossaryId,
@@ -697,6 +701,13 @@ angular.module('mm.addons.mod_glossary')
697701
params.options = $mmUtil.objectToArrayOfObjects(options, 'name', 'value');
698702
}
699703

704+
if (attachId) {
705+
params.options.push({
706+
name: 'attachmentsid',
707+
value: attachId
708+
});
709+
}
710+
700711
return addEntryOnline(site, params).then(function(response) {
701712
if (response.entryid) {
702713
return response.entryid;

www/addons/mod/glossary/services/glossary_offline.js

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ angular.module('mm.addons.mod_glossary')
4848
* @ngdoc service
4949
* @name $mmaModGlossaryOffline
5050
*/
51-
.factory('$mmaModGlossaryOffline', function($mmSitesManager, $log, mmaModGlossaryAddEntryStore) {
51+
.factory('$mmaModGlossaryOffline', function($mmSitesManager, $log, mmaModGlossaryAddEntryStore, $mmFS) {
5252
$log = $log.getInstance('$mmaModGlossaryOffline');
5353

5454
var self = {};
@@ -127,35 +127,75 @@ angular.module('mm.addons.mod_glossary')
127127
* @module mm.addons.mod_glossary
128128
* @ngdoc method
129129
* @name $mmaModGlossaryOffline#saveAddEntry
130-
* @param {Number} glossaryId Glossary ID.
131-
* @param {String} concept Glossary entry concept.
132-
* @param {String} definition Glossary entry concept definition.
133-
* @param {Number} courseId Course ID of the glossary.
134-
* @param {Array} [options] Array of options for the entry.
135-
* @param {String} [siteId] Site ID. If not defined, current site.
136-
* @param {Number} [userId] User the entry belong to. If not defined, current user in site.
137-
* @return {Promise} Promise resolved if stored, rejected if failure.
130+
* @param {Number} glossaryId Glossary ID.
131+
* @param {String} concept Glossary entry concept.
132+
* @param {String} definition Glossary entry concept definition.
133+
* @param {Number} courseId Course ID of the glossary.
134+
* @param {Array} [options] Array of options for the entry.
135+
* @param {Object} [attach] Result of $mmFileUploader#storeFilesToUpload for attachments.
136+
* @param {String} [siteId] Site ID. If not defined, current site.
137+
* @param {Number} [userId] User the entry belong to. If not defined, current user in site.
138+
* @return {Promise} Promise resolved if stored, rejected if failure.
138139
*/
139-
self.saveAddEntry = function(glossaryId, concept, definition, courseId, options, siteId, userId) {
140+
self.saveAddEntry = function(glossaryId, concept, definition, courseId, options, attach, siteId, userId) {
140141
return $mmSitesManager.getSite(siteId).then(function(site) {
141142
userId = userId || site.getUserId();
142143

143-
var now = new Date().getTime(),
144-
entry = {
144+
var entry = {
145145
glossaryid: glossaryId,
146146
courseid: courseId,
147147
concept: concept,
148148
definition: definition,
149149
definitionformat: 'html',
150150
options: options,
151151
userid: userId,
152-
timecreated: now,
153-
timemodified: now
152+
timecreated: new Date().getTime()
154153
};
155154

155+
if (attach) {
156+
entry.attachments = attach;
157+
}
158+
156159
return site.getDb().insert(mmaModGlossaryAddEntryStore, entry);
157160
});
158161
};
159162

163+
/**
164+
* Get the path to the folder where to store files for offline attachments in a glossary.
165+
*
166+
* @module mm.addons.mod_glossary
167+
* @ngdoc method
168+
* @name $mmaModGlossaryOffline#getGlossaryFolder
169+
* @param {Number} glossaryId Glossary ID.
170+
* @param {String} [siteId] Site ID. If not defined, current site.
171+
* @return {Promise} Promise resolved with the path.
172+
*/
173+
self.getGlossaryFolder = function(glossaryId, siteId) {
174+
return $mmSitesManager.getSite(siteId).then(function(site) {
175+
176+
var siteFolderPath = $mmFS.getSiteFolder(site.getId()),
177+
folderPath = 'offlineglossary/' + glossaryId;
178+
179+
return $mmFS.concatenatePaths(siteFolderPath, folderPath);
180+
});
181+
};
182+
183+
/**
184+
* Get the path to the folder where to store files for a new offline entry.
185+
*
186+
* @module mm.addons.mod_glossary
187+
* @ngdoc method
188+
* @name $mmaModGlossaryOffline#getEntryFolder
189+
* @param {Number} glossaryId Glossary ID.
190+
* @param {Number} entryName The name of the entry.
191+
* @param {String} [siteId] Site ID. If not defined, current site.
192+
* @return {Promise} Promise resolved with the path.
193+
*/
194+
self.getEntryFolder = function(glossaryId, entryName, siteId) {
195+
return self.getGlossaryFolder(glossaryId, siteId).then(function(folderPath) {
196+
return $mmFS.concatenatePaths(folderPath, 'newentry_' + entryName);
197+
});
198+
};
199+
160200
return self;
161201
});

0 commit comments

Comments
 (0)