Add edit student area feature.Add sign_up_setting_id to StudentDataField.(Every contest use different StudentDataField)

This commit is contained in:
BOHUNG 2020-03-18 16:12:08 +08:00
parent 18f2d1cd69
commit 0d9cacc77d
10 changed files with 119 additions and 53 deletions

View File

@ -161,19 +161,26 @@ class Admin::OlympiamanagementsController < OrbitAdminController
end
elsif params[:type] == 'sign_up_setting'
@sign_up_params = params.require(:sign_up_setting).permit!
StudentDataField.destroy_all
SchoolDataFields.destroy_all
StudentDataField.create
SchoolDataFields.create
# SchoolDataFields.destroy_all
# SchoolDataFields.create
if params[:new_sign_up] == "true"
#render :html => @sign_up_params and return
SignUpSetting.create(@sign_up_params)
sign_up_setting = SignUpSetting.create(@sign_up_params)
student_data_field = StudentDataField.create(:sign_up_setting_id=>sign_up_setting.id.to_s)
index = student_data_field.student_data_fields.map{|h| h.keys.first}.index('StudentArea')
student_data_field.student_data_fields[index]['StudentArea']['values'] = params[:StudentArea]
student_data_field.save
redirect_to :back
else
@sign_up_setting = SignUpSetting.find(params[:id]) rescue SignUpSetting.where(:active=>true).last
if @sign_up_setting.nil?
render_404_html
else
student_data_field = StudentDataField.where(:sign_up_setting_id=>@sign_up_setting.id.to_s).first
student_data_field = StudentDataField.create(:sign_up_setting_id=>@sign_up_setting.id.to_s) if student_data_field.nil?
index = student_data_field.student_data_fields.map{|h| h.keys.first}.index('StudentArea')
student_data_field.student_data_fields[index]['StudentArea']['values'] = params[:StudentArea]
student_data_field.save
@sign_up_setting.update_attributes(@sign_up_params)
redirect_to :back
end
@ -303,6 +310,8 @@ class Admin::OlympiamanagementsController < OrbitAdminController
@site_locales = Site.first.in_use_locales
@site_locales.delete(I18n.locale)
@site_locales.insert(0,I18n.locale)
@student_data_field = StudentDataField.where(:sign_up_setting_id=>@sign_up_setting.id).first
@student_data_field = StudentDataField.create(:sign_up_setting_id=>@sign_up_setting.id) if @student_data_field.nil?
end
def export_school_connection_data
@OlympiaSchoolDataFields = OlympiaSchoolDataFields.where(:sign_up_setting_id=>@sign_up_setting.id).asc(:id)
@ -320,7 +329,9 @@ class Admin::OlympiamanagementsController < OrbitAdminController
end
def sign_up_student_data_list
page_num = params[:page] || 1
@field_infos = StudentDataField.first.student_data_fields
@student_data_field = StudentDataField.where(:sign_up_setting_id=>@sign_up_setting.id).first
@student_data_field = StudentDataField.create(:sign_up_setting_id=>@sign_up_setting.id) if @student_data_field.nil?
@field_infos = @student_data_field.student_data_fields
@sign_up_setting_id = @sign_up_setting.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
@ -329,7 +340,9 @@ class Admin::OlympiamanagementsController < OrbitAdminController
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_field = StudentDataField.where(:sign_up_setting_id=>@sign_up_setting.id).first
@student_data_field = StudentDataField.create(:sign_up_setting_id=>@sign_up_setting.id) if @student_data_field.nil?
@field_infos = @student_data_field.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|
@ -426,27 +439,17 @@ class Admin::OlympiamanagementsController < OrbitAdminController
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]
@error_msg = []
@flag = true
@required_fields.each do |field|
if @olympia_school_data_field[field].to_s.blank?
@flag = false
break
end
end
if !@flag
I18n.t('olympiamanagement.please_set_school_connect_data')
@error_msg << "<p><a href=#{school_connection_data_olympiamanagements_path}>#{I18n.t('olympiamanagement.please_set_school_connect_data')}</a></p>".html_safe
render 'error_message'
else
@student_data_field = StudentDataField.first
@olympia_student_data = (OlympiaStudentDataField.find(params[:id]) rescue OlympiaStudentDataField.new)
end
@student_data_field = StudentDataField.where(:sign_up_setting_id=>@sign_up_setting.id).first
@student_data_field = StudentDataField.create(:sign_up_setting_id=>@sign_up_setting.id) if @student_data_field.nil?
@olympia_student_data = (OlympiaStudentDataField.find(params[:id]) rescue OlympiaStudentDataField.new)
end
def update_student_data
if params[:type] == 'add_student_data'
@error_msg = []
@student_params = params.require(:add_student_data).permit!
@all_fields_name = (StudentDataField.first || StudentDataField.new).student_data_fields.map{|hash| hash.keys[0]}
@student_data_field = StudentDataField.where(:sign_up_setting_id=>@sign_up_setting.id).first
@student_data_field = StudentDataField.create(:sign_up_setting_id=>@sign_up_setting.id) if @student_data_field.nil?
@all_fields_name = @student_data_field.student_data_fields.map{|hash| hash.keys[0]}
if @student_params.select{|key,value| !value.to_s.blank?}.count < @all_fields_name.slice(0,@all_fields_name.count-1).count
@error_msg << "缺少#{@all_fields_name.slice(0,@all_fields_name.count-1).count-@student_params.select{|key,value| !value.to_s.blank?}.count}個欄位值"
end

