Skip to content

Instantly share code, notes, and snippets.

@yoon
Last active December 19, 2015 05:09
Show Gist options
  • Save yoon/5901858 to your computer and use it in GitHub Desktop.
Save yoon/5901858 to your computer and use it in GitHub Desktop.
surveyor export
def self.export(params)
{:report => "", :name => "blank"} if params[:responses].blank?
# surveys
responses_surveys = Survey.includes({:sections => {:questions => :answers}}).find(params[:responses] || [])
questions = responses_surveys.map(&:sections).flatten.map(&:questions).flatten.reject{|q| q.display_type == "label"}
answers = questions.map(&:answers).flatten
# response sets
from = params[:date_from].blank? ? nil : Date.strptime(params[:date_from], '%m-%d-%Y')
to = params[:date_to].blank? ? nil : Date.strptime(params[:date_to], '%m-%d-%Y')
response_sets = ResponseSet.includes({:responses => [:question, :answer]}).where(:survey_id => responses_surveys)
if from
if to
response_sets = response_sets.where("effective_date >= ? AND effective_date <= ?", from, to)
else
response_sets = response_sets.where("effective_date >= ?", from)
end
else
response_sets = response_sets.where("effective_date <= ?", to) if to
end
response_sets = response_sets.sort_by(&:created_at)
# data for responses
data_method = case params[:data]
when "answer_reference_identifier"
lambda {|responses| responses.map{|r| r.answer.response_class == "answer" ? r.answer.reference_identifier : [r.answer.reference_identifier, r.json_value].compact.join(": ")}.compact.join(", ") }
when "answer_data_export_identifier"
lambda {|responses| responses.map{|r| r.answer.response_class == "answer" ? r.answer.data_export_identifier : [r.answer.data_export_identifier, r.json_value].compact.join(": ")}.compact.join(", ") }
when "answer_weight"
lambda {|responses| responses.map{|r| r.answer.response_class == "answer" ? r.answer.weight : [r.answer.weight, r.json_value].compact.join(": ")}.compact.join(", ") }
when "response_count"
lambda {|responses| responses.count }
else # :answer_text is default
lambda {|responses| responses.map{|r| r.answer.response_class == "answer" ? r.answer.text : [r.answer.text, r.json_value].compact.join(": ")}.compact.join(", ") }
end
csv_string = FasterCSV.generate do |csv|
if responses_surveys.blank? or questions.blank?
csv << ["Note: "]
elsif params[:column] == "question"
csv << ["Note: this sheet contains 3 different header rows. You may delete the header rows you do not need as well as this first column."]
csv << ["question.reference_identifier"] + questions.map(&:reference_identifier)
csv << ["question.data_export_identifier"] + questions.map(&:data_export_identifier)
csv << ["question.text"] + questions.map(&:text)
else # params[:column] == "answer" is default
csv << ["Note: this sheet contains 7 different header rows. You may delete the header rows you do not need as well as this first column."]
csv << ["question.reference_identifier"] + questions.map{|q| q.answers.count.times.map{|i| q.reference_identifier} }.flatten
csv << ["question.data_export_identifier"] + questions.map{|q| q.answers.count.times.map{|i| q.data_export_identifier} }.flatten
csv << ["question.text"] + questions.map{|q| q.answers.count.times.map{|i| q.text} }.flatten
csv << ["answer.weight"] + answers.map(&:weight)
csv << ["answer.reference_identifier"] + answers.map(&:reference_identifier)
csv << ["answer.data_export_identifier"] + answers.map(&:data_export_identifier)
csv << ["answer.text"] + answers.map(&:text)
end
response_sets.each do |rs|
csv << [""] +
if params[:column] == "question"
questions.map{|q| data_method.call(rs.responses.select{|r| r.question_id == q.id}) }
else
answers.map{|a| data_method.call(rs.responses.select{|r| r.answer_id == a.id}) }
end
end
end
{:report => csv_string, :name => "#{'responses_' if !responses_surveys.empty?}#{Time.now.strftime("%Y-%m-%d_%H-%M")}"}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment