add export record feature

This commit is contained in:
BOHUNG 2020-02-03 16:47:49 +08:00
parent 817ecf0377
commit db59135626
9 changed files with 134 additions and 4 deletions

View File

@ -52,4 +52,37 @@ table .checkbox{
.label_left{ .label_left{
float: left; float: left;
padding-right: 0.5em; padding-right: 0.5em;
}
.PageDetails {
padding-top: 0.1875em;
padding-bottom: 0.1875em;
font-weight: bold;
font-size: 0.875em;
border-bottom: 0.0625em solid #ddd;
overflow: hidden;
text-align: right;
color: black;
font-size: 1.3em;
}
.PageList {
margin: 0em 0em 0 0.5em;
font-weight: bold;
font-size: 0.875em;
overflow: hidden;
list-style: none;
display: inline;
background: none;
color: #999;
}
ol {
margin: 0 0 0.5em 0;
min-height: 0.625em;
height: auto !important;
height: 0.625em;
padding: 0;
clear: both;
float: right;
}
.PageList li {
display: inline;
} }

View File

@ -4,9 +4,10 @@ class Admin::CancerpredictsController < OrbitAdminController
require 'rubyXL' require 'rubyXL'
require 'fileutils' require 'fileutils'
require "axlsx" require "axlsx"
helper Admin::CancerpredictsHelper #include Admin::CancerpredictsHelper
before_action ->(module_app = @app_title) { set_variables module_app } before_action ->(module_app = @app_title) { set_variables module_app }
before_action :create_first_field before_action :create_first_field
helper Admin::CancerpredictsHelper
#before_action :load_access_levels #before_action :load_access_levels
def initialize def initialize
super super
@ -473,6 +474,21 @@ class Admin::CancerpredictsController < OrbitAdminController
end end
end end
end end
@pagination = create_pagination(@page_num.to_i)
end
def export_cancer_tool_record
@cancer_records = Cancerpredictrecord.where("title"=>@app_title).asc(:id)
dir_path = 'tmp/cancer_tool/'
#FileUtils.rm_r(dir_path, :force => true) if Dir.exist?(dir_path)
FileUtils.mkdir dir_path if !Dir.exist?(dir_path) #create dir for storing tmp_file if dir doesn't exist
@filename = ("#{Time.now.year}_%02s_%02s_export_cancer_tool_record.xlsx"%[Time.now.month,Time.now.day]).gsub(" ","0")
Dir.chdir(dir_path) do
File.open(@filename, 'w') do |f|
f.write render_to_string( :handlers=> [:axlsx], :formats=> [:xlsx] ,:partial=> 'export_cancer_tool_record.xlsx',:locals=> {:@results=>@cancer_records} )
end
end
tmp_filename_data = File.read(dir_path +@filename)
send_data(tmp_filename_data, type: 'application/xlsx', disposition: 'attachment', filename: @filename)
end end
def create_first_field def create_first_field
if Cancerpredictfields.where("title"=>(@app_title + "_back")).take_while{true}.count == 0 if Cancerpredictfields.where("title"=>(@app_title + "_back")).take_while{true}.count == 0
@ -500,4 +516,40 @@ class Admin::CancerpredictsController < OrbitAdminController
@form_to_show = Cancerpredictfields.where("title"=>@app_title).first @form_to_show = Cancerpredictfields.where("title"=>@app_title).first
end end
end end
def create_pagination(page=1,fields=Cancerpredictrecord.all,extra_params="")
page = 1 if page == 0
per_page_num = 10.0
all_page_num = (fields.count / per_page_num).ceil
all_page_num = 1 if all_page_num == 0
pagination = '<div class="PageDetails">'+((I18n.locale.to_s == 'zh_tw') ? "#{page}" : "page #{page}") +'/'+ ((I18n.locale.to_s == 'zh_tw') ? "#{all_page_num}" : "Total #{all_page_num} page(s)")+'<ol class="PageList">'+
((page == 1) ? '<li>['+I18n.t('cancerpredict.prev_page')+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+(page-1).to_s+'"> ['+I18n.t('cancerpredict.prev_page')+'] </a></li>')
if all_page_num > 7
if page <= (all_page_num / 2)
if page <= 7
(1..page).to_a.each{|page_num| pagination += ((page_num == page) ? '<li>['+page_num.to_s+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+page_num.to_s+'"> '+page_num.to_s+' </a></li>')}
else
(1..3).to_a.each{|page_num| pagination += ((page_num == page) ? '<li>['+page_num.to_s+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+page_num.to_s+'"> '+page_num.to_s+' </a></li>')}
pagination +='<li>...</li>'
(page-3..page).to_a.each{|page_num| pagination += ((page_num == page) ? '<li>['+page_num.to_s+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+page_num.to_s+'"> '+page_num.to_s+' </a></li>')}
end
(page+1..page+3).to_a.each{|page_num| pagination += ((page_num == page) ? '<li>['+page_num.to_s+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+page_num.to_s+'"> '+page_num.to_s+' </a></li>')}
pagination +='<li>...</li>'
(all_page_num-2..all_page_num).to_a.each{|page_num| pagination += ((page_num == page) ? '<li>['+page_num.to_s+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+page_num.to_s+'"> '+page_num.to_s+' </a></li>')}
else
(1..3).to_a.each{|page_num| pagination += ((page_num == page) ? '<li>['+page_num.to_s+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+page_num.to_s+'"> '+page_num.to_s+' </a></li>')}
pagination +='<li>...</li>'
(page-3..page).to_a.each{|page_num| pagination += ((page_num == page) ? '<li>['+page_num.to_s+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+page_num.to_s+'"> '+page_num.to_s+' </a></li>')}
if all_page_num - page <= 7
(page+1..all_page_num).to_a.each{|page_num| pagination += ((page_num == page) ? '<li>['+page_num.to_s+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+page_num.to_s+'"> '+page_num.to_s+' </a></li>')}
else
(page+1..page+3).to_a.each{|page_num| pagination += ((page_num == page) ? '<li>['+page_num.to_s+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+page_num.to_s+'"> '+page_num.to_s+' </a></li>')}
pagination +='<li>...</li>'
(all_page_num-2..all_page_num).to_a.each{|page_num| pagination += ((page_num == page) ? '<li>['+page_num.to_s+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+page_num.to_s+'"> '+page_num.to_s+' </a></li>')}
end
end
else
(1..all_page_num).to_a.each{|page_num| pagination += ((page_num == page) ? '<li>['+page_num.to_s+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+page_num.to_s+'"> '+page_num.to_s+' </a></li>')}
end
pagination += (((page==all_page_num) ? '<li>['+I18n.t('cancerpredict.next_page')+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+(page+1).to_s+'"> ['+I18n.t('cancerpredict.next_page')+'] </a></li>')+'</ol></div>')
end
end end

View File

@ -4,7 +4,7 @@ module Admin::CancerpredictsHelper
per_page_num = 10.0 per_page_num = 10.0
all_page_num = (fields.count / per_page_num).ceil all_page_num = (fields.count / per_page_num).ceil
pagination = '<div class="PageDetails">'+((I18n.locale.to_s == 'zh_tw') ? "#{page}" : "page #{page}") +'/'+ ((I18n.locale.to_s == 'zh_tw') ? "#{all_page_num}" : "Total #{all_page_num} page(s)")+'<ol class="PageList">'+ pagination = '<div class="PageDetails">'+((I18n.locale.to_s == 'zh_tw') ? "#{page}" : "page #{page}") +'/'+ ((I18n.locale.to_s == 'zh_tw') ? "#{all_page_num}" : "Total #{all_page_num} page(s)")+'<ol class="PageList">'+
((page == 1) ? '<li>['+t('cancerpredict.prev_page')+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+(page-1).to_s+'"> ['+t('cancerpredict.prev_page')+'] </a></li>') ((page == 1) ? '<li>['+I18n.t('cancerpredict.prev_page')+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+(page-1).to_s+'"> ['+I18n.t('cancerpredict.prev_page')+'] </a></li>')
if all_page_num > 7 if all_page_num > 7
if page <= (all_page_num / 2) if page <= (all_page_num / 2)
if page <= 7 if page <= 7
@ -32,6 +32,6 @@ module Admin::CancerpredictsHelper
else else
(1..all_page_num).to_a.each{|page_num| pagination += ((page_num == page) ? '<li>['+page_num.to_s+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+page_num.to_s+'"> '+page_num.to_s+' </a></li>')} (1..all_page_num).to_a.each{|page_num| pagination += ((page_num == page) ? '<li>['+page_num.to_s+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+page_num.to_s+'"> '+page_num.to_s+' </a></li>')}
end end
pagination += (((page==all_page_num) ? '<li>['+t('cancerpredict.next_page')+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+(page+1).to_s+'"> ['+t('cancerpredict.next_page')+'] </a></li>')+'</ol></div>') pagination += (((page==all_page_num) ? '<li>['+I18n.t('cancerpredict.next_page')+']</li>' : '<li><a href="'+(extra_params.blank? ? '?' : "?#{extra_params}&")+'page='+(page+1).to_s+'"> ['+I18n.t('cancerpredict.next_page')+'] </a></li>')+'</ol></div>')
end end
end end

View File

@ -0,0 +1,35 @@
# encoding: utf-8
wb = xlsx_package.workbook
wb.add_worksheet(name: "school_class") do |sheet|
row = []
title = sheet.styles.add_style(:bg_color => "FFFF00",:border=>{ :style => :thin, :color => "666666" },:alignment=>{ :horizontal => :center,:vertical => :center ,:wrap_text => true})
column = sheet.styles.add_style(:alignment=>{ :horizontal => :center,:vertical => :center ,:wrap_text => true},:border=>{ :style => :thin, :color => "666666" })
row << ((I18n.locale=="en") ? 'submit time' : '提交時間')
@fields = @results.first.names.keys
@fields.each do |field_name|
row << @results.first.names[field_name][I18n.locale.to_s].split('<br/>')[0]
end
#export data start
export_data_rows = []
@results.each do |result|
temp_row = []
temp_row << result.created_at.to_s
result.result.each do |key,value|
if key != "locale"
if result.values[key][I18n.locale.to_s].length == 0
temp_row << value
else
temp_row << (result.values[key][I18n.locale.to_s][value.to_i-1] rescue value)
end
end
end
export_data_rows << temp_row
end
#export data end
sheet.add_row row , :style=> title,:widths => row.map{|cell| (cell.length* 11/ 2).round}
export_data_rows.each do |infos_row|
sheet.add_row infos_row , :style=> column
end
end

View File

@ -1,3 +1,5 @@
<link href="/assets/admin/cancerpredict.css" media="print" rel="stylesheet">
<link href="/assets/admin/cancerpredict.css" media="screen" rel="stylesheet">
<table> <table>
<thead> <thead>
<tr> <tr>
@ -42,7 +44,7 @@
<%end%> <%end%>
</tbody> </tbody>
</table> </table>
<%=create_pagination(@page_num).html_safe%> <%=@pagination.html_safe%>
<style type="text/css"> <style type="text/css">
thead > tr > th{ thead > tr > th{
border:1px solid; border:1px solid;

View File

@ -25,6 +25,7 @@ en:
texts_between_Result_and_result_block: Texts between "Result" and "Result block" texts_between_Result_and_result_block: Texts between "Result" and "Result block"
prev_page: Previous page prev_page: Previous page
next_page: Next page next_page: Next page
export_cancer_predict_tool_records: Export cancer predict tool records
table: table:
welcome: Welcome to The after breast cancer healing system of Taiwanprepare!\nTo start, please enter the relevant information below. welcome: Welcome to The after breast cancer healing system of Taiwanprepare!\nTo start, please enter the relevant information below.
Reset: Reset Reset: Reset

View File

@ -25,6 +25,7 @@ zh_tw:
texts_between_Result_and_result_block: 在"結果"和"結果區塊"之間的文字 texts_between_Result_and_result_block: 在"結果"和"結果區塊"之間的文字
prev_page: 上一頁 prev_page: 上一頁
next_page: 下一頁 next_page: 下一頁
export_cancer_predict_tool_records: 匯出乳癌預測系統的使用者紀錄
table: table:
welcome: 歡迎使用台灣準備乳癌癒後系統!\n若要開始 請在下方輸入相關資訊 welcome: 歡迎使用台灣準備乳癌癒後系統!\n若要開始 請在下方輸入相關資訊
Reset: 重置 Reset: 重置

View File

@ -9,6 +9,7 @@ Rails.application.routes.draw do
post 'editCancerpredicts' , to: 'cancerpredicts#edit' post 'editCancerpredicts' , to: 'cancerpredicts#edit'
patch 'editCancerpredicts' , to: 'cancerpredicts#edit' patch 'editCancerpredicts' , to: 'cancerpredicts#edit'
get 'cancerpredicts/showSubmit' , to: 'cancerpredicts#showSubmit' get 'cancerpredicts/showSubmit' , to: 'cancerpredicts#showSubmit'
get 'cancerpredicts/export_cancer_tool_record' , to: 'cancerpredicts#export_cancer_tool_record'
resources :cancerpredicts resources :cancerpredicts
end end
end end

View File

@ -26,6 +26,11 @@ module Cancerpredict
:priority=>1, :priority=>1,
:active_for_action=>{'admin/cancerpredicts'=>'show_submit'}, :active_for_action=>{'admin/cancerpredicts'=>'show_submit'},
:available_for => 'users' :available_for => 'users'
context_link 'cancerpredict.export_cancer_predict_tool_records',
:link_path=>"admin_cancerpredicts_export_cancer_tool_record_path" ,
:priority=>1,
:active_for_action=>{'admin/cancerpredicts'=>'export_cancer_tool_record'},
:available_for => 'users'
end end
end end
end end