Fix: make sure values don't get reordered after copying over row values from survey item variants. This fixes a problem where cleaner would produce a row with likert scores that got shifted to align with the wrong column

mciea-main
rebuilt 3 years ago
parent 81f8738d59
commit 25a2698ac9

@ -51,7 +51,7 @@ class SurveyItemValues
def dese_id def dese_id
@dese_id ||= begin @dese_id ||= begin
dese_id = nil dese_id = nil
dese_headers = ['DESE ID', 'Dese ID', 'DeseId', 'DeseID', 'School', 'school'] dese_headers = ["DESE ID", "Dese ID", "DeseId", "DeseID", "School", "school"]
school_headers = headers.select { |header| /School-\s\w/.match(header) } school_headers = headers.select { |header| /School-\s\w/.match(header) }
dese_headers << school_headers dese_headers << school_headers
dese_headers.flatten.each do |header| dese_headers.flatten.each do |header|
@ -106,7 +106,10 @@ class SurveyItemValues
def to_a def to_a
copy_likert_scores_from_variant_survey_items copy_likert_scores_from_variant_survey_items
row.remove_unwanted_columns headers.select(&:present?)
.reject { |key, _value| key.start_with? "Q" }
.reject { |key, _value| key.end_with? "-1" }
.map { |header| row[header] }
end end
def duration def duration
@ -120,7 +123,7 @@ class SurveyItemValues
def respondent_type def respondent_type
return :teacher if headers return :teacher if headers
.filter(&:present?) .filter(&:present?)
.filter { |header| header.start_with? 't-' }.count > 0 .filter { |header| header.start_with? "t-" }.count > 0
:student :student
end end
@ -128,14 +131,14 @@ class SurveyItemValues
def survey_type def survey_type
survey_item_ids = headers survey_item_ids = headers
.filter(&:present?) .filter(&:present?)
.reject { |header| header.end_with?('-1') } .reject { |header| header.end_with?("-1") }
.filter { |header| header.start_with?('t-', 's-') } .filter { |header| header.start_with?("t-", "s-") }
SurveyItem.survey_type(survey_item_ids:) SurveyItem.survey_type(survey_item_ids:)
end end
def valid_duration? def valid_duration?
return true if duration.nil? || duration == '' || duration.downcase == 'n/a' || duration.downcase == 'na' return true if duration.nil? || duration == "" || duration.downcase == "n/a" || duration.downcase == "na"
span_in_seconds = duration.to_i span_in_seconds = duration.to_i
return span_in_seconds >= 300 if survey_type == :teacher return span_in_seconds >= 300 if survey_type == :teacher
@ -146,8 +149,8 @@ class SurveyItemValues
end end
def valid_progress? def valid_progress?
progress = row['Progress'] progress = row["Progress"]
return true if progress.nil? || progress == '' || progress.downcase == 'n/a' || progress.downcase == 'na' return true if progress.nil? || progress == "" || progress.downcase == "n/a" || progress.downcase == "na"
progress = progress.to_i progress = progress.to_i
progress.to_i >= 25 progress.to_i >= 25
@ -172,7 +175,7 @@ class SurveyItemValues
def valid_sd? def valid_sd?
return true if survey_type == :early_education return true if survey_type == :early_education
survey_item_headers = headers.filter(&:present?).filter { |header| header.start_with?('s-', 't-') } survey_item_headers = headers.filter(&:present?).filter { |header| header.start_with?("s-", "t-") }
likert_scores = [] likert_scores = []
survey_item_headers.each do |header| survey_item_headers.each do |header|
likert_scores << likert_score(survey_item_id: header).to_i likert_scores << likert_score(survey_item_id: header).to_i
@ -190,20 +193,10 @@ class SurveyItemValues
private private
def copy_likert_scores_from_variant_survey_items def copy_likert_scores_from_variant_survey_items
headers.filter(&:present?).filter { |header| header.end_with? '-1' }.each do |header| headers.filter(&:present?).filter { |header| header.end_with? "-1" }.each do |header|
likert_score = row[header] likert_score = row[header]
main_item = header.gsub('-1', '') main_item = header.gsub("-1", "")
row[main_item] = likert_score if likert_score.present? row[main_item] = likert_score if likert_score.present?
end end
end end
end end
module RowMonkeyPatches
def remove_unwanted_columns
to_h.filter do |key, _value|
key.present?
end.reject { |key, _value| key.start_with? 'Q' }.reject { |key, _value| key.end_with? '-1' }.values
end
end
CSV::Row.include RowMonkeyPatches

Loading…
Cancel
Save