From 24e4263139966c40f123ea04f5bf35b562fc7101 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Mon, 6 Jun 2016 19:39:12 +0800 Subject: [PATCH] added charts and excel --- .../admin/technology_transfers_controller.rb | 28 ++++- .../admin/technology_transfers_helper.rb | 55 +++++++++ .../technology_transfers/analysis.html.erb | 114 ++++++++++++++++++ .../analysis_report.html.erb | 1 + .../download_excel.xlsx.axlsx | 43 +++++++ config/locales/en.yml | 3 +- config/locales/zh_tw.yml | 3 +- config/routes.rb | 5 +- lib/personal_technology_transfer/engine.rb | 2 +- 9 files changed, 249 insertions(+), 5 deletions(-) create mode 100644 app/views/admin/technology_transfers/analysis.html.erb create mode 100644 app/views/admin/technology_transfers/analysis_report.html.erb create mode 100644 app/views/admin/technology_transfers/download_excel.xlsx.axlsx diff --git a/app/controllers/admin/technology_transfers_controller.rb b/app/controllers/admin/technology_transfers_controller.rb index 91fab2d..895693e 100644 --- a/app/controllers/admin/technology_transfers_controller.rb +++ b/app/controllers/admin/technology_transfers_controller.rb @@ -1,5 +1,5 @@ class Admin::TechnologyTransfersController < OrbitMemberController - include Admin::CertificatesHelper + include Admin::TechnologyTransfersHelper layout "member_plugin" before_action :set_tt, only: [:show, :edit , :update, :destroy] before_action :set_plugin @@ -21,6 +21,32 @@ class Admin::TechnologyTransfersController < OrbitMemberController tt = TechnologyTransfer.create(tt_params) redirect_to params[:referer_url] end + + + def analysis + end + + def analysis_report + role = params[:role_id] + year_start = params[:year_start].to_i + year_end = params[:year_end].to_i + graph_by = params[:graph_by] + + @data = get_chart_data(year_start,year_end,role,params[:graph_by]) + + render :layout => false + end + + def download_excel + year_start = params[:year_start].to_i + year_end = params[:year_end].to_i + @data = get_data_for_excel(year_start,year_end) + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="technology_transfers.xlsx"' + } + end + end def show end diff --git a/app/helpers/admin/technology_transfers_helper.rb b/app/helpers/admin/technology_transfers_helper.rb index 6c28a62..1267b69 100644 --- a/app/helpers/admin/technology_transfers_helper.rb +++ b/app/helpers/admin/technology_transfers_helper.rb @@ -6,4 +6,59 @@ module Admin::TechnologyTransfersHelper page = pages.first if page.nil? request.protocol+(request.host_with_port+page.url+'/'+course_object.to_param).gsub('//','/') rescue "/" end + + + def get_data_for_excel(year_start,year_end) + data = [] + roles = Role.where(:disabled => false, :title.ne => "", :title.ne => nil).asc(:key) + roles.each do |role| + d = {} + d["name"] = role.title + mps = role.member_profile_ids + # d1 = DateTime.new(year_start,1,1,0,0) + # d2 = DateTime.new(year_end,12,31,23,59) + d["data"] = TechnologyTransfer.where(:year.gte => year_start, :year.lte => year_end, :member_profile_id.in => mps) rescue [] + data << d + end + return data + end + + def get_chart_data(year_start,year_end,role,type) + case type + when "category" + jls = TechnologyTransferCategory.all + end + + finaldata = [] + role = Role.find(role) rescue nil + mps = [] + if !role.nil? + mps = role.member_profile_ids + end + jls.each do |jl| + data = {} + data["name"] = jl.title + data["data"] = {} + (year_start..year_end).each do |year| + # d1 = DateTime.new(year,1,1,0,0) + # d2 = DateTime.new(year,12,31,23,59) + t = jl.technology_transfers.where(:year => year, :member_profile_id.in => mps).count rescue 0 + data["data"][year.to_s] = t + end + finaldata << data + end + data = {"name" => "N/A", "data" => {}} + (year_start..year_end).each do |year| + # d1 = DateTime.new(year,1,1,0,0) + # d2 = DateTime.new(year,12,31,23,59) + case type + when "category" + t = TechnologyTransfer.where(:year => year, :member_profile_id.in => mps, :technology_transfer_category_id => nil).count rescue 0 + end + + data["data"][year.to_s] = t + end + finaldata << data + finaldata + end end \ No newline at end of file diff --git a/app/views/admin/technology_transfers/analysis.html.erb b/app/views/admin/technology_transfers/analysis.html.erb new file mode 100644 index 0000000..a9e2b40 --- /dev/null +++ b/app/views/admin/technology_transfers/analysis.html.erb @@ -0,0 +1,114 @@ +<% # encoding: utf-8 %> +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag "lib/main-forms" %> + <%= stylesheet_link_tag "lib/fileupload" %> + <%= stylesheet_link_tag "lib/main-list" %> + <%= stylesheet_link_tag "lib/main-form-col2" %> + +<% end %> +<% content_for :page_specific_javascript do %> + <%= javascript_include_tag "//www.google.com/jsapi", "chartkick"%> + <%= javascript_include_tag "justgage.1.0.1.min" %> + <%= javascript_include_tag "raphael.2.1.0.min" %> + <%= javascript_include_tag "validator" %> +<% end %> +
+
+
+
+
+ +
+ <%= select_year(DateTime.now.year - 5, {:start_year => DateTime.now.year, :end_year => 1950}, {:name => 'start_year', :class => "span1"} ) %> + - + <%= select_year(DateTime.now.year, {:start_year => DateTime.now.year, :end_year => 1950}, {:name => 'end_year', :class => "span1"} ) %> +
+
+
+ +
+ <%= t("personal_technology_transfer.category") %> +
+
+
+
+ + Export +
+
+
+
+ <% Role.where(:disabled => false, :title.ne => "", :title.ne => nil).asc(:key).each do |role| %> +
+

