@@ -177,6 +177,42 @@ def export_entries
177177 end
178178 end
179179
180+ def export_round_results
181+ begin
182+ @contest_instance = @contest_description . contest_instances . find ( params [ :id ] )
183+ authorize @contest_instance , :export_entries?
184+
185+ round_id = params [ :round_id ]
186+ judging_round = @contest_instance . judging_rounds . find_by ( id : round_id )
187+
188+ if judging_round . nil?
189+ redirect_to container_contest_description_contest_instance_path ( @container , @contest_description , @contest_instance ) ,
190+ alert : 'Judging round not found.'
191+ return
192+ end
193+
194+ @entries = judging_round . entries . distinct . includes (
195+ :profile , :category ,
196+ entry_rankings : [ :user ]
197+ )
198+
199+ respond_to do |format |
200+ format . csv do
201+ filename = "#{ @contest_description . name . parameterize } -round-#{ judging_round . round_number } -results-#{ Time . zone . today } .csv"
202+
203+ csv_data = generate_round_results_csv ( @entries , @contest_description , @contest_instance , judging_round )
204+
205+ send_data csv_data ,
206+ type : 'text/csv; charset=utf-8; header=present' ,
207+ disposition : "attachment; filename=#{ filename } "
208+ end
209+ end
210+ rescue Pundit ::NotAuthorizedError
211+ flash [ :alert ] = 'Not authorized to access this contest instance'
212+ redirect_to root_path
213+ end
214+ end
215+
180216 private
181217
182218 def authorize_container_access
@@ -202,7 +238,7 @@ def redirect_to_contest_instance_path
202238
203239 def contest_instance_params
204240 params . require ( :contest_instance ) . permit (
205- :active , :archived , : contest_description_id, :date_open , :date_closed ,
241+ :active , :contest_description_id , :date_open , :date_closed ,
206242 :notes , :judging_open , :judge_evaluations_complete ,
207243 :maximum_number_entries_per_applicant , :require_pen_name ,
208244 :require_campus_employment_info , :require_finaid_info , :created_by ,
@@ -255,4 +291,66 @@ def generate_entries_csv(entries, contest_description, contest_instance)
255291 end
256292 end
257293 end
294+
295+ def generate_round_results_csv ( entries , contest_description , contest_instance , judging_round )
296+ require 'csv'
297+
298+ CSV . generate do |csv |
299+ # Header section
300+ contest_info = "#{ contest_description . name } - Round #{ judging_round . round_number } Results"
301+ header_row1 = [ contest_info ] + Array . new ( 15 , '' )
302+ csv << header_row1
303+ csv << Array . new ( 16 , '' ) # Empty row as separator
304+
305+ # Column headers
306+ headers = [
307+ 'Title' , 'Category' ,
308+ 'Pen Name' , 'First Name' , 'Last Name' , 'UMID' , 'Uniqname' ,
309+ 'Class Level' , 'Campus' , 'Entry ID' , 'Selected for Next Round' ,
310+ 'Judge Name' , 'Score' , 'Judge Comments [External]' , 'Judge Comments [Internal]'
311+ ]
312+ csv << headers
313+
314+ # Entry data
315+ entries . each do |entry |
316+ profile = entry . profile
317+ rankings = entry . entry_rankings . where ( judging_round : judging_round )
318+ selected = rankings . exists? ( selected_for_next_round : true )
319+
320+ # Base entry data
321+ base_data = [
322+ entry . title ,
323+ entry . category &.kind ,
324+ entry . pen_name ,
325+ profile &.user &.first_name ,
326+ profile &.user &.last_name ,
327+ profile &.umid ,
328+ profile &.user &.uniqname ,
329+ profile &.class_level &.name ,
330+ profile &.campus &.campus_descr ,
331+ entry . id ,
332+ selected ? 'Yes' : 'No'
333+ ]
334+
335+ # If there are rankings, create a row for each judge's ranking
336+ if rankings . any?
337+ rankings . each do |ranking |
338+ score = ranking . rank
339+ external_comments = ranking . external_comments . presence || 'No comment entered'
340+ internal_comments = ranking . internal_comments . presence || 'No comment entered'
341+
342+ csv << base_data + [
343+ "#{ ranking . user . display_name_or_first_name_last_name } (#{ ranking . user . uid } )" ,
344+ score ,
345+ external_comments ,
346+ internal_comments
347+ ]
348+ end
349+ else
350+ # If no rankings, just output the base data with empty judge fields
351+ csv << base_data + [ '' , '' ]
352+ end
353+ end
354+ end
355+ end
258356end
0 commit comments