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 'roo'
|
||||
|
||||
# encoding: UTF-8
|
||||
|
||||
class Panel::Survey::BackEnd::SurveysController < OrbitBackendController
|
||||
|
||||
include AdminHelper
|
||||
|
@ -93,40 +95,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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue