Fix encoding issue and add import on title change

This commit is contained in:
Bernie Chiu 2013-10-23 14:10:04 +08:00 committed by Manson Wang
parent e91386b393
commit 5e446bc37f
2 changed files with 32 additions and 25 deletions

View File

@ -1,5 +1,7 @@
require 'csv'
# encoding: UTF-8
class Panel::Survey::BackEnd::SurveysController < OrbitBackendController
include AdminHelper
@ -91,40 +93,46 @@ class Panel::Survey::BackEnd::SurveysController < OrbitBackendController
def import
@survey = ::Survey.find(params[:id])
@file = params[:file]
@chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data
@file = params[:file]
case File.extname(@file.original_filename)
when ".csv" then @spreadsheet = Roo::CSV.new(@file.path)
when ".csv" then @spreadsheet = Roo::CSV.new(@file.path, csv_options: {encoding: Encoding::Big5})
# when ".xls" then Excel.new(file.path, nil, :ignore)
# when ".xlsx" then Excelx.new(file.path, nil, :ignore)
else raise "Unknown file type: #{file.original_filename}"
end
@start_row = 2
@survey.survey_answers.destroy
@survey_questions.each_with_index do |question,index|
case question.type
when ::SurveyQuestion::Radio, ::SurveyQuestion::Select
(@start_row..(@start_row + question.survey_question_options.count - 1)).each do |row|
(1..@spreadsheet.row(row)[index*2 + 1].to_i).each do
@answer_model = @survey.survey_answers.new
@answer_model[question.id.to_s] = @spreadsheet.row(row)[index*2]
@answer_model.save!
end
end
when ::SurveyQuestion::Check
(@start_row..(@start_row + question.survey_question_options.count - 1)).each do |row|
(1..@spreadsheet.row(row)[index*2 + 1].to_i).each do
@answer_model = @survey.survey_answers.new
@answer_model[question.id.to_s] = [ @spreadsheet.row(row)[index*2] ]
@answer_model.save!
end
end
else
end
# Modify titles if changed
titles = @spreadsheet.row(1) - [" "]
@survey_questions.each_with_index do |question, index|
question.update_attributes(title: titles[index])
end
# @start_row = 2
# @survey.survey_answers.destroy
# @survey_questions.each_with_index do |question,index|
# case question.type
# when ::SurveyQuestion::Radio, ::SurveyQuestion::Select
# (@start_row..(@start_row + question.survey_question_options.count - 1)).each do |row|
# (1..@spreadsheet.row(row)[index*2 + 1].to_i).each do
# @answer_model = @survey.survey_answers.new
# @answer_model[question.id.to_s] = @spreadsheet.row(row)[index*2]
# @answer_model.save!
# end
# end
# when ::SurveyQuestion::Check
# (@start_row..(@start_row + question.survey_question_options.count - 1)).each do |row|
# (1..@spreadsheet.row(row)[index*2 + 1].to_i).each do
# @answer_model = @survey.survey_answers.new
# @answer_model[question.id.to_s] = [ @spreadsheet.row(row)[index*2] ]
# @answer_model.save!
# end
# end
# else
# end
# end
redirect_to panel_survey_back_end_surveys_url, :notice => :success
end

View File

@ -44,5 +44,4 @@ class SurveyQuestion
def get_jump_tos
Hash[survey_question_options.select{ |o| !o.jump_to.blank? }.collect{ |o| [o.id.to_s, o.jump_to] }]
end
end