Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: class Version < ActiveRecord::Base Chris@909: after_update :update_issues_from_sharing_change Chris@909: belongs_to :project Chris@909: has_many :fixed_issues, :class_name => 'Issue', :foreign_key => 'fixed_version_id', :dependent => :nullify Chris@909: acts_as_customizable Chris@909: acts_as_attachable :view_permission => :view_files, Chris@909: :delete_permission => :manage_files Chris@909: Chris@909: VERSION_STATUSES = %w(open locked closed) Chris@909: VERSION_SHARINGS = %w(none descendants hierarchy tree system) Chris@909: Chris@909: validates_presence_of :name Chris@909: validates_uniqueness_of :name, :scope => [:project_id] Chris@909: validates_length_of :name, :maximum => 60 Chris@909: validates_format_of :effective_date, :with => /^\d{4}-\d{2}-\d{2}$/, :message => :not_a_date, :allow_nil => true Chris@909: validates_inclusion_of :status, :in => VERSION_STATUSES Chris@909: validates_inclusion_of :sharing, :in => VERSION_SHARINGS Chris@909: Chris@909: named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}} Chris@909: named_scope :open, :conditions => {:status => 'open'} Chris@909: named_scope :visible, lambda {|*args| { :include => :project, Chris@909: :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } } Chris@909: Chris@909: # Returns true if +user+ or current user is allowed to view the version Chris@909: def visible?(user=User.current) Chris@909: user.allowed_to?(:view_issues, self.project) Chris@909: end Chris@909: Chris@909: # Version files have same visibility as project files Chris@909: def attachments_visible?(*args) Chris@909: project.present? && project.attachments_visible?(*args) Chris@909: end Chris@909: Chris@909: def start_date Chris@909: @start_date ||= fixed_issues.minimum('start_date') Chris@909: end Chris@909: Chris@909: def due_date Chris@909: effective_date Chris@909: end Chris@909: Chris@909: def due_date=(arg) Chris@909: self.effective_date=(arg) Chris@909: end Chris@909: Chris@909: # Returns the total estimated time for this version Chris@909: # (sum of leaves estimated_hours) Chris@909: def estimated_hours Chris@909: @estimated_hours ||= fixed_issues.leaves.sum(:estimated_hours).to_f Chris@909: end Chris@909: Chris@909: # Returns the total reported time for this version Chris@909: def spent_hours Chris@909: @spent_hours ||= TimeEntry.sum(:hours, :include => :issue, :conditions => ["#{Issue.table_name}.fixed_version_id = ?", id]).to_f Chris@909: end Chris@909: Chris@909: def closed? Chris@909: status == 'closed' Chris@909: end Chris@909: Chris@909: def open? Chris@909: status == 'open' Chris@909: end Chris@909: Chris@909: # Returns true if the version is completed: due date reached and no open issues Chris@909: def completed? Chris@909: effective_date && (effective_date <= Date.today) && (open_issues_count == 0) Chris@909: end Chris@909: Chris@909: def behind_schedule? Chris@909: if completed_pourcent == 100 Chris@909: return false Chris@909: elsif due_date && start_date Chris@909: done_date = start_date + ((due_date - start_date+1)* completed_pourcent/100).floor Chris@909: return done_date <= Date.today Chris@909: else Chris@909: false # No issues so it's not late Chris@909: end Chris@909: end Chris@909: Chris@909: # Returns the completion percentage of this version based on the amount of open/closed issues Chris@909: # and the time spent on the open issues. Chris@909: def completed_pourcent Chris@909: if issues_count == 0 Chris@909: 0 Chris@909: elsif open_issues_count == 0 Chris@909: 100 Chris@909: else Chris@909: issues_progress(false) + issues_progress(true) Chris@909: end Chris@909: end Chris@909: Chris@909: # Returns the percentage of issues that have been marked as 'closed'. Chris@909: def closed_pourcent Chris@909: if issues_count == 0 Chris@909: 0 Chris@909: else Chris@909: issues_progress(false) Chris@909: end Chris@909: end Chris@909: Chris@909: # Returns true if the version is overdue: due date reached and some open issues Chris@909: def overdue? Chris@909: effective_date && (effective_date < Date.today) && (open_issues_count > 0) Chris@909: end Chris@909: Chris@909: # Returns assigned issues count Chris@909: def issues_count Chris@909: @issue_count ||= fixed_issues.count Chris@909: end Chris@909: Chris@909: # Returns the total amount of open issues for this version. Chris@909: def open_issues_count Chris@909: @open_issues_count ||= Issue.count(:all, :conditions => ["fixed_version_id = ? AND is_closed = ?", self.id, false], :include => :status) Chris@909: end Chris@909: Chris@909: # Returns the total amount of closed issues for this version. Chris@909: def closed_issues_count Chris@909: @closed_issues_count ||= Issue.count(:all, :conditions => ["fixed_version_id = ? AND is_closed = ?", self.id, true], :include => :status) Chris@909: end Chris@909: Chris@909: def wiki_page Chris@909: if project.wiki && !wiki_page_title.blank? Chris@909: @wiki_page ||= project.wiki.find_page(wiki_page_title) Chris@909: end Chris@909: @wiki_page Chris@909: end Chris@909: Chris@909: def to_s; name end Chris@909: Chris@909: def to_s_with_project Chris@909: "#{project} - #{name}" Chris@909: end Chris@909: Chris@909: # Versions are sorted by effective_date and "Project Name - Version name" Chris@909: # Those with no effective_date are at the end, sorted by "Project Name - Version name" Chris@909: def <=>(version) Chris@909: if self.effective_date Chris@909: if version.effective_date Chris@909: if self.effective_date == version.effective_date Chris@909: "#{self.project.name} - #{self.name}" <=> "#{version.project.name} - #{version.name}" Chris@909: else Chris@909: self.effective_date <=> version.effective_date Chris@909: end Chris@909: else Chris@909: -1 Chris@909: end Chris@909: else Chris@909: if version.effective_date Chris@909: 1 Chris@909: else Chris@909: "#{self.project.name} - #{self.name}" <=> "#{version.project.name} - #{version.name}" Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: # Returns the sharings that +user+ can set the version to Chris@909: def allowed_sharings(user = User.current) Chris@909: VERSION_SHARINGS.select do |s| Chris@909: if sharing == s Chris@909: true Chris@909: else Chris@909: case s Chris@909: when 'system' Chris@909: # Only admin users can set a systemwide sharing Chris@909: user.admin? Chris@909: when 'hierarchy', 'tree' Chris@909: # Only users allowed to manage versions of the root project can Chris@909: # set sharing to hierarchy or tree Chris@909: project.nil? || user.allowed_to?(:manage_versions, project.root) Chris@909: else Chris@909: true Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: private Chris@909: Chris@909: # Update the issue's fixed versions. Used if a version's sharing changes. Chris@909: def update_issues_from_sharing_change Chris@909: if sharing_changed? Chris@909: if VERSION_SHARINGS.index(sharing_was).nil? || Chris@909: VERSION_SHARINGS.index(sharing).nil? || Chris@909: VERSION_SHARINGS.index(sharing_was) > VERSION_SHARINGS.index(sharing) Chris@909: Issue.update_versions_from_sharing_change self Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: # Returns the average estimated time of assigned issues Chris@909: # or 1 if no issue has an estimated time Chris@909: # Used to weigth unestimated issues in progress calculation Chris@909: def estimated_average Chris@909: if @estimated_average.nil? Chris@909: average = fixed_issues.average(:estimated_hours).to_f Chris@909: if average == 0 Chris@909: average = 1 Chris@909: end Chris@909: @estimated_average = average Chris@909: end Chris@909: @estimated_average Chris@909: end Chris@909: Chris@909: # Returns the total progress of open or closed issues. The returned percentage takes into account Chris@909: # the amount of estimated time set for this version. Chris@909: # Chris@909: # Examples: Chris@909: # issues_progress(true) => returns the progress percentage for open issues. Chris@909: # issues_progress(false) => returns the progress percentage for closed issues. Chris@909: def issues_progress(open) Chris@909: @issues_progress ||= {} Chris@909: @issues_progress[open] ||= begin Chris@909: progress = 0 Chris@909: if issues_count > 0 Chris@909: ratio = open ? 'done_ratio' : 100 Chris@909: Chris@909: done = fixed_issues.sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}", Chris@909: :include => :status, Chris@909: :conditions => ["is_closed = ?", !open]).to_f Chris@909: progress = done / (estimated_average * issues_count) Chris@909: end Chris@909: progress Chris@909: end Chris@909: end Chris@909: end