To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / version.rb @ 954:42e2437766c2

History | View | Annotate | Download (7.9 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2011  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
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
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
class Version < ActiveRecord::Base
19
  include Redmine::SafeAttributes
20
  after_update :update_issues_from_sharing_change
21
  belongs_to :project
22
  has_many :fixed_issues, :class_name => 'Issue', :foreign_key => 'fixed_version_id', :dependent => :nullify
23
  acts_as_customizable
24
  acts_as_attachable :view_permission => :view_files,
25
                     :delete_permission => :manage_files
26

    
27
  VERSION_STATUSES = %w(open locked closed)
28
  VERSION_SHARINGS = %w(none descendants hierarchy tree system)
29

    
30
  validates_presence_of :name
31
  validates_uniqueness_of :name, :scope => [:project_id]
32
  validates_length_of :name, :maximum => 60
33
  validates_format_of :effective_date, :with => /^\d{4}-\d{2}-\d{2}$/, :message => :not_a_date, :allow_nil => true
34
  validates_inclusion_of :status, :in => VERSION_STATUSES
35
  validates_inclusion_of :sharing, :in => VERSION_SHARINGS
36

    
37
  named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
38
  named_scope :open, :conditions => {:status => 'open'}
39
  named_scope :visible, lambda {|*args| { :include => :project,
40
                                          :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
41

    
42
  safe_attributes 'name', 
43
    'description',
44
    'effective_date',
45
    'due_date',
46
    'wiki_page_title',
47
    'status',
48
    'sharing',
49
    'custom_field_values'
50

    
51
  # Returns true if +user+ or current user is allowed to view the version
52
  def visible?(user=User.current)
53
    user.allowed_to?(:view_issues, self.project)
54
  end
55

    
56
  # Version files have same visibility as project files
57
  def attachments_visible?(*args)
58
    project.present? && project.attachments_visible?(*args)
59
  end
60

    
61
  def start_date
62
    @start_date ||= fixed_issues.minimum('start_date')
63
  end
64

    
65
  def due_date
66
    effective_date
67
  end
68

    
69
  def due_date=(arg)
70
    self.effective_date=(arg)
71
  end
72

    
73
  # Returns the total estimated time for this version
74
  # (sum of leaves estimated_hours)
75
  def estimated_hours
76
    @estimated_hours ||= fixed_issues.leaves.sum(:estimated_hours).to_f
77
  end
78

    
79
  # Returns the total reported time for this version
80
  def spent_hours
81
    @spent_hours ||= TimeEntry.sum(:hours, :include => :issue, :conditions => ["#{Issue.table_name}.fixed_version_id = ?", id]).to_f
82
  end
83

    
84
  def closed?
85
    status == 'closed'
86
  end
87

    
88
  def open?
89
    status == 'open'
90
  end
91

    
92
  # Returns true if the version is completed: due date reached and no open issues
93
  def completed?
94
    effective_date && (effective_date <= Date.today) && (open_issues_count == 0)
95
  end
96

    
97
  def behind_schedule?
98
    if completed_pourcent == 100
99
      return false
100
    elsif due_date && start_date
101
      done_date = start_date + ((due_date - start_date+1)* completed_pourcent/100).floor
102
      return done_date <= Date.today
103
    else
104
      false # No issues so it's not late
105
    end
106
  end
107

    
108
  # Returns the completion percentage of this version based on the amount of open/closed issues
109
  # and the time spent on the open issues.
110
  def completed_pourcent
111
    if issues_count == 0
112
      0
113
    elsif open_issues_count == 0
114
      100
115
    else
116
      issues_progress(false) + issues_progress(true)
117
    end
118
  end
119

    
120
  # Returns the percentage of issues that have been marked as 'closed'.
121
  def closed_pourcent
122
    if issues_count == 0
123
      0
124
    else
125
      issues_progress(false)
126
    end
127
  end
128

    
129
  # Returns true if the version is overdue: due date reached and some open issues
130
  def overdue?
131
    effective_date && (effective_date < Date.today) && (open_issues_count > 0)
132
  end
133

    
134
  # Returns assigned issues count
135
  def issues_count
136
    @issue_count ||= fixed_issues.count
137
  end
138

    
139
  # Returns the total amount of open issues for this version.
140
  def open_issues_count
141
    @open_issues_count ||= Issue.count(:all, :conditions => ["fixed_version_id = ? AND is_closed = ?", self.id, false], :include => :status)
142
  end
143

    
144
  # Returns the total amount of closed issues for this version.
145
  def closed_issues_count
146
    @closed_issues_count ||= Issue.count(:all, :conditions => ["fixed_version_id = ? AND is_closed = ?", self.id, true], :include => :status)
147
  end
148

    
149
  def wiki_page
150
    if project.wiki && !wiki_page_title.blank?
151
      @wiki_page ||= project.wiki.find_page(wiki_page_title)
152
    end
153
    @wiki_page
154
  end
155

    
156
  def to_s; name end
157

    
158
  def to_s_with_project
159
    "#{project} - #{name}"
160
  end
161

    
162
  # Versions are sorted by effective_date and "Project Name - Version name"
163
  # Those with no effective_date are at the end, sorted by "Project Name - Version name"
164
  def <=>(version)
165
    if self.effective_date
166
      if version.effective_date
167
        if self.effective_date == version.effective_date
168
          "#{self.project.name} - #{self.name}" <=> "#{version.project.name} - #{version.name}"
169
        else
170
          self.effective_date <=> version.effective_date
171
        end
172
      else
173
        -1
174
      end
175
    else
176
      if version.effective_date
177
        1
178
      else
179
        "#{self.project.name} - #{self.name}" <=> "#{version.project.name} - #{version.name}"
180
      end
181
    end
182
  end
183

    
184
  # Returns the sharings that +user+ can set the version to
185
  def allowed_sharings(user = User.current)
186
    VERSION_SHARINGS.select do |s|
187
      if sharing == s
188
        true
189
      else
190
        case s
191
        when 'system'
192
          # Only admin users can set a systemwide sharing
193
          user.admin?
194
        when 'hierarchy', 'tree'
195
          # Only users allowed to manage versions of the root project can
196
          # set sharing to hierarchy or tree
197
          project.nil? || user.allowed_to?(:manage_versions, project.root)
198
        else
199
          true
200
        end
201
      end
202
    end
203
  end
204

    
205
  private
206

    
207
  # Update the issue's fixed versions. Used if a version's sharing changes.
208
  def update_issues_from_sharing_change
209
    if sharing_changed?
210
      if VERSION_SHARINGS.index(sharing_was).nil? ||
211
          VERSION_SHARINGS.index(sharing).nil? ||
212
          VERSION_SHARINGS.index(sharing_was) > VERSION_SHARINGS.index(sharing)
213
        Issue.update_versions_from_sharing_change self
214
      end
215
    end
216
  end
217

    
218
  # Returns the average estimated time of assigned issues
219
  # or 1 if no issue has an estimated time
220
  # Used to weigth unestimated issues in progress calculation
221
  def estimated_average
222
    if @estimated_average.nil?
223
      average = fixed_issues.average(:estimated_hours).to_f
224
      if average == 0
225
        average = 1
226
      end
227
      @estimated_average = average
228
    end
229
    @estimated_average
230
  end
231

    
232
  # Returns the total progress of open or closed issues.  The returned percentage takes into account
233
  # the amount of estimated time set for this version.
234
  #
235
  # Examples:
236
  # issues_progress(true)   => returns the progress percentage for open issues.
237
  # issues_progress(false)  => returns the progress percentage for closed issues.
238
  def issues_progress(open)
239
    @issues_progress ||= {}
240
    @issues_progress[open] ||= begin
241
      progress = 0
242
      if issues_count > 0
243
        ratio = open ? 'done_ratio' : 100
244

    
245
        done = fixed_issues.sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}",
246
                                  :include => :status,
247
                                  :conditions => ["is_closed = ?", !open]).to_f
248
        progress = done / (estimated_average * issues_count)
249
      end
250
      progress
251
    end
252
  end
253
end