diff --git a/vendor/built_in_modules/survey/app/controllers/panel/survey/back_end/surveys_controller.rb b/vendor/built_in_modules/survey/app/controllers/panel/survey/back_end/surveys_controller.rb index dbd2f4975..14f09510d 100644 --- a/vendor/built_in_modules/survey/app/controllers/panel/survey/back_end/surveys_controller.rb +++ b/vendor/built_in_modules/survey/app/controllers/panel/survey/back_end/surveys_controller.rb @@ -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 diff --git a/vendor/built_in_modules/survey/app/models/survey_question.rb b/vendor/built_in_modules/survey/app/models/survey_question.rb index 6feae2a5d..c21f06777 100644 --- a/vendor/built_in_modules/survey/app/models/survey_question.rb +++ b/vendor/built_in_modules/survey/app/models/survey_question.rb @@ -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