From f3f4dc842c53acc4e9b0b6224a291f6e3caedeab Mon Sep 17 00:00:00 2001 From: thomaschen <99703040@nccu.edu.tw> Date: Mon, 21 Oct 2013 16:15:06 +0800 Subject: [PATCH] import radio question success Conflicts: Gemfile vendor/built_in_modules/survey/app/controllers/panel/survey/back_end/surveys_controller.rb --- Gemfile | 3 ++ .../survey/back_end/surveys_controller.rb | 35 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/Gemfile b/Gemfile index 16c820c9a..ce0eae95d 100644 --- a/Gemfile +++ b/Gemfile @@ -29,6 +29,9 @@ gem 'mongoid-tree', :require => 'mongoid/tree' gem "mongo_session_store-rails3", '3.0.6' gem 'nokogiri' +# import csv +gem 'roo' + gem 'fb_graph' gem 'rack-gridfs' gem 'radius' 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 621efcb39..908e85cdd 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 @@ -90,6 +90,41 @@ class Panel::Survey::BackEnd::SurveysController < OrbitBackendController end def import + @survey = ::Survey.find(params[:id]) + @file = params[:file] + @chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data + + case File.extname(@file.original_filename) + when ".csv" then @spreadsheet = Roo::CSV.new(@file.path) + # 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 + + @current_row = 2 + @header = @spreadsheet.row(1) + + # (2..@spreadsheet.last_row).each do |i| + # parse each row here + # end + + @survey.survey_answers.destroy + @survey_questions.each do |question| + case question.type + when ::SurveyQuestion::Radio, ::SurveyQuestion::Select + (@current_row..(@current_row + question.survey_question_options.count - 1)).each do |i| + (1..@spreadsheet.row(i)[1].to_i).each do + @answer_model = @survey.survey_answers.new + @answer_model[question.id.to_s] = @spreadsheet.row(i)[0] + @answer_model.save! + end + end + @current_row = @current_row + question.survey_question_options.count - 1 + else + end + end + + redirect_to panel_survey_back_end_surveys_url, :notice => :success end def export