Skip to content

Commit 778e94d

Browse files
Change data download methods to allow multiple exercise ids
1 parent 55a5551 commit 778e94d

4 files changed

Lines changed: 35 additions & 23 deletions

File tree

app/admin/course_offering.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
index do
99
id_column
1010
column :course, sortable: 'courses.number' do |c|
11-
link_to c.course.number_and_org, admin_course_path(c.course)
11+
link_to c.course.number_and_org, admin_course_path(c.course.id)
1212
end
1313
column :term, sortable: 'term.ends_on' do |c|
1414
link_to c.term.display_name, admin_term_path(c.term)

app/controllers/exercises_controller.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@ def query_data
5656

5757
# -------------------------------------------------------------
5858
def download_attempt_data
59-
@exercise = Exercise.find params[:id]
59+
exercise_id = params[:id] # may be one or more ids
60+
course_offering_id = params[:course_offering_id]
61+
workout_id = params[:workout_id]
62+
63+
time = Time.now.utc.strftime("%Y%m%d%H%M%S")
6064

6165
if params[:progsnap].to_b
62-
main_events, code_states = @exercise.progsnap2_attempt_csv
66+
main_events, code_states = Exercise.progsnap2_attempt_csv(exercise_id, course_offering_id, workout_id)
6367
compressed_filestream = Zip::OutputStream.write_buffer do |zos|
6468
main_events_file = "MainTable.csv"
6569
zos.put_next_entry main_events_file
@@ -73,15 +77,15 @@ def download_attempt_data
7377
compressed_filestream.rewind
7478
respond_to do |format|
7579
format.zip do
76-
send_data compressed_filestream.read, filename: "X#{params[:id]}-progsnap.zip", type: 'application/zip'
80+
send_data compressed_filestream.read, filename: "CW-progsnap-#{time}.zip", type: 'application/zip'
7781
end
7882
end
7983
else
80-
result = @exercise.denormalized_attempt_csv
84+
result = Exercise.denormalized_attempt_csv(exercise_id)
8185

8286
respond_to do |format|
8387
format.csv do
84-
send_data result, filename: "X#{params[:id]}-submissions.csv"
88+
send_data result, filename: "CW-#{time}-submissions.csv"
8589
end
8690
end
8791
end

app/models/exercise.rb

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,15 @@ def is_publicly_available?
310310
end
311311
end
312312

313-
def progsnap2_attempt_csv
314-
denormalized = self.denormalized_attempt_data
315-
main_events = progsnap2_main_events_csv(denormalized)
316-
code_states = progsnap2_code_states_csv(denormalized)
313+
def self.progsnap2_attempt_csv(exercise_id, course_offering_id = nil, workout_id = nil)
314+
denormalized = Exercise.denormalized_attempt_data(exercise_id, course_offering_id, workout_id)
315+
main_events = Exercise.progsnap2_main_events_csv(denormalized)
316+
code_states = Exercise.progsnap2_code_states_csv(denormalized)
317317
return main_events, code_states
318318
end
319319

320-
def denormalized_attempt_csv
321-
denormalized_data = self.denormalized_attempt_data
320+
def self.denormalized_attempt_csv(exercise_id)
321+
denormalized_data = Exercise.denormalized_attempt_data(exercise_id)
322322
exercise_attributes = %w{ exercise_id exercise_name }
323323
attempt_attributes = %w{
324324
user_id
@@ -356,15 +356,23 @@ def denormalized_attempt_csv
356356
# All relationship fields are in the same table, so null values
357357
# are possible for workout_id, workout_offering_id, course_id,
358358
# course_offering_id, etc.
359-
def denormalized_attempt_data(workout_id = nil)
360-
result = exercise_versions.joins{ attempts.prompt_answers }
359+
def self.denormalized_attempt_data(exercise_id, course_offering_id = nil, workout_id = nil)
360+
course_offering_filter = course_offering_id ?
361+
"AND course_offerings.id = #{course_offering_id}" :
362+
""
363+
workout_filter = workout_id ?
364+
"AND workouts.id = #{workout_id}" :
365+
""
366+
367+
result = Exercise.where(:id, exercise_id)
368+
.joins(exercise_versions: { attempts: :prompt_answers })
361369
.joins('LEFT JOIN workout_scores ON
362370
workout_scores.id = attempts.workout_score_id')
363371
.joins('LEFT JOIN workout_offerings ON
364372
workout_offerings.id = workout_scores.workout_offering_id')
365-
.joins('LEFT JOIN workouts ON workouts.id = workout_scores.workout_id')
366-
.joins('LEFT JOIN course_offerings ON
367-
course_offerings.id = workout_offerings.course_offering_id')
373+
.joins("LEFT JOIN workouts ON workouts.id = workout_scores.workout_id #{workout_filter}")
374+
.joins("LEFT JOIN course_offerings ON
375+
course_offerings.id = workout_offerings.course_offering_id #{course_offering_filter}")
368376
.joins('LEFT JOIN terms ON terms.id = course_offerings.term_id')
369377
.joins('LEFT JOIN courses ON courses.id = course_offerings.course_id')
370378
.joins('LEFT JOIN coding_prompt_answers ON
@@ -389,9 +397,9 @@ def denormalized_attempt_data(workout_id = nil)
389397
courses.number as course_number,
390398
courses.name as course_name,
391399
terms.slug as term')
392-
if workout_id
393-
result = result.where("workouts.id = #{workout_id}")
394-
end
400+
# if workout_id
401+
# result = result.where("workouts.id = #{workout_id}")
402+
# end
395403

396404
return result
397405
end
@@ -412,7 +420,7 @@ def set_defaults
412420
self.experience ||= 10
413421
end
414422

415-
def progsnap2_main_events_csv(denormalized_data)
423+
def self.progsnap2_main_events_csv(denormalized_data)
416424
# MainTable
417425
main_attributes = %w{
418426
SubjectID
@@ -520,7 +528,7 @@ def progsnap2_main_events_csv(denormalized_data)
520528
return data
521529
end
522530

523-
def progsnap2_code_states_csv(denormalized_data)
531+
def self.progsnap2_code_states_csv(denormalized_data)
524532
code_state_attributes = %w{
525533
CodeStateID
526534
Code

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
get 'exercises/search' => 'exercises#search', as: :exercises_search
6565
get 'exercises/query_data' => 'exercises#query_data',
6666
as: :exercises_query_data
67-
get 'exercises/:id/download_attempt_data' =>
67+
get 'exercises/download_attempt_data' =>
6868
'exercises#download_attempt_data', as: :download_exercise_attempt_data
6969
# At the bottom, so the routes above take precedence over existing ids
7070
resources :exercises

0 commit comments

Comments
 (0)