2015-01-08 17:42:46 +08:00
class Poll < ActiveRecord :: Base
#attr_accessible :closed_at, :polls_group_id, :polls_name, :polls_status, :polls_type, :published_at, :user_id
include Redmine :: SafeAttributes
belongs_to :user
2015-01-14 11:50:59 +08:00
has_many :poll_questions , :dependent = > :destroy , :order = > " #{ PollQuestion . table_name } .question_number "
2015-01-08 17:42:46 +08:00
has_many :poll_users , :dependent = > :destroy
has_many :users , :through = > :poll_users #该文件被哪些用户提交答案过
2015-04-09 15:18:13 +08:00
# 添加课程的poll动态
2015-04-21 15:10:45 +08:00
has_many :acts , :class_name = > 'Activity' , :as = > :act , :dependent = > :destroy
2015-08-11 15:54:05 +08:00
# 课程动态
has_many :course_acts , :class_name = > 'CourseActivity' , :as = > :course_act , :dependent = > :destroy
2015-08-15 16:54:51 +08:00
after_create :act_as_activity , :act_as_course_activity
2015-08-14 15:48:23 +08:00
# 课程消息
has_many :course_messages , :class_name = > 'CourseMessage' , :as = > :course_message , :dependent = > :destroy
2015-08-21 15:55:17 +08:00
after_save :act_as_course_message , :act_as_activity , :act_as_course_activity
2016-01-12 15:53:15 +08:00
after_update :update_activity
2015-04-21 15:10:45 +08:00
acts_as_event :title = > Proc . new { | o | " #{ l ( :label_course_poll ) } : #{ o . polls_name } " } ,
:description = > :polls_description ,
:datetime = > :published_at ,
:author = > :user ,
:url = > Proc . new { | o | { :controller = > 'poll' , :action = > 'show' , :id = > o . id } }
2015-04-09 15:18:13 +08:00
2015-04-21 15:10:45 +08:00
acts_as_activity_provider :type = > 'polls' ,
#:permission => :view_course_polls,
:find_options = > { :select = > " #{ Poll . table_name } .* " ,
:joins = > " LEFT JOIN #{ Course . table_name } ON ( #{ Poll . table_name } .polls_type='Course' AND #{ Poll . table_name } .polls_status= 2 AND #{ Poll . table_name } .polls_group_id = #{ Course . table_name } .id ) " } ,
:timestamp = > " #{ self . table_name } .published_at " ,
:author_key = > :user_id
2015-04-09 15:18:13 +08:00
2015-04-21 15:10:45 +08:00
def act_as_activity
self . acts << Activity . new ( :user_id = > self . user_id )
end
2015-04-09 15:18:13 +08:00
2016-01-12 15:53:15 +08:00
#动态的更新
def update_activity
update_course_activity ( self . class , self . id )
update_user_activity ( self . class , self . id )
update_org_activity ( self . class , self . id )
end
2015-08-11 15:54:05 +08:00
#课程动态公共表记录
def act_as_course_activity
if self . polls_type == " Course "
2015-08-14 14:44:31 +08:00
if self . polls_status == 2 #问卷是发布状态
2015-08-15 17:06:19 +08:00
self . course_acts << CourseActivity . new ( :user_id = > self . user_id , :course_id = > self . polls_group_id )
2015-08-14 14:44:31 +08:00
elsif self . polls_status == 1 #问卷是新建状态
self . course_acts . destroy_all
2015-08-15 17:06:19 +08:00
end
2015-08-11 15:54:05 +08:00
end
end
2015-08-14 15:48:23 +08:00
# 发布问卷,出了发布者外,其他人都能收到消息通知
def act_as_course_message
if self . polls_type == " Course "
2015-08-21 11:46:04 +08:00
if self . polls_status == 2 #问卷是发布状态
Course . find ( self . polls_group_id ) . members . each do | m |
if m . user_id != self . user_id
self . course_messages << CourseMessage . new ( :user_id = > m . user_id , :course_id = > self . polls_group_id , :viewed = > false )
end
2015-08-14 15:48:23 +08:00
end
2015-08-21 11:46:04 +08:00
elsif self . polls_status == 1 #问卷是新建状态
self . course_messages . destroy_all
2015-08-14 15:48:23 +08:00
end
end
end
2015-01-08 17:42:46 +08:00
end