diff --git a/app/controllers/seminars_controller.rb b/app/controllers/seminars_controller.rb index 5765b2f..cc8f4fd 100644 --- a/app/controllers/seminars_controller.rb +++ b/app/controllers/seminars_controller.rb @@ -28,6 +28,7 @@ class SeminarsController < ApplicationController registration_is_open = seminar.registration_status.present? sign_up_not_yet = seminar.signup_start_date && @time_now && @seminar.signup_start_date > @time_now sign_up_overdue = seminar.signup_end_date && @time_now && (@seminar.signup_end_date + 1.day <= @time_now) + signup_is_full = seminar.signup_is_full? seminar_url = (@custom_slug ? OrbitHelper.url_to_show(@custom_slug) : OrbitHelper.url_to_show(seminar.to_param)) if !registration_is_open sign_up = t('seminar.sign_up_not_open') @@ -35,6 +36,8 @@ class SeminarsController < ApplicationController sign_up = t('seminar.sign_up_not_yet') elsif sign_up_overdue sign_up = t('seminar.sign_up_overdue') + elsif signup_is_full + sign_up = t('seminar.sign_up_is_full') else sign_up_text = t('seminar.signup') sign_up = link_to(sign_up_text, seminar.get_frontend_url(link_url), :target=>'_blank', :title=>sign_up_text) @@ -329,6 +332,7 @@ class SeminarsController < ApplicationController registration_is_open = seminar.registration_status.present? sign_up_not_yet = seminar.signup_start_date && @time_now && @seminar.signup_start_date > @time_now sign_up_overdue = seminar.signup_end_date && @time_now && (@seminar.signup_end_date + 1.day <= @time_now) + signup_is_full = seminar.signup_is_full? seminar_url = (@custom_slug ? OrbitHelper.url_to_show(@custom_slug) : OrbitHelper.url_to_show(seminar.to_param)) if !registration_is_open sign_up = t('seminar.sign_up_not_open') @@ -336,6 +340,8 @@ class SeminarsController < ApplicationController sign_up = t('seminar.sign_up_not_yet') elsif sign_up_overdue sign_up = t('seminar.sign_up_overdue') + elsif signup_is_full + sign_up = t('seminar.sign_up_is_full') else sign_up_text = t('seminar.signup') sign_up = link_to(sign_up_text, seminar.get_frontend_url(seminar_url), :target=>'_blank', :title=>sign_up_text) @@ -424,6 +430,36 @@ class SeminarsController < ApplicationController redirect_to referer_url, :notice => notice_words return end + signup_limit = @seminar.signup_limit + has_counter = false + if signup_limit + if defined?(OrbitHelper::SharedMutex) + OrbitHelper::SharedMutex.synchronize do + signup_count = OrbitHelper::SharedHash['seminar'][:counter][seminar_id] + if signup_count.nil? + signup_count = SeminarSignup.where(:seminar_main_id=>@seminar.id).count + OrbitHelper::SharedHash['seminar'][:counter][seminar_id] = signup_count + end + if signup_limit > signup_count + OrbitHelper::SharedHash['seminar'][:counter][seminar_id] = signup_count + 1 + has_counter = true + else + notice_words = t('seminar.sign_up_is_full') + referer_url = get_referer_url_for_notice(notice_words) + redirect_to referer_url, :notice => notice_words + return + end + end + else + signup_count = SeminarSignup.where(:seminar_main_id=>@seminar.id).count + if signup_limit <= signup_count + notice_words = t('seminar.sign_up_is_full') + referer_url = get_referer_url_for_notice(notice_words) + redirect_to referer_url, :notice => notice_words + return + end + end + end @signup = SeminarSignup.where(email: params[:seminar_signup][:email], seminar_main_id: seminar_id ).first @seminar_signup = SeminarSignup.new(seminar_signup_params) @@ -463,6 +499,11 @@ class SeminarsController < ApplicationController end redirect_to "#{params[:referer_url].to_s.chomp('/').gsub(/\/([^\/?#]+)(|[^\/]+)$/){|f| '/'+$1}}?method=signup_ok#{status_param}&serial_number=#{@seminar_signup.display_serial_number}" else + if has_counter + OrbitHelper::SharedMutex.synchronize do + OrbitHelper::SharedHash['seminar'][:counter][seminar_id] = OrbitHelper::SharedHash['seminar'][:counter][seminar_id] - 1 + end + end if !not_signup_yet notice_words = t('seminar.email_exist') referer_url = get_referer_url_for_notice(notice_words) diff --git a/app/models/seminar_main.rb b/app/models/seminar_main.rb index f95d74b..d1eb37c 100644 --- a/app/models/seminar_main.rb +++ b/app/models/seminar_main.rb @@ -51,6 +51,9 @@ class SeminarMain field :last_serial_number, :type => Integer, :default => 0 field :assign_mode, :type => Integer, :default => 0 # 0 => 用default signup ids來分配 ,1 => 用final_session來分配, 2 => 用final_session來分配(當有preffered session欄位時) field :update_old_flag, :type => Boolean, :default => false + + field :signup_limit, :type => Integer, :default => nil + belongs_to :seminar_item belongs_to :organizer , :class_name=>"MemberProfile", :foreign_key => :organizer_id has_many :seminar_sessions, :autosave => true, :dependent => :destroy @@ -80,6 +83,12 @@ class SeminarMain accepts_nested_attributes_for :seminar_signup_field_customs, :allow_destroy => true accepts_nested_attributes_for :seminar_template_setting, :allow_destroy => true before_save do + if self.signup_limit == 0 + self.signup_limit = nil + end + if self.signup_limit_changed? + self.sync_signup_count + end module_app_key = "seminar" add_module_app_member_ids = [] remove_module_app_member_ids = [] @@ -257,6 +266,37 @@ class SeminarMain self.enable_recaptcha = (self.seminar_signup_field_sets.where(:field_name=>'recaptcha', :disabled.ne=>true).count != 0) self.save end + def sync_signup_count + if defined?(OrbitHelper::SharedMutex) + seminar_id =self.id.to_s + OrbitHelper::SharedMutex.synchronize do + if self.signup_limit.nil? + OrbitHelper::SharedHash['seminar'][:counter][seminar_id] = nil + else + signup_count = SeminarSignup.where(:seminar_main_id=>self.id).count + OrbitHelper::SharedHash['seminar'][:counter][seminar_id] = signup_count + end + end + end + end + def signup_is_full? + tmp_signup_limit = self.signup_limit + return false if tmp_signup_limit.nil? + if defined?(OrbitHelper::SharedMutex) + seminar_id =self.id.to_s + signup_count = 0 + OrbitHelper::SharedMutex.synchronize do + signup_count = OrbitHelper::SharedHash['seminar'][:counter][seminar_id] + if signup_count.nil? + signup_count = SeminarSignup.where(:seminar_main_id=>self.id).count + OrbitHelper::SharedHash['seminar'][:counter][seminar_id] = signup_count + end + end + else + signup_count = SeminarSignup.where(:seminar_main_id=>self.id).count + end + return (tmp_signup_limit <= signup_count) + end def self.time_range(date1 = null, date2 = null) if !date1.blank? diff --git a/app/models/seminar_signup.rb b/app/models/seminar_signup.rb index 4fde0df..c73aef2 100644 --- a/app/models/seminar_signup.rb +++ b/app/models/seminar_signup.rb @@ -30,7 +30,18 @@ class SeminarSignup accepts_nested_attributes_for :seminar_signup_values, allow_destroy: true accepts_nested_attributes_for :seminar_signup_contributes, allow_destroy: true scope :sort_ordered, ->{ order_by(:sort_number=>1,:created_at=>1) } - + after_destroy do + if defined?(OrbitHelper::SharedMutex) + OrbitHelper::SharedMutex.synchronize do + seminar_id = self.seminar_main_id.to_s + signup_count = OrbitHelper::SharedHash['seminar'][:counter][seminar_id] + if signup_count + signup_count -= 1 + OrbitHelper::SharedHash['seminar'][:counter][seminar_id] = signup_count + end + end + end + end before_create do unit = self.unit_translations.values.select{|v| v.present?}.first tmp_unit_translations = self.unit_translations diff --git a/app/views/admin/seminars/_form.html.erb b/app/views/admin/seminars/_form.html.erb index b026be9..e0c6ecb 100644 --- a/app/views/admin/seminars/_form.html.erb +++ b/app/views/admin/seminars/_form.html.erb @@ -112,6 +112,13 @@ +