annotate .svn/pristine/c2/c286f6e2d240d2ed88571faa048b3bc293897d5e.svn-base @ 1478:5ca1f4a47171 bibplugin_db_migrations

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