<%= role.title %>

+
+ loading +
+
+ <% end %> +
+
+ + + + + diff --git a/app/views/admin/technology_transfers/analysis_report.html.erb b/app/views/admin/technology_transfers/analysis_report.html.erb new file mode 100644 index 0000000..b4e8aa4 --- /dev/null +++ b/app/views/admin/technology_transfers/analysis_report.html.erb @@ -0,0 +1 @@ +<%= column_chart @data, :id => params[:role_id], :height => "350px", :xtitle => "Year", :ytitle => "Total number" %> \ No newline at end of file diff --git a/app/views/admin/technology_transfers/download_excel.xlsx.axlsx b/app/views/admin/technology_transfers/download_excel.xlsx.axlsx new file mode 100644 index 0000000..26577eb --- /dev/null +++ b/app/views/admin/technology_transfers/download_excel.xlsx.axlsx @@ -0,0 +1,43 @@ +# encoding: utf-8 + +wb = xlsx_package.workbook +@data.each_with_index do |role,idx| + data = role["data"] + wb.add_worksheet(name: role["name"] + "-" + idx.to_s) do |sheet| + + heading = sheet.styles.add_style(:b => true, :locked => true) + + row = ["Name"] + @site_in_use_locales.each do |locale| + row << t("personal_technology_transfer.title") + " - " + t(locale.to_s) + end + + @site_in_use_locales.each do |locale| + row << t("personal_technology_transfer.patent_title") + " - " + t(locale.to_s) + end + + row << t("personal_technology_transfer.technology_transfer_category") + row << t("personal_technology_transfer.year") + row << t("personal_technology_transfer.patent_granted") + row << t("personal_technology_transfer.patent_number") + + sheet.add_row row, :style => heading + + data.each do |tt| + row = [tt.member_profile.name] + @site_in_use_locales.each do |locale| + row << tt.patent_title_translations[locale.to_s] + end + @site_in_use_locales.each do |locale| + row << tt.title_translations[locale.to_s] + end + + row << tt.technology_transfer_category.title rescue "" + row << tt.year + row << tt.patent_granted ? t("personal_technology_transfer.y_es") : t("personal_technology_transfer.n_o") + row << tt.patent_number + + sheet.add_row row + end + end +end \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 591a5fc..df196c8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -13,4 +13,5 @@ en: patent_title: Patent Title patent_number: Patent Number technology_transfer_category: Category - authors: Authors \ No newline at end of file + authors: Authors + graph_by : "Graph By" \ No newline at end of file diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index fb802b5..90197ea 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -13,4 +13,5 @@ zh_tw: patent_title: 專利名稱 patent_number: 專利證書字號 technology_transfer_category: 類別 - authors: 姓名 \ No newline at end of file + authors: 姓名 + graph_by : "Graph By" \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 91ec309..a08adc1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,7 +5,10 @@ Rails.application.routes.draw do get 'technology_transfers_setting' => "technology_transfers#setting" resources :technology_transfers do collection do - get 'toggle_hide' => 'technology_transfers#toggle_hide' + get 'toggle_hide' => 'technology_transfers#toggle_hide' + get 'analysis' + get 'analysis_report' + get "download_excel" end end diff --git a/lib/personal_technology_transfer/engine.rb b/lib/personal_technology_transfer/engine.rb index b2da652..ddf7974 100644 --- a/lib/personal_technology_transfer/engine.rb +++ b/lib/personal_technology_transfer/engine.rb @@ -4,7 +4,7 @@ module PersonalTechnologyTransfer OrbitApp.registration "PersonalTechnologyTransfer",:type=> 'ModuleApp' do module_label 'module_name.technology_transfers' base_url File.expand_path File.dirname(__FILE__) - personal_plugin :enable => true, :sort_number => '35', :app_name=>"TechnologyTransfer", :intro_app_name=>"TechnologyTransferIntro",:path=>"/plugin/personal_technology_transfer/profile",:front_path=>"/profile",:admin_path=>"/admin/technology_transfers/",:i18n=>'module_name.technology_transfers', :module_app_name=>'TechnologyTransfer', :field_modifiable => true + personal_plugin :enable => true, :sort_number => '35', :app_name=>"TechnologyTransfer", :intro_app_name=>"TechnologyTransferIntro",:path=>"/plugin/personal_technology_transfer/profile",:front_path=>"/profile",:admin_path=>"/admin/technology_transfers/",:i18n=>'module_name.technology_transfers', :module_app_name=>'TechnologyTransfer', :field_modifiable => true, :analysis => true, :analysis_path => "/admin/technology_transfers/analysis" version "0.1" desktop_enabled true