Skip to content

Commit 59e1b93

Browse files
Admin download attempt data from a course and term
1 parent 778e94d commit 59e1b93

3 files changed

Lines changed: 42 additions & 17 deletions

File tree

app/controllers/exercises_controller.rb

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

5757
# -------------------------------------------------------------
5858
def download_attempt_data
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]
59+
exercise_id = params[:id] # may be zero, one, or more ids (see Exercise.denormalized_attempt_data)
60+
course_id = params[:course_id]
61+
term_id = params[:term_id]
6262

6363
time = Time.now.utc.strftime("%Y%m%d%H%M%S")
6464

6565
if params[:progsnap].to_b
66-
main_events, code_states = Exercise.progsnap2_attempt_csv(exercise_id, course_offering_id, workout_id)
66+
main_events, code_states = Exercise.progsnap2_attempt_csv(exercise_id, course_id, term_id)
6767
compressed_filestream = Zip::OutputStream.write_buffer do |zos|
6868
main_events_file = "MainTable.csv"
6969
zos.put_next_entry main_events_file

app/models/exercise.rb

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ def is_publicly_available?
310310
end
311311
end
312312

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)
313+
def self.progsnap2_attempt_csv(exercise_id, course_id=nil, term_id=nil)
314+
denormalized = Exercise.denormalized_attempt_data(exercise_id, course_id, term_id)
315315
main_events = Exercise.progsnap2_main_events_csv(denormalized)
316316
code_states = Exercise.progsnap2_code_states_csv(denormalized)
317317
return main_events, code_states
@@ -356,28 +356,49 @@ def self.denormalized_attempt_csv(exercise_id)
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 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}" :
359+
def self.denormalized_attempt_data(exercise_id=nil, course_id=nil, term_id=nil)
360+
361+
unless exercise_id || (course_id && term_id)
362+
raise ArgumentError, 'Please specify one or more exercise ids, OR ' \
363+
'a course id and term id.'
364+
end
365+
366+
course_filter = course_id ?
367+
"AND courses.id = #{course_id}" :
362368
""
363-
workout_filter = workout_id ?
364-
"AND workouts.id = #{workout_id}" :
369+
term_filter = term_id ?
370+
"AND terms.id = #{term_id}" :
365371
""
366372

367-
result = Exercise.where(:id, exercise_id)
368-
.joins(exercise_versions: { attempts: :prompt_answers })
373+
if exercise_id
374+
result = Exercise.where(:id, exercise_id)
375+
.joins(exercise_versions: { attempts: :prompt_answers })
376+
else
377+
result = Exercise.joins(exercise_versions: { attempts: :prompt_answers })
378+
end
379+
result = result
369380
.joins('LEFT JOIN workout_scores ON
370381
workout_scores.id = attempts.workout_score_id')
371382
.joins('LEFT JOIN workout_offerings ON
372383
workout_offerings.id = workout_scores.workout_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}")
384+
.joins('LEFT JOIN workouts ON workouts.id = workout_scores.workout_id')
385+
.joins('LEFT JOIN course_offerings ON
386+
course_offerings.id = workout_offerings.course_offering_id')
376387
.joins('LEFT JOIN terms ON terms.id = course_offerings.term_id')
377388
.joins('LEFT JOIN courses ON courses.id = course_offerings.course_id')
378389
.joins('LEFT JOIN coding_prompt_answers ON
379390
prompt_answers.actable_id = coding_prompt_answers.id')
380-
.select('attempts.user_id,
391+
392+
if course_id
393+
result = result.where('courses.id = ?', course_id)
394+
end
395+
396+
if term_id
397+
result = result.where('terms.id = ?', term_id)
398+
end
399+
400+
result = result
401+
.select('attempts.user_id,
381402
exercise_versions.id as exercise_version_id,
382403
exercise_versions.version as version_no,
383404
coding_prompt_answers.id as answer_id,

app/views/courses/show.html.haml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
- if current_user.andand.is_a_member_of?(@course.user_group)
4646
= link_to "+ Add a section",
4747
organization_new_course_offering_path(organization_id: @course.organization.id, course_id: @course.id)
48+
- if current_user.global_role.is_admin?
49+
= link_to "Download exercise attempt data for this term",
50+
download_exercise_attempt_data_path(course_id: @course.id,
51+
term_id: @term.id, progsnap: true, format: :zip)
4852

4953
.container-fluid
5054
.navbar-collapse.collapse

0 commit comments

Comments
 (0)