2013-08-04 10:59:25 +08:00
# fq
2013-12-25 09:17:15 +08:00
# 数据库字段中带有m前缀和is_readed是二次开发添加, 之前的字段基本复用
2013-12-25 15:40:34 +08:00
# 注意reply_id 是提到人的id, 不是留言id, Base中叫做 at_user
2013-08-01 10:33:49 +08:00
class JournalsForMessage < ActiveRecord :: Base
2013-12-25 09:17:15 +08:00
include Redmine :: SafeAttributes
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)
acts_as_tree :foreign_key = > 'm_parent_id' , :counter_cache = > :m_reply_count , :order = > " #{ JournalsForMessage . table_name } .created_on ASC "
2014-07-01 09:15:53 +08:00
belongs_to :project ,
:foreign_key = > 'jour_id' ,
:conditions = > " #{ self . table_name } .jour_type = 'Project' "
2014-06-06 08:50:46 +08:00
belongs_to :course ,
:foreign_key = > 'jour_id' ,
:conditions = > " #{ self . table_name } .jour_type = 'Course' "
2014-03-19 17:32:32 +08:00
2013-08-01 10:33:49 +08:00
belongs_to :jour , :polymorphic = > true
belongs_to :user
2014-05-16 11:02:25 +08:00
belongs_to :homework_attach
2014-03-13 22:06:49 +08:00
belongs_to :at_user , :class_name = > " User " , :foreign_key = > 'reply_id'
acts_as_event :title = > Proc . new { | o | " #{ l ( :label_my_message ) } " } ,
2014-03-19 17:32:32 +08:00
:datetime = > Proc . new { | o | o . updated_on } ,
2014-03-13 22:06:49 +08:00
:author = > Proc . new { | o | o . user } ,
:description = > Proc . new { | o | o . notes } ,
2014-03-22 09:24:59 +08:00
:type = > Proc . new { | o | o . jour_type } ,
:url = > Proc . new { | o |
2014-06-27 11:24:48 +08:00
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 ,
2014-03-19 17:32:32 +08:00
:timestamp = > " #{ self . table_name } .updated_on " ,
:find_options = > { :include = > :project }
2013-12-25 09:17:15 +08:00
2014-06-13 11:49:28 +08:00
acts_as_activity_provider :type = > 'course_journals_for_messages' ,
:author_key = > :user_id ,
:timestamp = > " #{ self . table_name } .updated_on " ,
:find_options = > { :include = > :course }
2014-06-09 10:26:45 +08:00
2013-12-04 21:24:52 +08:00
has_many :acts , :class_name = > 'Activity' , :as = > :act , :dependent = > :destroy
2013-12-25 09:17:15 +08:00
validates :notes , presence : true
2013-12-04 21:24:52 +08:00
after_create :act_as_activity #huang
2014-06-17 15:57:31 +08:00
after_create :reset_counters! , :be_user_score
2013-12-25 09:17:15 +08:00
after_destroy :reset_counters!
2014-06-17 15:57:31 +08:00
#before_save :be_user_score
2014-06-11 17:41:47 +08:00
#before_destroy :down_user_score
2013-12-26 08:39:43 +08:00
2013-12-26 08:43:16 +08:00
# default_scope { where('m_parent_id IS NULL') }
2013-12-30 20:16:56 +08:00
2014-01-06 21:11:20 +08:00
def self . create_by_user? user
if user . anonymous?
return false
else
return true
end
end
def self . remove_by_user? user
2013-12-30 20:16:56 +08:00
if ( self . user == user ||
( self . jour . kind_of? ( User ) && self . jour == user )
)
true
else
false
end
end
2013-08-01 10:33:49 +08:00
def self . delete_message ( message_id )
2013-12-26 19:28:18 +08:00
self . find ( message_id ) . destroy
# self.destroy_all "id = #{message_id}"
2013-08-01 10:33:49 +08:00
end
2013-08-06 22:26:52 +08:00
def reference_user
User . find ( reply_id )
end
def self . reference_message ( user_id )
@user = User . find ( user_id )
2014-05-17 15:39:18 +08:00
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}))")
2013-08-06 22:26:52 +08:00
message
end
2013-12-04 21:24:52 +08:00
def act_as_activity
2013-12-09 16:01:50 +08:00
if self . jour_type == 'Principal'
2013-12-24 16:42:56 +08:00
unless self . user_id == self . jour . id && self . user_id != self . reply_id && self . reply_id != 0
2014-03-07 16:49:45 +08:00
# 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 )
2014-06-06 08:50:46 +08:00
elsif self . jour_type == 'Course'
self . acts << Activity . new ( :user_id = > self . reply_id )
2014-03-07 16:49:45 +08:00
else
2013-12-04 21:24:52 +08:00
end
end
2013-12-25 09:17:15 +08:00
def reset_counters!
2013-12-25 15:40:34 +08:00
self . class . reset_counters! ( self )
2013-12-25 09:17:15 +08:00
end
2013-12-25 15:40:34 +08:00
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 ] )
2013-12-25 09:17:15 +08:00
end
2014-06-10 17:47:10 +08:00
# 更新用户分数 -by zjc
def be_user_score
2014-06-11 17:41:47 +08:00
#新建了留言回复
2014-06-17 15:57:31 +08:00
if self . reply_id != 0
2014-06-10 17:47:10 +08:00
#协同得分加分
2014-06-17 15:57:31 +08:00
UserScore . joint ( :reply_message , User . current , User . find ( self . reply_id ) , self , { journals_for_messages_id : self . id } )
2014-06-10 17:47:10 +08:00
end
end
2014-06-11 17:41:47 +08:00
# 更新用户分数 -by zjc
def down_user_score
#删除了留言回复
if self . reply_id != 0
#协同得分减分
UserScore . joint ( :reply_message_delete , User . current , User . find ( self . reply_id ) , { journals_for_messages_id : self . id } )
end
end
2013-08-01 10:33:49 +08:00
end