2015-08-11 15:54:05 +08:00
|
|
|
|
class CourseActivity < ActiveRecord::Base
|
|
|
|
|
attr_accessible :user_id, :course_act_id,:course_act_type,:course_id
|
|
|
|
|
# 虚拟关联
|
2015-08-11 17:02:52 +08:00
|
|
|
|
belongs_to :course_act ,:polymorphic => true
|
2015-08-11 15:54:05 +08:00
|
|
|
|
belongs_to :course
|
|
|
|
|
belongs_to :user
|
2015-08-21 14:21:10 +08:00
|
|
|
|
has_many :user_acts, :class_name => 'UserAcivity',:as =>:act
|
2016-05-19 15:26:23 +08:00
|
|
|
|
after_create :add_course_lead,:add_user_activity, :add_org_activity
|
2015-11-20 17:42:43 +08:00
|
|
|
|
before_destroy :destroy_user_activity, :destroy_org_activity
|
2015-08-21 14:21:10 +08:00
|
|
|
|
|
|
|
|
|
#在个人动态里面增加当前动态
|
|
|
|
|
def add_user_activity
|
2015-08-29 15:34:18 +08:00
|
|
|
|
user_activity = UserActivity.where("act_type = '#{self.course_act_type.to_s}' and act_id = '#{self.course_act_id}'").first
|
2015-08-21 14:21:10 +08:00
|
|
|
|
if user_activity
|
|
|
|
|
user_activity.save
|
|
|
|
|
else
|
2015-09-11 16:42:24 +08:00
|
|
|
|
if self.course_act_type == 'Message' && !self.course_act.parent_id.nil?
|
|
|
|
|
user_activity = UserActivity.where("act_type = 'Message' and act_id = #{self.course_act.parent.id}").first
|
|
|
|
|
user_activity.created_at = self.created_at
|
|
|
|
|
user_activity.save
|
|
|
|
|
else
|
|
|
|
|
user_activity = UserActivity.new
|
|
|
|
|
user_activity.act_id = self.course_act_id
|
|
|
|
|
user_activity.act_type = self.course_act_type
|
|
|
|
|
user_activity.container_type = "Course"
|
|
|
|
|
user_activity.container_id = self.course_id
|
|
|
|
|
user_activity.user_id = self.user_id
|
|
|
|
|
user_activity.save
|
|
|
|
|
end
|
2015-08-21 14:21:10 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-03-10 16:43:50 +08:00
|
|
|
|
def add_org_activity
|
2015-11-18 16:05:08 +08:00
|
|
|
|
org_activity = OrgActivity.where("org_act_type = '#{self.course_act_type.to_s}' and org_act_id = '#{self.course_act_id}'").first
|
|
|
|
|
if org_activity
|
2016-03-10 16:43:50 +08:00
|
|
|
|
org_activity.updated_at = self.updated_at
|
2015-11-18 16:05:08 +08:00
|
|
|
|
org_activity.save
|
|
|
|
|
else
|
|
|
|
|
if self.course_act_type == 'Message' && !self.course_act.parent_id.nil?
|
|
|
|
|
org_activity = OrgActivity.where("org_act_type = 'Message' and org_act_id = #{self.course_act.parent.id}").first
|
|
|
|
|
org_activity.created_at = self.created_at
|
2016-03-10 16:43:50 +08:00
|
|
|
|
org_activity.updated_at = self.updated_at
|
2015-11-18 16:05:08 +08:00
|
|
|
|
org_activity.save
|
|
|
|
|
else
|
|
|
|
|
OrgActivity.create(:user_id => self.user_id,
|
|
|
|
|
:org_act_id => self.course_act_id,
|
|
|
|
|
:org_act_type => self.course_act_type,
|
|
|
|
|
:container_id => self.course_id,
|
|
|
|
|
:container_type => 'Course',
|
|
|
|
|
:created_at => self.created_at,
|
|
|
|
|
:updated_at => self.updated_at)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-08-21 14:21:10 +08:00
|
|
|
|
def destroy_user_activity
|
2015-08-29 15:34:18 +08:00
|
|
|
|
user_activity = UserActivity.where("act_type = '#{self.course_act_type.to_s}' and act_id = '#{self.course_act_id}'")
|
2015-08-21 14:21:10 +08:00
|
|
|
|
user_activity.destroy_all
|
|
|
|
|
end
|
2015-11-20 17:42:43 +08:00
|
|
|
|
|
|
|
|
|
def destroy_org_activity
|
|
|
|
|
org_activity = OrgActivity.where("org_act_type = '#{self.course_act_type.to_s}' and org_act_id = '#{self.course_act_id}'")
|
|
|
|
|
org_activity.destroy_all
|
|
|
|
|
end
|
2016-01-19 10:29:57 +08:00
|
|
|
|
|
|
|
|
|
# 发布新课导语
|
|
|
|
|
# 导语要放置在课程创建信息之后
|
|
|
|
|
def add_course_lead
|
2016-03-18 13:39:28 +08:00
|
|
|
|
# 避免空数据迁移报错问题
|
2016-03-14 11:00:55 +08:00
|
|
|
|
if self.course_act_type == "Course" and Message.where("id=12440").any?
|
2016-02-22 15:34:50 +08:00
|
|
|
|
lead_message = Message.find(12440)
|
|
|
|
|
name = lead_message.subject
|
|
|
|
|
content = lead_message.content
|
2016-01-19 10:29:57 +08:00
|
|
|
|
# message的status状态为0为正常,为1表示创建课程时发送的message
|
2016-03-17 15:12:28 +08:00
|
|
|
|
# author_id 默认为课程使者创建
|
|
|
|
|
message = Message.create(:subject => name, :content => content, :board_id => self.course.boards.first.id, :author_id => 1 , :sticky => true, :status => true )
|
2016-03-18 12:41:07 +08:00
|
|
|
|
# 更新的目的是为了排序,因为该条动态的时间可能与课程创建的动态创建时间一致
|
2016-01-29 22:09:16 +08:00
|
|
|
|
message.course_acts.first.update_attribute(:updated_at, message.course_acts.first.updated_at + 1) if message.course_acts.first
|
2016-01-19 10:29:57 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
2015-08-11 15:54:05 +08:00
|
|
|
|
end
|