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.

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