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
belongs_to :journalized , :polymorphic = > true
# added as a quick fix to allow eager loading of the polymorphic association
# since always associated to an issue, for now
belongs_to :issue , :foreign_key = > :journalized_id
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
# end
2013-08-01 10:33:49 +08:00
attr_accessor :indice
2014-05-15 16:59:15 +08:00
acts_as_event :title = > Proc . new { | o | status = ( ( s = o . new_status ) ? " ( #{ s } ) " : nil ) ; " #{ o . issue . tracker } # #{ o . issue . inProjectIndex } #{ status } : #{ o . issue . subject } " } ,
: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 ] ,
:conditions = > " #{ Journal . table_name } .journalized_type = 'Issue' AND " +
" ( #{ JournalDetail . table_name } .prop_key = 'status_id' OR #{ Journal . table_name } .notes <> '') " }
before_create :split_private_notes
2013-08-12 15:37:42 +08:00
# fq
2013-08-12 17:30:31 +08:00
after_create :act_as_activity
2013-08-12 15:37:42 +08:00
# end
2013-08-01 10:33:49 +08:00
2014-06-11 17:41:47 +08:00
before_save :be_user_score
#before_destroy :down_user_score
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
# 更新用户分数 -by zjc
def be_user_score
2014-06-11 17:41:47 +08:00
#新建了缺陷留言且留言不为空,不为空白
2014-06-10 17:47:10 +08:00
if self . new_record? && ! self . notes . nil? && self . notes . gsub ( ' ' , '' ) != ''
#协同得分加分
UserScore . joint ( :post_issue , User . current , self . issue . author , { message_id : self . id } )
end
end
2014-06-11 17:41:47 +08:00
# 减少用户分数 -by zjc
def down_user_score
#删除有效缺陷留言
if ! self . notes . nil? && self . notes . gsub ( ' ' , '' ) != ''
#协同得分减分
UserScore . joint ( :delete_issue , User . current , self . issue . author , { message_id : self . id } )
end
end
2013-08-01 10:33:49 +08:00
end