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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module FixedIssuesExtension
  19. # Returns the total estimated time for this version
  20. # (sum of leaves estimated_hours)
  21. def estimated_hours
  22. @estimated_hours ||= sum(:estimated_hours).to_f
  23. end
  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. issues_with_total_estimated_hours = select {|c| c.total_estimated_hours.to_f > 0.0}
  73. if issues_with_total_estimated_hours.any?
  74. average = issues_with_total_estimated_hours.sum(&:total_estimated_hours).to_f / issues_with_total_estimated_hours.count
  75. else
  76. average = 1.0
  77. end
  78. @estimated_average = average
  79. end
  80. @estimated_average
  81. end
  82. # Returns the total progress of open or closed issues. The returned percentage takes into account
  83. # the amount of estimated time set for this version.
  84. #
  85. # Examples:
  86. # issues_progress(true) => returns the progress percentage for open issues.
  87. # issues_progress(false) => returns the progress percentage for closed issues.
  88. def issues_progress(open)
  89. @issues_progress ||= {}
  90. @issues_progress[open] ||= begin
  91. progress = 0
  92. if count > 0
  93. done = open(open).sum do |c|
  94. estimated = c.total_estimated_hours.to_f
  95. estimated = estimated_average unless estimated > 0.0
  96. ratio = c.closed? ? 100 : (c.done_ratio || 0)
  97. estimated * ratio
  98. end
  99. progress = done / (estimated_average * count)
  100. end
  101. progress
  102. end
  103. end
  104. end
  105. class Version < ApplicationRecord
  106. include Redmine::SafeAttributes
  107. after_update :update_issues_from_sharing_change
  108. after_save :update_default_project_version
  109. before_destroy :nullify_projects_default_version
  110. belongs_to :project
  111. has_many :fixed_issues, :class_name => 'Issue', :foreign_key => 'fixed_version_id', :dependent => :nullify, :extend => FixedIssuesExtension
  112. acts_as_customizable
  113. acts_as_attachable :view_permission => :view_files,
  114. :edit_permission => :manage_files,
  115. :delete_permission => :manage_files
  116. VERSION_STATUSES = %w(open locked closed)
  117. VERSION_SHARINGS = %w(none descendants hierarchy tree system)
  118. validates_presence_of :name
  119. validates_uniqueness_of :name, :scope => [:project_id], :case_sensitive => true
  120. validates_length_of :name, :maximum => 60
  121. validates_length_of :description, :wiki_page_title, :maximum => 255
  122. validates :effective_date, :date => true
  123. validates_inclusion_of :status, :in => VERSION_STATUSES
  124. validates_inclusion_of :sharing, :in => VERSION_SHARINGS
  125. scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
  126. scope :like, (lambda do |arg|
  127. if arg.present?
  128. pattern = "%#{sanitize_sql_like arg.to_s.strip}%"
  129. where([Redmine::Database.like("#{Version.table_name}.name", '?'), pattern])
  130. end
  131. end)
  132. scope :open, lambda {where(:status => 'open')}
  133. scope :status, (lambda do |status|
  134. if status.present?
  135. where(:status => status.to_s)
  136. end
  137. end)
  138. scope :visible, (lambda do |*args|
  139. joins(:project).
  140. where(Project.allowed_to_condition(args.first || User.current, :view_issues))
  141. end)
  142. safe_attributes 'name',
  143. 'description',
  144. 'effective_date',
  145. 'due_date',
  146. 'wiki_page_title',
  147. 'status',
  148. 'sharing',
  149. 'default_project_version',
  150. 'custom_field_values',
  151. 'custom_fields'
  152. def safe_attributes=(attrs, user=User.current)
  153. if attrs.respond_to?(:to_unsafe_hash)
  154. attrs = attrs.to_unsafe_hash
  155. end
  156. return unless attrs.is_a?(Hash)
  157. attrs = attrs.deep_dup
  158. # Reject custom fields values not visible by the user
  159. if attrs['custom_field_values'].present?
  160. editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s}
  161. attrs['custom_field_values'].reject! {|k, v| !editable_custom_field_ids.include?(k.to_s)}
  162. end
  163. # Reject custom fields not visible by the user
  164. if attrs['custom_fields'].present?
  165. editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s}
  166. attrs['custom_fields'].reject! {|c| !editable_custom_field_ids.include?(c['id'].to_s)}
  167. end
  168. super(attrs, user)
  169. end
  170. # Returns true if +user+ or current user is allowed to view the version
  171. def visible?(user=User.current)
  172. user.allowed_to?(:view_issues, self.project)
  173. end
  174. # Returns the custom_field_values that can be edited by the given user
  175. def editable_custom_field_values(user=nil)
  176. visible_custom_field_values(user)
  177. end
  178. def visible_custom_field_values(user = nil)
  179. user ||= User.current
  180. custom_field_values.select do |value|
  181. value.custom_field.visible_by?(project, user)
  182. end
  183. end
  184. # Version files have same visibility as project files
  185. def attachments_visible?(*args)
  186. project.present? && project.attachments_visible?(*args)
  187. end
  188. def attachments_deletable?(usr=User.current)
  189. project.present? && project.attachments_deletable?(usr)
  190. end
  191. alias :base_reload :reload
  192. def reload(*args)
  193. @default_project_version = nil
  194. @visible_fixed_issues = nil
  195. base_reload(*args)
  196. end
  197. def start_date
  198. @start_date ||= fixed_issues.minimum('start_date')
  199. end
  200. def due_date
  201. effective_date
  202. end
  203. def due_date=(arg)
  204. self.effective_date=(arg)
  205. end
  206. # Returns the total estimated time for this version
  207. # (sum of leaves estimated_hours)
  208. def estimated_hours
  209. fixed_issues.estimated_hours
  210. end
  211. # Returns the total reported time for this version
  212. def spent_hours
  213. @spent_hours ||= TimeEntry.joins(:issue).where("#{Issue.table_name}.fixed_version_id = ?", id).sum(:hours).to_f
  214. end
  215. def closed?
  216. status == 'closed'
  217. end
  218. def open?
  219. status == 'open'
  220. end
  221. # Returns true if the version is completed: closed or due date reached and no open issues
  222. def completed?
  223. closed? || (effective_date && (effective_date < User.current.today) && (open_issues_count == 0))
  224. end
  225. def behind_schedule?
  226. if completed_percent == 100
  227. return false
  228. elsif due_date && start_date
  229. done_date = start_date + ((due_date - start_date+1)* completed_percent/100).floor
  230. return done_date <= User.current.today
  231. else
  232. false # No issues so it's not late
  233. end
  234. end
  235. # Returns the completion percentage of this version based on the amount of open/closed issues
  236. # and the time spent on the open issues.
  237. def completed_percent
  238. fixed_issues.completed_percent
  239. end
  240. # Returns the percentage of issues that have been marked as 'closed'.
  241. def closed_percent
  242. fixed_issues.closed_percent
  243. end
  244. # Returns true if the version is overdue: due date reached and some open issues
  245. def overdue?
  246. effective_date && (effective_date < User.current.today) && (open_issues_count > 0)
  247. end
  248. # Returns assigned issues count
  249. def issues_count
  250. fixed_issues.count
  251. end
  252. # Returns the total amount of open issues for this version.
  253. def open_issues_count
  254. fixed_issues.open_count
  255. end
  256. # Returns the total amount of closed issues for this version.
  257. def closed_issues_count
  258. fixed_issues.closed_count
  259. end
  260. def visible_fixed_issues
  261. @visible_fixed_issues ||= fixed_issues.visible
  262. end
  263. def wiki_page
  264. if project.wiki && !wiki_page_title.blank?
  265. @wiki_page ||= project.wiki.find_page(wiki_page_title)
  266. end
  267. @wiki_page
  268. end
  269. def to_s; name end
  270. def to_s_with_project
  271. "#{project} - #{name}"
  272. end
  273. # Versions are sorted by effective_date and name
  274. # Those with no effective_date are at the end, sorted by name
  275. def <=>(version)
  276. return nil unless version.is_a?(Version)
  277. if self.effective_date
  278. if version.effective_date
  279. if self.effective_date == version.effective_date
  280. name == version.name ? id <=> version.id : name <=> version.name
  281. else
  282. self.effective_date <=> version.effective_date
  283. end
  284. else
  285. -1
  286. end
  287. else
  288. if version.effective_date
  289. 1
  290. else
  291. name == version.name ? id <=> version.id : name <=> version.name
  292. end
  293. end
  294. end
  295. # Sort versions by status (open, locked then closed versions)
  296. def self.sort_by_status(versions)
  297. versions.sort do |a, b|
  298. if a.status == b.status
  299. a <=> b
  300. else
  301. b.status <=> a.status
  302. end
  303. end
  304. end
  305. def css_classes
  306. [
  307. completed? ? 'version-completed' : 'version-incompleted',
  308. "version-#{status}"
  309. ].join(' ')
  310. end
  311. def self.fields_for_order_statement(table=nil)
  312. table ||= table_name
  313. [Arel.sql("(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)"), "#{table}.effective_date", "#{table}.name", "#{table}.id"]
  314. end
  315. scope :sorted, lambda {order(fields_for_order_statement)}
  316. # Returns the sharings that +user+ can set the version to
  317. def allowed_sharings(user = User.current)
  318. VERSION_SHARINGS.select do |s|
  319. if sharing == s
  320. true
  321. else
  322. case s
  323. when 'system'
  324. # Only admin users can set a systemwide sharing
  325. user.admin?
  326. when 'hierarchy', 'tree'
  327. # Only users allowed to manage versions of the root project can
  328. # set sharing to hierarchy or tree
  329. project.nil? || user.allowed_to?(:manage_versions, project.root)
  330. else
  331. true
  332. end
  333. end
  334. end
  335. end
  336. # Returns true if the version is shared, otherwise false
  337. def shared?
  338. sharing != 'none'
  339. end
  340. def deletable?
  341. fixed_issues.empty? && !referenced_by_a_custom_field? && attachments.empty?
  342. end
  343. def default_project_version
  344. if @default_project_version.nil?
  345. project.present? && project.default_version == self
  346. else
  347. @default_project_version
  348. end
  349. end
  350. def default_project_version=(arg)
  351. @default_project_version = (arg == '1' || arg == true)
  352. end
  353. private
  354. # Update the issue's fixed versions. Used if a version's sharing changes.
  355. def update_issues_from_sharing_change
  356. if saved_change_to_sharing?
  357. if VERSION_SHARINGS.index(sharing_before_last_save).nil? ||
  358. VERSION_SHARINGS.index(sharing).nil? ||
  359. VERSION_SHARINGS.index(sharing_before_last_save) > VERSION_SHARINGS.index(sharing)
  360. Issue.update_versions_from_sharing_change self
  361. end
  362. end
  363. end
  364. def update_default_project_version
  365. if @default_project_version && project.present?
  366. project.update_columns :default_version_id => id
  367. end
  368. end
  369. def referenced_by_a_custom_field?
  370. CustomValue.joins(:custom_field).
  371. where(:value => id.to_s, :custom_fields => {:field_format => 'version'}).any?
  372. end
  373. def nullify_projects_default_version
  374. Project.where(:default_version_id => id).update_all(:default_version_id => nil)
  375. end
  376. end