@@ -1,160 +1,164 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class TimeEntry < ActiveRecord::Base |
|
18 | class TimeEntry < ActiveRecord::Base | |
19 | include Redmine::SafeAttributes |
|
19 | include Redmine::SafeAttributes | |
20 | # could have used polymorphic association |
|
20 | # could have used polymorphic association | |
21 | # project association here allows easy loading of time entries at project level with one database trip |
|
21 | # project association here allows easy loading of time entries at project level with one database trip | |
22 | belongs_to :project |
|
22 | belongs_to :project | |
23 | belongs_to :issue |
|
23 | belongs_to :issue | |
24 | belongs_to :user |
|
24 | belongs_to :user | |
25 | belongs_to :activity, :class_name => 'TimeEntryActivity' |
|
25 | belongs_to :activity, :class_name => 'TimeEntryActivity' | |
26 |
|
26 | |||
27 | attr_protected :user_id, :tyear, :tmonth, :tweek |
|
27 | attr_protected :user_id, :tyear, :tmonth, :tweek | |
28 |
|
28 | |||
29 | acts_as_customizable |
|
29 | acts_as_customizable | |
30 | acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"}, |
|
30 | acts_as_event :title => Proc.new { |o| | |
|
31 | related = o.issue if o.issue && o.issue.visible? | |||
|
32 | related ||= o.project | |||
|
33 | "#{l_hours(o.hours)} (#{related.event_title})" | |||
|
34 | }, | |||
31 | :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}}, |
|
35 | :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}}, | |
32 | :author => :user, |
|
36 | :author => :user, | |
33 | :group => :issue, |
|
37 | :group => :issue, | |
34 | :description => :comments |
|
38 | :description => :comments | |
35 |
|
39 | |||
36 | acts_as_activity_provider :timestamp => "#{table_name}.created_on", |
|
40 | acts_as_activity_provider :timestamp => "#{table_name}.created_on", | |
37 | :author_key => :user_id, |
|
41 | :author_key => :user_id, | |
38 | :scope => joins(:project).preload(:project) |
|
42 | :scope => joins(:project).preload(:project) | |
39 |
|
43 | |||
40 | validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on |
|
44 | validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on | |
41 | validates_numericality_of :hours, :allow_nil => true, :message => :invalid |
|
45 | validates_numericality_of :hours, :allow_nil => true, :message => :invalid | |
42 | validates_length_of :comments, :maximum => 1024, :allow_nil => true |
|
46 | validates_length_of :comments, :maximum => 1024, :allow_nil => true | |
43 | validates :spent_on, :date => true |
|
47 | validates :spent_on, :date => true | |
44 | before_validation :set_project_if_nil |
|
48 | before_validation :set_project_if_nil | |
45 | validate :validate_time_entry |
|
49 | validate :validate_time_entry | |
46 |
|
50 | |||
47 | scope :visible, lambda {|*args| |
|
51 | scope :visible, lambda {|*args| | |
48 | joins(:project). |
|
52 | joins(:project). | |
49 | where(TimeEntry.visible_condition(args.shift || User.current, *args)) |
|
53 | where(TimeEntry.visible_condition(args.shift || User.current, *args)) | |
50 | } |
|
54 | } | |
51 | scope :on_issue, lambda {|issue| |
|
55 | scope :on_issue, lambda {|issue| | |
52 | joins(:issue). |
|
56 | joins(:issue). | |
53 | where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}") |
|
57 | where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}") | |
54 | } |
|
58 | } | |
55 |
|
59 | |||
56 | safe_attributes 'hours', 'comments', 'project_id', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields' |
|
60 | safe_attributes 'hours', 'comments', 'project_id', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields' | |
57 |
|
61 | |||
58 | # Returns a SQL conditions string used to find all time entries visible by the specified user |
|
62 | # Returns a SQL conditions string used to find all time entries visible by the specified user | |
59 | def self.visible_condition(user, options={}) |
|
63 | def self.visible_condition(user, options={}) | |
60 | Project.allowed_to_condition(user, :view_time_entries, options) do |role, user| |
|
64 | Project.allowed_to_condition(user, :view_time_entries, options) do |role, user| | |
61 | if role.time_entries_visibility == 'all' |
|
65 | if role.time_entries_visibility == 'all' | |
62 | nil |
|
66 | nil | |
63 | elsif role.time_entries_visibility == 'own' && user.id && user.logged? |
|
67 | elsif role.time_entries_visibility == 'own' && user.id && user.logged? | |
64 | "#{table_name}.user_id = #{user.id}" |
|
68 | "#{table_name}.user_id = #{user.id}" | |
65 | else |
|
69 | else | |
66 | '1=0' |
|
70 | '1=0' | |
67 | end |
|
71 | end | |
68 | end |
|
72 | end | |
69 | end |
|
73 | end | |
70 |
|
74 | |||
71 | # Returns true if user or current user is allowed to view the time entry |
|
75 | # Returns true if user or current user is allowed to view the time entry | |
72 | def visible?(user=nil) |
|
76 | def visible?(user=nil) | |
73 | (user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user| |
|
77 | (user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user| | |
74 | if role.time_entries_visibility == 'all' |
|
78 | if role.time_entries_visibility == 'all' | |
75 | true |
|
79 | true | |
76 | elsif role.time_entries_visibility == 'own' |
|
80 | elsif role.time_entries_visibility == 'own' | |
77 | self.user == user |
|
81 | self.user == user | |
78 | else |
|
82 | else | |
79 | false |
|
83 | false | |
80 | end |
|
84 | end | |
81 | end |
|
85 | end | |
82 | end |
|
86 | end | |
83 |
|
87 | |||
84 | def initialize(attributes=nil, *args) |
|
88 | def initialize(attributes=nil, *args) | |
85 | super |
|
89 | super | |
86 | if new_record? && self.activity.nil? |
|
90 | if new_record? && self.activity.nil? | |
87 | if default_activity = TimeEntryActivity.default |
|
91 | if default_activity = TimeEntryActivity.default | |
88 | self.activity_id = default_activity.id |
|
92 | self.activity_id = default_activity.id | |
89 | end |
|
93 | end | |
90 | self.hours = nil if hours == 0 |
|
94 | self.hours = nil if hours == 0 | |
91 | end |
|
95 | end | |
92 | end |
|
96 | end | |
93 |
|
97 | |||
94 | def safe_attributes=(attrs, user=User.current) |
|
98 | def safe_attributes=(attrs, user=User.current) | |
95 | if attrs |
|
99 | if attrs | |
96 | attrs = super(attrs) |
|
100 | attrs = super(attrs) | |
97 | if issue_id_changed? && issue |
|
101 | if issue_id_changed? && issue | |
98 | if issue.visible?(user) && user.allowed_to?(:log_time, issue.project) |
|
102 | if issue.visible?(user) && user.allowed_to?(:log_time, issue.project) | |
99 | if attrs[:project_id].blank? && issue.project_id != project_id |
|
103 | if attrs[:project_id].blank? && issue.project_id != project_id | |
100 | self.project_id = issue.project_id |
|
104 | self.project_id = issue.project_id | |
101 | end |
|
105 | end | |
102 | @invalid_issue_id = nil |
|
106 | @invalid_issue_id = nil | |
103 | else |
|
107 | else | |
104 | @invalid_issue_id = issue_id |
|
108 | @invalid_issue_id = issue_id | |
105 | end |
|
109 | end | |
106 | end |
|
110 | end | |
107 | end |
|
111 | end | |
108 | attrs |
|
112 | attrs | |
109 | end |
|
113 | end | |
110 |
|
114 | |||
111 | def set_project_if_nil |
|
115 | def set_project_if_nil | |
112 | self.project = issue.project if issue && project.nil? |
|
116 | self.project = issue.project if issue && project.nil? | |
113 | end |
|
117 | end | |
114 |
|
118 | |||
115 | def validate_time_entry |
|
119 | def validate_time_entry | |
116 | errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000) |
|
120 | errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000) | |
117 | errors.add :project_id, :invalid if project.nil? |
|
121 | errors.add :project_id, :invalid if project.nil? | |
118 | errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id |
|
122 | errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id | |
119 | errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity) |
|
123 | errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity) | |
120 | end |
|
124 | end | |
121 |
|
125 | |||
122 | def hours=(h) |
|
126 | def hours=(h) | |
123 | write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h) |
|
127 | write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h) | |
124 | end |
|
128 | end | |
125 |
|
129 | |||
126 | def hours |
|
130 | def hours | |
127 | h = read_attribute(:hours) |
|
131 | h = read_attribute(:hours) | |
128 | if h.is_a?(Float) |
|
132 | if h.is_a?(Float) | |
129 | h.round(2) |
|
133 | h.round(2) | |
130 | else |
|
134 | else | |
131 | h |
|
135 | h | |
132 | end |
|
136 | end | |
133 | end |
|
137 | end | |
134 |
|
138 | |||
135 | # tyear, tmonth, tweek assigned where setting spent_on attributes |
|
139 | # tyear, tmonth, tweek assigned where setting spent_on attributes | |
136 | # these attributes make time aggregations easier |
|
140 | # these attributes make time aggregations easier | |
137 | def spent_on=(date) |
|
141 | def spent_on=(date) | |
138 | super |
|
142 | super | |
139 | self.tyear = spent_on ? spent_on.year : nil |
|
143 | self.tyear = spent_on ? spent_on.year : nil | |
140 | self.tmonth = spent_on ? spent_on.month : nil |
|
144 | self.tmonth = spent_on ? spent_on.month : nil | |
141 | self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil |
|
145 | self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil | |
142 | end |
|
146 | end | |
143 |
|
147 | |||
144 | # Returns true if the time entry can be edited by usr, otherwise false |
|
148 | # Returns true if the time entry can be edited by usr, otherwise false | |
145 | def editable_by?(usr) |
|
149 | def editable_by?(usr) | |
146 | visible?(usr) && ( |
|
150 | visible?(usr) && ( | |
147 | (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project) |
|
151 | (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project) | |
148 | ) |
|
152 | ) | |
149 | end |
|
153 | end | |
150 |
|
154 | |||
151 | # Returns the custom_field_values that can be edited by the given user |
|
155 | # Returns the custom_field_values that can be edited by the given user | |
152 | def editable_custom_field_values(user=nil) |
|
156 | def editable_custom_field_values(user=nil) | |
153 | visible_custom_field_values |
|
157 | visible_custom_field_values | |
154 | end |
|
158 | end | |
155 |
|
159 | |||
156 | # Returns the custom fields that can be edited by the given user |
|
160 | # Returns the custom fields that can be edited by the given user | |
157 | def editable_custom_fields(user=nil) |
|
161 | def editable_custom_fields(user=nil) | |
158 | editable_custom_field_values(user).map(&:custom_field).uniq |
|
162 | editable_custom_field_values(user).map(&:custom_field).uniq | |
159 | end |
|
163 | end | |
160 | end |
|
164 | end |
General Comments 0
You need to be logged in to leave comments.
Login now