download manager
This commit is contained in:
parent
58c2df6749
commit
ff75d0bef8
|
@ -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])
|
# redirect_to panel_survey_back_end_surveys_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
|
||||||
end
|
end
|
||||||
|
|
||||||
def export
|
def checkforthread
|
||||||
|
render :json => {"status" => @download_thread.alive?}.to_json
|
||||||
|
end
|
||||||
|
|
||||||
@chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data
|
def export
|
||||||
respond_to do |format|
|
@download_thread = Thread.new do
|
||||||
format.xlsx {
|
# %x[rake survey_tasks:prepare_download[#{@survey.id}]]
|
||||||
response.headers['Content-Disposition'] = 'attachment; filename="export.xlsx"'
|
(1..10).each do |i|
|
||||||
}
|
sleep(1)
|
||||||
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
<li><a href="/admin/surveys/<%=survey.id.to_s%>/duplicate_it"><%= t('survey.duplicate_it') %></a></li>
|
<li><a href="/admin/surveys/<%=survey.id.to_s%>/duplicate_it"><%= t('survey.duplicate_it') %></a></li>
|
||||||
<li><a href="/admin/surveys/<%=survey.id.to_s%>/jump"><%= t('survey.jump') %></a></li>
|
<li><a href="/admin/surveys/<%=survey.id.to_s%>/jump"><%= t('survey.jump') %></a></li>
|
||||||
<li><a href="/admin/surveys/<%=survey.id.to_s%>/set_answers"><%= t('survey.set_answers') %></a></li>
|
<li><a href="/admin/surveys/<%=survey.id.to_s%>/set_answers"><%= t('survey.set_answers') %></a></li>
|
||||||
<li><a href="/admin/surveys/<%=survey.id.to_s%>/export?format=xlsx" target="_blank"><%= t('survey.export_csv') %></a></li>
|
<li><a href="/admin/surveys/<%=survey.id.to_s%>/export?format=xlsx" target="_blank" class="export-xls"><%= t('survey.export_csv') %></a></li>
|
||||||
<li><a href="<%=page_for_survey(survey)%>?method=result&force_chart=true" target="_blank"><%= t('survey.chart') %></a></li>
|
<li><a href="<%=page_for_survey(survey)%>?method=result&force_chart=true" target="_blank"><%= t('survey.chart') %></a></li>
|
||||||
<li><a href="#" class="delete text-error" rel="/admin/surveys/<%=survey.id.to_s%>"><%= t(:delete_) %></a></li>
|
<li><a href="#" class="delete text-error" rel="/admin/surveys/<%=survey.id.to_s%>"><%= t(:delete_) %></a></li>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
|
@ -4,3 +4,49 @@
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
<%= render 'layouts/delete_modal', delete_options: @delete_options %>
|
<%= render 'layouts/delete_modal', delete_options: @delete_options %>
|
||||||
|
|
||||||
|
<div id="downloadModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="downloadModalLabel" aria-hidden="true">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h3 id="downloadModalLabel">Download</h3>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body">
|
||||||
|
<p id="wait-zone" style="text-align: center;">
|
||||||
|
Please wait while we prepare your download. This may take a while.
|
||||||
|
<br />
|
||||||
|
<img src="/assets/spin.gif" />
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
var downloadModal = $("#downloadModal");
|
||||||
|
$(document).on("click", ".export-xls", function(){
|
||||||
|
var link = $(this).attr("href");
|
||||||
|
$.ajax({
|
||||||
|
url : link,
|
||||||
|
type : "get",
|
||||||
|
dataType : "json"
|
||||||
|
}).done(function(data){
|
||||||
|
|
||||||
|
})
|
||||||
|
checkForThread();
|
||||||
|
downloadModal.modal("show");
|
||||||
|
return false;
|
||||||
|
})
|
||||||
|
|
||||||
|
var checkForThread = function(){
|
||||||
|
$.ajax({
|
||||||
|
url : "/admin/surveys/checkforthread",
|
||||||
|
type : "get",
|
||||||
|
dataType : "json"
|
||||||
|
}).done(function(data){
|
||||||
|
console.log("Status : " + data.status);
|
||||||
|
if(!data.status){
|
||||||
|
checkForThread();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
</script>
|
|
@ -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
|
||||||
|
|
|
@ -4,6 +4,7 @@ Rails.application.routes.draw do
|
||||||
|
|
||||||
scope "(:locale)", locale: Regexp.new(locales.join("|")) do
|
scope "(:locale)", locale: Regexp.new(locales.join("|")) do
|
||||||
namespace :admin do
|
namespace :admin do
|
||||||
|
get "surveys/checkforthread", to: "surveys#checkforthread"
|
||||||
resources :surveys do
|
resources :surveys do
|
||||||
collection do
|
collection do
|
||||||
get 'delete'
|
get 'delete'
|
||||||
|
|
|
@ -2,3 +2,33 @@
|
||||||
# task :survey do
|
# task :survey do
|
||||||
# # Task goes here
|
# # Task goes here
|
||||||
# end
|
# 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
|
Loading…
Reference in New Issue