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