socialforge/app/models/journals_for_message.rb

294 lines
11 KiB
Ruby
Raw Normal View History

# fq
# 数据库字段中带有m前缀和is_readed是二次开发添加之前的字段基本复用
# 注意reply_id 是提到人的id不是留言id, Base中叫做 at_user
class JournalsForMessage < ActiveRecord::Base
include Redmine::SafeAttributes
include UserScoreHelper
include ApplicationHelper
has_many_kindeditor_assets :assets, :dependent => :destroy
safe_attributes "jour_type", # 留言所属类型
"jour_id", # 留言所属类型的id
"notes", # 留言内容
"reply_id", # 留言被回复留言者的用户id(用户a回复了用户b这是b的id用以查询谁给b留言了)
"status", # 留言是否被查看(弃用)
"user_id", # 留言者的id
"m_parent_id", # 留言信息的父留言id
"is_readed", # 留言是否已读
"m_reply_count", # 留言的回复数量
"m_reply_id" , # 回复某留言的留言id(a留言回复了b留言这是b留言的id)
"is_comprehensive_evaluation", # 1 教师评论、2 匿评、3 留言
"private"
acts_as_tree :foreign_key => 'm_parent_id', :counter_cache => :m_reply_count, :order => "#{JournalsForMessage.table_name}.created_on ASC"
after_destroy :delete_kindeditor_assets
belongs_to :project,
:foreign_key => 'jour_id',
2015-11-26 16:43:10 +08:00
:conditions => "#{self.table_name}.jour_type = 'Project' ",:touch => true
belongs_to :course,
:foreign_key => 'jour_id',:touch=>true
belongs_to :jour, :polymorphic => true
belongs_to :user
belongs_to :homework_attach
belongs_to :student_works_score
belongs_to :at_user, :class_name => "User", :foreign_key => 'reply_id'
acts_as_event :title => Proc.new {|o| "#{l(:label_my_message)}"},
:datetime => Proc.new {|o| o.updated_on },
:author => Proc.new {|o| o.user },
:description => Proc.new{|o| o.notes },
:type => Proc.new {|o| o.jour_type },
:url => Proc.new {|o|
if o.jour.kind_of? Project
{:controller => 'projects', :action => 'feedback', :id => o.jour, :r => o.id, :anchor => "word_li_#{o.id}"}
elsif o.jour.kind_of? Course
{:controller => 'courses', :action => 'feedback', :id => o.jour, :r => o.id, :anchor => "word_li_#{o.id}"}
end
}
acts_as_activity_provider :author_key => :user_id,
:timestamp => "#{self.table_name}.updated_on",
:find_options => {:include => :project }
acts_as_activity_provider :type => 'course_journals_for_messages',
:author_key => :user_id,
:permission => :view_course_journals_for_messages,
:timestamp => "#{self.table_name}.updated_on",
:find_options => {:include => :course }
acts_as_attachable
has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy
2015-10-13 16:51:44 +08:00
has_many :principal_acts, :class_name => 'PrincipalActivity',:as =>:principal_act ,:dependent => :destroy
# 课程动态
has_many :course_acts, :class_name => 'CourseActivity',:as =>:course_act ,:dependent => :destroy
# 消息关联
has_many :course_messages, :class_name => 'CourseMessage',:as =>:course_message ,:dependent => :destroy
2015-08-21 17:00:17 +08:00
has_many :user_feedback_messages, :class_name => 'UserFeedbackMessage', :as =>:journals_for_message, :dependent => :destroy
2015-12-17 15:14:52 +08:00
has_many :at_messages, as: :at_message, dependent: :destroy
validates :notes, presence: true, if: :is_homework_jour?
2015-12-17 15:14:52 +08:00
after_create :act_as_activity, :act_as_course_activity, :act_as_course_message, :act_as_at_message, :act_as_user_feedback_message, :act_as_principal_activity, :act_as_student_score
after_create :reset_counters!
2016-01-26 11:17:55 +08:00
#after_update :update_activity
after_destroy :reset_counters!
after_save :be_user_score
after_destroy :down_user_score
# default_scope { where('m_parent_id IS NULL') }
#动态的更新
def update_activity
update_course_activity(self.class, self.id)
update_user_activity(self.class, self.id)
update_forge_activity(self.class, self.id)
update_org_activity(self.class, self.id)
update_principal_activity(self.class, self.id)
end
def self.create_by_user? user
if user.anonymous?
return false
else
return true
end
end
def self.remove_by_user? user
if( self.user == user ||
( self.jour.kind_of?(User) && self.jour== user )
)
true
else
false
end
end
def self.delete_message(message_id)
self.find(message_id).destroy
# self.destroy_all "id = #{message_id}"
end
def is_homework_jour?
self.jour_type != "HomeworkAttach"
end
def reference_user
User.find(reply_id)
end
def delete_by_user?user
# 用户可删除自己的留言
if self.user.id == user.id || user.admin?
return true
else
return false
end
end
def self.reference_message(user_id)
@user = User.find(user_id)
message = JournalsForMessage.find_by_sql("select * from journals_for_messages where reply_id = #{@user.id}
or (jour_type = 'Bid' and jour_id in (select id from bids where author_id = #{@user.id}))")
message
end
def act_as_activity
if self.jour_type == 'Principal'
unless self.user_id == self.jour.id && self.user_id != self.reply_id && self.reply_id != 0
# self.acts << Activity.new(:user_id => self.user_id)
self.acts << Activity.new(:user_id => self.jour_id)
end
elsif self.jour_type == 'Project'
self.acts << Activity.new(:user_id => self.reply_id)
elsif self.jour_type == 'Course'
self.acts << Activity.new(:user_id => self.reply_id)
else
end
end
2015-10-13 16:51:44 +08:00
#用户动态公共表记录
def act_as_principal_activity
if self.jour_type == 'Principal' && self.m_parent_id.nil?
self.principal_acts << PrincipalActivity.new(:user_id => self.user_id, :principal_id => self.jour_id)
2015-10-14 11:38:16 +08:00
end
2015-10-13 16:51:44 +08:00
end
def reset_counters!
self.class.reset_counters!(self)
end
def self.reset_counters! journals_for_messages
# jfm_id = journals_for_messages.id.to_i
count = find_all_by_m_parent_id(journals_for_messages.m_parent_id).count #(SELECT COUNT(*) FROM #{JournalsForMessage.table_name} WHERE m_parent_id = #{jfm_id} )
update_all("m_reply_count = #{count.to_i}", ["id = ?", journals_for_messages.m_parent_id])
end
#如果是在项目中留言则返回该项目否则返回nil - zjc
def project
if self.jour_type == 'Project'
Project.find(self.jour_id)
else
nil
end
end
# 更新用户分数 -by zjc
def be_user_score
#新建了留言回复
if self.reply_id != 0
#协同得分加分
UserScore.joint(:reply_message, self.user,User.find(self.reply_id),self, { journals_for_messages_id: self.id })
update_replay_for_message(self.user,1)
if self.jour_type == "Project"
update_replay_for_message(self.user,2,self.jour)
end
end
end
# 更新用户分数 -by zjc
def down_user_score
#删除了留言回复
if self.reply_id != 0
#协同得分减分
UserScore.joint(:reply_message_delete, self.user,User.find(self.reply_id), { journals_for_messages_id: self.id })
update_replay_for_message(self.user,1)
if self.jour_type == "Project"
update_replay_for_message(self.user,2,self.jour)
end
end
end
# Time 2015-04-01 14:15:06
# Author lizanle
# Description 删除对应课程留言的图片资源
def delete_kindeditor_assets
delete_kindeditor_assets_from_disk self.id,7
end
#课程动态公共表记录
def act_as_course_activity
2015-09-25 16:29:47 +08:00
if self.jour_type == 'Course' && self.m_parent_id.nil?
self.course_acts << CourseActivity.new(:user_id => self.user_id,:course_id => self.jour_id)
end
end
# 课程/作品回复 留言消息通知
def act_as_course_message
if self.jour_type == 'StudentWorksScore'
if self.user_id != self.jour.user_id
self.course_messages << CourseMessage.new(:user_id => self.jour.user_id,:course_id => self.jour.student_work.homework_common.course.id, :viewed => false)
end
end
# 课程留言
if self.jour_type == 'Course'
receivers = []
teachers = []
# 获取课程的老师
self.jour.members.each do |m|
if m.user.allowed_to?(:as_teacher, self.jour)
teachers << m
end
end
if self.reply_id == 0 # 主留言,即不是回复某条留言
teachers.each do |teacher|
if teacher.user_id != self.user_id
receivers << teacher.user_id
end
end
else # 留言回复
reply_to = User.find(self.reply_id)
if self.user_id != self.reply_id # 添加我回复的那个人
receivers << reply_to.id
end
# 给老师发送。 过滤条件:老师自己给自己发;回复对象为老师则排除改老师
teachers.each do |teacher|
if teacher.user_id != self.user_id && self.reply_id != teacher.user_id
receivers << teacher.user_id
end
end
end
receivers.each do |r|
self.course_messages << CourseMessage.new(:user_id => r, :course_id => self.jour.id, :viewed => false)
end
end
end
2015-12-17 15:14:52 +08:00
def act_as_at_message
users = self.notes.scan /<span class="at" data-user-id="(\d+?)">/m
users && users.flatten.uniq.each do |uid|
self.at_messages << AtMessage.new(user_id: uid, sender_id: self.user_id)
end
end
# 用户留言消息通知
def act_as_user_feedback_message
# 主留言
if self.jour_type == 'Principal'
2015-08-31 16:41:52 +08:00
receivers = []
if self.reply_id == 0
if self.user_id != self.jour_id # 过滤自己给自己的留言消息
receivers << self.jour
2015-08-20 09:25:48 +08:00
end
2015-08-31 16:41:52 +08:00
else # 留言回复
reply_to = User.find(self.reply_id)
if self.user_id != self.reply_id # 添加我回复的那个人
receivers << reply_to
end
if self.user_id != self.parent.jour_id && self.reply_id != self.parent.jour_id # 给东家发信息,如果回复的对象是东家则不发
receivers << self.parent.jour
end
end
receivers.each do |r|
self.user_feedback_messages << UserFeedbackMessage.new(:user_id => r.id, :journals_for_message_id => self.id, :journals_for_message_type => "Principal", :viewed => false)
end
2015-08-20 12:44:32 +08:00
end
end
# 课程成员得分(英雄榜)
def act_as_student_score
if !self.user.allowed_to?(:as_teacher, self.jour) && self.jour_type == "Course"
course_member_score(self.jour_id, self.user_id, "JournalForMessage")
end
end
end