View File

@ -227,7 +227,8 @@ class OlympiamanagementsController < ApplicationController
I18n.t('olympiamanagement.please_set_school_connect_data')
render_contents_in_index_page(@breadcumb+"<p><a href=#{school_connection_data_olympiamanagements_path}>#{I18n.t('olympiamanagement.please_set_school_connect_data')}</a></p>".html_safe)
else
@student_data_field = StudentDataField.first
@student_data_field = StudentDataField.where(:sign_up_setting_id=>@sign_up_setting.id).first
@student_data_field = StudentDataField.create(:sign_up_setting_id=>@sign_up_setting.id) if @student_data_field.nil?
@olympia_student_data = (OlympiaStudentDataField.find(params[:id]) rescue OlympiaStudentDataField.new)
@page_content = render_to_string(:formats=> [:html] ,:partial=>'add_student_data.html',:locals=>{:@error=>(@@error rescue nil),:@student_data_field=>@student_data_field,:@olympia_student_data=>@olympia_student_data})
render_contents_in_index_page(@breadcumb+@page_content.html_safe)
@ -236,7 +237,9 @@ class OlympiamanagementsController < ApplicationController
end
def sign_up_student_data_list
page_num = params[:page] || 1
@field_infos = StudentDataField.first.student_data_fields
@student_data_field = StudentDataField.where(:sign_up_setting_id=>@sign_up_setting.id).first
@student_data_field = StudentDataField.create(:sign_up_setting_id=>@sign_up_setting.id) if @student_data_field.nil?
@field_infos = @student_data_field.student_data_fields
@olympia_school_data_fields_id = OlympiaSchoolDataFields.where(:account_number=>session[:olympia_login_id],:sign_up_setting_id=>@sign_up_setting.id,:approved=>true).first.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)
@ -284,7 +287,9 @@ class OlympiamanagementsController < ApplicationController
@record_fields = [['school_name','school_code'],['school_address'],['class_number','enrollment_limited'],
['office_tel_number','fax'],['mobile_number','email'],['principal_signature','dean_signature']]
@student_fields = ['serial_number','StudentName','StudentSex','StudentClass','StudentIDNO','birth_date','StudentArea','StudentIdentity']
@field_infos = StudentDataField.first.student_data_fields
@student_data_field = StudentDataField.where(:sign_up_setting_id=>@sign_up_setting.id).first
@student_data_field = StudentDataField.create(:sign_up_setting_id=>@sign_up_setting.id) if @student_data_field.nil?
@field_infos = @student_data_field.student_data_fields
@olympia_school_data_fields_id = OlympiaSchoolDataFields.where(:account_number=>session[:olympia_login_id],:sign_up_setting_id=>@sign_up_setting.id,:approved=>true).first.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)
@ -673,7 +678,9 @@ class OlympiamanagementsController < ApplicationController
@student_params[key] = @olympia_student_data[key]
end
end
@all_fields_name = (StudentDataField.first || StudentDataField.new).student_data_fields.map{|hash| hash.keys[0]}
@student_data_field = StudentDataField.where(:sign_up_setting_id=>@sign_up_setting.id).first
@student_data_field = StudentDataField.create(:sign_up_setting_id=>@sign_up_setting.id) if @student_data_field.nil?
@all_fields_name = @student_data_field.student_data_fields.map{|hash| hash.keys[0]}
if @student_params.select{|key,value| !value.to_s.blank?}.count < @all_fields_name.slice(0,@all_fields_name.count-1).count
@error_msg << "缺少#{@all_fields_name.slice(0,@all_fields_name.count-1).count-@student_params.select{|key,value| !value.to_s.blank?}.count}個欄位值"
end

