Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize column names in csv exports #3011

Merged
merged 8 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions app/controllers/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,18 @@ def scoresheet
format.js
format.json
format.csv do
sheet = CSV.generate do |csv|
csv << [I18n.t('courses.scoresheet.explanation')]
columns = [User.human_attribute_name('first_name'), User.human_attribute_name('last_name'), User.human_attribute_name('username'), User.human_attribute_name('email')]
sheet = CSV.generate(force_quotes: true) do |csv|
users_labels = @course.course_memberships
.includes(:course_labels, :user)
.map { |m| [m.user, m.course_labels] }
.to_h
columns = %w[id username last_name first_name email labels]
columns.concat(@series.map(&:name))
columns.concat(@series.map { |s| I18n.t('courses.scoresheet.started', series: s.name) })
csv << columns
csv << ['Maximum', '', '', ''].concat(@series.map(&:activity_count)).concat(@series.map(&:activity_count))
csv << ['Maximum', '', '', '', '', ''].concat(@series.map(&:activity_count)).concat(@series.map(&:activity_count))
@users.each do |u|
row = [u.first_name, u.last_name, u.username, u.email]
row = [u.id, u.username, u.first_name, u.last_name, u.email, users_labels[u].map(&:name).join(';')]
row.concat(@series.map { |s| @hash[[u.id, s.id]][:accepted] })
row.concat(@series.map { |s| @hash[[u.id, s.id]][:started] })
csv << row
Expand Down
13 changes: 8 additions & 5 deletions app/controllers/series_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,18 @@ def scoresheet
format.js
format.json
format.csv do
sheet = CSV.generate do |csv|
csv << [I18n.t('series.scoresheet.explanation')]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this and the other explanation string are used in other places, so they can be removed from the locales files.

columns = [User.human_attribute_name('first_name'), User.human_attribute_name('last_name'), User.human_attribute_name('username'), User.human_attribute_name('email'), @series.name]
users_labels = @course.course_memberships
.includes(:course_labels, :user)
.map { |m| [m.user, m.course_labels] }
.to_h
sheet = CSV.generate(force_quotes: true) do |csv|
columns = ['id', 'username', 'last_name', 'first_name', 'email', 'labels', @series.name]
columns.concat(@activities.map(&:name))
columns.concat(@activities.map { |a| I18n.t('series.scoresheet.status', activity: a.name) })
csv << columns
csv << ['Maximum', '', '', '', @activities.count].concat(@activities.map { 1 }).concat(@activities.map { '' })
csv << ['Maximum', '', '', '', '', '', @activities.count].concat(@activities.map { 1 }).concat(@activities.map { '' })
@users.each do |u|
row = [u.first_name, u.last_name, u.username, u.email]
row = [u.id, u.username, u.first_name, u.last_name, u.email, users_labels[u].map(&:name).join(';')]
succeeded_exercises = @activities.map do |a|
if a.exercise?
@submissions[[u.id, a.id]]&.accepted ? 1 : 0
Expand Down
6 changes: 4 additions & 2 deletions app/helpers/export_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ def generate_zip_data(users, exercises, submissions)
csv << if indianio?
%w[filename status submission_id name_en name_nl exercise_id]
else
headers = %w[filename full_name id status submission_id name_en name_nl exercise_id created_at]
headers = %w[filename id username last_name first_name full_name email]
headers << 'labels' if labels?
headers += %w[status submission_id name_en name_nl exercise_id created_at]
headers
end
submissions.each do |submission|
Expand Down Expand Up @@ -226,8 +227,9 @@ def csv_submission(csv, user, exercise, submission, filename)
csv << if indianio?
[filename, submission&.status, submission&.id, exercise.name_en, exercise.name_nl, exercise.id]
else
row = [filename, user.full_name, user.id, submission&.status, submission&.id, exercise.name_en, exercise.name_nl, exercise.id, submission&.created_at]
row = [filename, user.id, user.username, user.last_name, user.first_name, user.full_name, user.email]
row << @users_labels[user].map(&:name).join(';') if labels?
row += [submission&.status, submission&.id, exercise.name_en, exercise.name_nl, exercise.id, submission&.created_at]
row
end
end
Expand Down
8 changes: 6 additions & 2 deletions app/models/evaluation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,19 @@ def change_grade_visibility!(visible)

