From ff75d0bef80cc866070f8eba03f5d4aac0d86362 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Tue, 11 Apr 2017 14:10:01 +0800 Subject: [PATCH] download manager --- app/controllers/admin/surveys_controller.rb | 23 +++-- app/views/admin/surveys/_index.html.erb | 2 +- app/views/admin/surveys/index.html.erb | 48 ++++++++++- app/views/survey_export/export.xlsx.axlsx | 95 +++++++++++++++++++++ config/routes.rb | 1 + lib/tasks/survey_tasks.rake | 30 +++++++ 6 files changed, 191 insertions(+), 8 deletions(-) create mode 100644 app/views/survey_export/export.xlsx.axlsx diff --git a/app/controllers/admin/surveys_controller.rb b/app/controllers/admin/surveys_controller.rb index aee9932..5c5454e 100644 --- a/app/controllers/admin/surveys_controller.rb +++ b/app/controllers/admin/surveys_controller.rb @@ -145,14 +145,25 @@ class Admin::SurveysController < OrbitAdminController # redirect_to panel_survey_back_end_surveys_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options]) end - def export + def checkforthread + render :json => {"status" => @download_thread.alive?}.to_json + end - @chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data - respond_to do |format| - format.xlsx { - response.headers['Content-Disposition'] = 'attachment; filename="export.xlsx"' - } + def export + @download_thread = Thread.new do + # %x[rake survey_tasks:prepare_download[#{@survey.id}]] + (1..10).each do |i| + sleep(1) + end end + @download_thread.join + render :json => {"success" => true}.to_json + # @chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data + # respond_to do |format| + # format.xlsx { + # response.headers['Content-Disposition'] = 'attachment; filename="export.xlsx"' + # } + # end end diff --git a/app/views/admin/surveys/_index.html.erb b/app/views/admin/surveys/_index.html.erb index 8731df0..626fb83 100644 --- a/app/views/admin/surveys/_index.html.erb +++ b/app/views/admin/surveys/_index.html.erb @@ -30,7 +30,7 @@
  • <%= t('survey.duplicate_it') %>
  • <%= t('survey.jump') %>
  • <%= t('survey.set_answers') %>
  • -
  • <%= t('survey.export_csv') %>
  • +
  • <%= t('survey.export_csv') %>
  • <%= t('survey.chart') %>
  • <%= t(:delete_) %>
  • <% end %> diff --git a/app/views/admin/surveys/index.html.erb b/app/views/admin/surveys/index.html.erb index 1ad9a26..53f7f4e 100644 --- a/app/views/admin/surveys/index.html.erb +++ b/app/views/admin/surveys/index.html.erb @@ -3,4 +3,50 @@ <%= render 'index'%> -<%= render 'layouts/delete_modal', delete_options: @delete_options %> \ No newline at end of file +<%= render 'layouts/delete_modal', delete_options: @delete_options %> + + + + \ No newline at end of file diff --git a/app/views/survey_export/export.xlsx.axlsx b/app/views/survey_export/export.xlsx.axlsx new file mode 100644 index 0000000..488857e --- /dev/null +++ b/app/views/survey_export/export.xlsx.axlsx @@ -0,0 +1,95 @@ +# encoding: utf-8 + +wb = xlsx_package.workbook +wb.add_worksheet(name: remove_illegal_utf8(survey.title[0..15])) do |sheet| + + row = [] + survey_questions.each_with_index do |question, i| + if question.type == 2 or question.type == 3 or question.type == 4 + question.survey_question_options.each do |option| + row << "#{i+1}. #{remove_illegal_utf8 question.title} - #{remove_illegal_utf8 option.name}" + end + elsif question.type == 5 + question.survey_question_options.each do |option| + question.survey_question_radiogroups.each do |radiogroup| + row << "#{i+1}. #{remove_illegal_utf8 question.title} - #{remove_illegal_utf8 option.name} - #{remove_illegal_utf8 radiogroup.name}" + end + end + else + row << "#{i+1}. #{remove_illegal_utf8 question.title}" + end + if question.custom_option + row << "#{i+1}. #{remove_illegal_utf8 question.title} - #{t('survey_question.use_custom_option')}" + end + end + + sheet.add_row row + + survey_answers.each do |answer| + answer_row = [] + survey_questions.each do |question| + + options = question.survey_question_options.collect{|o| o.name } + + if question.type == 2 or question.type == 4 + + options.collect do |o| + + if !answer[question.id.to_s].blank? && answer[question.id.to_s].include?(o) + answer_row << '1' + else + answer_row << '' + end + + end + + elsif question.type == 3 + + options.collect do |o| + + if !answer[question.id.to_s].blank? && answer[question.id.to_s].include?(o) + answer_row << '1' + else + answer_row << '' + end + end + + elsif question.type == 5 + + question.survey_question_options.each do |option| + question.survey_question_radiogroups.each do |radiogroup| + if !answer[question.id.to_s].blank? && answer[question.id.to_s][option.id.to_s] == radiogroup.name + answer_row << '1' + else + answer_row << '' + end + end + end + + else + if answer[question.id.to_s] + answer_row << remove_illegal_utf8(answer[question.id.to_s]) + else + answer_row << '' + end + end + + if question.custom_option + if !answer[question.id.to_s].blank? + if answer[question.id.to_s].class == Array + answer_row << remove_illegal_utf8((answer[question.id.to_s] - options).join) + elsif answer[question.id.to_s].class == String + answer_row << remove_illegal_utf8(([answer[question.id.to_s]] - options).join) + end + else + answer_row << '' + end + end + end + + sheet.add_row answer_row + + end + +end + diff --git a/config/routes.rb b/config/routes.rb index e7d0705..5a15b89 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ Rails.application.routes.draw do scope "(:locale)", locale: Regexp.new(locales.join("|")) do namespace :admin do + get "surveys/checkforthread", to: "surveys#checkforthread" resources :surveys do collection do get 'delete' diff --git a/lib/tasks/survey_tasks.rake b/lib/tasks/survey_tasks.rake index 7fcf4ea..216e8cf 100644 --- a/lib/tasks/survey_tasks.rake +++ b/lib/tasks/survey_tasks.rake @@ -2,3 +2,33 @@ # task :survey do # # Task goes here # end +namespace :survey_tasks do + task :prepare_download,[:surveyid] => :environment do |task,args| + puts "Cron initiated with #{args.surveyid}" + id = args.surveyid + I18n.locale = :zh_tw + survey = QuestionnaireSurvey.find(id) + chart_data, survey_questions, survey_answers = survey.generate_chart_data + ac = ActionController::Base.new() + xlsx = ac.render_to_string handlers: [:axlsx], formats: [:xlsx], template: "survey_export/export", locals: {survey: survey, survey_questions: survey_questions, survey_answers: survey_answers} + dirname = "public/survey_export/#{id}" + FileUtils.mkdir_p(dirname) unless File.exists?(dirname) + f = "#{dirname}/#{survey.title.gusb(' ', '_')}.xlsx" + if File.exists?(f) + File.delete(f) + end + file = File.open(f, "w") + xlsx.force_encoding("utf-8") + file.write(xlsx) + end +end + +def remove_illegal_utf8(str) + if String.method_defined?(:encode) + str.encode!('UTF-8', 'UTF-8', :invalid => :replace) + else + ic = Iconv.new('UTF-8', 'UTF-8//IGNORE') + str = ic.iconv(str) + end + str +end \ No newline at end of file