Application_Form/app/models/application_form_review.rb

126 lines
6.8 KiB
Ruby

# encoding: utf-8
require "orbit_form_helper"
class ApplicationFormReview
include Mongoid::Document
include Mongoid::Timestamps
field :review_start_date, :type => Date, :default => Time.now #Already move to application_form_main
field :review_end_date, :type => Date, :default => Time.now #Already move to application_form_main
field :reviewer_id
field :application_form_signup_ids, :type => Array, :default => []
field :remove_application_form_signup_ids, :type => Array, :default => []
field :application_form_signup_contribute_ids, :type => Array, :default => []
field :remove_application_form_signup_contribute_ids, :type => Array, :default => []
field :default_application_form_signup_ids, :type => Array, :default => []
field :default_topics, :type => Array, :default => []
field :default_topics_values, :type => Array, :default => []
field :final_session
field :session_ids
field :application_form_session_ids
belongs_to :application_form_main
after_initialize do
unless self.new_record? || @triggered_initialize
@triggered_initialize = true
save_flag = false
destroy_flag = false
if (self.reviewer_id.class == Array rescue false)
reviewer_ids = self.reviewer_id[1..-1]
reviewer_ids.each do |reviewer_id|
self.class.create(:reviewer_id=>reviewer_id,:application_form_main_id=>self.application_form_main_id)
end
self.reviewer_id = self.reviewer_id.first
save_flag = true
elsif self[:reviewer_id].nil?
self.destroy
destroy_flag = true
end
if !destroy_flag
if (self.session_ids.nil? rescue true)
session_ids = []
self.default_topics.each_with_index do |t,i|
next if t != "application_form_signup_contributes.description"
session_ids << self.default_topics_values[i]
end
session_ids = session_ids.flatten.map{|i| i.to_i}
self.session_ids = session_ids
save_flag = true
end
if self.application_form_session_ids.nil? && self.session_ids.class == Array
self.application_form_session_ids = ApplicationFormSession.where(:application_form_main_id=>self.application_form_main_id,:key.in=>self.session_ids).pluck(:id) rescue nil
save_flag = true
end
self.save(:validate=>false) if save_flag
end
end
end
before_save do
default_application_form_signup_ids = []
self.session_ids = []
self.default_topics.each_with_index do |topic,i|
fn,id = (topic.split(".") rescue [])
next if fn.nil?
if fn == "application_form_signup_fields"
default_application_form_signup_ids += ApplicationFormSignupValue.where(:application_form_signup_field_id=>id,:val.in=>self.default_topics_values[i].to_a).pluck(:application_form_signup_id)
elsif fn == "application_form_submission_fields"
application_form_signup_contribute_ids = ApplicationFormSubmissionValue.where(:application_form_submission_field_id=>id,:val.in=>self.default_topics_values[i].to_a).pluck(:application_form_signup_contribute)
default_application_form_signup_ids += ApplicationFormSignupContribute.where(:id.in=>application_form_signup_contribute_ids).pluck(:application_form_signup_id)
elsif fn == "application_form_signup_contributes"
application_form_signup_ids_from_contribute = ApplicationFormSignupContribute.where(:description.in=>self.default_topics_values[i].to_a).pluck(:application_form_signup_id)
application_form_signup_ids_from_contribute = application_form_signup_ids_from_contribute & (ApplicationFormSignup.where(:application_form_main_id=>self.application_form_main_id).pluck(:id) rescue [])
default_application_form_signup_ids += application_form_signup_ids_from_contribute
self.session_ids = self.session_ids.concat(self.default_topics_values[i].map{|v| v.to_i})
end
end
self.application_form_session_ids = ApplicationFormSession.where(:application_form_main_id=>self.application_form_main_id,:key.in=>self.session_ids.to_a).pluck(:id) rescue []
default_application_form_signup_ids.uniq!
default_application_form_signup_ids = default_application_form_signup_ids.map{|s| s.to_s}
self.default_application_form_signup_ids = default_application_form_signup_ids
end
def review
MemberProfile.find(self.reviewer_id) rescue nil
end
def get_all_application_form_signup_ids(mode = nil)
if (self.default_topics.include?("application_form_signup_contributes.description") && self.application_form_main.assign_mode != 0) || mode != 0
final_assign_signup_ids = ApplicationFormSession.where(:id.in=>self.application_form_session_ids).pluck(:application_form_signup_ids).flatten.map{|id| id.to_s} rescue []
default_application_form_signup_ids = ApplicationFormSession.where(:id.in=>self.application_form_session_ids).pluck(:default_application_form_signup_ids).flatten
default_application_form_signup_ids = ApplicationFormSignup.where(:application_form_session_id=>nil,:id.in=>default_application_form_signup_ids).pluck(:id).map{|id| id.to_s} rescue []
result = final_assign_signup_ids + default_application_form_signup_ids + self.application_form_signup_ids - self.remove_application_form_signup_ids
result = result.uniq
else
result = all_application_form_signup_ids
end
result
end
def all_application_form_signup_ids
result = self.application_form_signup_ids - self.remove_application_form_signup_ids + self.default_application_form_signup_ids
result.uniq
end
def append_default_signup_id(signup_id)
sid = signup_id.to_s
unless self.default_application_form_signup_ids.include?(sid)
self.default_application_form_signup_ids << sid
end
end
def fix_assign
application_form_signups = ApplicationFormSignup.where(:id.in=>self.application_form_signup_ids)
self.application_form_signup_ids = application_form_signups.pluck(:id).map{|id| id.to_s}
application_form_signups = application_form_signups.to_a
self.application_form_signup_contribute_ids = application_form_signups.map{|ss| ss.application_form_signup_contribute_ids}.flatten.map{|id| id.to_s}
remove_application_form_signups = ApplicationFormSignup.where(:id.in=>self.remove_application_form_signup_ids)
self.remove_application_form_signup_ids = remove_application_form_signups.pluck(:id).map{|id| id.to_s}
remove_application_form_signups = remove_application_form_signups.to_a
self.remove_application_form_signup_contribute_ids = remove_application_form_signups.map{|ss| ss.application_form_signup_contribute_ids}.flatten.map{|id| id.to_s}
self.save
end
def clear_all_assign
self.application_form_signup_ids = []
self.remove_application_form_signup_ids = []
self.application_form_signup_contribute_ids = []
self.remove_application_form_signup_contribute_ids = []
self.save
end
end