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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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. module FixedIssuesExtension
  18. # Returns the total estimated time for this version
  19. # (sum of leaves estimated_hours)
  20. def estimated_hours
  21. @estimated_hours ||= sum(:estimated_hours).to_f
  22. end
  23. #
  24. # Returns the total amount of open issues for this version.
  25. def open_count
  26. load_counts
  27. @open_count
  28. end
  29. # Returns the total amount of closed issues for this version.
  30. def closed_count
  31. load_counts
  32. @closed_count
  33. end
  34. # Returns the completion percentage of this version based on the amount of open/closed issues
  35. # and the time spent on the open issues.
  36. def completed_percent
  37. if count == 0
  38. 0
  39. elsif open_count == 0
  40. 100
  41. else
  42. issues_progress(false) + issues_progress(true)
  43. end
  44. end
  45. # Returns the percentage of issues that have been marked as 'closed'.
  46. def closed_percent
  47. if count == 0
  48. 0
  49. else
  50. issues_progress(false)
  51. end
  52. end
  53. private
  54. def load_counts
  55. unless @open_count
  56. @open_count = 0
  57. @closed_count = 0
  58. self.group(:status).count.each do |status, count|
  59. if status.is_closed?
  60. @closed_count += count
  61. else
  62. @open_count += count
  63. end
  64. end
  65. end
  66. end
  67. # Returns the average estimated time of assigned issues
  68. # or 1 if no issue has an estimated time
  69. # Used to weight unestimated issues in progress calculation
  70. def estimated_average
  71. if @estimated_average.nil?
  72. average = average(:estimated_hours).to_f
  73. if average == 0
  74. average = 1
  75. end
  76. @estimated_average = average
  77. end
  78. @estimated_average
  79. end
  80. # Returns the total progress of open or closed issues. The returned percentage takes into account
  81. # the amount of estimated time set for this version.
  82. #
  83. # Examples:
  84. # issues_progress(true) => returns the progress percentage for open issues.
  85. # issues_progress(false) => returns the progress percentage for closed issues.
  86. def issues_progress(open)
  87. @issues_progress ||= {}
  88. @issues_progress[open] ||= begin
  89. progress = 0
  90. if count > 0
  91. ratio = open ? 'done_ratio' : 100
  92. done = open(open).sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}").to_f
  93. progress = done / (estimated_average * count)
  94. end
  95. progress
  96. end
  97. end
  98. end
  99. class Version < ActiveRecord::Base
  100. include Redmine::SafeAttributes
  101. after_update :update_issues_from_sharing_change
  102. after_save :update_default_project_version
  103. before_destroy :nullify_projects_default_version
  104. belongs_to :project
  105. has_many :fixed_issues, :class_name => 'Issue', :foreign_key => 'fixed_version_id', :dependent => :nullify, :extend => FixedIssuesExtension
  106. acts_as_customizable
  107. acts_as_attachable :view_permission => :view_files,
  108. :edit_permission => :manage_files,
  109. :delete_permission => :manage_files
  110. VERSION_STATUSES = %w(open locked closed)
  111. VERSION_SHARINGS = %w(none descendants hierarchy tree system)
  112. validates_presence_of :name
  113. validates_uniqueness_of :name, :scope => [:project_id]
  114. validates_length_of :name, :maximum => 60
  115. validates_length_of :description, :wiki_page_title, :maximum => 255
  116. validates :effective_date, :date => true
  117. validates_inclusion_of :status, :in => VERSION_STATUSES
  118. validates_inclusion_of :sharing, :in => VERSION_SHARINGS
  119. scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
  120. scope :like, lambda {|arg|
  121. if arg.present?
  122. pattern = "%#{arg.to_s.strip}%"
  123. where([Redmine::Database.like("#{Version.table_name}.name", '?'), pattern])
  124. end
  125. }
  126. scope :open, lambda { where(:status => 'open') }
  127. scope :status, lambda {|status|
  128. if status.present?
  129. where(:status => status.to_s)
  130. end
  131. }
  132. scope :visible, lambda {|*args|
  133. joins(:project).
  134. where(Project.allowed_to_condition(args.first || User.current, :view_issues))
  135. }
  136. safe_attributes 'name',
  137. 'description',
  138. 'effective_date',
  139. 'due_date',
  140. 'wiki_page_title',
  141. 'status',
  142. 'sharing',
  143. 'default_project_version',
  144. 'custom_field_values',
  145. 'custom_fields'
  146. # Returns true if +user+ or current user is allowed to view the version
  147. def visible?(user=User.current)
  148. user.allowed_to?(:view_issues, self.project)
  149. end
  150. # Version files have same visibility as project files
  151. def attachments_visible?(*args)
  152. project.present? && project.attachments_visible?(*args)
  153. end
  154. def attachments_deletable?(usr=User.current)
  155. project.present? && project.attachments_deletable?(usr)
  156. end
  157. alias :base_reload :reload
  158. def reload(*args)
  159. @default_project_version = nil
  160. @visible_fixed_issues = nil
  161. base_reload(*args)
  162. end
  163. def start_date
  164. @start_date ||= fixed_issues.minimum('start_date')
  165. end
  166. def due_date
  167. effective_date
  168. end
  169. def due_date=(arg)
  170. self.effective_date=(arg)
  171. end
  172. # Returns the total estimated time for this version
  173. # (sum of leaves estimated_hours)
  174. def estimated_hours
  175. fixed_issues.estimated_hours
  176. end
  177. # Returns the total reported time for this version
  178. def spent_hours
  179. @spent_hours ||= TimeEntry.joins(:issue).where("#{Issue.table_name}.fixed_version_id = ?", id).sum(:hours).to_f
  180. end
  181. def closed?
  182. status == 'closed'
  183. end
  184. def open?
  185. status == 'open'
  186. end
  187. # Returns true if the version is completed: closed or due date reached and no open issues
  188. def completed?
  189. closed? || (effective_date && (effective_date < User.current.today) && (open_issues_count == 0))
  190. end
  191. def behind_schedule?
  192. if completed_percent == 100
  193. return false
  194. elsif due_date && start_date
  195. done_date = start_date + ((due_date - start_date+1)* completed_percent/100).floor
  196. return done_date <= User.current.today
  197. else
  198. false # No issues so it's not late
  199. end
  200. end
  201. # Returns the completion percentage of this version based on the amount of open/closed issues
  202. # and the time spent on the open issues.
  203. def completed_percent
  204. fixed_issues.completed_percent
  205. end
  206. # Returns the percentage of issues that have been marked as 'closed'.
  207. def closed_percent
  208. fixed_issues.closed_percent
  209. end
  210. # Returns true if the version is overdue: due date reached and some open issues
  211. def overdue?
  212. effective_date && (effective_date < User.current.today) && (open_issues_count > 0)
  213. end
  214. # Returns assigned issues count
  215. def issues_count
  216. fixed_issues.count
  217. end
  218. # Returns the total amount of open issues for this version.
  219. def open_issues_count
  220. fixed_issues.open_count
  221. end
  222. # Returns the total amount of closed issues for this version.
  223. def closed_issues_count
  224. fixed_issues.closed_count
  225. end
  226. def visible_fixed_issues
  227. @visible_fixed_issues ||= fixed_issues.visible
  228. end
  229. def wiki_page
  230. if project.wiki && !wiki_page_title.blank?
  231. @wiki_page ||= project.wiki.find_page(wiki_page_title)
  232. end
  233. @wiki_page
  234. end
  235. def to_s; name end
  236. def to_s_with_project
  237. "#{project} - #{name}"
  238. end
  239. # Versions are sorted by effective_date and name
  240. # Those with no effective_date are at the end, sorted by name
  241. def <=>(version)
  242. if self.effective_date
  243. if version.effective_date
  244. if self.effective_date == version.effective_date
  245. name == version.name ? id <=> version.id : name <=> version.name
  246. else
  247. self.effective_date <=> version.effective_date
  248. end
  249. else
  250. -1
  251. end
  252. else
  253. if version.effective_date
  254. 1
  255. else
  256. name == version.name ? id <=> version.id : name <=> version.name
  257. end
  258. end
  259. end
  260. # Sort versions by status (open, locked then closed versions)
  261. def self.sort_by_status(versions)
  262. versions.sort do |a, b|
  263. if a.status == b.status
  264. a <=> b
  265. else
  266. b.status <=> a.status
  267. end
  268. end
  269. end
  270. def css_classes
  271. [
  272. completed? ? 'version-completed' : 'version-incompleted',
  273. "version-#{status}"
  274. ].join(' ')
  275. end
  276. def self.fields_for_order_statement(table=nil)
  277. table ||= table_name
  278. [Arel.sql("(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)"), "#{table}.effective_date", "#{table}.name", "#{table}.id"]
  279. end
  280. scope :sorted, lambda { order(fields_for_order_statement) }
  281. # Returns the sharings that +user+ can set the version to
  282. def allowed_sharings(user = User.current)
  283. VERSION_SHARINGS.select do |s|
  284. if sharing == s
  285. true
  286. else
  287. case s
  288. when 'system'
  289. # Only admin users can set a systemwide sharing
  290. user.admin?
  291. when 'hierarchy', 'tree'
  292. # Only users allowed to manage versions of the root project can
  293. # set sharing to hierarchy or tree
  294. project.nil? || user.allowed_to?(:manage_versions, project.root)
  295. else
  296. true
  297. end
  298. end
  299. end
  300. end
  301. # Returns true if the version is shared, otherwise false
  302. def shared?
  303. sharing != 'none'
  304. end
  305. def deletable?
  306. fixed_issues.empty? && !referenced_by_a_custom_field?
  307. end
  308. def default_project_version
  309. if @default_project_version.nil?
  310. project.present? && project.default_version == self
  311. else
  312. @default_project_version
  313. end
  314. end
  315. def default_project_version=(arg)
  316. @default_project_version = (arg == '1' || arg == true)
  317. end
  318. private
  319. # Update the issue's fixed versions. Used if a version's sharing changes.
  320. def update_issues_from_sharing_change
  321. if saved_change_to_sharing?
  322. if VERSION_SHARINGS.index(sharing_before_last_save).nil? ||
  323. VERSION_SHARINGS.index(sharing).nil? ||
  324. VERSION_SHARINGS.index(sharing_before_last_save) > VERSION_SHARINGS.index(sharing)
  325. Issue.update_versions_from_sharing_change self
  326. end
  327. end
  328. end
  329. def update_default_project_version
  330. if @default_project_version && project.present?
  331. project.update_columns :default_version_id => id
  332. end
  333. end
  334. def referenced_by_a_custom_field?
  335. CustomValue.joins(:custom_field).
  336. where(:value => id.to_s, :custom_fields => {:field_format => 'version'}).any?
  337. end
  338. def nullify_projects_default_version
  339. Project.where(:default_version_id => id).update_all(:default_version_id => nil)
  340. end
  341. end