def grades_csv
sheet = evaluation_sheet
users_labels = series.course.course_memberships
.includes(:course_labels, :user)
.map { |m| [m.user, m.course_labels] }
.to_h
CSV.generate(force_quotes: true) do |csv|
headers = ['Name', 'Email', 'Total Score', 'Total Max']
headers = ['id', 'username', 'last_name', 'first_name', 'full_name', 'email', 'labels', 'Total Score', 'Total Max']
headers += sheet[:evaluation_exercises].flat_map { |e| ["#{e.exercise.name} Score", "#{e.exercise.name} Max"] }
csv << headers
users.order(last_name: :asc, first_name: :asc).each do |user|
feedback_l = sheet[:feedbacks][user.id]
total_score = sheet[:averages][user.id]
total_max = sheet[:evaluation_exercises].map(&:maximum_score).reject(&:nil?).sum
row = [user.full_name, user.email, total_score, total_max]
row = [user.id, user.username, user.first_name, user.last_name, user.full_name, user.email, users_labels[user].map(&:name).join(';'), total_score, total_max]
row += feedback_l.flat_map { |f| [f.score, f.maximum_score] }
csv << row
end
Expand Down
1 change: 0 additions & 1 deletion config/locales/views/courses/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ en:
manage_series: "Manage series"
scoresheet:
user: User
explanation: "Showing the number of correctly solved exercises before the deadline"
at_least_one_started: "Only show users that started at least one exercise"
download: Download scoresheet
scoresheet: Scoresheet
Expand Down
1 change: 0 additions & 1 deletion config/locales/views/courses/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ nl:
create: "Cursus aanmaken"
scoresheet:
user: Gebruiker
explanation: "Toont het aantal correct opgeloste oefeningen voor het verstrijken van de deadline"
at_least_one_started: "Toon enkel gebruikers die tenminste aan één oefening begonnen zijn"
scoresheet: Statusoverzicht
download: Statusoverzicht downloaden
Expand Down
1 change: 0 additions & 1 deletion config/locales/views/series/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ en:
confirm_evaluation: Note there is an evaluation linked to this series. If you delete this series all feedback in that evaluation will be deleted as well.
scoresheet:
user: User
explanation: "Showing the number of correctly solved exercises and completed reading activities before the deadline"
at_least_one_started: "Only show users that started at least one learning activity"
no_activities_html: "This series doesn't have any learning activities. You can add some on the <a href=%{series_edit_url}>series edit page</a>."
download: Download scoresheet
Expand Down
1 change: 0 additions & 1 deletion config/locales/views/series/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ nl:
confirm_evaluation: Merk op dat er een evaluatie aan deze reeks gelinked is. Als je de reeks verwijderd zal alle feedback binnen die evaluatie ook verwijderd worden.
scoresheet:
user: Gebruiker
explanation: "Toont het aantal correct opgeloste oefeningen en afgewerkte leesactiviteiten voor het verstrijken van de deadline"
at_least_one_started: "Toon enkel gebruikers die tenminste aan één leeractiviteit begonnen zijn"
no_activities_html: "Deze reeks bevat nog geen leeractiviteiten. Je kan er toevoegen op de <a href=%{series_edit_url}>edit pagina van de reeks</a>."
download: Statusoverzicht downloaden
Expand Down
19 changes: 9 additions & 10 deletions test/controllers/evaluations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def setup
assert_equal 1 + evaluation.evaluation_users.length, csv.size

header = csv.shift
assert_equal 4 + evaluation.evaluation_exercises.length * 2, header.length
assert_equal 9 + evaluation.evaluation_exercises.length * 2, header.length

# Get which users will have a score
# First, the users we added a score for.
Expand All @@ -422,9 +422,9 @@ def setup
score_item_exercise_position = header.index { |h| h == "#{exercise.exercise.name} Score" }
csv.each do |line|
# Only one exercise has a score.
if users.key?(line[1])
if users.key?(line[5])
exported_score = BigDecimal(line.delete_at(score_item_exercise_position))
assert_equal users[line[1]], exported_score
assert_equal users[line[5]], exported_score
else
exported_score = line.delete_at(score_item_exercise_position)
assert_equal '', exported_score
Expand All @@ -433,7 +433,7 @@ def setup
assert_equal score_item.maximum, exported_max

# All other scores should be nil.
assert line[4..].all?(&:empty?)
assert line[9..].all?(&:empty?)
end

sign_out @course_admin
Expand Down Expand Up @@ -482,25 +482,24 @@ def setup

# Total score should equal sum of scores, Total max should equal the sum of maximum scores
csv.each do |line|
puts line
# Possible that total maximum is present, but all scores are empty en thus total score is empty
total_maximum = 0
if line[2] == ''
(4...line.length - 1).step(2).each do |index|
if line[7] == ''
(9...line.length - 1).step(2).each do |index|
assert_equal line[index], ''
total_maximum += BigDecimal(line[index + 1]) unless line[index + 1] == ''
end
else
total_score = 0
(4..line.length - 1).step(2).each do |index|
(9..line.length - 1).step(2).each do |index|
total_score += BigDecimal(line[index]) unless line[index] == ''
total_maximum += BigDecimal(line[index + 1]) unless line[index + 1] == ''
end

assert_equal BigDecimal(line[2]), total_score
assert_equal BigDecimal(line[7]), total_score

end
assert_equal BigDecimal(line[3]), total_maximum
assert_equal BigDecimal(line[8]), total_maximum
end
end

Expand Down