Revision 912:5e80956cc792 lib/redmine/activity

View differences:

lib/redmine/activity/fetcher.rb
1 1
# Redmine - project management software
2
# Copyright (C) 2006-2008  Jean-Philippe Lang
2
# Copyright (C) 2006-2011  Jean-Philippe Lang
3 3
#
4 4
# This program is free software; you can redistribute it and/or
5 5
# modify it under the terms of the GNU General Public License
6 6
# as published by the Free Software Foundation; either version 2
7 7
# of the License, or (at your option) any later version.
8
# 
8
#
9 9
# This program is distributed in the hope that it will be useful,
10 10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 12
# GNU General Public License for more details.
13
# 
13
#
14 14
# You should have received a copy of the GNU General Public License
15 15
# along with this program; if not, write to the Free Software
16 16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
......
20 20
    # Class used to retrieve activity events
21 21
    class Fetcher
22 22
      attr_reader :user, :project, :scope
23
      
23

  
24 24
      # Needs to be unloaded in development mode
25 25
      @@constantized_providers = Hash.new {|h,k| h[k] = Redmine::Activity.providers[k].collect {|t| t.constantize } }
26
      
26

  
27 27
      def initialize(user, options={})
28 28
        options.assert_valid_keys(:project, :with_subprojects, :author)
29 29
        @user = user
30 30
        @project = options[:project]
31 31
        @options = options
32
        
32

  
33 33
        @scope = event_types
34 34
      end
35
      
35

  
36 36
      # Returns an array of available event types
37 37
      def event_types
38 38
        return @event_types unless @event_types.nil?
39
        
39

  
40 40
        @event_types = Redmine::Activity.available_event_types
41 41
        @event_types = @event_types.select {|o| @project.self_and_descendants.detect {|p| @user.allowed_to?("view_#{o}".to_sym, p)}} if @project
42 42
        @event_types
43 43
      end
44
      
44

  
45 45
      # Yields to filter the activity scope
46 46
      def scope_select(&block)
47 47
        @scope = @scope.select {|t| yield t }
48 48
      end
49
      
49

  
50 50
      # Sets the scope
51 51
      # Argument can be :all, :default or an array of event types
52 52
      def scope=(s)
......
59 59
          @scope = s & event_types
60 60
        end
61 61
      end
62
      
62

  
63 63
      # Resets the scope to the default scope
64 64
      def default_scope!
65 65
        @scope = Redmine::Activity.default_event_types
66 66
      end
67
      
67

  
68 68
      # Returns an array of events for the given date range
69 69
      # sorted in reverse chronological order
70 70
      def events(from = nil, to = nil, options={})
71 71
        e = []
72 72
        @options[:limit] = options[:limit]
73
        
73

  
74 74
        @scope.each do |event_type|
75 75
          constantized_providers(event_type).each do |provider|
76 76
            e += provider.find_events(event_type, @user, from, to, @options)
77 77
          end
78 78
        end
79
        
79

  
80 80
        e.sort! {|a,b| b.event_datetime <=> a.event_datetime}
81
        
81

  
82 82
        if options[:limit]
83 83
          e = e.slice(0, options[:limit])
84 84
        end
85 85
        e
86 86
      end
87
      
87

  
88 88
      private
89
      
89

  
90 90
      def constantized_providers(event_type)
91 91
        @@constantized_providers[event_type]
92 92
      end

Also available in: Unified diff