@@ -152,7 +152,7 @@ class FeedbackChipApi < Grape::API
152152 params do
153153 requires :id , type : Integer , desc : 'The ID of the context'
154154 end
155- get '/:context_type_plural/:context_id/outcomes/:id/feedback_chips/csv' do # '/:context_type_plural/:context_id/feedback_chips/csv'
155+ get '/:context_type_plural/:context_id/outcomes/:id/feedback_chips/csv' do
156156 # find context model dynamically
157157 context_type = params [ :context_type_plural ] . singularize . camelize
158158 context_model = context_type . classify . constantize . find ( params [ :context_id ] )
@@ -200,7 +200,7 @@ class FeedbackChipApi < Grape::API
200200 requires :file , type : File , desc : 'CSV upload file.'
201201 requires :id , type : Integer , desc : 'The id of the learning outcome'
202202 end
203- post '/:context_type_plural/:context_id/outcomes/:id/feedback_chips/csv' do # '/:context_type_plural/:context_id/feedback_chips/csv'
203+ post '/:context_type_plural/:context_id/outcomes/:id/feedback_chips/csv' do
204204 # check mime is correct before uploading
205205 ensure_csv! ( params [ :file ] [ :tempfile ] )
206206
@@ -212,7 +212,7 @@ class FeedbackChipApi < Grape::API
212212 end
213213
214214 # Actually import...
215- learning_outcome . import_feedback_chips_from_csv ( params [ :file ] [ :tempfile ] )
215+ Feedback :: FeedbackChip . import_feedback_chips_from_csv ( params [ :file ] [ :tempfile ] , 'LearningOutcome' , learning_outcome )
216216 end
217217
218218 desc 'Upload the feedback chips for a specified context from a csv'
@@ -228,17 +228,17 @@ class FeedbackChipApi < Grape::API
228228 context_type = params [ :context_type_plural ] . singularize . camelize
229229 context_model = context_type . classify . constantize . find ( params [ :context_id ] )
230230
231- unless authorise? current_user , context_model , :upload_csv
231+ unless authorise? current_user , context_model , :create_feedback_chips
232232 error! ( { error : 'Not authorised to upload CSV of outcomes' } , 403 )
233233 end
234234
235235 # Actually import...
236- context_model . import_feedback_chips_from_csv ( params [ :file ] [ :tempfile ] )
236+ Feedback :: FeedbackChip . import_feedback_chips_from_csv ( params [ :file ] [ :tempfile ] , context_type , context_model )
237237 end
238238
239239 desc 'Download the feedback chips for a global context'
240240 get '/global/feedback_chips/csv' do
241- unless authorise? current_user , User , :feedback_chips
241+ unless authorise? current_user , User , :create_feedback_chips
242242 error! ( { error : 'You are not authorised to download feedback chips globally.' } , 403 )
243243 end
244244
@@ -268,35 +268,11 @@ class FeedbackChipApi < Grape::API
268268 # check mime is correct before uploading
269269 ensure_csv! ( params [ :file ] [ :tempfile ] )
270270
271- unless authorise? current_user , User , :feedback_chips
272- error! ( { error : 'Not authorised to upload CSV of outcomes' } , 403 )
273- end
274-
275- # Actually import...
276- file = params [ :file ] [ :tempfile ]
277-
278- result = {
279- success : [ ] ,
280- errors : [ ] ,
281- ignored : [ ]
282- }
283-
284- data = read_file_to_str ( file )
285- CSV . parse ( data ,
286- headers : true ,
287- header_converters : [ -> ( i ) { i . nil? ? '' : i } , :downcase , -> ( hdr ) { hdr . strip unless hdr . nil? } ] ,
288- converters : [ -> ( body ) { body . encode! ( 'UTF-8' , 'binary' , invalid : :replace , undef : :replace , replace : '' ) unless body . nil? } ] ) . each do |row |
289- # Make sure we're not looking at the header or an empty line
290- next if row [ 0 ] =~ /unit_code/
291-
292- begin
293- Feedback ::FeedbackChip . create_from_csv ( row , result )
294- rescue Exception => e
295- result [ :errors ] << { row : row , message : e . message . to_s }
296- end
271+ unless authorise? current_user , User , :create_feedback_chips
272+ error! ( { error : 'Not authorised to upload CSV of feedback' } , 403 )
297273 end
298274
299- result
275+ Feedback :: FeedbackChip . import_feedback_chips_from_csv ( params [ :file ] [ :tempfile ] , nil , nil )
300276 end
301277 end
302278end
0 commit comments