diff --git a/app/controllers/panel/survey/back_end/surveys_controller.rb b/app/controllers/panel/survey/back_end/surveys_controller.rb index 7b31514..5ac2151 100644 --- a/app/controllers/panel/survey/back_end/surveys_controller.rb +++ b/app/controllers/panel/survey/back_end/surveys_controller.rb @@ -1,7 +1,9 @@ -require 'csv' - class Panel::Survey::BackEnd::SurveysController < OrbitBackendController + require 'csv' + require 'iconv' + require 'roo' + include AdminHelper include OrbitControllerLib::DivisionForDisable @@ -105,6 +107,145 @@ class Panel::Survey::BackEnd::SurveysController < OrbitBackendController redirect_to panel_survey_back_end_surveys_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options]) end + def set_import + @survey = ::Survey.find(params[:id]) + end + + def import + @survey = ::Survey.find(params[:id]) + @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, 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 + + # Modify titles if changed + titles = @spreadsheet.row(1) - [" "] + @survey_questions.each_with_index do |question, index| + question.update_attributes(title: titles[index]) + end + + @survey_answers.destroy + + # Start and end of speadsheet + @end_row = @spreadsheet.count + + @max_row_start = 0 + @survey_questions.each do |question| + case question.type + when ::SurveyQuestion::Radio, ::SurveyQuestion::Select, ::SurveyQuestion::Check + if question.survey_question_options.count > @max_row_start + @max_row_start = question.survey_question_options.count + end + when ::SurveyQuestion::Radiogroup + if question.survey_question_options.count * question.survey_question_radiogroups.count > @max_row_start + @max_row_start = question.survey_question_options.count * question.survey_question_radiogroups.count + end + end + end + @start_row = @max_row_start + 2 + + # Modify multiline options if changed + (@start_row..@end_row).each do |row| + @answer_model = @survey.survey_answers.new + @survey_questions.each_with_index do |question, index| + case question.type + + when ::SurveyQuestion::Oneline, ::SurveyQuestion::Multiline + @answer_model[question.id.to_s] = @spreadsheet.row(row)[index * 2] + + when ::SurveyQuestion::Radio, ::SurveyQuestion::Select + if !@spreadsheet.row(row)[index*2].nil? && @spreadsheet.row(row)[index*2] != " " + @answer_model[question.id.to_s] = @spreadsheet.row(row)[index*2] + end + + when ::SurveyQuestion::Check + if !@spreadsheet.row(row)[index*2].nil? && @spreadsheet.row(row)[index*2] != " " + @answer_model[question.id.to_s] = @spreadsheet.row(row)[index*2].split("\"").select.each_with_index { |str, i| i.odd? } + end + + when ::SurveyQuestion::Radiogroup + radio_groups = [] + spreadsheet_radiogroups_lines = + question.survey_question_options.count * question.survey_question_radiogroups.count + 2 + + # Grab each radiogroups line for update + (2...spreadsheet_radiogroups_lines).each do |line| + radio_groups << @spreadsheet.row(line)[index * 2] + end + + # Grab answers info + answers = [] + if not @spreadsheet.row(row)[index * 2].blank? + answers << eval(@spreadsheet.row(row)[index * 2]) + end + + # Save the answers + answers.each do |answer| + options = Hash[question.survey_question_options.collect { |o| [ o.id.to_s, o.name ] }] + # @answer_model = @survey.survey_answers.new + @answer_model[question.id.to_s] = {} + + answer.each do |option, value| + @answer_model[question.id.to_s][options.invert[option]] = value + end + + # @answer_model.save! + end + + # Parse the needed info in the array + radio_titles = [] + radio_options = [] + radio_groups.each do |group| + option_with_radio = group.split(' - ') + radio_titles << option_with_radio[0] + radio_options << option_with_radio[1] + end + + # Update the spreadsheet info to the DB + groups_of_options = [] + groups_of_radios = [] + radio_titles.each_slice(question.survey_question_radiogroups.count) do |options| + groups_of_options << options + end + + radio_options.each_slice(question.survey_question_radiogroups.count) do |options| + groups_of_radios << options + end + + # Update option names + question.survey_question_options.each_with_index do |option, index| + groups_of_options[index].each do |modified_option| + if option.name_translations["zh_tw"] != modified_option + option.update_attributes(name: modified_option) + break + end + end + end + + # Update radio names + question.survey_question_radiogroups.each_with_index do |option, index| + groups_of_radios.each do |radios| + if radios[index] != option.name_translations["zh_tw"] + option.update_attributes(name: radios[index]) + break + end + end + end + end + end + + @answer_model.save! + + end #end row + + redirect_to panel_survey_back_end_surveys_url, :notice => :success + end + def export @survey = ::Survey.find(params[:id]) @chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data @@ -199,6 +340,7 @@ class Panel::Survey::BackEnd::SurveysController < OrbitBackendController end end end + @survey.survey_questions.all.each do |question| new_question = @new_survey.survey_questions.new question.attributes.each do |key, value| @@ -210,30 +352,32 @@ class Panel::Survey::BackEnd::SurveysController < OrbitBackendController end end end - question.survey_question_options.all.each do |option| - new_option = new_question.survey_question_options.new - option.attributes.each do |key, value| - unless ['_id', 'survey_question_id'].include? key - if option.respond_to?(key + '_translations') - new_option.send(key + '_translations=', value) - else - new_option.write_attribute(key, value) - end - end - end - end - question.survey_question_radiogroups.all.each do |radiogroup| - new_radiogroup = new_question.survey_question_radiogroups.new - radiogroup.attributes.each do |key, value| - unless ['_id', 'survey_question_id'].include? key - if radiogroup.respond_to?(key + '_translations') - new_radiogroup.send(key + '_translations=', value) - else - new_radiogroup.write_attribute(key, value) - end - end - end - end + + # question.survey_question_options.all.each do |option| + # new_option = new_question.survey_question_options.new + # option.attributes.each do |key, value| + # unless ['_id', 'survey_question_id'].include? key + # if option.respond_to?(key + '_translations') + # new_option.name_translations = value + # else + # new_option.write_attribute(key, value) + # end + # end + # end + # end + + # question.survey_question_radiogroups.all.each do |radiogroup| + # new_radiogroup = new_question.survey_question_radiogroups.new + # radiogroup.attributes.each do |key, value| + # unless ['_id', 'survey_question_id'].include? key + # if radiogroup.respond_to?(key + '_translations') + # new_option.name_translations = value + # else + # new_radiogroup.write_attribute(key, value) + # end + # end + # end + # end end @new_survey.create_user_id = current_user.id diff --git a/app/views/panel/survey/back_end/surveys/_survey.html.erb b/app/views/panel/survey/back_end/surveys/_survey.html.erb index 1383bc0..c8f27ca 100644 --- a/app/views/panel/survey/back_end/surveys/_survey.html.erb +++ b/app/views/panel/survey/back_end/surveys/_survey.html.erb @@ -13,7 +13,8 @@
<%= t('survey.title') %> | ++ | + |
---|---|---|
<%= @survey.title %> | +<%= file_field_tag :file %> | +<%= submit_tag t("survey.import_csv"), :class => 'btn' %> | +