HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux vmi1674223.contaboserver.net 5.4.0-182-generic #202-Ubuntu SMP Fri Apr 26 12:29:36 UTC 2024 x86_64
User: root (0)
PHP: 7.4.3-4ubuntu2.22
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //opt/openproject/app/controllers/activities_controller.rb
#-- encoding: UTF-8

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2020 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2017 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# 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.
#
# See docs/COPYRIGHT.rdoc for more details.
#++

class ActivitiesController < ApplicationController
  menu_item :activity
  before_action :find_optional_project,
                :verify_activities_module_activated,
                :determine_date_range,
                :determine_subprojects,
                :determine_author

  after_action :set_session

  accept_key_auth :index

  def index
    @activity = Activities::Fetcher.new(User.current,
                                        project: @project,
                                        with_subprojects: @with_subprojects,
                                        author: @author,
                                        scope: activity_scope)

    events = @activity.events(@date_from, @date_to)

    respond_to do |format|
      format.html do
        respond_html(events)
      end
      format.atom do
        respond_atom(events)
      end
    end
  rescue ActiveRecord::RecordNotFound => e
    op_handle_warning "Failed to find all resources in activities: #{e.message}"
    render_404 I18n.t(:error_can_not_find_all_resources)
  end

  private

  def verify_activities_module_activated
    render_403 if @project && !@project.module_enabled?('activity')
  end

  def determine_date_range
    @days = Setting.activity_days_default.to_i

    if params[:from]
      begin; @date_to = params[:from].to_date + 1.day; rescue; end
    end

    @date_to ||= User.current.today + 1.day
    @date_from = @date_to - @days
  end

  def determine_subprojects
    @with_subprojects = if params[:with_subprojects].nil?
                          Setting.display_subprojects_work_packages?
                        else
                          params[:with_subprojects] == '1'
                        end
  end

  def determine_author
    @author = params[:user_id].blank? ? nil : User.active.find(params[:user_id])
  end

  def respond_html(events)
    @events_by_day = events.group_by { |e| e.event_datetime.in_time_zone(User.current.time_zone).to_date }
    render layout: !request.xhr?
  end

  def respond_atom(events)
    title = t(:label_activity)
    if @author
      title = @author.name
    elsif @activity.scope.size == 1
      title = t("label_#{@activity.scope.first.singularize}_plural")
    end
    render_feed(events, title: "#{@project || Setting.app_title}: #{title}")
  end

  def activity_scope
    if params[:event_types]
      params[:event_types]
    elsif session[:activity]
      session[:activity]
    elsif @author.nil?
      :default
    else
      :all
    end
  end

  def set_session
    session[:activity] = @activity.scope
  end
end