Compare commits

...

1 Commits

Author SHA1 Message Date
Manson Wang 5c9c3fcabd ntu_ga 2014-02-11 14:35:55 +08:00
6 changed files with 196 additions and 29 deletions

View File

@ -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

View File

@ -13,7 +13,8 @@
<li><%= link_to t('survey.duplicate_it'), duplicate_it_panel_survey_back_end_survey_path(survey, :page => params[:page]) %></li>
<li><%= link_to t('survey.jump'), jump_panel_survey_back_end_survey_path(survey, :page => params[:page]) %></li>
<li><%= link_to t('survey.set_answers'), set_answers_panel_survey_back_end_survey_path(survey, :page => params[:page]) %></li>
<li><%= link_to t('survey.export_csv'), export_panel_survey_back_end_survey_path(survey, :format => :csv), :target => '_blank' %></li>
<li><%= link_to t('survey.export_csv'), export_panel_survey_back_end_survey_path(survey, :format => :csv) %></li>
<li><%= link_to t('survey.import_csv'), set_import_panel_survey_back_end_survey_path(survey) %></li>
<li><%= link_to t('survey.chart'), result_panel_survey_front_end_survey_path(survey, :force_chart => true, :standalone => true), :target => '_blank' %></li>
<li><%= link_to t(:delete_), panel_survey_back_end_survey_path(survey), :confirm => t('sure?'), :method => :delete, :remote => true %></li>
<% end -%>

View File

@ -0,0 +1,18 @@
<%= form_tag import_panel_survey_back_end_survey_path(@survey), multipart: true do %>
<table class="table main-list">
<thead>
<tr>
<th class="span2"><%= t('survey.title') %></th>
<th class="span2"></th>
<th class="span2"></th>
</tr>
</thead>
<tbody id="tbody_surveys" class="sort-holder">
<tr>
<td><%= @survey.title %></td>
<td><%= file_field_tag :file %></td>
<td><%= submit_tag t("survey.import_csv"), :class => 'btn' %></td>
</tr>
</tbody>
</table>
<% end %>

View File

@ -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

View File

@ -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: 下移
move_down: 下移

View File

@ -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
end