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 @@
  • <%= link_to t('survey.duplicate_it'), duplicate_it_panel_survey_back_end_survey_path(survey, :page => params[:page]) %>
  • <%= link_to t('survey.jump'), jump_panel_survey_back_end_survey_path(survey, :page => params[:page]) %>
  • <%= link_to t('survey.set_answers'), set_answers_panel_survey_back_end_survey_path(survey, :page => params[:page]) %>
  • -
  • <%= link_to t('survey.export_csv'), export_panel_survey_back_end_survey_path(survey, :format => :csv), :target => '_blank' %>
  • +
  • <%= link_to t('survey.export_csv'), export_panel_survey_back_end_survey_path(survey, :format => :csv) %>
  • +
  • <%= link_to t('survey.import_csv'), set_import_panel_survey_back_end_survey_path(survey) %>
  • <%= link_to t('survey.chart'), result_panel_survey_front_end_survey_path(survey, :force_chart => true, :standalone => true), :target => '_blank' %>
  • <%= link_to t(:delete_), panel_survey_back_end_survey_path(survey), :confirm => t('sure?'), :method => :delete, :remote => true %>
  • <% end -%> diff --git a/app/views/panel/survey/back_end/surveys/set_import.html.erb b/app/views/panel/survey/back_end/surveys/set_import.html.erb new file mode 100644 index 0000000..40f8b5a --- /dev/null +++ b/app/views/panel/survey/back_end/surveys/set_import.html.erb @@ -0,0 +1,18 @@ +<%= form_tag import_panel_survey_back_end_survey_path(@survey), multipart: true do %> + + + + + + + + + + + + + + + +
    <%= t('survey.title') %>
    <%= @survey.title %><%= file_field_tag :file %><%= submit_tag t("survey.import_csv"), :class => 'btn' %>
    +<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 94e49ec..895cf62 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -24,6 +24,7 @@ en: answer_error: Unsuccessful answer answers_list: Answers List + import_csv: Import CSV export_csv: Export CSV chart: Chart set_answers: Set Answers diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 337012d..d545b98 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -25,6 +25,7 @@ zh_tw: answer_error: 問卷填寫錯誤 answers_list: 結果清單 + import_csv: 匯入CSV export_csv: 匯出CSV chart: 結果圖表 set_answers: 設定結果 @@ -80,4 +81,4 @@ zh_tw: have_not_chart: 此種類型問題不含結果圖表 move_up: 上移 - move_down: 下移 \ No newline at end of file + move_down: 下移 diff --git a/config/routes.rb b/config/routes.rb index c33b363..79d601d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,6 +9,8 @@ Rails.application.routes.draw do end member do get 'export' + get 'set_import' + post 'import' get 'set_answers' get 'jump' get 'duplicate_it' @@ -30,4 +32,4 @@ Rails.application.routes.draw do end end match "/appfront/*path" => redirect("/panel/*path") -end \ No newline at end of file +end