View File

@ -1,11 +1,9 @@
class ClassSettingFields
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitModel::Impression
# encoding: utf-8
include OrbitTag::Taggable
include OrbitCategory::Categorizable
include OrbitCategory::Categorizable
field :class_number_range , type: Array , default: []
field :enrollment_available , type: Fixnum , default: 0
end

View File

@ -1,8 +1,6 @@
class OlympiaSchoolDataFields
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitModel::Impression
# encoding: utf-8
field :account_number , type: String , default: ''
field :password , type: String , default: ''
@ -34,7 +32,7 @@ class OlympiaSchoolDataFields
self.approved = true
self.save!
end
if self.enrollment.nil? || self.enrollment.class != Fixnum
if self.enrollment.nil? || self.enrollment.class != Fixnum || self.enrollment < 0
self.enrollment = 0
self.save!
end

View File

@ -1,19 +1,17 @@
class OlympiaStudentDataField
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitModel::Impression
# encoding: utf-8
include OrbitTag::Taggable
include OrbitCategory::Categorizable
include OrbitCategory::Categorizable
field :StudentIdentity , type: String , default: ''
field :StudentName , type: String , default: ''
field :StudentName , type: String , default: ''
field :StudentSex , type: String , default: ''
field :StudentIDNO , type: String , default: ''
field :StudentBirthYear , type: Fixnum , default: Time.now.year
field :StudentIDNO , type: String , default: ''
field :StudentBirthYear , type: Fixnum , default: Time.now.year
field :StudentBirthMonth , type: Fixnum , default: Time.now.month
field :StudentBirthDay , type: Fixnum , default: Time.now.day
field :StudentClass , type: String , default: ''
field :StudentClass , type: String , default: ''
field :StudentPhone , type: String , default: ''
field :StudentCode , type: String , default: ''
field :StudentAddress , type: String , default: ''

View File

@ -1,8 +1,6 @@
class OlympiaStudentImage
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitModel::Impression
# encoding: utf-8
include OrbitTag::Taggable
include OrbitCategory::Categorizable

View File

