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 @ 441:cbce1fd3b1b7

History | View | Annotate | Download (7.44 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2010  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
  after_update :update_issues_from_sharing_change
20
  belongs_to :project
21
  has_many :fixed_issues, :class_name => 'Issue', :foreign_key => 'fixed_version_id', :dependent => :nullify
22
  acts_as_customizable
23
  acts_as_attachable :view_permission => :view_files,
24
                     :delete_permission => :manage_files
25

    
26
  VERSION_STATUSES = %w(open locked closed)
27
  VERSION_SHARINGS = %w(none descendants hierarchy tree system)
28
  
29
  validates_presence_of :name
30
  validates_uniqueness_of :name, :scope => [:project_id]
31
  validates_length_of :name, :maximum => 60
32
  validates_format_of :effective_date, :with => /^\d{4}-\d{2}-\d{2}$/, :message => :not_a_date, :allow_nil => true
33
  validates_inclusion_of :status, :in => VERSION_STATUSES
34
  validates_inclusion_of :sharing, :in => VERSION_SHARINGS
35

    
36
  named_scope :open, :conditions => {:status => 'open'}
37
  named_scope :visible, lambda {|*args| { :include => :project,
38
                                          :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
39

    
40
  # Returns true if +user+ or current user is allowed to view the version
41
  def visible?(user=User.current)
42
    user.allowed_to?(:view_issues, self.project)
43
  end
44
  
45
  def start_date
46
    @start_date ||= fixed_issues.minimum('start_date')
47
  end
48
  
49
  def due_date
50
    effective_date
51
  end
52
  
53
  # Returns the total estimated time for this version
54
  # (sum of leaves estimated_hours)
55
  def estimated_hours
56
    @estimated_hours ||= fixed_issues.leaves.sum(:estimated_hours).to_f
57
  end
58
  
59
  # Returns the total reported time for this version
60
  def spent_hours
61
    @spent_hours ||= TimeEntry.sum(:hours, :include => :issue, :conditions => ["#{Issue.table_name}.fixed_version_id = ?", id]).to_f
62
  end
63
  
64
  def closed?
65
    status == 'closed'
66
  end
67

    
68
  def open?
69
    status == 'open'
70
  end
71
  
72
  # Returns true if the version is completed: due date reached and no open issues
73
  def completed?
74
    effective_date && (effective_date <= Date.today) && (open_issues_count == 0)
75
  end
76

    
77
  def behind_schedule?
78
    if completed_pourcent == 100
79
      return false
80
    elsif due_date && start_date
81
      done_date = start_date + ((due_date - start_date+1)* completed_pourcent/100).floor
82
      return done_date <= Date.today
83
    else
84
      false # No issues so it's not late
85
    end
86
  end
87
  
88
  # Returns the completion percentage of this version based on the amount of open/closed issues
89
  # and the time spent on the open issues.
90
  def completed_pourcent
91
    if issues_count == 0
92
      0
93
    elsif open_issues_count == 0
94
      100
95
    else
96
      issues_progress(false) + issues_progress(true)
97
    end
98
  end
99
  
100
  # Returns the percentage of issues that have been marked as 'closed'.
101
  def closed_pourcent
102
    if issues_count == 0
103
      0
104
    else
105
      issues_progress(false)
106
    end
107
  end
108
  
109
  # Returns true if the version is overdue: due date reached and some open issues
110
  def overdue?
111
    effective_date && (effective_date < Date.today) && (open_issues_count > 0)
112
  end
113
  
114
  # Returns assigned issues count
115
  def issues_count
116
    @issue_count ||= fixed_issues.count
117
  end
118
  
119
  # Returns the total amount of open issues for this version.
120
  def open_issues_count
121
    @open_issues_count ||= Issue.count(:all, :conditions => ["fixed_version_id = ? AND is_closed = ?", self.id, false], :include => :status)
122
  end
123

    
124
  # Returns the total amount of closed issues for this version.
125
  def closed_issues_count
126
    @closed_issues_count ||= Issue.count(:all, :conditions => ["fixed_version_id = ? AND is_closed = ?", self.id, true], :include => :status)
127
  end
128
  
129
  def wiki_page
130
    if project.wiki && !wiki_page_title.blank?
131
      @wiki_page ||= project.wiki.find_page(wiki_page_title)
132
    end
133
    @wiki_page
134
  end
135
  
136
  def to_s; name end
137

    
138
  def to_s_with_project
139
    "#{project} - #{name}"
140
  end
141
  
142
  # Versions are sorted by effective_date and "Project Name - Version name"
143
  # Those with no effective_date are at the end, sorted by "Project Name - Version name"
144
  def <=>(version)
145
    if self.effective_date
146
      if version.effective_date
147
        if self.effective_date == version.effective_date
148
          "#{self.project.name} - #{self.name}" <=> "#{version.project.name} - #{version.name}"
149
        else
150
          self.effective_date <=> version.effective_date
151
        end
152
      else
153
        -1
154
      end
155
    else
156
      if version.effective_date
157
        1
158
      else
159
        "#{self.project.name} - #{self.name}" <=> "#{version.project.name} - #{version.name}"
160
      end
161
    end
162
  end
163
  
164
  # Returns the sharings that +user+ can set the version to
165
  def allowed_sharings(user = User.current)
166
    VERSION_SHARINGS.select do |s|
167
      if sharing == s
168
        true
169
      else
170
        case s
171
        when 'system'
172
          # Only admin users can set a systemwide sharing
173
          user.admin?
174
        when 'hierarchy', 'tree'
175
          # Only users allowed to manage versions of the root project can
176
          # set sharing to hierarchy or tree
177
          project.nil? || user.allowed_to?(:manage_versions, project.root)
178
        else
179
          true
180
        end
181
      end
182
    end
183
  end
184
  
185
  private
186

    
187
  # Update the issue's fixed versions. Used if a version's sharing changes.
188
  def update_issues_from_sharing_change
189
    if sharing_changed?
190
      if VERSION_SHARINGS.index(sharing_was).nil? ||
191
          VERSION_SHARINGS.index(sharing).nil? ||
192
          VERSION_SHARINGS.index(sharing_was) > VERSION_SHARINGS.index(sharing)
193
        Issue.update_versions_from_sharing_change self
194
      end
195
    end
196
  end
197
  
198
  # Returns the average estimated time of assigned issues
199
  # or 1 if no issue has an estimated time
200
  # Used to weigth unestimated issues in progress calculation
201
  def estimated_average
202
    if @estimated_average.nil?
203
      average = fixed_issues.average(:estimated_hours).to_f
204
      if average == 0
205
        average = 1
206
      end
207
      @estimated_average = average
208
    end
209
    @estimated_average
210
  end
211
  
212
  # Returns the total progress of open or closed issues.  The returned percentage takes into account
213
  # the amount of estimated time set for this version.
214
  #
215
  # Examples:
216
  # issues_progress(true)   => returns the progress percentage for open issues.
217
  # issues_progress(false)  => returns the progress percentage for closed issues.
218
  def issues_progress(open)
219
    @issues_progress ||= {}
220
    @issues_progress[open] ||= begin
221
      progress = 0
222
      if issues_count > 0
223
        ratio = open ? 'done_ratio' : 100
224
        
225
        done = fixed_issues.sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}",
226
                                  :include => :status,
227
                                  :conditions => ["is_closed = ?", !open]).to_f
228
        progress = done / (estimated_average * issues_count)
229
      end
230
      progress
231
    end
232
  end
233
end