2013-08-01 10:33:49 +08:00
# Redmine - project management software
# Copyright (C) 2006-2013 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class Journal < ActiveRecord :: Base
2014-07-24 18:05:37 +08:00
include UserScoreHelper
2015-12-31 15:17:48 +08:00
belongs_to :journalized , :polymorphic = > true , :touch = > true
2013-08-01 10:33:49 +08:00
# added as a quick fix to allow eager loading of the polymorphic association
# since always associated to an issue, for now
2015-12-31 15:17:48 +08:00
belongs_to :issue , :foreign_key = > :journalized_id , :touch = > true
2013-08-01 10:33:49 +08:00
belongs_to :user
has_many :details , :class_name = > " JournalDetail " , :dependent = > :delete_all
2013-08-11 10:36:55 +08:00
# added by fq
has_one :journal_reply
2013-08-12 15:37:42 +08:00
has_many :acts , :class_name = > 'Activity' , :as = > :act , :dependent = > :destroy
2015-03-04 15:32:00 +08:00
# 被ForgeActivity虚拟关联
2015-12-18 10:19:18 +08:00
#has_many :forge_acts, :class_name => 'ForgeActivity',:as =>:forge_act ,:dependent => :destroy 评论不应该算入
2015-08-13 16:55:25 +08:00
# 被ForgeMessage虚拟关联
has_many :forge_messages , :class_name = > 'ForgeMessage' , :as = > :forge_message , :dependent = > :destroy
2015-12-17 15:14:52 +08:00
has_many :at_messages , as : :at_message , dependent : :destroy
2015-12-31 17:08:27 +08:00
acts_as_attachable
2013-08-01 10:33:49 +08:00
attr_accessor :indice
2014-07-07 13:41:44 +08:00
acts_as_event :title = > Proc . new { | o | status = ( ( s = o . new_status ) ? " ( #{ s } ) " : nil ) ; " #{ o . issue . tracker } # #{ o . issue . project_index } #{ status } : #{ o . issue . subject } " } ,
2014-05-15 16:59:15 +08:00
:description = > :notes ,
2013-08-01 10:33:49 +08:00
:author = > :user ,
:group = > :issue ,
:type = > Proc . new { | o | ( s = o . new_status ) ? ( s . is_closed? ? 'issue-closed' : 'issue-edit' ) : 'issue-note' } ,
:url = > Proc . new { | o | { :controller = > 'issues' , :action = > 'show' , :id = > o . issue . id , :anchor = > " change- #{ o . id } " } }
acts_as_activity_provider :type = > 'issues' ,
:author_key = > :user_id ,
:find_options = > { :include = > [ { :issue = > :project } , :details , :user ] ,
2015-12-31 17:08:27 +08:00
:conditions = > " #{ Journal . table_name } .journalized_type = 'Issue' AND " +
" ( #{ JournalDetail . table_name } .prop_key = 'status_id' OR #{ Journal . table_name } .notes <> '') " }
2013-08-01 10:33:49 +08:00
2016-01-13 12:53:09 +08:00
before_create :split_private_notes , :add_journals_count
2013-08-12 15:37:42 +08:00
# fq
2016-03-11 18:50:37 +08:00
after_save :act_as_activity , :be_user_score , :act_as_forge_message , act_as_at_message ( :notes , :user_id )
2016-02-22 16:56:37 +08:00
after_create :update_issue_time
2013-08-12 15:37:42 +08:00
# end
2014-07-29 18:04:35 +08:00
#after_destroy :down_user_score
2014-06-17 15:57:31 +08:00
#before_save :be_user_score
2016-01-13 12:53:09 +08:00
after_destroy :down_user_score , :decrease_issues_journal_count
2014-06-11 17:41:47 +08:00
2013-08-01 10:33:49 +08:00
scope :visible , lambda { | * args |
user = args . shift || User . current
includes ( :issue = > :project ) .
where ( Issue . visible_condition ( user , * args ) ) .
where ( " ( #{ Journal . table_name } .private_notes = ? OR ( #{ Project . allowed_to_condition ( user , :view_private_notes , * args ) } )) " , false )
}
def save ( * args )
# Do not save an empty journal
( details . empty? && notes . blank? ) ? false : super
end
# Returns the new status if the journal contains a status change, otherwise nil
def new_status
c = details . detect { | detail | detail . prop_key == 'status_id' }
( c && c . value ) ? IssueStatus . find_by_id ( c . value . to_i ) : nil
end
def new_value_for ( prop )
c = details . detect { | detail | detail . prop_key == prop }
c ? c . value : nil
end
def editable_by? ( usr )
usr && usr . logged? && ( usr . allowed_to? ( :edit_issue_notes , project ) || ( self . user == usr && usr . allowed_to? ( :edit_own_issue_notes , project ) ) )
end
def project
journalized . respond_to? ( :project ) ? journalized . project : nil
end
def attachments
journalized . respond_to? ( :attachments ) ? journalized . attachments : nil
end
# Returns a string of css classes
def css_classes
s = 'journal'
s << ' has-notes' unless notes . blank?
s << ' has-details' unless details . blank?
s << ' private-notes' if private_notes?
s
end
def notify?
@notify != false
end
def notify = ( arg )
@notify = arg
end
def recipients
notified = journalized . notified_users
if private_notes?
notified = notified . select { | user | user . allowed_to? ( :view_private_notes , journalized . project ) }
end
notified . map ( & :mail )
end
def watcher_recipients
notified = journalized . notified_watchers
if private_notes?
notified = notified . select { | user | user . allowed_to? ( :view_private_notes , journalized . project ) }
end
notified . map ( & :mail )
end
2013-08-11 10:36:55 +08:00
## added_by_fq
def self . reference_message ( user_id )
message = Journal . find_by_sql ( " select id, journalized_id, user_id, notes, created_on from journals where id in (select journal_id from journal_replies where reply_id = #{ user_id } ) " )
message += Journal . find_by_sql ( " select id, journalized_id, user_id, notes, created_on from journals where journalized_id in (select id from issues where assigned_to_id = #{ user_id } ) " )
message
end
##
2013-08-01 10:33:49 +08:00
private
def split_private_notes
if private_notes?
if notes . present?
if details . any?
# Split the journal (notes/changes) so we don't have half-private journals
journal = Journal . new ( :journalized = > journalized , :user = > user , :notes = > nil , :private_notes = > false )
journal . details = details
journal . save
self . details = [ ]
self . created_on = journal . created_on
end
else
# Blank notes should not be private
self . private_notes = false
end
end
true
end
2013-08-12 15:37:42 +08:00
# fq
def act_as_activity
self . acts << Activity . new ( :user_id = > self . user_id )
end
# end
2014-06-10 17:47:10 +08:00
2015-12-18 10:19:18 +08:00
# # Time 2015-02-27 13:30:19
# # Author lizanle
# # Description 公共表中需要保存一份该记录
# def act_as_forge_activity
# self.forge_acts << ForgeActivity.new(:user_id => self.user_id,
# :project_id => self.issue.project.id)
#
# end
2015-03-04 15:32:00 +08:00
2015-08-13 16:55:25 +08:00
# 缺陷状态更改,消息提醒
def act_as_forge_message
2015-09-21 17:51:00 +08:00
receivers = [ ]
# 直接回复
2015-08-21 15:27:01 +08:00
if self . user_id != self . issue . author_id
2015-09-21 17:51:00 +08:00
receivers << self . issue . author_id
2015-08-25 19:30:22 +08:00
end
2015-08-28 14:38:46 +08:00
if self . user_id != self . issue . assigned_to_id && self . issue . assigned_to_id != self . issue . author_id # 指派人不是自己的话,则给指派人发送
2015-09-21 17:51:00 +08:00
receivers << self . issue . assigned_to_id
end
receivers . each do | r |
self . forge_messages << ForgeMessage . new ( :user_id = > r , :project_id = > self . issue . project_id , :viewed = > false )
2015-08-21 15:27:01 +08:00
end
2015-08-13 16:55:25 +08:00
end
2014-06-10 17:47:10 +08:00
# 更新用户分数 -by zjc
def be_user_score
2014-07-29 18:04:35 +08:00
#新建了缺陷留言且留言不为空,不为空白
if ! self . notes . nil? && self . notes . gsub ( ' ' , '' ) != ''
2014-06-10 17:47:10 +08:00
#协同得分加分
2014-07-29 18:04:35 +08:00
UserScore . joint ( :post_issue_message , self . user , self . issue . author , self , { message_id : self . id } )
2014-08-13 11:17:22 +08:00
update_messges_for_issue ( self . user , 1 )
2014-08-18 17:14:31 +08:00
update_messges_for_issue ( self . user , 2 , self . issue . project )
2014-07-29 18:04:35 +08:00
end
2014-08-13 11:17:22 +08:00
2014-06-10 17:47:10 +08:00
end
2014-06-11 17:41:47 +08:00
# 减少用户分数 -by zjc
def down_user_score
#删除有效缺陷留言
2014-07-29 18:04:35 +08:00
if ! self . notes . nil? && self . notes . gsub ( ' ' , '' ) != ''
#协同得分减分
UserScore . joint ( :delete_issue_message , self . user , self . issue . author , self , { message_id : self . id } )
2014-08-13 11:17:22 +08:00
update_messges_for_issue ( self . user , 1 )
2014-08-18 17:14:31 +08:00
update_messges_for_issue ( self . user , 2 , self . issue . project )
2014-07-29 18:04:35 +08:00
end
2014-08-13 11:17:22 +08:00
2014-06-11 17:41:47 +08:00
end
2016-01-13 12:53:09 +08:00
# 减少留言数量统计
def decrease_issues_journal_count
unless self . issue . project . nil?
2016-02-23 13:24:20 +08:00
journal_count = self . issue . project . project_score . issue_journal_num - 1
# project = self.issue.project
self . issue . project . project_score . update_attribute ( :issue_journal_num , journal_count < 0 ? 0 : journal_count )
2016-01-13 12:53:09 +08:00
end
end
# issue留言总数更新
def add_journals_count
2016-01-14 15:52:14 +08:00
if ! self . issue . project . nil? && self . journalized_type == " Issue " && ! self . issue . project . project_score . nil?
2016-01-13 12:53:09 +08:00
project = self . issue . project
2016-01-14 15:52:14 +08:00
project . project_score . update_attribute ( :issue_journal_num , project . project_score . issue_journal_num + 1 )
2016-01-13 12:53:09 +08:00
end
end
2016-02-22 16:56:37 +08:00
# 回复issue的时候, 更新issue的时候
def update_issue_time
if self . journalized_type == " Issue "
forge_activity = ForgeActivity . where ( " forge_act_id =? and forge_act_type =? " , self . issue , " Issue " ) . first
forge_activity . update_attribute ( :created_at , self . created_on ) unless forge_activity . nil?
end
end
2013-08-01 10:33:49 +08:00
end