@ -1,11 +1,9 @@
class SchoolDataFields
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitModel::Impression
# encoding: utf-8
include OrbitTag::Taggable
include OrbitCategory::Categorizable
include OrbitCategory::Categorizable
field :school_data_fields , type: Array , default: [{'account_number'=>{'type'=>'String','size'=>'20','hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true"}},
{'password'=>{'type'=>'String','size'=>'','hint'=>{'zh_tw'=>'未修改即依照預設密碼','en'=>'Use default password without modification'}}},
{'school_name'=>{'type'=>'String','size'=>'20','hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true"}},

View File

@ -1,11 +1,7 @@
class SignUpSetting
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitModel::Impression
# encoding: utf-8
include OrbitTag::Taggable
include OrbitCategory::Categorizable
field :start_year , type: String , default: Time.now.strftime("%Y")
field :end_year , type: String , default: Time.now.strftime("%Y")
field :start_month , type: String , default: Time.now.strftime("%m")
@ -40,7 +36,8 @@ class SignUpSetting
field :active , type: Boolean , default: true
after_save :change_all_password , :check_active
after_destroy do |record|
OlympiaSchoolDataFields.where(:sign_up_setting_id => record.id).destroy_all
OlympiaSchoolDataFields.where(:sign_up_setting_id => record.id.to_s).destroy
StudentDataField.where(:sign_up_setting_id => record.id.to_s).destroy
end
after_create do |record|
SignUpSetting.where(:id.ne=>record.id).each{|record| record.update(:active=>false)}

View File

@ -1,11 +1,10 @@
class StudentDataField
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitModel::Impression
# encoding: utf-8
include OrbitTag::Taggable
include OrbitCategory::Categorizable
include OrbitCategory::Categorizable
field :sign_up_setting_id , type: String , default: (SignUpSetting.where(:active=>true).last.id.to_s rescue "")
field :student_data_fields , type: Array , default: [{'StudentIdentity'=>{'type'=>'select_string','hint'=>{'zh_tw'=>'','en'=>''},'values'=>{'zh_tw'=>['一般身分','科展得獎者','科學班'],'en'=>['General identity','Science Fair Winner','Science Class']},'required'=>"true"}},
{'StudentName'=>{'type'=>'String','size'=>'20','hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true"}},
{'StudentSex'=>{'type'=>'radio','size'=>'20','choices'=>['M','F'],'values'=>{'zh_tw'=>['男','女'],'en'=>['Male','Female']},'hint'=>{'zh_tw'=>'','en'=>''},'required'=>"true"}},

View File

@ -11,6 +11,32 @@
<%=render :partial=>'checkbox',:locals=>{:name=>t('olympiamanagement.new_sign_up'),:params_name=>'new_sign_up',:id=>'new_sign_up',:extra_class=>'lleft red_text',:is_checked=>nil}%>
<div style="clear: both;"></div>
<hr style="border-color: black;border-top: 0em;">
<div class="col_text">
<h4 class="col" for="StudentArea"><%= t('olympiamanagement.StudentArea')%></h4>
<div class="col_r" id="StudentArea">
<% index = @student_data_field.student_data_fields.map{|h| h.keys.first}.index('StudentArea') %>
<% for i in 0...(@student_data_field.student_data_fields[index]['StudentArea']['values'][@site_locales.first.to_s].count) %>
<div style="clear: both;">
<div class="remove_btn"><a class="remove_locale_fields" href="#" title="<%=t(:remove)%>">❌</a></div>
<div class="locale_fields">
<% @site_locales.each_with_index do |locale,locale_index|%>
<input class="locale_text" type="text" name="[StudentArea][<%=locale%>][]" value="<%= @student_data_field.student_data_fields[index]['StudentArea']['values'][locale.to_s][i]%>" <%=locale_index != 0 ? 'style=display:none;' : ''%>>
<% end %>
</div>
<div class="locale_buttons">
<% @site_locales.each_with_index do |locale,locale_index|%>
<a title="<%=t(locale)%>" class="btn show_locale_field_btn <%=locale_index == 0 ? 'active' : ''%>"><%=t(locale)%></a>
<% end %>
</div>
</div>
<% end %>
<div style="clear: both;">
<a id="add_area_choice" class="btn btn-small btn-primary"><i class="icons-plus"></i><%=t(:add)%></a>
</div>
</div>
</div>
<div style="clear: both;"></div>
<hr style="border-color: black;border-top: 0em;">
<div class="col_text">
<li class="col" for="start_time"><%= t('olympiamanagement.sign_up_start_time')%></li>
<li class="col_r" id="start_time">
@ -94,3 +120,47 @@
<input type="reset" name="Submit" value="<%=t('olympiamanagement.cancel')%>">
<% end %>
</fieldset>
<style type="text/css">
.locale_buttons{
float: left;
}
.locale_fields{
float: left;
}
.remove_btn{
float: left;
}
.icons-plus{
margin-right: 0.3em;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$('.show_locale_field_btn').off('click').on('click',function(){
var index = $(this).index();
$(this).parent().siblings('.locale_fields').find('.locale_text').css('display','none');
$(this).parent().siblings('.locale_fields').find('.locale_text').eq(index).css('display','block');
$(this).parent().find('.show_locale_field_btn').removeClass('active');
$(this).addClass('active');
});
$('#add_area_choice').click(function(){
var new_locale_fields_html = '<div style="clear: both;"><div class="remove_btn"><a class="remove_locale_fields" href="#" title="<%=t(:remove)%>">❌</a></div><div class="locale_fields"><% @site_locales.each_with_index do |locale,locale_index|%><input class="locale_text" type="text" name="[StudentArea][<%=locale%>][]" value="" <%=locale_index != 0 ? 'style=display:none;' : ''%>><% end %></div><div class="locale_buttons"><% @site_locales.each_with_index do |locale,locale_index|%><a title="<%=t(locale)%>" class="btn show_locale_field_btn <%=locale_index == 0 ? 'active' : ''%>"><%=t(locale)%></a><% end %></div></div>';
$(this).parent().before(new_locale_fields_html);
$('.remove_locale_fields').off('click').on('click',function(){
if( window.confirm('請確認是否刪除?'))
$(this).parent().parent().remove();
});
$('.show_locale_field_btn').off('click').on('click',function(){
var index = $(this).index();
$(this).parent().siblings('.locale_fields').find('.locale_text').css('display','none');
$(this).parent().siblings('.locale_fields').find('.locale_text').eq(index).css('display','block');
$(this).parent().find('.show_locale_field_btn').removeClass('active');
$(this).addClass('active');
});
});
$('.remove_locale_fields').off('click').on('click',function(){
if( window.confirm('請確認是否刪除?'))
$(this).parent().parent().remove();
});
});
</script>