new export csv table

Conflicts:

	vendor/built_in_modules/survey/app/controllers/panel/survey/back_end/surveys_controller.rb
This commit is contained in:
iCross 2013-05-23 20:56:51 +08:00
parent 15347f25dc
commit cf0d7cfc64
1 changed files with 59 additions and 46 deletions

View File

@ -89,53 +89,66 @@ class Panel::Survey::BackEnd::SurveysController < OrbitBackendController
@survey = ::Survey.find(params[:id])
@chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data
csv = CSV.generate do |csv|
row = []
@survey_questions.each do |question|
row << question.title
row << ' '
end
csv << row
csv_stats = []
@survey_questions.each do |question|
csv_options = []
csv_counts = []
case question.type
when ::SurveyQuestion::Radio, ::SurveyQuestion::Select, ::SurveyQuestion::Check
question.survey_question_options.map do |option|
csv_options << option.name
csv_counts << "#{(@chart_data[question.id.to_s][option.name] || 0 rescue 0)}"
end
when ::SurveyQuestion::Radiogroup
question.survey_question_options.map do |option|
question.survey_question_radiogroups.map do |group|
csv_options << "#{option.name} - #{group.name}"
csv_counts << "#{(@chart_data[question.id.to_s][option.name][group.name] || 0 rescue 0)}"
end
end.flatten
else
csv_options << " "
csv_counts << " "
end
csv_stats << csv_options
csv_stats << csv_counts
end
max_length = csv_stats.map(&:length).max
csv_stats.map do |l|
while l.length < max_length
l.push ' '
end
l
end.transpose.each do |l|
csv << l
end
@survey_answers.each do |answer|
row = []
@survey_questions.each do |question|
if question.type == ::SurveyQuestion::Radiogroup
options = Hash[question.survey_question_options.collect{|o| [ o.id.to_s, o.name ] }]
row << Hash[answer[question.id.to_s].map {|o, g| [options[o], g]}]
else
row << answer[question.id.to_s]
end
row << ' '
end
csv << row
end
end
respond_to do |format|
format.csv do
csv = CSV.generate do |csv|
csv << @survey_questions.map(&:title)
csv_stats = []
csv_stats = @survey_questions.map do |question|
case question.type
when ::SurveyQuestion::Radio, ::SurveyQuestion::Select, ::SurveyQuestion::Check
question.survey_question_options.map do |option|
"#{option.name} : #{(@chart_data[question.id.to_s][option.name] || 0 rescue 0)}"
end
when ::SurveyQuestion::Radiogroup
question.survey_question_options.map do |option|
question.survey_question_radiogroups.map do |group|
"#{option.name} - #{group.name} : #{(@chart_data[question.id.to_s][option.name][group.name] || 0 rescue 0)}"
end
end.flatten
else
[' ']
end
end
max_length = csv_stats.map(&:length).max
csv_stats.map do |l|
while l.length < max_length
l.push ' '
end
l
end.transpose.each do |l|
csv << l
end
@survey_answers.each do |answer|
row = []
@survey_questions.each do |question|
if question.type == ::SurveyQuestion::Radiogroup
options = Hash[question.survey_question_options.collect{|o| [ o.id.to_s, o.name ] }]
row << Hash[answer[question.id.to_s].map {|o, g| [options[o], g]}]
else
row << answer[question.id.to_s]
end
end
csv << row
end
end
render :text => csv
response.headers["Content-Type"] = "text/csv; charset=big5"
render :text => csv.encode('Big5')
end
end
end
@ -214,4 +227,4 @@ class Panel::Survey::BackEnd::SurveysController < OrbitBackendController
end
end
end
end