@@ -1,290 +1,291 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2015 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 Version < ActiveRecord::Base |
|
18 | class Version < ActiveRecord::Base | |
19 | include Redmine::SafeAttributes |
|
19 | include Redmine::SafeAttributes | |
20 | after_update :update_issues_from_sharing_change |
|
20 | after_update :update_issues_from_sharing_change | |
21 | belongs_to :project |
|
21 | belongs_to :project | |
22 | has_many :fixed_issues, :class_name => 'Issue', :foreign_key => 'fixed_version_id', :dependent => :nullify |
|
22 | has_many :fixed_issues, :class_name => 'Issue', :foreign_key => 'fixed_version_id', :dependent => :nullify | |
23 | acts_as_customizable |
|
23 | acts_as_customizable | |
24 | acts_as_attachable :view_permission => :view_files, |
|
24 | acts_as_attachable :view_permission => :view_files, | |
25 | :edit_permission => :manage_files, |
|
25 | :edit_permission => :manage_files, | |
26 | :delete_permission => :manage_files |
|
26 | :delete_permission => :manage_files | |
27 |
|
27 | |||
28 | VERSION_STATUSES = %w(open locked closed) |
|
28 | VERSION_STATUSES = %w(open locked closed) | |
29 | VERSION_SHARINGS = %w(none descendants hierarchy tree system) |
|
29 | VERSION_SHARINGS = %w(none descendants hierarchy tree system) | |
30 |
|
30 | |||
31 | validates_presence_of :name |
|
31 | validates_presence_of :name | |
32 | validates_uniqueness_of :name, :scope => [:project_id] |
|
32 | validates_uniqueness_of :name, :scope => [:project_id] | |
33 | validates_length_of :name, :maximum => 60 |
|
33 | validates_length_of :name, :maximum => 60 | |
|
34 | validates_length_of :description, :maximum => 255 | |||
34 | validates :effective_date, :date => true |
|
35 | validates :effective_date, :date => true | |
35 | validates_inclusion_of :status, :in => VERSION_STATUSES |
|
36 | validates_inclusion_of :status, :in => VERSION_STATUSES | |
36 | validates_inclusion_of :sharing, :in => VERSION_SHARINGS |
|
37 | validates_inclusion_of :sharing, :in => VERSION_SHARINGS | |
37 | attr_protected :id |
|
38 | attr_protected :id | |
38 |
|
39 | |||
39 | scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)} |
|
40 | scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)} | |
40 | scope :open, lambda { where(:status => 'open') } |
|
41 | scope :open, lambda { where(:status => 'open') } | |
41 | scope :visible, lambda {|*args| |
|
42 | scope :visible, lambda {|*args| | |
42 | joins(:project). |
|
43 | joins(:project). | |
43 | where(Project.allowed_to_condition(args.first || User.current, :view_issues)) |
|
44 | where(Project.allowed_to_condition(args.first || User.current, :view_issues)) | |
44 | } |
|
45 | } | |
45 |
|
46 | |||
46 | safe_attributes 'name', |
|
47 | safe_attributes 'name', | |
47 | 'description', |
|
48 | 'description', | |
48 | 'effective_date', |
|
49 | 'effective_date', | |
49 | 'due_date', |
|
50 | 'due_date', | |
50 | 'wiki_page_title', |
|
51 | 'wiki_page_title', | |
51 | 'status', |
|
52 | 'status', | |
52 | 'sharing', |
|
53 | 'sharing', | |
53 | 'custom_field_values', |
|
54 | 'custom_field_values', | |
54 | 'custom_fields' |
|
55 | 'custom_fields' | |
55 |
|
56 | |||
56 | # Returns true if +user+ or current user is allowed to view the version |
|
57 | # Returns true if +user+ or current user is allowed to view the version | |
57 | def visible?(user=User.current) |
|
58 | def visible?(user=User.current) | |
58 | user.allowed_to?(:view_issues, self.project) |
|
59 | user.allowed_to?(:view_issues, self.project) | |
59 | end |
|
60 | end | |
60 |
|
61 | |||
61 | # Version files have same visibility as project files |
|
62 | # Version files have same visibility as project files | |
62 | def attachments_visible?(*args) |
|
63 | def attachments_visible?(*args) | |
63 | project.present? && project.attachments_visible?(*args) |
|
64 | project.present? && project.attachments_visible?(*args) | |
64 | end |
|
65 | end | |
65 |
|
66 | |||
66 | def attachments_deletable?(usr=User.current) |
|
67 | def attachments_deletable?(usr=User.current) | |
67 | project.present? && project.attachments_deletable?(usr) |
|
68 | project.present? && project.attachments_deletable?(usr) | |
68 | end |
|
69 | end | |
69 |
|
70 | |||
70 | def start_date |
|
71 | def start_date | |
71 | @start_date ||= fixed_issues.minimum('start_date') |
|
72 | @start_date ||= fixed_issues.minimum('start_date') | |
72 | end |
|
73 | end | |
73 |
|
74 | |||
74 | def due_date |
|
75 | def due_date | |
75 | effective_date |
|
76 | effective_date | |
76 | end |
|
77 | end | |
77 |
|
78 | |||
78 | def due_date=(arg) |
|
79 | def due_date=(arg) | |
79 | self.effective_date=(arg) |
|
80 | self.effective_date=(arg) | |
80 | end |
|
81 | end | |
81 |
|
82 | |||
82 | # Returns the total estimated time for this version |
|
83 | # Returns the total estimated time for this version | |
83 | # (sum of leaves estimated_hours) |
|
84 | # (sum of leaves estimated_hours) | |
84 | def estimated_hours |
|
85 | def estimated_hours | |
85 | @estimated_hours ||= fixed_issues.leaves.sum(:estimated_hours).to_f |
|
86 | @estimated_hours ||= fixed_issues.leaves.sum(:estimated_hours).to_f | |
86 | end |
|
87 | end | |
87 |
|
88 | |||
88 | # Returns the total reported time for this version |
|
89 | # Returns the total reported time for this version | |
89 | def spent_hours |
|
90 | def spent_hours | |
90 | @spent_hours ||= TimeEntry.joins(:issue).where("#{Issue.table_name}.fixed_version_id = ?", id).sum(:hours).to_f |
|
91 | @spent_hours ||= TimeEntry.joins(:issue).where("#{Issue.table_name}.fixed_version_id = ?", id).sum(:hours).to_f | |
91 | end |
|
92 | end | |
92 |
|
93 | |||
93 | def closed? |
|
94 | def closed? | |
94 | status == 'closed' |
|
95 | status == 'closed' | |
95 | end |
|
96 | end | |
96 |
|
97 | |||
97 | def open? |
|
98 | def open? | |
98 | status == 'open' |
|
99 | status == 'open' | |
99 | end |
|
100 | end | |
100 |
|
101 | |||
101 | # Returns true if the version is completed: due date reached and no open issues |
|
102 | # Returns true if the version is completed: due date reached and no open issues | |
102 | def completed? |
|
103 | def completed? | |
103 | effective_date && (effective_date < Date.today) && (open_issues_count == 0) |
|
104 | effective_date && (effective_date < Date.today) && (open_issues_count == 0) | |
104 | end |
|
105 | end | |
105 |
|
106 | |||
106 | def behind_schedule? |
|
107 | def behind_schedule? | |
107 | if completed_percent == 100 |
|
108 | if completed_percent == 100 | |
108 | return false |
|
109 | return false | |
109 | elsif due_date && start_date |
|
110 | elsif due_date && start_date | |
110 | done_date = start_date + ((due_date - start_date+1)* completed_percent/100).floor |
|
111 | done_date = start_date + ((due_date - start_date+1)* completed_percent/100).floor | |
111 | return done_date <= Date.today |
|
112 | return done_date <= Date.today | |
112 | else |
|
113 | else | |
113 | false # No issues so it's not late |
|
114 | false # No issues so it's not late | |
114 | end |
|
115 | end | |
115 | end |
|
116 | end | |
116 |
|
117 | |||
117 | # Returns the completion percentage of this version based on the amount of open/closed issues |
|
118 | # Returns the completion percentage of this version based on the amount of open/closed issues | |
118 | # and the time spent on the open issues. |
|
119 | # and the time spent on the open issues. | |
119 | def completed_percent |
|
120 | def completed_percent | |
120 | if issues_count == 0 |
|
121 | if issues_count == 0 | |
121 | 0 |
|
122 | 0 | |
122 | elsif open_issues_count == 0 |
|
123 | elsif open_issues_count == 0 | |
123 | 100 |
|
124 | 100 | |
124 | else |
|
125 | else | |
125 | issues_progress(false) + issues_progress(true) |
|
126 | issues_progress(false) + issues_progress(true) | |
126 | end |
|
127 | end | |
127 | end |
|
128 | end | |
128 |
|
129 | |||
129 | # Returns the percentage of issues that have been marked as 'closed'. |
|
130 | # Returns the percentage of issues that have been marked as 'closed'. | |
130 | def closed_percent |
|
131 | def closed_percent | |
131 | if issues_count == 0 |
|
132 | if issues_count == 0 | |
132 | 0 |
|
133 | 0 | |
133 | else |
|
134 | else | |
134 | issues_progress(false) |
|
135 | issues_progress(false) | |
135 | end |
|
136 | end | |
136 | end |
|
137 | end | |
137 |
|
138 | |||
138 | # Returns true if the version is overdue: due date reached and some open issues |
|
139 | # Returns true if the version is overdue: due date reached and some open issues | |
139 | def overdue? |
|
140 | def overdue? | |
140 | effective_date && (effective_date < Date.today) && (open_issues_count > 0) |
|
141 | effective_date && (effective_date < Date.today) && (open_issues_count > 0) | |
141 | end |
|
142 | end | |
142 |
|
143 | |||
143 | # Returns assigned issues count |
|
144 | # Returns assigned issues count | |
144 | def issues_count |
|
145 | def issues_count | |
145 | load_issue_counts |
|
146 | load_issue_counts | |
146 | @issue_count |
|
147 | @issue_count | |
147 | end |
|
148 | end | |
148 |
|
149 | |||
149 | # Returns the total amount of open issues for this version. |
|
150 | # Returns the total amount of open issues for this version. | |
150 | def open_issues_count |
|
151 | def open_issues_count | |
151 | load_issue_counts |
|
152 | load_issue_counts | |
152 | @open_issues_count |
|
153 | @open_issues_count | |
153 | end |
|
154 | end | |
154 |
|
155 | |||
155 | # Returns the total amount of closed issues for this version. |
|
156 | # Returns the total amount of closed issues for this version. | |
156 | def closed_issues_count |
|
157 | def closed_issues_count | |
157 | load_issue_counts |
|
158 | load_issue_counts | |
158 | @closed_issues_count |
|
159 | @closed_issues_count | |
159 | end |
|
160 | end | |
160 |
|
161 | |||
161 | def wiki_page |
|
162 | def wiki_page | |
162 | if project.wiki && !wiki_page_title.blank? |
|
163 | if project.wiki && !wiki_page_title.blank? | |
163 | @wiki_page ||= project.wiki.find_page(wiki_page_title) |
|
164 | @wiki_page ||= project.wiki.find_page(wiki_page_title) | |
164 | end |
|
165 | end | |
165 | @wiki_page |
|
166 | @wiki_page | |
166 | end |
|
167 | end | |
167 |
|
168 | |||
168 | def to_s; name end |
|
169 | def to_s; name end | |
169 |
|
170 | |||
170 | def to_s_with_project |
|
171 | def to_s_with_project | |
171 | "#{project} - #{name}" |
|
172 | "#{project} - #{name}" | |
172 | end |
|
173 | end | |
173 |
|
174 | |||
174 | # Versions are sorted by effective_date and name |
|
175 | # Versions are sorted by effective_date and name | |
175 | # Those with no effective_date are at the end, sorted by name |
|
176 | # Those with no effective_date are at the end, sorted by name | |
176 | def <=>(version) |
|
177 | def <=>(version) | |
177 | if self.effective_date |
|
178 | if self.effective_date | |
178 | if version.effective_date |
|
179 | if version.effective_date | |
179 | if self.effective_date == version.effective_date |
|
180 | if self.effective_date == version.effective_date | |
180 | name == version.name ? id <=> version.id : name <=> version.name |
|
181 | name == version.name ? id <=> version.id : name <=> version.name | |
181 | else |
|
182 | else | |
182 | self.effective_date <=> version.effective_date |
|
183 | self.effective_date <=> version.effective_date | |
183 | end |
|
184 | end | |
184 | else |
|
185 | else | |
185 | -1 |
|
186 | -1 | |
186 | end |
|
187 | end | |
187 | else |
|
188 | else | |
188 | if version.effective_date |
|
189 | if version.effective_date | |
189 | 1 |
|
190 | 1 | |
190 | else |
|
191 | else | |
191 | name == version.name ? id <=> version.id : name <=> version.name |
|
192 | name == version.name ? id <=> version.id : name <=> version.name | |
192 | end |
|
193 | end | |
193 | end |
|
194 | end | |
194 | end |
|
195 | end | |
195 |
|
196 | |||
196 | def self.fields_for_order_statement(table=nil) |
|
197 | def self.fields_for_order_statement(table=nil) | |
197 | table ||= table_name |
|
198 | table ||= table_name | |
198 | ["(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)", "#{table}.effective_date", "#{table}.name", "#{table}.id"] |
|
199 | ["(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)", "#{table}.effective_date", "#{table}.name", "#{table}.id"] | |
199 | end |
|
200 | end | |
200 |
|
201 | |||
201 | scope :sorted, lambda { order(fields_for_order_statement) } |
|
202 | scope :sorted, lambda { order(fields_for_order_statement) } | |
202 |
|
203 | |||
203 | # Returns the sharings that +user+ can set the version to |
|
204 | # Returns the sharings that +user+ can set the version to | |
204 | def allowed_sharings(user = User.current) |
|
205 | def allowed_sharings(user = User.current) | |
205 | VERSION_SHARINGS.select do |s| |
|
206 | VERSION_SHARINGS.select do |s| | |
206 | if sharing == s |
|
207 | if sharing == s | |
207 | true |
|
208 | true | |
208 | else |
|
209 | else | |
209 | case s |
|
210 | case s | |
210 | when 'system' |
|
211 | when 'system' | |
211 | # Only admin users can set a systemwide sharing |
|
212 | # Only admin users can set a systemwide sharing | |
212 | user.admin? |
|
213 | user.admin? | |
213 | when 'hierarchy', 'tree' |
|
214 | when 'hierarchy', 'tree' | |
214 | # Only users allowed to manage versions of the root project can |
|
215 | # Only users allowed to manage versions of the root project can | |
215 | # set sharing to hierarchy or tree |
|
216 | # set sharing to hierarchy or tree | |
216 | project.nil? || user.allowed_to?(:manage_versions, project.root) |
|
217 | project.nil? || user.allowed_to?(:manage_versions, project.root) | |
217 | else |
|
218 | else | |
218 | true |
|
219 | true | |
219 | end |
|
220 | end | |
220 | end |
|
221 | end | |
221 | end |
|
222 | end | |
222 | end |
|
223 | end | |
223 |
|
224 | |||
224 | # Returns true if the version is shared, otherwise false |
|
225 | # Returns true if the version is shared, otherwise false | |
225 | def shared? |
|
226 | def shared? | |
226 | sharing != 'none' |
|
227 | sharing != 'none' | |
227 | end |
|
228 | end | |
228 |
|
229 | |||
229 | private |
|
230 | private | |
230 |
|
231 | |||
231 | def load_issue_counts |
|
232 | def load_issue_counts | |
232 | unless @issue_count |
|
233 | unless @issue_count | |
233 | @open_issues_count = 0 |
|
234 | @open_issues_count = 0 | |
234 | @closed_issues_count = 0 |
|
235 | @closed_issues_count = 0 | |
235 | fixed_issues.group(:status).count.each do |status, count| |
|
236 | fixed_issues.group(:status).count.each do |status, count| | |
236 | if status.is_closed? |
|
237 | if status.is_closed? | |
237 | @closed_issues_count += count |
|
238 | @closed_issues_count += count | |
238 | else |
|
239 | else | |
239 | @open_issues_count += count |
|
240 | @open_issues_count += count | |
240 | end |
|
241 | end | |
241 | end |
|
242 | end | |
242 | @issue_count = @open_issues_count + @closed_issues_count |
|
243 | @issue_count = @open_issues_count + @closed_issues_count | |
243 | end |
|
244 | end | |
244 | end |
|
245 | end | |
245 |
|
246 | |||
246 | # Update the issue's fixed versions. Used if a version's sharing changes. |
|
247 | # Update the issue's fixed versions. Used if a version's sharing changes. | |
247 | def update_issues_from_sharing_change |
|
248 | def update_issues_from_sharing_change | |
248 | if sharing_changed? |
|
249 | if sharing_changed? | |
249 | if VERSION_SHARINGS.index(sharing_was).nil? || |
|
250 | if VERSION_SHARINGS.index(sharing_was).nil? || | |
250 | VERSION_SHARINGS.index(sharing).nil? || |
|
251 | VERSION_SHARINGS.index(sharing).nil? || | |
251 | VERSION_SHARINGS.index(sharing_was) > VERSION_SHARINGS.index(sharing) |
|
252 | VERSION_SHARINGS.index(sharing_was) > VERSION_SHARINGS.index(sharing) | |
252 | Issue.update_versions_from_sharing_change self |
|
253 | Issue.update_versions_from_sharing_change self | |
253 | end |
|
254 | end | |
254 | end |
|
255 | end | |
255 | end |
|
256 | end | |
256 |
|
257 | |||
257 | # Returns the average estimated time of assigned issues |
|
258 | # Returns the average estimated time of assigned issues | |
258 | # or 1 if no issue has an estimated time |
|
259 | # or 1 if no issue has an estimated time | |
259 | # Used to weight unestimated issues in progress calculation |
|
260 | # Used to weight unestimated issues in progress calculation | |
260 | def estimated_average |
|
261 | def estimated_average | |
261 | if @estimated_average.nil? |
|
262 | if @estimated_average.nil? | |
262 | average = fixed_issues.average(:estimated_hours).to_f |
|
263 | average = fixed_issues.average(:estimated_hours).to_f | |
263 | if average == 0 |
|
264 | if average == 0 | |
264 | average = 1 |
|
265 | average = 1 | |
265 | end |
|
266 | end | |
266 | @estimated_average = average |
|
267 | @estimated_average = average | |
267 | end |
|
268 | end | |
268 | @estimated_average |
|
269 | @estimated_average | |
269 | end |
|
270 | end | |
270 |
|
271 | |||
271 | # Returns the total progress of open or closed issues. The returned percentage takes into account |
|
272 | # Returns the total progress of open or closed issues. The returned percentage takes into account | |
272 | # the amount of estimated time set for this version. |
|
273 | # the amount of estimated time set for this version. | |
273 | # |
|
274 | # | |
274 | # Examples: |
|
275 | # Examples: | |
275 | # issues_progress(true) => returns the progress percentage for open issues. |
|
276 | # issues_progress(true) => returns the progress percentage for open issues. | |
276 | # issues_progress(false) => returns the progress percentage for closed issues. |
|
277 | # issues_progress(false) => returns the progress percentage for closed issues. | |
277 | def issues_progress(open) |
|
278 | def issues_progress(open) | |
278 | @issues_progress ||= {} |
|
279 | @issues_progress ||= {} | |
279 | @issues_progress[open] ||= begin |
|
280 | @issues_progress[open] ||= begin | |
280 | progress = 0 |
|
281 | progress = 0 | |
281 | if issues_count > 0 |
|
282 | if issues_count > 0 | |
282 | ratio = open ? 'done_ratio' : 100 |
|
283 | ratio = open ? 'done_ratio' : 100 | |
283 |
|
284 | |||
284 | done = fixed_issues.open(open).sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}").to_f |
|
285 | done = fixed_issues.open(open).sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}").to_f | |
285 | progress = done / (estimated_average * issues_count) |
|
286 | progress = done / (estimated_average * issues_count) | |
286 | end |
|
287 | end | |
287 | progress |
|
288 | progress | |
288 | end |
|
289 | end | |
289 | end |
|
290 | end | |
290 | end |
|
291 | end |
General Comments 0
You need to be logged in to leave comments.
Login now