Skip to content

Commit

Permalink
Also use headers in exercises:tag:assessments
Browse files Browse the repository at this point in the history
  • Loading branch information
Dantemss committed Dec 18, 2024
1 parent 6821a30 commit 979d22a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 32 deletions.
4 changes: 2 additions & 2 deletions app/routines/exercises/map/spreadsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def exec(filename:, skip_first_row: true)
dest_uuids = values.second.split(',')
dest_tags = dest_uuids.map { |uuid| "context-cnxmod:#{uuid}" }

row_number = row_index + row_offset + 1
row_number = row_index + 1

begin
tag(exercises, dest_tags, row_number)
tag exercises, dest_tags
rescue StandardError => se
Rails.logger.error { "Failed to import row ##{row_number} - #{se.message}" }
failures[row_number] = se.to_s
Expand Down
43 changes: 28 additions & 15 deletions app/routines/exercises/tag/assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,35 @@ class Assessments
include RowParser
include ::Exercises::Tagger

def exec(filename:, book_uuid:, skip_first_row: true)
def exec(filename:, book_uuid:)
Rails.logger.info { "Filename: #{filename}" }

row_offset = skip_first_row ? 1 : 0

uuid_index = nil
pre_indices = nil
post_indices = nil
record_failures do |failures|
ProcessSpreadsheet.call(filename: filename, offset: row_offset) do |row, row_index|
values = 0.upto(row.size - 1).map do |index|
row[index]&.value&.to_s
end.compact
next if values.size <= 2
ProcessSpreadsheet.call(filename: filename, headers: :downcase) do |headers, row, row_index|
uuid_index ||= headers.index { |header| header.include?('uuid') }
raise ArgumentError, 'Could not find page UUID column' if uuid_index.nil?

pre_indices ||= headers.filter_map.with_index do |header, index|
index if header.include?('pre')
end
raise ArgumentError, 'Could not find pre-section columns' if pre_indices.empty?

page_uuid = values.first
post_indices ||= headers.filter_map.with_index do |header, index|
index if header.include?('post')
end
raise ArgumentError, 'Could not find post-section columns' if post_indices.empty?

page_uuid = row[uuid_index].to_s
pre_section_exercise_numbers = row.values_at(*pre_indices).filter_map do |val|
Integer(val.value) unless val.blank?
end
post_section_exercise_numbers = row.values_at(*post_indices).filter_map do |val|
Integer(val.value) unless val.blank?
end

pre_section_exercise_numbers = values[2..4].reject(&:blank?).map(&:to_i)
post_section_exercise_numbers = values[5..7].reject(&:blank?).map(&:to_i)
exercise_numbers = pre_section_exercise_numbers + post_section_exercise_numbers
exercises = Exercise.joins(publication: :publication_group)
.where(publication: { publication_group: { number: exercise_numbers } })
Expand All @@ -50,12 +63,12 @@ def exec(filename:, book_uuid:, skip_first_row: true)
pre_section_tag = "assessment:preparedness:https://openstax.org/orn/book:page/#{book_uuid}:#{page_uuid}"
post_section_tag = "assessment:practice:https://openstax.org/orn/book:page/#{book_uuid}:#{page_uuid}"

row_number = row_index + row_offset + 1
row_number = row_index + 1

begin
tag pre_and_post_section_exercises, [ pre_section_tag, post_section_tag ], row_number
tag pre_section_exercises, [ pre_section_tag ], row_number
tag post_section_exercises, [ post_section_tag ], row_number
tag pre_and_post_section_exercises, [ pre_section_tag, post_section_tag ]
tag pre_section_exercises, [ pre_section_tag ]
tag post_section_exercises, [ post_section_tag ]
rescue StandardError => se
Rails.logger.error { "Failed to import row ##{row_number} - #{se.message}" }
failures[row_number] = se.to_s
Expand Down
4 changes: 2 additions & 2 deletions app/routines/exercises/tag/spreadsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def exec(filename:, skip_first_row: true)

tags = values.slice(1..-1).flat_map { |value| value.split(',') }

row_number = row_index + row_offset + 1
row_number = row_index + 1

