Skip to content

Commit

Permalink
Merge pull request #5894 from dodona-edu/fix/merge_users
Browse files Browse the repository at this point in the history
Correctly transfer 'last_updated_by' for annotations and scores when merging users
  • Loading branch information
jorg-vr authored Oct 29, 2024
2 parents 61496da + bc8b877 commit b34ff3a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,10 @@ def merge_into(other, force: false, force_institution: false)
events.each { |e| e.update!(user: other) }
exports.each { |e| e.update!(user: other) }
notifications.each { |n| n.update!(user: other) }
annotations.each { |a| a.update!(user: other, last_updated_by_id: other.id) }
annotations.each { |a| a.update!(user: other) }
Annotation.where(last_updated_by_id: id).find_each { |a| a.update!(last_updated_by: other) }
questions.each { |q| q.update!(user: other) }
Score.where(last_updated_by_id: id).find_each { |s| s.update!(last_updated_by: other) }

evaluation_users.each do |eu|
if other.evaluation_users.find { |oeu| oeu.evaluation_id == eu.evaluation_id }
Expand Down
44 changes: 44 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,50 @@ def setup
assert_equal 2, u2.announcement_views.count
end

test 'merge should transfer last_updated_by of non owned annotations' do
u1 = create :user
u2 = create :user

student = create :user

c = create :course, series_count: 1, exercises_per_series: 1
c.administrating_members << u1
c.enrolled_members << student

s = create :submission, user: student, course: c

a1 = create :annotation, user: u1, submission: s
a2 = create :annotation, user: create(:user), submission: s

a2.update(last_updated_by: u1)

result = u1.merge_into(u2)

assert result
assert_not u1.persisted?
assert_equal u2, a1.reload.last_updated_by
assert_equal u2, a2.reload.last_updated_by
end

test 'merge should transfer scores last updated by' do
u1 = create :user
u2 = create :user

evaluation = create :evaluation, :with_submissions
exercise = evaluation.evaluation_exercises.first
score_item1 = create :score_item, evaluation_exercise: exercise,
description: 'First item',
maximum: '10.0'
feedback = evaluation.feedbacks.first
s = create :score, last_updated_by: u1, score_item: score_item1, score: 5, feedback: feedback

result = u1.merge_into(u2)

assert result
assert_not u1.persisted?
assert_equal u2, s.reload.last_updated_by
end

test 'jump back in should return most recent incomplete activity' do
user = create :user

Expand Down

0 comments on commit b34ff3a

Please sign in to comment.