From 71acee200c3e8ad5df43026087a6e64cd8afb486 Mon Sep 17 00:00:00 2001 From: Bernie Chiu Date: Wed, 23 Oct 2013 14:10:04 +0800 Subject: [PATCH] Fix encoding issue and add import on title change --- .../survey/back_end/surveys_controller.rb | 56 +++++++++++-------- .../survey/app/models/survey_question.rb | 1 - 2 files changed, 32 insertions(+), 25 deletions(-) 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 4cd0b6480..23729bd03 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 @@ -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 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