Skip to content

CORE-861 Added feedback columns to assessments spreadsheet import #418

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

Merged
merged 3 commits into from
Apr 7, 2025
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
33 changes: 28 additions & 5 deletions app/routines/exercises/import/assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@
record_failures do |failures|
ProcessSpreadsheet.call(filename: filename, headers: :downcase) do |headers, row, row_index|
uuid_index ||= headers.index { |header| header == 'uuid' || header == 'page uuid' }
raise ArgumentError, 'Could not find page "UUID" column' if uuid_index.nil?

section_index ||= headers.index { |header| header == 'section' }
raise ArgumentError, 'Could not find "UUID" or "Section" columns' if uuid_index.nil? && section_index.nil?

unless section_index.nil?
book = OpenStax::Content::Abl.new.approved_books.find { |book| book.uuid == book_uuid }
page_uuid_by_book_location = {}
book.all_pages.each { |page| page_uuid_by_book_location[page.book_location] = page.uuid }
raise ArgumentError, "Could not find book with UUID #{book_uuid} in the ABL" if book.nil?
end

pre_or_post_index ||= headers.index { |header| header&.start_with?('pre') && header.end_with?('post') }
raise ArgumentError, 'Could not find "Pre or Post" column' if pre_or_post_index.nil?
Expand All @@ -45,10 +54,14 @@
raise ArgumentError, 'Could not find "Question Stem" column' if question_stem_index.nil?

answer_choice_indices ||= headers.filter_map.with_index do |header, index|
index if header&.start_with?('answer') || header&.end_with?('choice')
index if (header&.start_with?('answer') || header&.end_with?('choice')) && !header.include?('feedback')
end
raise ArgumentError, 'Could not find "Answer Choice" columns' if answer_choice_indices.empty?

feedback_indices ||= headers.filter_map.with_index do |header, index|
index if header&.include?('feedback')
end

correct_answer_index ||= headers.index { |header| header&.start_with?('correct') }
raise ArgumentError, 'Could not find "Correct Answer" column' if correct_answer_index.nil?

Expand All @@ -57,9 +70,14 @@

row_number = row_index + 1

page_uuid = row[uuid_index]
page_uuid = if uuid_index.nil? || row[uuid_index].blank?
page_uuid_by_book_location[row[section_index].split('.').map(&:to_i)] unless row[section_index].blank?
else
row[uuid_index]
end

if page_uuid.blank?
Rails.logger.info { "Skipped row ##{row_number} with blank page UUID" }
Rails.logger.info { "Skipped row ##{row_number} with blank Section or Page UUID" }

Check warning on line 80 in app/routines/exercises/import/assessments.rb

View check run for this annotation

Codecov / codecov/patch

app/routines/exercises/import/assessments.rb#L80

Added line #L80 was not covered by tests
next
end

Expand All @@ -85,9 +103,14 @@
answer_choice_indices.each_with_index do |row_index, answer_index|
content = row[row_index]
next if content.blank?

feedback_index = feedback_indices[answer_index]
feedback = row[feedback_index] unless feedback_index.nil?

stem.stem_answers << StemAnswer.new(
answer: Answer.new(question: question, content: parse(content, exercise)),
correctness: answer_index == correct_answer ? 1 : 0
correctness: answer_index == correct_answer ? 1 : 0,
feedback: parse(feedback, exercise)
)
end

Expand Down
2 changes: 1 addition & 1 deletion app/routines/process_spreadsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def exec(filename:, offset: 1, pad_cells: true, headers: false, &block)
args = []
pad_to_size = 0 if pad_cells
klass.new(filename).public_send(method).each_with_index do |row, row_index|
normalized_row = row.map { |value| value&.to_s&.strip }
normalized_row = row.map { |cell| cell.value&.to_s&.strip }

if headers && row_index == 0
header_row = normalized_row
Expand Down

Large diffs are not rendered by default.

Binary file added spec/fixtures/sample_import_assessments.xlsx
Binary file not shown.
45 changes: 45 additions & 0 deletions spec/routines/exercises/import/assessments_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'rails_helper'
require 'vcr_helper'

RSpec.describe Exercises::Import::Assessments, type: :routine, vcr: VCR_OPTS do
let(:book_uuid) { '62a49025-8cd8-407c-9cfb-c7eba55cf1c6' }
let(:fixture_path) { 'spec/fixtures/sample_import_assessments.xlsx' }

it 'imports exercises from the spreadsheet and adds Assignable tags' do
allow(User).to receive(:find) { FactoryBot.create :user, :agreed_to_terms }

allow_any_instance_of(OpenStax::Content::Book).to receive(:version).and_return('964da1b')

expect { described_class.call(book_uuid: book_uuid, filename: fixture_path) }.to change { Exercise.count }.by(2)

exercises = Exercise.order(:created_at).last(2)

expect(Set.new exercises[0].tags.map(&:to_s)).to eq Set[
'assessment:preparedness:https://openstax.org/orn/book:page/62a49025-8cd8-407c-9cfb-c7eba55cf1c6:97af6b57-0004-4218-99c1-f2cfedea8f30',
'context-cnxmod:97af6b57-0004-4218-99c1-f2cfedea8f30',
'book-slug:college-success-concise',
'book-slug:preparing-for-college-success',
'module-slug:college-success-concise:1-1-why-college',
'module-slug:preparing-for-college-success:2-1-why-college',
]
expect(exercises[0].questions.first.stems.first.content).to eq 'Some question?'
expect(Set.new(exercises[0].questions.first.stems.first.stem_answers.map do |stem_answer|
[stem_answer.answer.content, stem_answer.correctness.to_s, stem_answer.feedback]
end)).to eq Set[['Right', '1.0', 'This is why A is correct'], ['Wrong', '0.0', 'This is why B is wrong']]
expect(exercises[0].questions.first.collaborator_solutions.first.content).to eq 'Some solution'
expect(exercises[0].publication.published_at?).to eq true

expect(Set.new exercises[1].tags.map(&:to_s)).to eq Set[
'assessment:practice:https://openstax.org/orn/book:page/62a49025-8cd8-407c-9cfb-c7eba55cf1c6:6c30d0cc-e435-4081-be68-5ff2f558cbec',
'context-cnxmod:6c30d0cc-e435-4081-be68-5ff2f558cbec',
'book-slug:college-success',
'module-slug:college-success:1-2-the-first-year-of-college-will-be-an-experience',
]
expect(exercises[1].questions.first.stems.first.content).to eq 'Another question?'
expect(Set.new(exercises[1].questions.first.stems.first.stem_answers.map do |stem_answer|
[stem_answer.answer.content, stem_answer.correctness.to_s, stem_answer.feedback]
end)).to eq Set[['Not this one', '0.0', 'Better luck next time'], ['This one', '1.0', 'Good job']]
expect(exercises[1].questions.first.collaborator_solutions.first.content).to eq 'Another solution'
expect(exercises[1].publication.published_at?).to eq true
end
end
Loading