2013-12-04 15:31:18 +08:00
|
|
|
|
####by fq
|
2013-12-12 17:18:55 +08:00
|
|
|
|
=begin
|
|
|
|
|
bid一类多用,根据reward_type字段分类
|
2014-04-08 14:53:11 +08:00
|
|
|
|
reward_type == 1 企业众包需求
|
|
|
|
|
reward_type == 2 创新竞赛
|
|
|
|
|
reward_type == 3 课程中布置的作业
|
|
|
|
|
|
|
|
|
|
homework_type作业类型
|
|
|
|
|
homework_type == 1 文件提交
|
|
|
|
|
homework_type == 2 Project提交
|
2013-12-12 17:18:55 +08:00
|
|
|
|
=end
|
2013-12-04 15:31:18 +08:00
|
|
|
|
class Bid < ActiveRecord::Base
|
2014-04-08 14:53:11 +08:00
|
|
|
|
Enterprise = 1
|
|
|
|
|
Contest = 2
|
|
|
|
|
Homework = 3
|
|
|
|
|
HomeworkFile = 1
|
|
|
|
|
HomeworkProject = 2
|
2013-12-04 15:31:18 +08:00
|
|
|
|
attr_accessible :author_id, :budget, :deadline, :name, :description, :homework_type, :password
|
|
|
|
|
include Redmine::SafeAttributes
|
2015-01-09 15:28:43 +08:00
|
|
|
|
include ExpireHelper
|
2013-12-04 15:31:18 +08:00
|
|
|
|
|
|
|
|
|
belongs_to :author, :class_name => 'User', :foreign_key => :author_id
|
|
|
|
|
belongs_to :course
|
|
|
|
|
has_many :biding_projects, :dependent => :destroy
|
2014-06-17 15:06:31 +08:00
|
|
|
|
has_many :projects, :through => :biding_projects
|
2014-06-17 11:32:41 +08:00
|
|
|
|
has_many :courses_member, :class_name => 'User', :through => :courses
|
2013-12-04 15:31:18 +08:00
|
|
|
|
has_many :journals_for_messages, :as => :jour, :dependent => :destroy
|
|
|
|
|
has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy
|
|
|
|
|
has_many :homework_for_courses, :dependent => :destroy
|
2014-06-17 11:32:41 +08:00
|
|
|
|
has_many :courses, :through => :homework_for_courses, :source => :course
|
2013-12-04 15:31:18 +08:00
|
|
|
|
has_many :homeworks, :class_name => 'HomeworkAttach', :dependent => :destroy
|
2014-10-31 19:30:49 +08:00
|
|
|
|
has_many :homework_evaluations, :through => :homeworks
|
2013-12-04 15:31:18 +08:00
|
|
|
|
has_many :join_in_contests, :dependent => :destroy
|
2014-04-22 08:56:26 +08:00
|
|
|
|
has_many :praise_tread, as: :praise_tread_object, dependent: :destroy
|
2013-12-04 15:31:18 +08:00
|
|
|
|
# has_many :fork_homework, :class_name => 'Bid', :conditions => "#{Bid.table_name}.parent_id = #{id}"
|
2015-01-07 09:27:44 +08:00
|
|
|
|
|
|
|
|
|
|
2015-01-09 15:28:43 +08:00
|
|
|
|
after_create :expire_activitie_cache
|
|
|
|
|
after_update :expire_activitie_cache
|
|
|
|
|
before_destroy :expire_activitie_cache
|
2013-12-04 15:31:18 +08:00
|
|
|
|
|
|
|
|
|
acts_as_attachable
|
|
|
|
|
|
|
|
|
|
NAME_LENGTH_LIMIT = 60
|
2014-10-29 15:16:00 +08:00
|
|
|
|
DESCRIPTION_LENGTH_LIMIT = 3000
|
2014-10-15 11:08:41 +08:00
|
|
|
|
validates :name, length: {maximum: NAME_LENGTH_LIMIT}, presence: true
|
|
|
|
|
validates :description, length: {maximum: DESCRIPTION_LENGTH_LIMIT}
|
|
|
|
|
validates :author_id, presence: true
|
|
|
|
|
validates :deadline, presence: true, format: {:with => /^[\d]{4}[-][\d]{1,2}[-][\d]{1,2}$/}
|
|
|
|
|
validates :name, length: {maximum: NAME_LENGTH_LIMIT}
|
2014-10-15 16:49:48 +08:00
|
|
|
|
validates :budget, format: { with: ->(p) { if p.reward_type == 1 then /^(\d+)$|^(\d+).([0-9]{2})|^(\d+).([0-9]{1})$/
|
|
|
|
|
elsif p.reward_type == 3 then /^(\d+)$|^(\d+).([0-9]{1})$/ end } }
|
|
|
|
|
|
2013-12-04 15:31:18 +08:00
|
|
|
|
validate :validate_user
|
|
|
|
|
validate :validate_reward_type
|
|
|
|
|
after_create :act_as_activity
|
|
|
|
|
|
|
|
|
|
scope :visible, lambda {|*args|
|
|
|
|
|
nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scope :like, lambda {|arg|
|
|
|
|
|
if arg.blank?
|
|
|
|
|
where(nil)
|
|
|
|
|
else
|
|
|
|
|
pattern = "%#{arg.to_s.strip.downcase}%"
|
|
|
|
|
where("LOWER(id) LIKE :p OR LOWER(name) LIKE :p OR LOWER(description) LIKE :p", :p => pattern)
|
|
|
|
|
end
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acts_as_watchable
|
|
|
|
|
acts_as_taggable
|
|
|
|
|
|
|
|
|
|
acts_as_event :title => Proc.new {|o| "#{l(:label_requirement)} ##{o.id}: #{o.name}" },
|
|
|
|
|
:description => :description,
|
|
|
|
|
:author => :author,
|
|
|
|
|
:url => Proc.new {|o| {:controller => 'bids', :action => 'show', :id => o.id}}
|
|
|
|
|
|
|
|
|
|
acts_as_activity_provider :find_options => {:include => [:projects, :author]},
|
|
|
|
|
:author_key => :author_id
|
|
|
|
|
|
|
|
|
|
safe_attributes 'name',
|
|
|
|
|
'description',
|
|
|
|
|
'budget',
|
|
|
|
|
'deadline',
|
|
|
|
|
'homework_type',
|
|
|
|
|
'reward_type',
|
|
|
|
|
'password'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# safe_attributes 'name',
|
|
|
|
|
# 'description',
|
|
|
|
|
# 'deadline'
|
2014-01-02 17:29:46 +08:00
|
|
|
|
def add_jour(user, notes, reference_user_id = 0, options = {})
|
|
|
|
|
if options.count == 0
|
|
|
|
|
self.journals_for_messages << JournalsForMessage.new(:user_id => user.id, :notes => notes, :reply_id => reference_user_id)
|
|
|
|
|
else
|
|
|
|
|
jfm = self.journals_for_messages.build(options)
|
|
|
|
|
jfm.save
|
|
|
|
|
jfm
|
|
|
|
|
end
|
2013-12-04 15:31:18 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.creat_bids(budget, deadline, name, description=nil, reward_type)
|
|
|
|
|
self.create(:author_id => User.current.id, :budget => budget,
|
|
|
|
|
:deadline => deadline, :name => name, :description => description, :commit => 0, :reward_type => reward_type)
|
|
|
|
|
# self.acts << Activity.new(:user_id => self.author_id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update_bids(budget, deadline, name, description=nil)
|
|
|
|
|
if(User.current.id == self.author_id)
|
|
|
|
|
self.name = name
|
|
|
|
|
self.budget = budget
|
|
|
|
|
self.deadline = deadline
|
|
|
|
|
self.description = description
|
|
|
|
|
self.save
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def delete_bids
|
|
|
|
|
unless self.nil?
|
|
|
|
|
if User.current.id == self.author_id
|
|
|
|
|
self.destroy
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def set_commit(commit)
|
|
|
|
|
self.update_attribute(:commit, commit)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def validate_user
|
|
|
|
|
errors.add :author_id, :invalid if author.nil? || !author.active?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def validate_reward_type
|
|
|
|
|
errors.add :reward_type, :invalid if self.reward_type == 0
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def act_as_activity
|
|
|
|
|
self.acts << Activity.new(:user_id => self.author_id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# used to validate weather the user is the creater of the bid
|
|
|
|
|
# added by william
|
|
|
|
|
def validate_bid_manager(user_id)
|
|
|
|
|
unless user_id.nil?
|
|
|
|
|
if self.author_id == user_id
|
|
|
|
|
return true
|
|
|
|
|
else
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-01-07 09:27:44 +08:00
|
|
|
|
|
2015-01-09 15:28:43 +08:00
|
|
|
|
|
2013-12-04 15:31:18 +08:00
|
|
|
|
end
|