Fix encoding issue and add import on title change
This commit is contained in:
parent
a838803b28
commit
477eaeaf5c
|
@ -2,6 +2,8 @@ require 'csv'
|
||||||
require 'iconv'
|
require 'iconv'
|
||||||
require 'roo'
|
require 'roo'
|
||||||
|
|
||||||
|
# encoding: UTF-8
|
||||||
|
|
||||||
class Panel::Survey::BackEnd::SurveysController < OrbitBackendController
|
class Panel::Survey::BackEnd::SurveysController < OrbitBackendController
|
||||||
|
|
||||||
include AdminHelper
|
include AdminHelper
|
||||||
|
@ -93,40 +95,46 @@ class Panel::Survey::BackEnd::SurveysController < OrbitBackendController
|
||||||
|
|
||||||
def import
|
def import
|
||||||
@survey = ::Survey.find(params[:id])
|
@survey = ::Survey.find(params[:id])
|
||||||
@file = params[:file]
|
|
||||||
@chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data
|
@chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data
|
||||||
|
@file = params[:file]
|
||||||
|
|
||||||
case File.extname(@file.original_filename)
|
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 ".xls" then Excel.new(file.path, nil, :ignore)
|
||||||
# when ".xlsx" then Excelx.new(file.path, nil, :ignore)
|
# when ".xlsx" then Excelx.new(file.path, nil, :ignore)
|
||||||
else raise "Unknown file type: #{file.original_filename}"
|
else raise "Unknown file type: #{file.original_filename}"
|
||||||
end
|
end
|
||||||
|
|
||||||
@start_row = 2
|
# Modify titles if changed
|
||||||
@survey.survey_answers.destroy
|
titles = @spreadsheet.row(1) - [" "]
|
||||||
@survey_questions.each_with_index do |question, index|
|
@survey_questions.each_with_index do |question, index|
|
||||||
case question.type
|
question.update_attributes(title: titles[index])
|
||||||
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
|
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
|
redirect_to panel_survey_back_end_surveys_url, :notice => :success
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -44,5 +44,4 @@ class SurveyQuestion
|
||||||
def get_jump_tos
|
def get_jump_tos
|
||||||
Hash[survey_question_options.select{ |o| !o.jump_to.blank? }.collect{ |o| [o.id.to_s, o.jump_to] }]
|
Hash[survey_question_options.select{ |o| !o.jump_to.blank? }.collect{ |o| [o.id.to_s, o.jump_to] }]
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue