seminar2_old/app/models/seminar_main.rb

452 lines
20 KiB
Ruby

class SeminarMain
require 'fileutils'
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Impression
# encoding: utf-8
include OrbitCategory::Categorizable
include Slug
ChoiceTypes = ["checkbox","radio","select"]
field :copy_id
field :except_clone_relations, :type=>Array, :default => []
field :signup_note, localize: true
field :title, as: :slug_title, type: String, localize: true
field :speaker, localize: true
field :content, localize: true
field :act_place, localize: true
field :enable_summary_choice, :type => Boolean, :default => false
field :summary_chioice_type, :type => String, :default => "checkbox"
field :summary_chioices, :type => Array, :default => ["A. Energy Conversion","B. Biocatalysis","C. Electrocatalysis",
"D. Photocatalysis","E. Catalytic and Porous Materials",
"F. Environmental and Reaction Engineering","G. Industrial Applications",
"H. Student Oral Presentation Contest (in English)","I. Student Poster Competition"
]
field :seminar_start_date, :type => Date, :default => Time.now
field :seminar_end_date, :type => Date, :default => Time.now
field :signup_start_date, :type => Date, :default => Time.now
field :signup_end_date, :type => Date, :default => Time.now
field :contribute_start_date, :type => Date
field :contribute_end_date, :type => Date
field :contribute_file_count, type: String
field :registration_status , :type => Array #C: 投稿者 G:一般
field :create_user_id
field :update_user_id
field :reviewer_ids
field :reviewer_show_ids
field :session_show_ids
field :unassigned_seminar_signup_ids
field :unassigned_mode_1_seminar_signup_ids
field :review_start_date, :type => Date
field :review_end_date, :type => Date
field :last_serial_number, :type => Integer, :default => 0
field :assign_mode, :type => Integer, :default => 0 # 0 => 用default signup ids來分配 ,1 => 用final_session來分配
belongs_to :seminar_item
belongs_to :organizer , :class_name=>"MemberProfile", :foreign_key => :organizer_id
has_many :seminar_sessions, :autosave => true, :dependent => :destroy
has_many :seminar_links, :autosave => true, :dependent => :destroy
has_many :seminar_files, :autosave => true, :dependent => :destroy
has_many :seminar_item_contents, :autosave => true, :dependent => :destroy
has_many :seminar_signups, :autosave => true, :dependent => :destroy
has_many :seminar_submission_fields, :autosave => true, :dependent => :destroy
has_many :seminar_signup_fields, :autosave => true, :dependent => :destroy
has_many :seminar_reviews, :autosave => true, :dependent => :destroy
has_many :seminar_signup_field_sets, autosave: true, dependent: :destroy
has_many :seminar_submission_field_sets, autosave: true, dependent: :destroy
has_many :seminar_email_sets, autosave: true, dependent: :destroy
has_many :seminar_signup_field_customs, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :seminar_sessions, :allow_destroy => true
accepts_nested_attributes_for :seminar_links, :allow_destroy => true
accepts_nested_attributes_for :seminar_files, :allow_destroy => true
accepts_nested_attributes_for :seminar_item_contents, :allow_destroy => true
accepts_nested_attributes_for :seminar_signups, :allow_destroy => true
accepts_nested_attributes_for :seminar_submission_fields, :allow_destroy => true
accepts_nested_attributes_for :seminar_signup_fields, :allow_destroy => true
accepts_nested_attributes_for :seminar_reviews, :allow_destroy => true
accepts_nested_attributes_for :seminar_signup_field_sets, :allow_destroy => true
accepts_nested_attributes_for :seminar_submission_field_sets, :allow_destroy => true
accepts_nested_attributes_for :seminar_email_sets, :allow_destroy => true
accepts_nested_attributes_for :seminar_signup_field_customs, :allow_destroy => true
before_save do
module_app_key = "seminar"
add_module_app_member_ids = []
remove_module_app_member_ids = []
if self.reviewer_ids_changed? && (self.copy_id.nil? || !self.new_record?)
new_reviewer_ids = self.reviewer_ids.to_a - self.reviewer_ids_was.to_a
remove_reviewer_ids = self.reviewer_ids_was.to_a - self.reviewer_ids.to_a
new_reviewer_ids.each do |reviewer_id|
SeminarReview.create(:reviewer_id=>reviewer_id,:seminar_main=>self)
add_module_app_member_ids << reviewer_id
end
remove_reviewer_ids.each do |reviewer_id|
SeminarReview.where(:reviewer_id=>reviewer_id,:seminar_main=>self).destroy
remove_module_app_member_ids << reviewer_id
end
unassigned_seminar_signup_ids = self.seminar_signup_ids.map{|s| s.to_s}
seminar_reviews = self.seminar_reviews
unassigned_mode_1_seminar_signup_ids = unassigned_seminar_signup_ids.dup
seminar_reviews.each do |sr|
unassigned_mode_1_seminar_signup_ids = unassigned_mode_1_seminar_signup_ids - sr.get_all_seminar_signup_ids(1)
unassigned_seminar_signup_ids = unassigned_seminar_signup_ids - sr.all_seminar_signup_ids
end
self.unassigned_mode_1_seminar_signup_ids = unassigned_mode_1_seminar_signup_ids
self.unassigned_seminar_signup_ids = unassigned_seminar_signup_ids
end
if self.organizer_id_changed?
if self.organizer_id_was.present?
remove_module_app_member_ids << self.organizer_id_was
end
if self.organizer_id.present?
add_module_app_member_ids << self.organizer_id
end
end
remove_module_app_member_ids.delete(self.organizer_id)
self.reviewer_ids.each{|r| remove_module_app_member_ids.delete(r)} rescue nil
remove_module_app_member_ids.each do |m_id|
user = User.where(:member_profile_id=>m_id).first
begin
user.extra_link_module_app_keys.delete(module_app_key)
user.save
rescue
next
end
end
add_module_app_member_ids.each do |m_id|
user = User.where(:member_profile_id=>m_id).first
begin
user.extra_link_module_app_keys << module_app_key
user.save
rescue
next
end
end
if self.enable_summary_choice
(0...self.summary_chioices.count).to_a.each do |i|
seminar_session = self.seminar_sessions.where(:key=>i).first
if seminar_session.nil?
self.seminar_sessions << SeminarSession.new(:key=>i)
end
end
(self.summary_chioices.count...self.summary_chioices_was.to_a.count).to_a.each do |i|
self.seminar_sessions.where(:key=>i).destroy
end
end
end
before_create do
self.unassigned_seminar_signup_ids = []
self.unassigned_mode_1_seminar_signup_ids = []
if self.copy_id.present?
clone_new(true)
self.created_at = DateTime.now
self.updated_at = DateTime.now
end
end
after_create do
if self.copy_id.present?
self.seminar_signup_fields.each do |seminar_signup_field|
seminar_signup_field.seminar_signup_values.update_all(:key=>seminar_signup_field.key)
end
self.seminar_submission_fields.each do |seminar_submission_field|
seminar_submission_field.seminar_submission_values.update_all(:key=>seminar_submission_field.key)
end
current_reviewer_ids = self.reviewer_ids.to_a
self.seminar_reviews.where(:reviewer_id.nin=>current_reviewer_ids).destroy
module_app_key = "seminar"
current_reviewer_ids.each do |reviewer_id|
seminar_review = self.seminar_reviews.select{|sr| sr.reviewer_id == reviewer_id}.first
if seminar_review.nil?
self.seminar_reviews << SeminarReview.create(:reviewer_id=>reviewer_id,:seminar_main=>self)
user = User.where(:member_profile_id=>reviewer_id).first
if user.present?
user.extra_link_module_app_keys << module_app_key
user.save
end
end
end
end
end
after_initialize do
unless self.new_record?
save_flag = false
seminar_reviews = self.seminar_reviews
if seminar_reviews.present?
if self["reviewer_ids"].nil?
self["reviewer_ids"] = seminar_reviews.pluck(:reviewer_id).flatten.select{|s| !s.nil?}.to_a
save_flag = true
end
if seminar_reviews.count != 0 && self.review_start_date.nil? && self.review_end_date.nil?
review_start_date = seminar_reviews.pluck(:review_start_date).select{|t| t.present?}.first.to_datetime rescue nil
review_end_date = seminar_reviews.pluck(:review_end_date).select{|t| t.present?}.first.to_datetime rescue nil
self.review_start_date = review_start_date
self.review_end_date = review_end_date
if (self.review_start_date_changed? || self.review_end_date_changed?)
save_flag = true
end
end
end
if self.unassigned_seminar_signup_ids.nil?
self.unassigned_seminar_signup_ids = self.seminar_signup_ids.map{|s| s.to_s}
save_flag = true
end
if self.unassigned_mode_1_seminar_signup_ids.nil?
self.unassigned_mode_1_seminar_signup_ids = self.seminar_signup_ids.map{|s| s.to_s}
save_flag = true
end
unless self.last_serial_number_changed_from_default?
self.seminar_signups.sort_ordered.each do |ss|
if ss.serial_number.nil?
self.last_serial_number += 1
ss.serial_number = self.last_serial_number
ss.save(:validate=>false)
save_flag = true
end
end
end
self.save(:validate=>false) if save_flag
end
end
def self.time_range(date1 = null, date2 = null)
if !date1.blank?
r = "#{date1.strftime('%Y-%m-%d')}"
if date2
r += " - #{date2.strftime('%Y-%m-%d')}"
else
r += " - #{I18n.t(:no_deadline)}"
end
r
end
end
def get_attribute_value(attribute_field, signup_id)
SeminarSignupValue.find_by(seminar_signup_field_id: attribute_field.id, seminar_signup_id: signup_id)
end
def get_attribute_values(attribute_type=nil)
@attribute_values = attribute_type.seminar_signup_values rescue nil
end
def get_value_from_field_id(field_id,attribute_type=nil)
values = get_attribute_values(attribute_type)
value = values.detect {|value| value.seminar_signup_field_id == field_id} rescue nil
value ? value : nil
end
def clone_new(clone_mode=false)
@records_all = {}
if clone_mode
clone_target = self.class.find(object.copy_id) rescue nil
else
clone_target = self
end
seminar,clone_target = clone_new_for_object(self,clone_target,clone_mode)
if clone_mode
copy_settings = SeminarSignupAdminSetting.where(:seminar_main_id=>clone_target.id.to_s).to_a
copy_settings.each do |setting|
new_setting = setting.dup
new_setting.seminar_main_id = seminar.id.to_s
new_setting.save
end
count_array = (0...self.seminar_sessions.to_a.count).to_a
count_array.each do |i|
seminar_session = self.seminar_sessions[i]
seminar_session.seminar_signup_ids = map_seminar_signup_ids(seminar_session.seminar_signup_ids)
end
self.unassigned_seminar_signup_ids = map_seminar_signup_ids(clone_target.unassigned_seminar_signup_ids)
self.unassigned_mode_1_seminar_signup_ids = map_seminar_signup_ids(clone_target.unassigned_mode_1_seminar_signup_ids)
need_change_seminar_signups = self.seminar_signups.where(:seminar_session_id.ne=>nil).to_a
count_array = (0...need_change_seminar_signups.count).to_a
count_array.each do |i|
seminar_signup = need_change_seminar_signups[i]
if seminar_signup.seminar_session_id.present?
seminar_signup.seminar_session_id = @records_all["seminar_session_ids"][seminar_signup.seminar_session_id] rescue nil
end
end
count_array = (0...self.seminar_reviews.to_a.count).to_a
change_fields = [:seminar_signup_ids, :remove_seminar_signup_ids, :default_seminar_signup_ids]
count_array.each do |i|
seminar_review = self.seminar_reviews[i]
change_fields.each do |f|
seminar_review.send("#{f}=",map_seminar_signup_ids( seminar_review.send(f)))
end
seminar_review.seminar_session_ids = seminar_review.seminar_session_ids.map{|seminar_session_id| @records_all["seminar_session_ids"][seminar_session_id] rescue nil}.select{|id| !id.nil?}
seminar_review.default_topics = seminar_review.default_topics.map do |default_topic|
fn,id = default_topic.split(".")
if fn == "seminar_signup_fields"
id = @records_all["seminar_signup_field_ids"][BSON::ObjectId(id)].to_s rescue nil
elsif fn == "seminar_submission_fields"
id = @records_all["seminar_submission_field_ids"][BSON::ObjectId(id)].to_s rescue nil
end
"#{fn}.#{id}"
end
end
end
seminar
end
end
def map_seminar_signup_ids(old_seminar_signup_ids)
if @records_all["seminar_signup_ids"].present?
old_seminar_signup_ids.map{|seminar_signup_id| @records_all["seminar_signup_ids"][BSON::ObjectId(seminar_signup_id)].to_s rescue ""}.select{|t| t.present?}
else
[]
end
end
def clone_new_for_object(object,clone_target=nil,clone_mode=false)
if clone_mode
new_object = object
clone_target = object.class.find(object.copy_id) rescue nil if clone_target.nil?
else
clone_target = object if clone_target.nil?
new_object = object.dup
end
return if self.except_clone_relations.to_s.include?(new_object.class.to_s.underscore)
@records_all["#{new_object.class.to_s.underscore.singularize}_ids"] = {} if @records_all["#{new_object.class.to_s.underscore.singularize}_ids"].nil?
begin
@records_all["#{new_object.class.to_s.underscore.singularize}_ids"][clone_target.id] = object.id
rescue
nil
end
if !clone_target.nil? && !new_object.nil?
initialize_fields = ["uid","created_at","updated_at"]
initialize_fields.each do |f|
new_object.send("#{f}=",nil) if new_object.fields.keys.include?(f)
end
relations_fields = clone_target.relations.except("impressions").keys
all_fields = clone_target.fields.keys - relations_fields
all_fields = all_fields - relations_fields.map{|k| "#{k}_id"}
all_fields = all_fields - relations_fields.map{|k| "#{k.singularize}_ids"}
new_object_class_name = new_object.class.to_s.underscore
relations_fields = clone_target.relations.except("impressions").keys
unsort_relation_keys = clone_target.relations.keys
unless @parent_level
fields_to_delete = [new_object_class_name]
tmp_relations_fields = [new_object_class_name]
while relations_fields.count > 0
tmp_singularize_relations_fields = tmp_relations_fields.map{|f| f.singularize}
approve_append = nil
relations_fields.each do |k|
belongs_to_class = clone_target.relations[k].class_name.constantize.relations.select{|k,v| v.macro == :belongs_to}.keys
has_many_class = clone_target.relations[k].class_name.constantize.relations.select{|k,v| v.macro == :has_many}.keys
if (belongs_to_class - tmp_singularize_relations_fields).count == 0
other_has_many_class = (has_many_class - unsort_relation_keys)
if other_has_many_class.count == 0
tmp_relations_fields << k
else
result = other_has_many_class.map do |k|
belongs_to_class = k.classify.constantize.relations.select{|kk,v| v.macro == :belongs_to}.keys
has_many_class = k.classify.constantize.relations.select{|kk,v| v.macro == :has_many}.keys
if (belongs_to_class - tmp_singularize_relations_fields).count == 0
true
else
fields_to_delete = fields_to_delete.concat(belongs_to_class)
tmp_relations_fields.concat(belongs_to_class)
false
end
end
if result.select{|t| !t}.count == 0
if (fields_to_delete.map{|f| f.pluralize} - tmp_relations_fields).count == 0
tmp_relations_fields << k
elsif clone_target.relations[k].class_name.constantize.fields.keys.include?("key")
tmp_relations_fields << k
elsif (clone_target.relations[k].class_name.constantize.relations.keys.map{|f| f.singularize} & fields_to_delete).count != 0
approve_append = k
end
end
end
elsif !unsort_relation_keys.include?(clone_target.relations[k].class_name.underscore) && !unsort_relation_keys.include?(clone_target.relations[k].class_name.underscore.pluralize)
tmp_relations_fields << k
end
end
tmp_relations_fields << approve_append if approve_append.present?
approve_append = nil
relations_fields = relations_fields - tmp_relations_fields
end
relations_fields = tmp_relations_fields
fields_to_delete.each{|f| relations_fields.delete(f)}
@clone_mode = clone_mode
end
@parent_level = true
if clone_mode
all_fields.each do |f|
next if f == "uid"
unless new_object.send("#{f}_changed?") && new_object.send("#{f}_changed_from_default?")
new_object.send("#{f}=",clone_target.send(f))
end
end
end
relations_fields.each do |f|
no_dup_flag = false
if clone_target.relations[f].macro == :belongs_to
no_dup_flag = new_object.send(f).present?
elsif clone_target.relations[f].macro == :has_many
no_dup_flag = new_object.send(f).to_a.count != 0
end
if clone_target.relations[f].macro == :belongs_to || clone_target.relations[f].class_name == "MemberProfile"
if @records_all["#{f}_ids"].nil?
new_object.send("#{f}_id=",clone_target.send("#{f}_id"))
else
new_object.send("#{f}_id=",(@records_all["#{f}_ids"][clone_target.send("#{f}_id")]))
end
elsif clone_target.relations[f].macro == :has_many
next if self.except_clone_relations.to_s.include?(f)
clone_relations = []
need_clone_relations = clone_target.send(f).asc(:_id).to_a
file_flag = false
need_clone_relations.each_with_index do |r,i|
clone_relation = new_object.send(f)[i]
clone_relation = r.dup if clone_relation.nil?
initialize_fields.each do |f|
clone_relation.send("#{f}=",nil) if clone_relation.fields.keys.include?(f)
end
check_fields = clone_relation.fields.except(initialize_fields)
check_fields.keys.each do |f|
if (clone_relation.send(f).class.to_s.match(/uploader/i) rescue false)
if clone_relation[f].blank? && (clone_relation.send(f).file.nil? rescue true)
clone_relation[f] = r[f]
source_filepath = r.send(f).file.file
if @clone_mode
dest_filepath = clone_relation.send(f).file.file
FileUtils.mkdir_p(File.dirname(dest_filepath))
FileUtils.cp(source_filepath,dest_filepath)
end
elsif (clone_relation.send(f).file rescue nil)
clone_relation[f] = File.basename(clone_relation.send(f).file.file.to_s)
end
file_flag = true
end
end
clone_relations << clone_relation
end
if !no_dup_flag || (no_dup_flag && file_flag)
new_object_relations = new_object.send(f).to_a
if new_object_relations.count != 0
if clone_relations.count > new_object_relations.count
clone_relations = clone_relations[0...new_object_relations.count]
else
clone_relations = clone_relations.concat(new_object.send(f)[clone_relations.count...new_object_relations.count])
end
new_object.send("#{f}=",clone_relations)
else
new_object.send("#{f}=",clone_relations)
end
end
count_array = (0...new_object.send(f).to_a.count).to_a
count_array.each do |i|
clone_new_for_object(new_object.send(f)[i],need_clone_relations[i],true)
end
end
end
new_object.copy_id = clone_target.id if new_object.fields.keys.include?("copy_id")
return new_object, clone_target
end
end