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