OSDN Git Service

Enhanced the Issue Bulk Copy feature:
[redminele/redmine.git] / app / models / workflow.rb
1 # redMine - project management software
2 # Copyright (C) 2006  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
18 class Workflow < ActiveRecord::Base
19   belongs_to :role
20   belongs_to :old_status, :class_name => 'IssueStatus', :foreign_key => 'old_status_id'
21   belongs_to :new_status, :class_name => 'IssueStatus', :foreign_key => 'new_status_id'
22
23   validates_presence_of :role, :old_status, :new_status
24   
25   # Returns workflow transitions count by tracker and role
26   def self.count_by_tracker_and_role
27     counts = connection.select_all("SELECT role_id, tracker_id, count(id) AS c FROM #{Workflow.table_name} GROUP BY role_id, tracker_id")
28     roles = Role.find(:all, :order => 'builtin, position')
29     trackers = Tracker.find(:all, :order => 'position')
30     
31     result = []
32     trackers.each do |tracker|
33       t = []
34       roles.each do |role|
35         row = counts.detect {|c| c['role_id'] == role.id.to_s && c['tracker_id'] == tracker.id.to_s}
36         t << [role, (row.nil? ? 0 : row['c'].to_i)]
37       end
38       result << [tracker, t]
39     end
40     
41     result
42   end
43
44   # Find potential statuses the user could be allowed to switch issues to
45   def self.available_statuses(project, user=User.current)
46     Workflow.find(:all,
47                   :include => :new_status,
48                   :conditions => {:role_id => user.roles_for_project(project).collect(&:id)}).
49       collect(&:new_status).
50       compact.
51       uniq.
52       sort
53   end
54 end