diff --git a/app/helpers/welcome_helper.rb b/app/helpers/welcome_helper.rb index 545f4da7d..9218e3908 100644 --- a/app/helpers/welcome_helper.rb +++ b/app/helpers/welcome_helper.rb @@ -37,17 +37,17 @@ module WelcomeHelper end def find_all_hot_course - sort_course_by_hot + sort_course_by_hot end def find_all_hot_bid - sort_bid_by_hot + sort_bid_by_hot end - - def find_all_hot_contest - sort_contest_by_hot - end - + + def find_all_hot_contest + sort_contest_by_hot + end + private def search_project @@ -70,45 +70,54 @@ module WelcomeHelper end def sort_project_by_hot - return sort_project_by_hot_rails 0 - end - - def sort_course_by_hot - return sort_project_by_hot_rails 1 - end - - def sort_bid_by_hot - return sort_bid_by_hot_rails 1 - end - - def sort_contest_by_hot - return sort_bid_by_hot_rails 2 - end -#取得所有活动 - def find_all_activities - - end -#取得论坛数据 - def find_hot_forum_topics - - end - def sort_project_by_hot_rails project_type - limit = 10 - project_type == 0 ? Project.find_by_sql(" - SELECT p.id, p.name, p.description, p.identifier, t.project_id - FROM projects AS p RIGHT OUTER JOIN ( - SELECT project_id,grade FROM project_statuses - WHERE project_type = #{project_type} ORDER BY grade DESC LIMIT #{limit} ) AS t ON p.id = t.project_id ") - : Project.find_by_sql(" - SELECT p.id, p.name, p.description, p.identifier, t.project_id - FROM projects AS p RIGHT OUTER JOIN ( - SELECT project_id,grade FROM project_statuses - WHERE project_type = #{project_type} ORDER BY course_ac_para DESC LIMIT #{limit} ) AS t ON p.id = t.project_id ") - end - - def sort_bid_by_hot_rails reward_type - limit = 10 - Bid.visible.where('reward_type = ?', reward_type).reorder('bids.commit desc').limit(limit).all - end - + return sort_project_by_hot_rails 0 + end + + def sort_course_by_hot + return sort_project_by_hot_rails 1 + end + + def sort_bid_by_hot + return sort_bid_by_hot_rails 1 + end + + def sort_contest_by_hot + return sort_bid_by_hot_rails 2 + end + #取得所有活动 + def find_all_activities limit=10 + users = [] + activities = Activity.find_by_sql("select distinct user_id from activities order by id DESC limit #{limit}" ) + activities.each { |activity| + users << activity.user_id + } + user_objs = User.find_by_sql("SELECT * FROM users WHERE (users.id IN #{"(" << users.join(',') << ")"} )") + activity = Redmine::Activity::Fetcher.new(user_objs) + + activity.events_welcome(nil, nil, {:limit => limit}) + end + + #取得论坛数据 + def find_hot_forum_topics + + end + def sort_project_by_hot_rails project_type + limit = 10 + project_type == 0 ? Project.find_by_sql(" + SELECT p.id, p.name, p.description, p.identifier, t.project_id + FROM projects AS p RIGHT OUTER JOIN ( + SELECT project_id,grade FROM project_statuses + WHERE project_type = #{project_type} ORDER BY grade DESC LIMIT #{limit} ) AS t ON p.id = t.project_id ") + : Project.find_by_sql(" + SELECT p.id, p.name, p.description, p.identifier, t.project_id + FROM projects AS p RIGHT OUTER JOIN ( + SELECT project_id,grade FROM project_statuses + WHERE project_type = #{project_type} ORDER BY course_ac_para DESC LIMIT #{limit} ) AS t ON p.id = t.project_id ") + end + + def sort_bid_by_hot_rails reward_type + limit = 10 + Bid.visible.where('reward_type = ?', reward_type).reorder('bids.commit desc').limit(limit).all + end + end diff --git a/lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb b/lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb index f92aae250..4c0a1c8a5 100644 --- a/lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb +++ b/lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb @@ -1,88 +1,122 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -module Redmine - module Acts - module ActivityProvider - def self.included(base) - base.extend ClassMethods - end - - module ClassMethods - def acts_as_activity_provider(options = {}) - unless self.included_modules.include?(Redmine::Acts::ActivityProvider::InstanceMethods) - cattr_accessor :activity_provider_options - send :include, Redmine::Acts::ActivityProvider::InstanceMethods - end - - options.assert_valid_keys(:type, :permission, :timestamp, :author_key, :find_options) - self.activity_provider_options ||= {} - - # One model can provide different event types - # We store these options in activity_provider_options hash - event_type = options.delete(:type) || self.name.underscore.pluralize - - options[:timestamp] ||= "#{table_name}.created_on" - options[:find_options] ||= {} - options[:author_key] = "#{table_name}.#{options[:author_key]}" if options[:author_key].is_a?(Symbol) - self.activity_provider_options[event_type] = options - end - end - - module InstanceMethods - def self.included(base) - base.extend ClassMethods - end - - module ClassMethods - # Returns events of type event_type visible by user that occured between from and to - def find_events(event_type, user, from, to, options) - provider_options = activity_provider_options[event_type] - raise "#{self.name} can not provide #{event_type} events." if provider_options.nil? - - scope = self - - if from && to - scope = scope.scoped(:conditions => ["#{provider_options[:timestamp]} BETWEEN ? AND ?", from, to]) - end - - if options[:author] - return [] if provider_options[:author_key].nil? - scope = scope.scoped(:conditions => ["#{provider_options[:author_key]} = ?", options[:author].id]) - end - - if options[:limit] - # id and creation time should be in same order in most cases - scope = scope.scoped(:order => "#{table_name}.id DESC", :limit => options[:limit]) - end - - if provider_options.has_key?(:permission) - scope = scope.scoped(:conditions => Project.allowed_to_condition(user, provider_options[:permission] || :view_project, options)) - elsif respond_to?(:visible) - scope = scope.visible(user, options) - else - ActiveSupport::Deprecation.warn "acts_as_activity_provider with implicit :permission option is deprecated. Add a visible scope to the #{self.name} model or use explicit :permission option." - scope = scope.scoped(:conditions => Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym, options)) - end - - scope.all(provider_options[:find_options].dup) - end - end - end - end - end -end +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module Redmine + module Acts + module ActivityProvider + def self.included(base) + base.extend ClassMethods + end + + module ClassMethods + def acts_as_activity_provider(options = {}) + unless self.included_modules.include?(Redmine::Acts::ActivityProvider::InstanceMethods) + cattr_accessor :activity_provider_options + send :include, Redmine::Acts::ActivityProvider::InstanceMethods + end + + options.assert_valid_keys(:type, :permission, :timestamp, :author_key, :find_options) + self.activity_provider_options ||= {} + + # One model can provide different event types + # We store these options in activity_provider_options hash + event_type = options.delete(:type) || self.name.underscore.pluralize + + options[:timestamp] ||= "#{table_name}.created_on" + options[:find_options] ||= {} + options[:author_key] = "#{table_name}.#{options[:author_key]}" if options[:author_key].is_a?(Symbol) + self.activity_provider_options[event_type] = options + end + end + + module InstanceMethods + def self.included(base) + base.extend ClassMethods + end + + module ClassMethods + # Returns events of type event_type visible by user that occured between from and to + def find_events(event_type, user, from, to, options) + provider_options = activity_provider_options[event_type] + raise "#{self.name} can not provide #{event_type} events." if provider_options.nil? + + scope = self + + if from && to + scope = scope.scoped(:conditions => ["#{provider_options[:timestamp]} BETWEEN ? AND ?", from, to]) + end + + if options[:author] + return [] if provider_options[:author_key].nil? + scope = scope.scoped(:conditions => ["#{provider_options[:author_key]} = ?", options[:author].id]) + end + + if options[:limit] + # id and creation time should be in same order in most cases + scope = scope.scoped(:order => "#{table_name}.id DESC", :limit => options[:limit]) + end + + if provider_options.has_key?(:permission) + scope = scope.scoped(:conditions => Project.allowed_to_condition(user, provider_options[:permission] || :view_project, options)) + elsif respond_to?(:visible) + scope = scope.visible(user, options) + else + ActiveSupport::Deprecation.warn "acts_as_activity_provider with implicit :permission option is deprecated. Add a visible scope to the #{self.name} model or use explicit :permission option." + scope = scope.scoped(:conditions => Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym, options)) + end + + scope.all(provider_options[:find_options].dup) + end + + def find_events1(event_type, user, from, to, options) + provider_options = activity_provider_options[event_type] + raise "#{self.name} can not provide #{event_type} events." if provider_options.nil? + + scope = self + + if from && to + scope = scope.scoped(:conditions => ["#{provider_options[:timestamp]} BETWEEN ? AND ?", from, to]) + end + + if options[:author] + return [] if provider_options[:author_key].nil? + scope = scope.scoped(:conditions => ["#{provider_options[:author_key]} = ?", options[:author].id]) + end + + if options[:limit] + # id and creation time should be in same order in most cases + scope = scope.scoped(:order => "#{table_name}.id DESC", :limit => options[:limit]) + end + + if provider_options.has_key?(:permission) + user1 = User.find_by_login('user') + scope = scope.scoped(:conditions => Project.allowed_to_condition(user1, provider_options[:permission] || :view_project, options)) + elsif respond_to?(:visible) + scope = scope.visible(user1, options) + else + ActiveSupport::Deprecation.warn "acts_as_activity_provider with implicit :permission option is deprecated. Add a visible scope to the #{self.name} model or use explicit :permission option." + scope = scope.scoped(:conditions => Project.allowed_to_condition(user1, "view_#{self.name.underscore.pluralize}".to_sym, options)) + end + + scope.all(provider_options[:find_options].dup) + end + + end + end + end + end +end diff --git a/lib/redmine/activity/fetcher.rb b/lib/redmine/activity/fetcher.rb index c2e174ef2..6ea8c3258 100644 --- a/lib/redmine/activity/fetcher.rb +++ b/lib/redmine/activity/fetcher.rb @@ -1,95 +1,113 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -module Redmine - module Activity - # Class used to retrieve activity events - class Fetcher - attr_reader :user, :project, :scope - - # Needs to be unloaded in development mode - @@constantized_providers = Hash.new {|h,k| h[k] = Redmine::Activity.providers[k].collect {|t| t.constantize } } - - def initialize(user, options={}) - options.assert_valid_keys(:project, :with_subprojects, :author) - @user = user - @project = options[:project] - @options = options - - @scope = event_types - end - - # Returns an array of available event types - def event_types - return @event_types unless @event_types.nil? - - @event_types = Redmine::Activity.available_event_types - @event_types = @event_types.select {|o| @project.self_and_descendants.detect {|p| @user.allowed_to?("view_#{o}".to_sym, p)}} if @project - @event_types - end - - # Yields to filter the activity scope - def scope_select(&block) - @scope = @scope.select {|t| yield t } - end - - # Sets the scope - # Argument can be :all, :default or an array of event types - def scope=(s) - case s - when :all - @scope = event_types - when :default - default_scope! - else - @scope = s & event_types - end - end - - # Resets the scope to the default scope - def default_scope! - @scope = Redmine::Activity.default_event_types - end - - # Returns an array of events for the given date range - # sorted in reverse chronological order - def events(from = nil, to = nil, options={}) - e = [] - @options[:limit] = options[:limit] - - @scope.each do |event_type| - constantized_providers(event_type).each do |provider| - e += provider.find_events(event_type, @user, from, to, @options) - end - end - - e.sort! {|a,b| b.event_datetime <=> a.event_datetime} - - if options[:limit] - e = e.slice(0, options[:limit]) - end - e - end - - private - - def constantized_providers(event_type) - @@constantized_providers[event_type] - end - end - end -end +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module Redmine + module Activity + # Class used to retrieve activity events + class Fetcher + attr_reader :user, :project, :scope + + # Needs to be unloaded in development mode + @@constantized_providers = Hash.new {|h,k| h[k] = Redmine::Activity.providers[k].collect {|t| t.constantize } } + + def initialize(user, options={}) + options.assert_valid_keys(:project, :with_subprojects, :author) + @user = user + @project = options[:project] + @options = options + + @scope = event_types + end + + # Returns an array of available event types + def event_types + return @event_types unless @event_types.nil? + + @event_types = Redmine::Activity.available_event_types + @event_types = @event_types.select {|o| @project.self_and_descendants.detect {|p| @user.allowed_to?("view_#{o}".to_sym, p)}} if @project + @event_types + end + + # Yields to filter the activity scope + def scope_select(&block) + @scope = @scope.select {|t| yield t } + end + + # Sets the scope + # Argument can be :all, :default or an array of event types + def scope=(s) + case s + when :all + @scope = event_types + when :default + default_scope! + else + @scope = s & event_types + end + end + + # Resets the scope to the default scope + def default_scope! + @scope = Redmine::Activity.default_event_types + end + + # Returns an array of events for the given date range + # sorted in reverse chronological order + def events(from = nil, to = nil, options={}) + e = [] + @options[:limit] = options[:limit] + + @scope.each do |event_type| + constantized_providers(event_type).each do |provider| + e += provider.find_events(event_type, @user, from, to, @options) + end + end + + e.sort! {|a,b| b.event_datetime <=> a.event_datetime} + + if options[:limit] + e = e.slice(0, options[:limit]) + end + e + end + + def events_welcome(from = nil, to = nil, options={}) + e = [] + @options[:limit] = options[:limit] + + @scope.each do |event_type| + constantized_providers(event_type).each do |provider| + e += provider.find_events1(event_type, @user, from, to, @options) + end + end + + e.sort! {|a,b| b.event_datetime <=> a.event_datetime} + + if options[:limit] + e = e.slice(0, options[:limit]) + end + e + end + + private + + def constantized_providers(event_type) + @@constantized_providers[event_type] + end + end + end +end