@@ -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.
0 commit comments