diff --git a/app/assets/images/2020131_download_school_code.xlsx b/app/assets/images/2020131_download_school_code.xlsx
deleted file mode 100644
index 5f43ae4..0000000
Binary files a/app/assets/images/2020131_download_school_code.xlsx and /dev/null differ
diff --git a/app/controllers/admin/olympiamanagements_controller.rb b/app/controllers/admin/olympiamanagements_controller.rb
index d88682f..13ca7ba 100644
--- a/app/controllers/admin/olympiamanagements_controller.rb
+++ b/app/controllers/admin/olympiamanagements_controller.rb
@@ -77,7 +77,7 @@ class Admin::OlympiamanagementsController < OrbitAdminController
redirect_to :back
end
elsif params[:type] == 'sign_up_setting'
- @sign_up_setting = SignUpSetting.find(params[:id]) rescue SignUpSetting.new
+ @sign_up_setting = SignUpSetting.find(params[:id]) rescue SignUpSetting.where(:active=>true).last
if @sign_up_setting.nil?
render_404_html
else
@@ -206,7 +206,6 @@ class Admin::OlympiamanagementsController < OrbitAdminController
end
end
def sign_up_setting
- @sign_up_setting = (SignUpSetting.all.length == 0) ? (SignUpSetting.new) : (SignUpSetting.last)
@site_locales = Site.first.in_use_locales
@site_locales.delete(I18n.locale)
@site_locales.insert(0,I18n.locale)
@@ -228,32 +227,59 @@ class Admin::OlympiamanagementsController < OrbitAdminController
def sign_up_student_data_list
page_num = params[:page] || 1
@field_infos = StudentDataField.first.student_data_fields
- @sign_up_setting_id = SignUpSetting.last.id
+ @sign_up_setting_id = SignUpSetting.id
@student_fields = OlympiaStudentDataField.where(:olympia_school_data_fields_id=>params[:olympia_school_id],:sign_up_setting_id=>@sign_up_setting_id).asc(:id).page(page_num).per(10)
end
def export_sign_up_student_data
- # @OlympiaSchoolDataFields = OlympiaSchoolDataFields.all.asc(:id)
- # dir_path = 'tmp/olypiamanagement/'
- # #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 = 'school_export.xlsx'
- # Dir.chdir(dir_path) do
- # File.open(@filename, 'w') do |f|
- # f.write render_to_string( :handlers=> [:axlsx], :formats=> [:xlsx] ,:partial=> 'school_export.xlsx',:locals=> {:@OlympiaSchoolDataFields=>@OlympiaSchoolDataFields} )
- # end
- # end
- # tmp_filename_data = File.read(dir_path +@filename)
- # send_data(tmp_filename_data, type: 'application/xlsx', disposition: 'attachment', filename: @filename)
+ @student_data_fields = OlympiaStudentDataField.all.asc(:id)
+ dir_path = 'tmp/olypiamanagement/'
+ #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
+ @field_infos = (StudentDataField.first || StudentDataField.new).student_data_fields
+ @student_data_list = []
+ @fields = [ "school_code", "school_name" , "StudentName" ,"StudentSex","StudentIDNO","birth_date","StudentClass","StudentPhone","StudentCode","StudentAddress","StudentArea","StudentIdentity"]
+ @student_data_fields.each do |field|
+ @tmp_student_data = []
+ @fields.each do |field_name|
+ @value = field[field_name] rescue ""
+ @index = @field_infos.map{|hash| hash.keys[0]}.index(field_name) rescue nil
+ if !@index.nil?
+ if !['String','Year','select_num','File'].include?(@field_infos.map{|hash| hash.values[0]}[@index]['type'])
+ @value = (@field_infos.map{|hash| hash.values[0]}[@index]['values'][I18n.locale.to_s][@value.to_i].to_s rescue @value)
+ end
+ else
+ if field_name == 'birth_date'
+ @value = "#{field.StudentBirthYear}-#{field.StudentBirthMonth}-#{field.StudentBirthDay}"
+ else
+ @school = OlympiaSchoolDataFields.find(field.olympia_school_data_fields_id) rescue nil
+ if !@school.nil?
+ @value = @school[field_name].to_s
+ end
+ end
+ end
+ @tmp_student_data << @value
+ end
+ @student_data_list << @tmp_student_data
+ end
+ #render :html => @student_data_list and return
+ @filename = 'student_export.xlsx'
+ Dir.chdir(dir_path) do
+ File.open(@filename, 'w') do |f|
+ f.write render_to_string( :handlers=> [:axlsx], :formats=> [:xlsx] ,:partial=> 'student_export.xlsx',:locals=> {:@student_data_list=>@student_data_list,:@fields=>@fields} )
+ end
+ end
+ tmp_filename_data = File.read(dir_path +@filename)
+ send_data(tmp_filename_data, type: 'application/xlsx', disposition: 'attachment', filename: @filename)
end
def download_scan_file_of_certificate
- @sign_up_setting_id = SignUpSetting.last.id
+ @sign_up_setting_id = @sign_up_setting.id
if !params[:student_id].to_s.blank?
- @student = OlympiaStudentDataField.where(:student_id=>params[:student_id]).first
+ @student = OlympiaStudentDataField.find(params[:student_id]) rescue nil
if @student.nil?
redirect_to :back
else
tmp_filename_data = @student.olympia_student_images.last.student_file.file.read
- send_data(tmp_filename_data, type: 'image/jpg', disposition: 'attachment', filename: "#{params[:student_name]}.jpg")
+ send_data(tmp_filename_data, type: 'image/jpg', disposition: 'attachment', filename: "#{@student.StudentName}.jpg")
end
else
@student_data_list = OlympiaStudentDataField.where(:sign_up_setting_id=>@sign_up_setting_id)
@@ -290,7 +316,7 @@ class Admin::OlympiamanagementsController < OrbitAdminController
end
end
def add_sign_up_student_data
- @olympia_school_data_field = OlympiaSchoolDataFields.where(:account_number=>session[:olympia_login_id]).first
+ @olympia_school_data_field = OlympiaSchoolDataFields.find(params[:school_id]) rescue nil
redirect_to :back if @olympia_school_data_field.nil?
@required_fields = [:school_contact_person_name,:department_job_title,:office_tel_number,:fax,:mobile_number,:email]
@flag = true
@@ -339,12 +365,12 @@ class Admin::OlympiamanagementsController < OrbitAdminController
if @olympia_student_data.nil?
@olympia_student_data = OlympiaStudentDataField.create(@student_params)
@olympia_student_data.olympia_school_data_fields_id = @olympia_school_data_field.id
- @olympia_student_data.sign_up_setting_id = SignUpSetting.last.id
+ @olympia_student_data.sign_up_setting_id = @sign_up_setting.id
@olympia_student_data.save
else
@olympia_student_data.update_attributes(@student_params)
end
- redirect_to admin_olympiamanagements_sign_up_student_data_list_path
+ redirect_to admin_olympiamanagements_sign_up_student_data_list_path+"?olympia_school_id=#{params[:school_id]}"
else
render 'import_error_msg'
end
@@ -361,8 +387,9 @@ class Admin::OlympiamanagementsController < OrbitAdminController
end
def create_first_fields
StudentDataField.create if StudentDataField.all.length == 0
- SchoolDataFields.create if StudentDataField.all.length == 0
+ SchoolDataFields.create if SchoolDataFields.all.length == 0
SignUpSetting.create if SignUpSetting.all.length == 0
+ @sign_up_setting = SignUpSetting.where(:active=>true).last
end
def download_import_file
@OlympiaSchoolDataFields = OlympiaSchoolDataFields.all.asc(:id)
diff --git a/app/controllers/olympiamanagements_controller.rb b/app/controllers/olympiamanagements_controller.rb
index 80fb947..871856c 100644
--- a/app/controllers/olympiamanagements_controller.rb
+++ b/app/controllers/olympiamanagements_controller.rb
@@ -18,14 +18,15 @@ class OlympiamanagementsController < ApplicationController
rescue
@@error = nil
end
- if request.session[:olympia_login_id].to_s.blank?
+ @school = OlympiaSchoolDataFields.where(:account_number=>request.session[:olympia_login_id],:approved=>true).first rescue nil
+ if @school.nil?
render_contents_in_index_page(render_to_string(:formats=> [:html] ,:partial=>'login',:locals=>{:@error=>@@error})) and return
end
end
def login
@@error = nil
if !params[:login_id].to_s.blank?
- if OlympiaSchoolDataFields.where(:account_number=>params[:login_id]).length == 0
+ if OlympiaSchoolDataFields.where(:account_number=>params[:login_id],:approved=>true).length == 0
@@error = "no_account"
else
if OlympiaSchoolDataFields.where(:account_number=>params[:login_id]).first.password != params[:login_pw]
@@ -43,7 +44,7 @@ class OlympiamanagementsController < ApplicationController
end
def set_sign_up_setting
@key = Site.first.template rescue "" #for_render_pages
- @sign_up_setting = SignUpSetting.last
+ @sign_up_setting = SignUpSetting.where(:active=>true).last
data_arr = [{:url=>'/olympiamanagements/school_connection_data',:name=>t('olympiamanagement.school_connection_data')},
{:url=>'/olympiamanagements/add_sign_up_student_data',:name=>t('olympiamanagement.add_sign_up_student_data')},
{:url=>'/olympiamanagements/sign_up_student_data_list',:name=>t('olympiamanagement.sign_up_student_data_list')},
@@ -133,7 +134,21 @@ class OlympiamanagementsController < ApplicationController
@page_content = render_to_string(:formats=> [:html] ,:partial=>'login',:locals=>{:@error=>@error})
else
@@error = nil
+ @school_data_fields = [{'account_number'=>{'type'=>'String','size'=>'20','hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true",'readonly'=>"readonly"}},
+ {'school_name'=>{'type'=>'String','size'=>'20','hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true",'readonly'=>"readonly"}},
+ {'school_code'=>{'type'=>'String','size'=>'20','hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true",'readonly'=>"readonly"}},
+ {'school_address'=>{'type'=>'String','size'=>'40','hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true",'readonly'=>"readonly"}},
+ {'class_number'=>{'type'=>'String','size'=>'5','hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true",'readonly'=>"readonly"}},
+ {'enrollment_limited'=>{'type'=>'Fixnum','size'=>'5','hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true",'readonly'=>"readonly"}},
+ {'school_contact_person_name'=>{'type'=>'String','size'=>'20','hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true"}},
+ {'department_job_title'=>{'type'=>'String','size'=>'20','hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true"}},
+ {'office_tel_number'=>{'type'=>'String','size'=>'20','hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true"}},
+ {'fax'=>{'type'=>'String','size'=>'20','hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true"}},
+ {'mobile_number'=>{'type'=>'String','size'=>'20','hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true"}},
+ {'email'=>{'type'=>'String','size'=>'40','hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true"}}]
+ @olympia_school_data_field = OlympiaSchoolDataFields.where(:account_number=>@@session[:olympia_login_id]).first
@page_content = @breadcumb
+ @page_content += render_to_string(:formats=> [:html] ,:partial=>'edit_school_data_for_index.html',:locals=>{:@school_data_fields=>@school_data_fields,:@olympia_school_data_field=>@olympia_school_data_field,:@@session=>@@session}) #form_for not work for rendering in Pagescontroller => edit to form
end
else
if !@instructions_pages.empty?
@@ -207,7 +222,7 @@ class OlympiamanagementsController < ApplicationController
page_num = params[:page] || 1
@field_infos = StudentDataField.first.student_data_fields
@olympia_school_data_fields_id = OlympiaSchoolDataFields.where(:account_number=>session[:olympia_login_id]).first.id
- @sign_up_setting_id = SignUpSetting.last.id
+ @sign_up_setting_id = @sign_up_setting.id
@student_fields = OlympiaStudentDataField.where(:olympia_school_data_fields_id=>@olympia_school_data_fields_id,:sign_up_setting_id=>@sign_up_setting_id).asc(:id).page(page_num).per(10)
@page_content = render_to_string(:formats=> [:html] ,:partial=>'student_data_list.html',:locals=>{:@student_fields=>(@student_fields rescue [])}).html_safe
render_contents_in_index_page(@breadcumb+@page_content)
@@ -243,7 +258,10 @@ class OlympiamanagementsController < ApplicationController
@page = Page.where(:layout=>"olympia_managements_index",:parent_page_id=>(Page.where(:name=> ((I18n.locale.to_s == "en") ? "Home" : "首頁")).first.id)).first
data_arr = @page.child_page.map{|page| {:url=>"#{request.protocol}#{request.host_with_port}/#{I18n.locale}#{page.url}",:name=>page.name}}
@breadcumb = render_to_string(:formats=> [:html] ,:partial=>'breadcumb',:locals=>{:data_arr=>data_arr,:@title=>I18n.t('olympiamanagement.sign_up_title')})
- @remark_texts =SignUpSetting.last.remark_field[I18n.locale.to_s].html_safe
+ @remark_texts = @sign_up_setting.remark_field[I18n.locale.to_s].html_safe
+ @sign_up_setting.attributes.to_h.each do |key,value|
+ @remark_texts = @remark_texts.gsub("{{#{key}}}",value.to_s)
+ end
@record_form_title = ((I18n.locale.to_s == "zh_tw") ? "#{Time.now.year}年國中科學奧林匹亞競賽國家代表隊選拔初選報名表" : "#{Time.now.year} Year International Junior Science Olympiad National Team Selection First Stage Sign up Form")
@school_data = OlympiaSchoolDataFields.where(:account_number=>session[:olympia_login_id]).first
@record_fields = [['school_name','school_code'],['school_address'],['class_number','enrollment_limited'],
@@ -251,7 +269,7 @@ class OlympiamanagementsController < ApplicationController
@student_fields = ['serial_number','StudentName','StudentSex','StudentClass','StudentIDNO','birth_date','StudentArea','StudentIdentity']
@field_infos = StudentDataField.first.student_data_fields
@olympia_school_data_fields_id = OlympiaSchoolDataFields.where(:account_number=>session[:olympia_login_id]).first.id
- @sign_up_setting_id = SignUpSetting.last.id
+ @sign_up_setting_id = @sign_up_setting.id
@student_data_list = OlympiaStudentDataField.where(:olympia_school_data_fields_id=>@olympia_school_data_fields_id,:sign_up_setting_id=>@sign_up_setting_id)
@head = render_to_string(:formats=>[:html],:file=>"#{Rails.root}/app/templates/#{@key}/partial/_head.html")
end
@@ -264,7 +282,6 @@ class OlympiamanagementsController < ApplicationController
uid = OrbitHelper.params[:uid] rescue ""
tags = OrbitHelper.widget_tags
categories = OrbitHelper.widget_categories || []
- @sign_up_setting = SignUpSetting.last
@olympiamanagement_instruction = @sign_up_setting.registration_instructions[I18n.locale.to_s].to_s
@sign_up_setting.attributes.to_h.each do |key,value|
@olympiamanagement_instruction = @olympiamanagement_instruction.gsub("{{#{key}}}",value.to_s)
@@ -579,10 +596,8 @@ class OlympiamanagementsController < ApplicationController
if @error_msg.empty?
@olympia_student_data = OlympiaStudentDataField.find(params[:id]) rescue nil
if @olympia_student_data.nil?
- @olympia_student_data = OlympiaStudentDataField.create(@student_params)
- @olympia_student_data.olympia_school_data_fields_id = @olympia_school_data_field.id
- @olympia_student_data.sign_up_setting_id = SignUpSetting.last.id
- @olympia_student_data.save
+ @olympia_student_data = OlympiaStudentDataField.create(@student_params.merge({
+ :olympia_school_data_fields_id=>@olympia_school_data_field.id,:sign_up_setting_id => @sign_up_setting.id}))
else
@olympia_student_data.update_attributes(@student_params)
end
diff --git a/app/models/olympia_school_data_fields.rb b/app/models/olympia_school_data_fields.rb
index 6a16fab..da8b34a 100644
--- a/app/models/olympia_school_data_fields.rb
+++ b/app/models/olympia_school_data_fields.rb
@@ -22,6 +22,9 @@ class OlympiaSchoolDataFields
field :enrollment , type: Fixnum , default: 0
field :approved , type: Boolean , default: true
field :sign_up_setting_id , type: String , default: SignUpSetting.last.id.to_s
+ after_destroy do |record|
+ OlympiaStudentDataField.where(:olympia_school_data_fields_id => record.id,:sign_up_setting_id=>SignUpSetting.last.id).destroy_all
+ end
after_initialize do
if !self.new_record?
if self.approved.nil?
@@ -32,6 +35,10 @@ class OlympiaSchoolDataFields
self.enrollment = 0
self.save!
end
+ if self.enrollment == 0 #check enrollment student number
+ self.enrollment = OlympiaStudentDataField.where(:olympia_school_data_fields_id=>self.id,:sign_up_setting_id=>SignUpSetting.last.id).length
+ self.save!
+ end
if self.enrollment_limited.nil?
self.enrollment_limited = 0
self.save!
diff --git a/app/models/olympia_student_data_field.rb b/app/models/olympia_student_data_field.rb
index 1b63098..d11ce38 100644
--- a/app/models/olympia_student_data_field.rb
+++ b/app/models/olympia_student_data_field.rb
@@ -24,6 +24,24 @@ class OlympiaStudentDataField
field :sign_up_setting_id , type: String , default: ''
has_many :olympia_student_images , :dependent => :destroy , autosave: true
after_save :check_file
+ after_create do |record|
+ if !record.olympia_school_data_fields_id.blank?
+ @school = OlympiaSchoolDataFields.find(record.olympia_school_data_fields_id) rescue nil
+ if !@school.nil?
+ @school.enrollment = @school.enrollment + 1
+ @school.save
+ end
+ end
+ end
+ after_destroy do |record|
+ if !record.olympia_school_data_fields_id.blank?
+ @school = OlympiaSchoolDataFields.find(record.olympia_school_data_fields_id) rescue nil
+ if !@school.nil?
+ @school.enrollment = @school.enrollment - 1
+ @school.save
+ end
+ end
+ end
after_initialize do
if !self.new_record?
if self.approved.nil?
diff --git a/app/models/sign_up_setting.rb b/app/models/sign_up_setting.rb
index ae04947..a826cdc 100644
--- a/app/models/sign_up_setting.rb
+++ b/app/models/sign_up_setting.rb
@@ -18,9 +18,13 @@ class SignUpSetting
field :end_minute , type: String , default: Time.now.strftime("%M")
field :default_password , type: String , default: ""
field :set_default_password , type: Boolean , default: false
- field :remark_field , type: Hash , default: {:zh_tw=>"",:en=>""}
+ field :remark_field , type: Hash , default: {:zh_tw=>"注意事項:
+請於2019年5月3日前(以郵戳為憑),將本報名表正本(需核章)以掛號方式郵寄至【11677台北市文山區汀州路4段88號科教中心,收件人: IJSO選訓委員會】,逾期概不受理報名。
+
+
",:en=>""}
field :registration_instructions , type: Hash , default: {:zh_tw=>"",:en=>""}
- after_save :change_all_password
+ field :active , type: Boolean , default: true
+ after_save :change_all_password , :check_active
def change_all_password
if self.set_default_password
OlympiaSchoolDataFields.all.each do |school_field|
@@ -31,4 +35,11 @@ class SignUpSetting
self.save
end
end
+ def check_active
+ if self.active
+ SignUpSetting.where.not(:id=>self.id).each do |setting|
+ setting.active = false
+ end
+ end
+ end
end
\ No newline at end of file
diff --git a/app/views/admin/olympiamanagements/_student_export.xlsx.axlsx b/app/views/admin/olympiamanagements/_student_export.xlsx.axlsx
new file mode 100644
index 0000000..41141b7
--- /dev/null
+++ b/app/views/admin/olympiamanagements/_student_export.xlsx.axlsx
@@ -0,0 +1,15 @@
+# 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" })
+ @fields.each do |field|
+ row << t('olympiamanagement.'+field)
+ end
+ sheet.add_row row , :style=> title,:widths => row.map{|cell| (cell.length* 11/ 2).round}
+ @student_data_list.each do |infos_row|
+ sheet.add_row infos_row , :style=> column
+ end
+end
\ No newline at end of file
diff --git a/app/views/admin/olympiamanagements/export_school_connection_data.html.erb b/app/views/admin/olympiamanagements/export_school_connection_data.html.erb
deleted file mode 100644
index e69de29..0000000
diff --git a/app/views/admin/olympiamanagements/export_sign_up_student_data.html.erb b/app/views/admin/olympiamanagements/export_sign_up_student_data.html.erb
deleted file mode 100644
index e69de29..0000000
diff --git a/app/views/admin/olympiamanagements/sign_up_student_data_list.html.erb b/app/views/admin/olympiamanagements/sign_up_student_data_list.html.erb
index 2526ce9..a590c9f 100644
--- a/app/views/admin/olympiamanagements/sign_up_student_data_list.html.erb
+++ b/app/views/admin/olympiamanagements/sign_up_student_data_list.html.erb
@@ -2,6 +2,10 @@
<% th_name_list = ['StudentName','StudentSex','StudentClass','birth_date','StudentIdentity','StudentArea','scan_file_of_certificate','action']%>
+<% @school = (OlympiaSchoolDataFields.find(params[:olympia_school_id]) rescue nil) %>
+<% if !@school.nil? %>
+
<%= "#{field.StudentBirthYear}-#{field.StudentBirthMonth}-#{field.StudentBirthDay}" %> | <% elsif th_name == 'scan_file_of_certificate'%> <% if !field.olympia_student_images.map{|image| image.student_file.file}.select { |file| file.present?}.empty? %> -<%= "#{field.StudentName}.gif" %>"> | +<%= "#{field.StudentName}.jpg" %>"> | <% else %><% end %> diff --git a/app/views/olympiamanagements/_edit_school_data_for_index.html.erb b/app/views/olympiamanagements/_edit_school_data_for_index.html.erb new file mode 100644 index 0000000..e4fee8d --- /dev/null +++ b/app/views/olympiamanagements/_edit_school_data_for_index.html.erb @@ -0,0 +1,35 @@ + + + + \ No newline at end of file |