Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

version.rb 11KB

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