You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

version.rb 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 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. class Version < ActiveRecord::Base
  18. include Redmine::SafeAttributes
  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. VERSION_STATUSES = %w(open locked closed)
  26. VERSION_SHARINGS = %w(none descendants hierarchy tree system)
  27. validates_presence_of :name
  28. validates_uniqueness_of :name, :scope => [:project_id]
  29. validates_length_of :name, :maximum => 60
  30. validates :effective_date, :date => true
  31. validates_inclusion_of :status, :in => VERSION_STATUSES
  32. validates_inclusion_of :sharing, :in => VERSION_SHARINGS
  33. scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
  34. scope :open, lambda { where(:status => 'open') }
  35. scope :visible, lambda {|*args|
  36. includes(:project).where(Project.allowed_to_condition(args.first || User.current, :view_issues))
  37. }
  38. safe_attributes 'name',
  39. 'description',
  40. 'effective_date',
  41. 'due_date',
  42. 'wiki_page_title',
  43. 'status',
  44. 'sharing',
  45. 'custom_field_values'
  46. # Returns true if +user+ or current user is allowed to view the version
  47. def visible?(user=User.current)
  48. user.allowed_to?(:view_issues, self.project)
  49. end
  50. # Version files have same visibility as project files
  51. def attachments_visible?(*args)
  52. project.present? && project.attachments_visible?(*args)
  53. end
  54. def start_date
  55. @start_date ||= fixed_issues.minimum('start_date')
  56. end
  57. def due_date
  58. effective_date
  59. end
  60. def due_date=(arg)
  61. self.effective_date=(arg)
  62. end
  63. # Returns the total estimated time for this version
  64. # (sum of leaves estimated_hours)
  65. def estimated_hours
  66. @estimated_hours ||= fixed_issues.leaves.sum(:estimated_hours).to_f
  67. end
  68. # Returns the total reported time for this version
  69. def spent_hours
  70. @spent_hours ||= TimeEntry.joins(:issue).where("#{Issue.table_name}.fixed_version_id = ?", id).sum(:hours).to_f
  71. end
  72. def closed?
  73. status == 'closed'
  74. end
  75. def open?
  76. status == 'open'
  77. end
  78. # Returns true if the version is completed: due date reached and no open issues
  79. def completed?
  80. effective_date && (effective_date < Date.today) && (open_issues_count == 0)
  81. end
  82. def behind_schedule?
  83. if completed_percent == 100
  84. return false
  85. elsif due_date && start_date
  86. done_date = start_date + ((due_date - start_date+1)* completed_percent/100).floor
  87. return done_date <= Date.today
  88. else
  89. false # No issues so it's not late
  90. end
  91. end
  92. # Returns the completion percentage of this version based on the amount of open/closed issues
  93. # and the time spent on the open issues.
  94. def completed_percent
  95. if issues_count == 0
  96. 0
  97. elsif open_issues_count == 0
  98. 100
  99. else
  100. issues_progress(false) + issues_progress(true)
  101. end
  102. end
  103. # TODO: remove in Redmine 3.0
  104. def completed_pourcent
  105. ActiveSupport::Deprecation.warn "Version#completed_pourcent is deprecated and will be removed in Redmine 3.0. Please use #completed_percent instead."
  106. completed_percent
  107. end
  108. # Returns the percentage of issues that have been marked as 'closed'.
  109. def closed_percent
  110. if issues_count == 0
  111. 0
  112. else
  113. issues_progress(false)
  114. end
  115. end
  116. # TODO: remove in Redmine 3.0
  117. def closed_pourcent
  118. ActiveSupport::Deprecation.warn "Version#closed_pourcent is deprecated and will be removed in Redmine 3.0. Please use #closed_percent instead."
  119. closed_percent
  120. end
  121. # Returns true if the version is overdue: due date reached and some open issues
  122. def overdue?
  123. effective_date && (effective_date < Date.today) && (open_issues_count > 0)
  124. end
  125. # Returns assigned issues count
  126. def issues_count
  127. load_issue_counts
  128. @issue_count
  129. end
  130. # Returns the total amount of open issues for this version.
  131. def open_issues_count
  132. load_issue_counts
  133. @open_issues_count
  134. end
  135. # Returns the total amount of closed issues for this version.
  136. def closed_issues_count
  137. load_issue_counts
  138. @closed_issues_count
  139. end
  140. def wiki_page
  141. if project.wiki && !wiki_page_title.blank?
  142. @wiki_page ||= project.wiki.find_page(wiki_page_title)
  143. end
  144. @wiki_page
  145. end
  146. def to_s; name end
  147. def to_s_with_project
  148. "#{project} - #{name}"
  149. end
  150. # Versions are sorted by effective_date and name
  151. # Those with no effective_date are at the end, sorted by name
  152. def <=>(version)
  153. if self.effective_date
  154. if version.effective_date
  155. if self.effective_date == version.effective_date
  156. name == version.name ? id <=> version.id : name <=> version.name
  157. else
  158. self.effective_date <=> version.effective_date
  159. end
  160. else
  161. -1
  162. end
  163. else
  164. if version.effective_date
  165. 1
  166. else
  167. name == version.name ? id <=> version.id : name <=> version.name
  168. end
  169. end
  170. end
  171. def self.fields_for_order_statement(table=nil)
  172. table ||= table_name
  173. ["(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)", "#{table}.effective_date", "#{table}.name", "#{table}.id"]
  174. end
  175. scope :sorted, order(fields_for_order_statement)
  176. # Returns the sharings that +user+ can set the version to
  177. def allowed_sharings(user = User.current)
  178. VERSION_SHARINGS.select do |s|
  179. if sharing == s
  180. true
  181. else
  182. case s
  183. when 'system'
  184. # Only admin users can set a systemwide sharing
  185. user.admin?
  186. when 'hierarchy', 'tree'
  187. # Only users allowed to manage versions of the root project can
  188. # set sharing to hierarchy or tree
  189. project.nil? || user.allowed_to?(:manage_versions, project.root)
  190. else
  191. true
  192. end
  193. end
  194. end
  195. end
  196. private
  197. def load_issue_counts
  198. unless @issue_count
  199. @open_issues_count = 0
  200. @closed_issues_count = 0
  201. fixed_issues.count(:all, :group => :status).each do |status, count|
  202. if status.is_closed?
  203. @closed_issues_count += count
  204. else
  205. @open_issues_count += count
  206. end
  207. end
  208. @issue_count = @open_issues_count + @closed_issues_count
  209. end
  210. end
  211. # Update the issue's fixed versions. Used if a version's sharing changes.
  212. def update_issues_from_sharing_change
  213. if sharing_changed?
  214. if VERSION_SHARINGS.index(sharing_was).nil? ||
  215. VERSION_SHARINGS.index(sharing).nil? ||
  216. VERSION_SHARINGS.index(sharing_was) > VERSION_SHARINGS.index(sharing)
  217. Issue.update_versions_from_sharing_change self
  218. end
  219. end
  220. end
  221. # Returns the average estimated time of assigned issues
  222. # or 1 if no issue has an estimated time
  223. # Used to weigth unestimated issues in progress calculation
  224. def estimated_average
  225. if @estimated_average.nil?
  226. average = fixed_issues.average(:estimated_hours).to_f
  227. if average == 0
  228. average = 1
  229. end
  230. @estimated_average = average
  231. end
  232. @estimated_average
  233. end
  234. # Returns the total progress of open or closed issues. The returned percentage takes into account
  235. # the amount of estimated time set for this version.
  236. #
  237. # Examples:
  238. # issues_progress(true) => returns the progress percentage for open issues.
  239. # issues_progress(false) => returns the progress percentage for closed issues.
  240. def issues_progress(open)
  241. @issues_progress ||= {}
  242. @issues_progress[open] ||= begin
  243. progress = 0
  244. if issues_count > 0
  245. ratio = open ? 'done_ratio' : 100
  246. done = fixed_issues.open(open).sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}").to_f
  247. progress = done / (estimated_average * issues_count)
  248. end
  249. progress
  250. end
  251. end
  252. end