annotate .svn/pristine/cf/cfa38091d70dac5ed0710a8ebb3ecb812073f2f5.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 module Redmine
Chris@1295 19 module Activity
Chris@1295 20 # Class used to retrieve activity events
Chris@1295 21 class Fetcher
Chris@1295 22 attr_reader :user, :project, :scope
Chris@1295 23
Chris@1295 24 # Needs to be unloaded in development mode
Chris@1295 25 @@constantized_providers = Hash.new {|h,k| h[k] = Redmine::Activity.providers[k].collect {|t| t.constantize } }
Chris@1295 26
Chris@1295 27 def initialize(user, options={})
Chris@1295 28 options.assert_valid_keys(:project, :with_subprojects, :author)
Chris@1295 29 @user = user
Chris@1295 30 @project = options[:project]
Chris@1295 31 @options = options
Chris@1295 32
Chris@1295 33 @scope = event_types
Chris@1295 34 end
Chris@1295 35
Chris@1295 36 # Returns an array of available event types
Chris@1295 37 def event_types
Chris@1295 38 return @event_types unless @event_types.nil?
Chris@1295 39
Chris@1295 40 @event_types = Redmine::Activity.available_event_types
Chris@1295 41 @event_types = @event_types.select {|o| @project.self_and_descendants.detect {|p| @user.allowed_to?("view_#{o}".to_sym, p)}} if @project
Chris@1295 42 @event_types
Chris@1295 43 end
Chris@1295 44
Chris@1295 45 # Yields to filter the activity scope
Chris@1295 46 def scope_select(&block)
Chris@1295 47 @scope = @scope.select {|t| yield t }
Chris@1295 48 end
Chris@1295 49
Chris@1295 50 # Sets the scope
Chris@1295 51 # Argument can be :all, :default or an array of event types
Chris@1295 52 def scope=(s)
Chris@1295 53 case s
Chris@1295 54 when :all
Chris@1295 55 @scope = event_types
Chris@1295 56 when :default
Chris@1295 57 default_scope!
Chris@1295 58 else
Chris@1295 59 @scope = s & event_types
Chris@1295 60 end
Chris@1295 61 end
Chris@1295 62
Chris@1295 63 # Resets the scope to the default scope
Chris@1295 64 def default_scope!
Chris@1295 65 @scope = Redmine::Activity.default_event_types
Chris@1295 66 end
Chris@1295 67
Chris@1295 68 # Returns an array of events for the given date range
Chris@1295 69 # sorted in reverse chronological order
Chris@1295 70 def events(from = nil, to = nil, options={})
Chris@1295 71 e = []
Chris@1295 72 @options[:limit] = options[:limit]
Chris@1295 73
Chris@1295 74 @scope.each do |event_type|
Chris@1295 75 constantized_providers(event_type).each do |provider|
Chris@1295 76 e += provider.find_events(event_type, @user, from, to, @options)
Chris@1295 77 end
Chris@1295 78 end
Chris@1295 79
Chris@1295 80 e.sort! {|a,b| b.event_datetime <=> a.event_datetime}
Chris@1295 81
Chris@1295 82 if options[:limit]
Chris@1295 83 e = e.slice(0, options[:limit])
Chris@1295 84 end
Chris@1295 85 e
Chris@1295 86 end
Chris@1295 87
Chris@1295 88 private
Chris@1295 89
Chris@1295 90 def constantized_providers(event_type)
Chris@1295 91 @@constantized_providers[event_type]
Chris@1295 92 end
Chris@1295 93 end
Chris@1295 94 end
Chris@1295 95 end