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