begin
tag(exercises, tags, row_number)
tag exercises, tags
rescue StandardError => se
Rails.logger.error { "Failed to import row ##{row_number} - #{se.message}" }
failures[row_number] = se.to_s
Expand Down
10 changes: 6 additions & 4 deletions app/routines/process_spreadsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class ProcessSpreadsheet

protected

def exec(filename:, offset: 1, pad_cells: true, return_headers: false, &block)
def exec(filename:, offset: 1, pad_cells: true, headers: false, &block)
raise ArgumentError, 'A block must be provided' if block.nil?

if File.extname(filename) == 'csv'
Expand All @@ -17,14 +17,16 @@ def exec(filename:, offset: 1, pad_cells: true, return_headers: false, &block)
args = []
pad_to_size = 0 if pad_cells
klass.new(filename).public_send(method).each_with_index do |row, row_index|
if return_headers && row_index == 0
args << row.map(&:to_s)
if headers && row_index == 0
header_row = row.map { |header| header.to_s || '' }
header_row = header_row.map { |header| header.send(headers) } if [String, Symbol].include?(headers.class)
args << header_row
elsif pad_cells
row += [nil] * (pad_to_size - row.length) if pad_to_size > row.length
pad_to_size = row.length
end

block.call(*args.concat([row, row_index])) if row_index >= offset
block.call(*(args + [row, row_index])) if row_index >= offset
end
end
end
2 changes: 1 addition & 1 deletion lib/exercises/tagger.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Exercises
module Tagger
def tag(exercises, new_tags, row_number)
def tag(exercises, new_tags)
skipped_uids = []
unpublished_uids = []
published_uids = []
Expand Down
13 changes: 5 additions & 8 deletions lib/tasks/exercises/tag.rake
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ namespace :exercises do

# Tags exercises for OpenStax Assessments using a spreadsheet
# Arguments are, in order:
# filename, book_uuid, [skip_first_row]
# filename, book_uuid
# Example: rake exercises:tag:assessments[tags.xlsx,12345]
# will tag exercises based on tags.xlsx with the book_uuid 12345
desc 'tags exercises for OpenStax Assessments using a spreadsheet'
task :assessments, [:filename, :book_uuid, :skip_first_row] => :environment do |t, args|
task :assessments, [:filename, :book_uuid] => :environment do |t, args|
# Output import logging info to the console (except in the test environment)
original_logger = Rails.logger

Expand Down Expand Up @@ -83,19 +83,16 @@ namespace :exercises do
Rails.logger.info { "Processing \"#{args[:filename]}\"" }

output_filename = "#{book.slug}.csv"
downcased_headers = nil
chapter_index = nil
exercise_id_index = nil

Check warning on line 87 in lib/tasks/exercises/tag.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/exercises/tag.rake#L86-L87

Added lines #L86 - L87 were not covered by tests
CSV.open(output_filename, 'w') do |csv|
csv << [ 'Exercise UID', 'Tags...' ]

ProcessSpreadsheet.call(filename: args[:filename], return_headers: true) do |headers, row, index|
downcased_headers ||= headers.map(&:downcase)

chapter_index ||= downcased_headers.find_index { |header| header.include? 'chapter' }
ProcessSpreadsheet.call(filename: args[:filename], headers: :downcase) do |headers, row, index|
chapter_index ||= headers.index { |header| header.include? 'chapter' }
raise ArgumentError, 'Could not find Chapter column' if chapter_index.nil?

Check warning on line 93 in lib/tasks/exercises/tag.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/exercises/tag.rake#L91-L93

Added lines #L91 - L93 were not covered by tests

exercise_id_index ||= downcased_headers.find_index do |header|
exercise_id_index ||= headers.index do |header|
header.include?('assessment') || header.include?('exercise')

Check warning on line 96 in lib/tasks/exercises/tag.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/exercises/tag.rake#L95-L96

Added lines #L95 - L96 were not covered by tests
end
raise ArgumentError, 'Could not find Assessment ID column' if exercise_id_index.nil?

Check warning on line 98 in lib/tasks/exercises/tag.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/exercises/tag.rake#L98

Added line #L98 was not covered by tests
Expand Down

0 comments on commit 979d22a

Please sign in to comment.