Skip to content

Commit

Permalink
Merge pull request #5022 from dodona-edu/fix/sorting-by-date
Browse files Browse the repository at this point in the history
Fix sorting for most recent submission per user
  • Loading branch information
jorg-vr authored Oct 3, 2023
2 parents 12f2329 + dd305b2 commit ec81f58
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
14 changes: 13 additions & 1 deletion app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,20 @@ def set_submissions

@course_membership = CourseMembership.find_by(user: @user, course: @course) if @user.present? && @course.present?

return unless params[:most_recent_per_user]

# this cannot use has_scope, because we need the scopes in this method
# to be applied before this one
@submissions = @submissions.most_recent_per_user if params[:most_recent_per_user]
@submissions = @submissions.most_recent_per_user

if params[:order_by].present?
# reapplies the order_by scope if present in the params
# this is needed because the previous line creates a group by query, which breaks the order_by scope
@submissions = Submission.where(id: @submissions) # otherwise the group_by breaks order_by scopes that use joins
@submissions = apply_scopes(@submissions, { order_by: params[:order_by] })
else
# reapply the default order scope, as most_recent_per_user breaks it
@submissions = @submissions.reorder(id: :desc)
end
end
end
52 changes: 52 additions & 0 deletions test/controllers/submissions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,56 @@ def expected_score_string(*args)
assert_match expected_score_string(feedback.score, feedback.maximum_score), response.body
end
end

test 'should be able to order most recent submissions by user' do
u = create :user, first_name: 'abcd'
u2 = create :user, first_name: 'efgh'
course = create :course, series_count: 1, activities_per_series: 1
e = course.series.first.exercises.first
create :submission, exercise: e, user: u, course: course, created_at: 2.minutes.ago, status: :correct
least_recent = create :submission, exercise: e, user: u2, course: course, created_at: 1.minute.ago, status: :wrong
most_recent = create :submission, exercise: e, user: u, course: course, status: :running

get course_series_activity_submissions_path(course, course.series.first, e), params: { most_recent_per_user: true, format: :json }

assert_equal 2, response.parsed_body.count
assert_equal most_recent.id, response.parsed_body.first['id']
assert_equal least_recent.id, response.parsed_body.second['id']

get course_series_activity_submissions_path(course, course.series.first, e), params: { most_recent_per_user: true, order_by: { column: 'created_at', direction: 'ASC' }, format: :json }

assert_equal 2, response.parsed_body.count
assert_equal least_recent.id, response.parsed_body.first['id']
assert_equal most_recent.id, response.parsed_body.second['id']

get course_series_activity_submissions_path(course, course.series.first, e), params: { most_recent_per_user: true, order_by: { column: 'created_at', direction: 'DESC' }, format: :json }

assert_equal 2, response.parsed_body.count
assert_equal most_recent.id, response.parsed_body.first['id']
assert_equal least_recent.id, response.parsed_body.second['id']

get course_series_activity_submissions_path(course, course.series.first, e), params: { most_recent_per_user: true, order_by: { column: 'user', direction: 'DESC' }, format: :json }

assert_equal 2, response.parsed_body.count
assert_equal least_recent.id, response.parsed_body.first['id']
assert_equal most_recent.id, response.parsed_body.second['id']

get course_series_activity_submissions_path(course, course.series.first, e), params: { most_recent_per_user: true, order_by: { column: 'user', direction: 'ASC' }, format: :json }

assert_equal 2, response.parsed_body.count
assert_equal most_recent.id, response.parsed_body.first['id']
assert_equal least_recent.id, response.parsed_body.second['id']

get course_series_activity_submissions_path(course, course.series.first, e), params: { most_recent_per_user: true, order_by: { column: 'status', direction: 'DESC' }, format: :json }

assert_equal 2, response.parsed_body.count
assert_equal most_recent.id, response.parsed_body.first['id']
assert_equal least_recent.id, response.parsed_body.second['id']

get course_series_activity_submissions_path(course, course.series.first, e), params: { most_recent_per_user: true, order_by: { column: 'status', direction: 'ASC' }, format: :json }

assert_equal 2, response.parsed_body.count
assert_equal least_recent.id, response.parsed_body.first['id']
assert_equal most_recent.id, response.parsed_body.second['id']
end
end

0 comments on commit ec81f58

Please sign in to comment.