@@ -22,14 +22,17 @@ 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 , $mmaModGlossaryHelper ) {
25+ $translate , $ionicHistory , $mmEvents , mmaModGlossaryAddEntryEvent , $mmaModGlossaryOffline , $mmaModGlossaryHelper , $mmLang ,
26+ $mmFileUploaderHelper ) {
2627
2728 var module = $stateParams . module ,
2829 courseId = $stateParams . courseid ,
2930 cmid = $stateParams . cmid ,
3031 glossaryId = $stateParams . glossaryid ,
3132 glossary = $stateParams . glossary || { } ,
32- entry = $stateParams . entry || false ;
33+ originalData = null ,
34+ entry = $stateParams . entry || false ,
35+ allowDuplicateEntries = ! ! glossary . allowduplicatedentries ;
3336
3437 $scope . entry = {
3538 concept : '' ,
@@ -38,11 +41,11 @@ angular.module('mm.addons.mod_glossary')
3841 $scope . title = module . name ;
3942 $scope . component = mmaModGlossaryComponent ;
4043 $scope . componentId = module . id ;
41- $scope . autolinking = glossary . usedynalink ;
44+ $scope . autolinking = ! ! glossary . usedynalink ;
4245 $scope . options = {
4346 categories : null ,
4447 aliases : "" ,
45- usedynalink : glossary . usedynalink ,
48+ usedynalink : false ,
4649 casesensitive : false ,
4750 fullmatch : false
4851 } ;
@@ -51,18 +54,27 @@ angular.module('mm.addons.mod_glossary')
5154 if ( entry ) {
5255 $scope . entry . concept = entry . concept || '' ;
5356 $scope . entry . text = entry . definition || '' ;
57+
58+ originalData = { } ;
59+ originalData . text = $scope . entry . text ;
60+ originalData . concept = $scope . entry . concept ;
61+ originalData . files = [ ] ;
62+
5463 if ( entry . options ) {
5564 $scope . options . categories = entry . options . categories || null ;
5665 $scope . options . aliases = entry . options . aliases || "" ;
57- $scope . options . usedynalink = ! ! entry . options . usedynalink || glossary . usedynalink ;
58- $scope . options . casesensitive = ! ! entry . options . casesensitive ;
59- $scope . options . fullmatch = ! ! entry . options . fullmatch ;
66+ $scope . options . usedynalink = ! ! entry . options . usedynalink ;
67+ if ( $scope . options . usedynalink ) {
68+ $scope . options . casesensitive = ! ! entry . options . casesensitive ;
69+ $scope . options . fullmatch = ! ! entry . options . fullmatch ;
70+ }
6071 }
6172
6273 // Treat offline attachments if any.
6374 if ( entry . attachments && entry . attachments . offline ) {
64- $mmaModGlossaryHelper . getStoredFiles ( glossaryId , entry . concept ) . then ( function ( files ) {
75+ $mmaModGlossaryHelper . getStoredFiles ( glossaryId , entry . concept , entry . timecreated ) . then ( function ( files ) {
6576 $scope . attachments = files ;
77+ originalData . files = angular . copy ( files ) ;
6678 } ) ;
6779 }
6880 }
@@ -92,7 +104,7 @@ angular.module('mm.addons.mod_glossary')
92104 function cancel ( ) {
93105 var promise ;
94106
95- if ( ! $mmaModGlossaryHelper . hasEntryDataChanged ( $scope . entry , $scope . attachments ) ) {
107+ if ( ! $mmaModGlossaryHelper . hasEntryDataChanged ( $scope . entry , $scope . attachments , originalData ) ) {
96108 promise = $q . when ( ) ;
97109 } else {
98110 // Show confirmation if some data has been modified.
@@ -101,7 +113,7 @@ angular.module('mm.addons.mod_glossary')
101113
102114 return promise . then ( function ( ) {
103115 // Delete the local files from the tmp folder.
104- $mmaModGlossaryHelper . clearTmpFiles ( $scope . attachments ) ;
116+ $mmFileUploaderHelper . clearTmpFiles ( $scope . attachments ) ;
105117 } ) ;
106118 }
107119
@@ -110,6 +122,7 @@ angular.module('mm.addons.mod_glossary')
110122 definition = $scope . entry . text ,
111123 modal ,
112124 attachments ,
125+ timecreated = entry && entry . timecreated || Date . now ( ) ,
113126 saveOffline = false ;
114127
115128 if ( ! concept || ! definition ) {
@@ -126,22 +139,15 @@ angular.module('mm.addons.mod_glossary')
126139 definition = $mmText . formatHtmlLines ( definition ) ;
127140 }
128141
129- // If editing an offline entry and concept is different, delete previous first.
130- if ( entry . concept && entry . concept != concept ) {
131- return $mmaModGlossaryOffline . deleteAddEntry ( glossaryId , entry . concept ) ;
132- }
133- return $q . when ( ) ;
134-
135- } ) . then ( function ( ) {
136142 attachments = $scope . attachments ;
137143
138144 // Upload attachments first if any.
139145 if ( attachments . length ) {
140- return $mmaModGlossaryHelper . uploadOrStoreFiles ( glossaryId , concept , attachments , false )
146+ return $mmaModGlossaryHelper . uploadOrStoreFiles ( glossaryId , concept , timecreated , attachments , false )
141147 . catch ( function ( ) {
142148 // Cannot upload them in online, save them in offline.
143149 saveOffline = true ;
144- return $mmaModGlossaryHelper . uploadOrStoreFiles ( glossaryId , concept , attachments , true ) ;
150+ return $mmaModGlossaryHelper . uploadOrStoreFiles ( glossaryId , concept , timecreated , attachments , true ) ;
145151 } ) ;
146152 }
147153 } ) . then ( function ( attach ) {
@@ -165,21 +171,37 @@ angular.module('mm.addons.mod_glossary')
165171 }
166172
167173 if ( saveOffline ) {
168- // Save entry in offline.
169- return $mmaModGlossaryOffline . saveAddEntry ( glossaryId , concept , definition , courseId , options , attach ) . then ( function ( ) {
170- // Don't return anything.
174+ var promise ;
175+ if ( entry && ! allowDuplicateEntries ) {
176+ // Check if the entry is duplicated in online or offline mode.
177+ promise = $mmaModGlossary . isConceptUsed ( glossaryId , concept ) . then ( function ( ) {
178+ // There's a entry with same name, reject with error message.
179+ return $mmLang . translateAndReject ( 'mma.mod_glossary.errconceptalreadyexists' ) ;
180+ } , function ( ) {
181+ // Not found, entry can be sent.
182+ } ) ;
183+ } else {
184+ promise = $q . when ( ) ;
185+ }
186+
187+ return promise . then ( function ( ) {
188+ // Save entry in offline.
189+ return $mmaModGlossaryOffline . saveAddEntry ( glossaryId , concept , definition , courseId , options , attach ,
190+ undefined , undefined , entry ) . then ( function ( ) {
191+ // Don't return anything.
192+ } ) ;
171193 } ) ;
172194 } else {
173195 // Try to send it to server.
174196 // Don't allow offline if there are attachments since they were uploaded fine.
175197 return $mmaModGlossary . addEntry ( glossaryId , concept , definition , courseId , options , attach , undefined ,
176- ! attachments . length ) ;
198+ entry , ! attachments . length ) ;
177199 }
178200 } ) . then ( function ( entryId ) {
179201 if ( entryId ) {
180202 $scope . entry . id = entryId ;
181203 // Data sent to server, delete stored files (if any).
182- $mmaModGlossaryHelper . deleteStoredFiles ( glossaryId , concept ) ;
204+ $mmaModGlossaryHelper . deleteStoredFiles ( glossaryId , concept , timecreated ) ;
183205 }
184206 $scope . entry . glossaryid = glossaryId ;
185207 $scope . entry . definition = definition ;
@@ -198,7 +220,7 @@ angular.module('mm.addons.mod_glossary')
198220 entry : $scope . entry
199221 } ;
200222
201- $mmaModGlossaryHelper . clearTmpFiles ( $scope . attachments ) ;
223+ $mmFileUploaderHelper . clearTmpFiles ( $scope . attachments ) ;
202224
203225 $mmEvents . trigger ( mmaModGlossaryAddEntryEvent , data ) ;
204226
0 commit comments