From 0b91f8450470573fd99278d29404548dcad3c242 Mon Sep 17 00:00:00 2001 From: donny-wong <141858744+donny-wong@users.noreply.github.com> Date: Sun, 6 Oct 2024 11:15:34 -0400 Subject: [PATCH] Fix grader csv download for marks spreadsheets (#7257) --- Changelog.md | 1 + app/models/grade_entry_form.rb | 9 +++--- spec/models/grade_entry_form_spec.rb | 43 ++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/Changelog.md b/Changelog.md index 8baae3d36b..b658d4768b 100644 --- a/Changelog.md +++ b/Changelog.md @@ -13,6 +13,7 @@ - Fix incorrect calculation of token penalties when submissions are on time (#7216) - Ensured submissions that have been released cannot be recollected from the repo browser (#7254) - Fix bug where renaming a group to an existing group in a different assignment resulted in incorrect repository mapping (#7224) +- Fix Marks Spreadsheet csv bug of showing incorrect marks (#7257) ### 🔧 Internal changes diff --git a/app/models/grade_entry_form.rb b/app/models/grade_entry_form.rb index 4a8bbad4dd..6c54c82187 100644 --- a/app/models/grade_entry_form.rb +++ b/app/models/grade_entry_form.rb @@ -122,11 +122,10 @@ def export_as_csv(role) .group_by { |x| x[0] } num_items = self.grade_entry_items.count elsif role.ta? - grade_data = role.grade_entry_students - .joins(:user) - .joins(:grades) - .joins(:grade_entry_items) - .where(grade_entry_form: self) + grade_data = self.grades + .joins(:grade_entry_item, grade_entry_student: :user) + .joins(grade_entry_student: :grade_entry_student_tas) + .where('grade_entry_student_tas.ta_id': role.id) .pluck('users.user_name', 'grade_entry_items.position', 'grades.grade') .group_by { |x| x[0] } num_items = self.grade_entry_items.count diff --git a/spec/models/grade_entry_form_spec.rb b/spec/models/grade_entry_form_spec.rb index 3dac204889..92978ed736 100644 --- a/spec/models/grade_entry_form_spec.rb +++ b/spec/models/grade_entry_form_spec.rb @@ -385,4 +385,47 @@ end end end + + describe 'when TA downloads a csv file of the grades' do + let(:ta) { create(:ta) } + let!(:student1) { create(:student) } + let!(:student2) { create(:student) } + let!(:grade_entry_form_with_data) { create(:grade_entry_form_with_data) } + + before do + grade_entry_student1 = GradeEntryStudent.find_by(role: student1) + grade_entry_student2 = GradeEntryStudent.find_by(role: student2) + + GradeEntryStudentTa.create(grade_entry_student: grade_entry_student1, ta: ta) + GradeEntryStudentTa.create(grade_entry_student: grade_entry_student2, ta: ta) + + item2 = create(:grade_entry_item, name: 'Test2', position: 2, grade_entry_form: grade_entry_form_with_data) + + grade_entry_student1.grades.create(grade: Random.rand(item2.out_of), grade_entry_item: item2) + grade_entry_student1.save + + grade_entry_student2.grades.create(grade: Random.rand(item2.out_of), grade_entry_item: item2) + grade_entry_student2.save + + grade_spreadsheet = grade_entry_form_with_data.export_as_csv(ta) + results = CSV.parse(grade_spreadsheet, headers: false).drop(2) + + @res_marks = {} + results.each do |x| + mark1 = x[6] == '' ? nil : x[6].to_f + mark2 = x[7] == '' ? nil : x[7].to_f + @res_marks[x[0]] = [mark1, mark2] + end + + @student_marks = {} + @student_marks[student1.user.user_name] = + [grade_entry_student1.grades.first.grade, grade_entry_student1.grades.second.grade] + @student_marks[student2.user.user_name] = + [grade_entry_student2.grades.first.grade, grade_entry_student2.grades.second.grade] + end + + it 'correctly displays the marks' do + expect(@res_marks).to eq(@student_marks) + end + end end