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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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. 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], :case_sensitive => false
  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. def safe_attributes=(attrs, user=User.current)
  147. if attrs.respond_to?(:to_unsafe_hash)
  148. attrs = attrs.to_unsafe_hash
  149. end
  150. return unless attrs.is_a?(Hash)
  151. attrs = attrs.deep_dup
  152. # Reject custom fields values not visible by the user
  153. if attrs['custom_field_values'].present?
  154. editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s}
  155. attrs['custom_field_values'].reject! {|k, v| !editable_custom_field_ids.include?(k.to_s)}
  156. end
  157. # Reject custom fields not visible by the user
  158. if attrs['custom_fields'].present?
  159. editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s}
  160. attrs['custom_fields'].reject! {|c| !editable_custom_field_ids.include?(c['id'].to_s)}
  161. end
  162. super(attrs, user)
  163. end
  164. # Returns true if +user+ or current user is allowed to view the version
  165. def visible?(user=User.current)
  166. user.allowed_to?(:view_issues, self.project)
  167. end
  168. # Returns the custom_field_values that can be edited by the given user
  169. def editable_custom_field_values(user=nil)
  170. visible_custom_field_values(user)
  171. end
  172. def visible_custom_field_values(user = nil)
  173. user ||= User.current
  174. custom_field_values.select do |value|
  175. value.custom_field.visible_by?(project, user)
  176. end
  177. end
  178. # Version files have same visibility as project files
  179. def attachments_visible?(*args)
  180. project.present? && project.attachments_visible?(*args)
  181. end
  182. def attachments_deletable?(usr=User.current)
  183. project.present? && project.attachments_deletable?(usr)
  184. end
  185. alias :base_reload :reload
  186. def reload(*args)
  187. @default_project_version = nil
  188. @visible_fixed_issues = nil
  189. base_reload(*args)
  190. end
  191. def start_date
  192. @start_date ||= fixed_issues.minimum('start_date')
  193. end
  194. def due_date
  195. effective_date
  196. end
  197. def due_date=(arg)
  198. self.effective_date=(arg)
  199. end
  200. # Returns the total estimated time for this version
  201. # (sum of leaves estimated_hours)
  202. def estimated_hours
  203. fixed_issues.estimated_hours
  204. end
  205. # Returns the total reported time for this version
  206. def spent_hours
  207. @spent_hours ||= TimeEntry.joins(:issue).where("#{Issue.table_name}.fixed_version_id = ?", id).sum(:hours).to_f
  208. end
  209. def closed?
  210. status == 'closed'
  211. end
  212. def open?
  213. status == 'open'
  214. end
  215. # Returns true if the version is completed: closed or due date reached and no open issues
  216. def completed?
  217. closed? || (effective_date && (effective_date < User.current.today) && (open_issues_count == 0))
  218. end
  219. def behind_schedule?
  220. if completed_percent == 100
  221. return false
  222. elsif due_date && start_date
  223. done_date = start_date + ((due_date - start_date+1)* completed_percent/100).floor
  224. return done_date <= User.current.today
  225. else
  226. false # No issues so it's not late
  227. end
  228. end
  229. # Returns the completion percentage of this version based on the amount of open/closed issues
  230. # and the time spent on the open issues.
  231. def completed_percent
  232. fixed_issues.completed_percent
  233. end
  234. # Returns the percentage of issues that have been marked as 'closed'.
  235. def closed_percent
  236. fixed_issues.closed_percent
  237. end
  238. # Returns true if the version is overdue: due date reached and some open issues
  239. def overdue?
  240. effective_date && (effective_date < User.current.today) && (open_issues_count > 0)
  241. end
  242. # Returns assigned issues count
  243. def issues_count
  244. fixed_issues.count
  245. end
  246. # Returns the total amount of open issues for this version.
  247. def open_issues_count
  248. fixed_issues.open_count
  249. end
  250. # Returns the total amount of closed issues for this version.
  251. def closed_issues_count
  252. fixed_issues.closed_count
  253. end
  254. def visible_fixed_issues
  255. @visible_fixed_issues ||= fixed_issues.visible
  256. end
  257. def wiki_page
  258. if project.wiki && !wiki_page_title.blank?
  259. @wiki_page ||= project.wiki.find_page(wiki_page_title)
  260. end
  261. @wiki_page
  262. end
  263. def to_s; name end
  264. def to_s_with_project
  265. "#{project} - #{name}"
  266. end
  267. # Versions are sorted by effective_date and name
  268. # Those with no effective_date are at the end, sorted by name
  269. def <=>(version)
  270. if self.effective_date
  271. if version.effective_date
  272. if self.effective_date == version.effective_date
  273. name == version.name ? id <=> version.id : name <=> version.name
  274. else
  275. self.effective_date <=> version.effective_date
  276. end
  277. else
  278. -1
  279. end
  280. else
  281. if version.effective_date
  282. 1
  283. else
  284. name == version.name ? id <=> version.id : name <=> version.name
  285. end
  286. end
  287. end
  288. # Sort versions by status (open, locked then closed versions)
  289. def self.sort_by_status(versions)
  290. versions.sort do |a, b|
  291. if a.status == b.status
  292. a <=> b
  293. else
  294. b.status <=> a.status
  295. end
  296. end
  297. end
  298. def css_classes
  299. [
  300. completed? ? 'version-completed' : 'version-incompleted',
  301. "version-#{status}"
  302. ].join(' ')
  303. end
  304. def self.fields_for_order_statement(table=nil)
  305. table ||= table_name
  306. [Arel.sql("(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)"), "#{table}.effective_date", "#{table}.name", "#{table}.id"]
  307. end
  308. scope :sorted, lambda { order(fields_for_order_statement) }
  309. # Returns the sharings that +user+ can set the version to
  310. def allowed_sharings(user = User.current)
  311. VERSION_SHARINGS.select do |s|
  312. if sharing == s
  313. true
  314. else
  315. case s
  316. when 'system'
  317. # Only admin users can set a systemwide sharing
  318. user.admin?
  319. when 'hierarchy', 'tree'
  320. # Only users allowed to manage versions of the root project can
  321. # set sharing to hierarchy or tree
  322. project.nil? || user.allowed_to?(:manage_versions, project.root)
  323. else
  324. true
  325. end
  326. end
  327. end
  328. end
  329. # Returns true if the version is shared, otherwise false
  330. def shared?
  331. sharing != 'none'
  332. end
  333. def deletable?
  334. fixed_issues.empty? && !referenced_by_a_custom_field? && attachments.empty?
  335. end
  336. def default_project_version
  337. if @default_project_version.nil?
  338. project.present? && project.default_version == self
  339. else
  340. @default_project_version
  341. end
  342. end
  343. def default_project_version=(arg)
  344. @default_project_version = (arg == '1' || arg == true)
  345. end
  346. private
  347. # Update the issue's fixed versions. Used if a version's sharing changes.
  348. def update_issues_from_sharing_change
  349. if saved_change_to_sharing?
  350. if VERSION_SHARINGS.index(sharing_before_last_save).nil? ||
  351. VERSION_SHARINGS.index(sharing).nil? ||
  352. VERSION_SHARINGS.index(sharing_before_last_save) > VERSION_SHARINGS.index(sharing)
  353. Issue.update_versions_from_sharing_change self
  354. end
  355. end
  356. end
  357. def update_default_project_version
  358. if @default_project_version && project.present?
  359. project.update_columns :default_version_id => id
  360. end
  361. end
  362. def referenced_by_a_custom_field?
  363. CustomValue.joins(:custom_field).
  364. where(:value => id.to_s, :custom_fields => {:field_format => 'version'}).any?
  365. end
  366. def nullify_projects_default_version
  367. Project.where(:default_version_id => id).update_all(:default_version_id => nil)
  368. end
  369. end