1 # Redmine - project management software
2 # Copyright (C) 2006-2011 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 module Redmine
19 module Activity
20 # Class used to retrieve activity events
21 class Fetcher
22 attr_reader :user, :project, :scope
23
24 # Needs to be unloaded in development mode
25 @@constantized_providers = Hash.new {|h,k| h[k] = Redmine::Activity.providers[k].collect {|t| t.constantize } }
26
27 def initialize(user, options={})
28 options.assert_valid_keys(:project, :with_subprojects, :author)
29 @user = user
30 @project = options[:project]
31 @options = options
32
33 @scope = event_types
34 end
35
36 # Returns an array of available event types
37 def event_types
38 return @event_types unless @event_types.nil?
39
40 @event_types = Redmine::Activity.available_event_types
41 @event_types = @event_types.select {|o| @project.self_and_descendants.detect {|p| @user.allowed_to?("view_#{o}".to_sym, p)}} if @project
42 @event_types
43 end
44
45 # Yields to filter the activity scope
46 def scope_select(&block)
47 @scope = @scope.select {|t| yield t }
48 end
49
50 # Sets the scope
51 # Argument can be :all, :default or an array of event types
52 def scope=(s)
53 case s
54 when :all
55 @scope = event_types
56 when :default
57 default_scope!
58 else
59 @scope = s & event_types
60 end
61 end
62
63 # Resets the scope to the default scope
64 def default_scope!
65 @scope = Redmine::Activity.default_event_types
66 end
67
68 # Returns an array of events for the given date range
69 # sorted in reverse chronological order
70 def events(from = nil, to = nil, options={})
71 e = []
72 @options[:limit] = options[:limit]
73
74 @scope.each do |event_type|
75 constantized_providers(event_type).each do |provider|
76 e += provider.find_events(event_type, @user, from, to, @options)
77 end
78 end
79
80 e.sort! {|a,b| b.event_datetime <=> a.event_datetime}
81
82 if options[:limit]
83 e = e.slice(0, options[:limit])
84 end
85 e
86 end
87
88 private
89
90 def constantized_providers(event_type)
91 @@constantized_providers[event_type]
92 end
93 end
94 end
95 end