Class: TimelogController

Inherits:
ApplicationController show all
Includes:
CustomFieldsHelper, SortHelper, TimelogHelper
Defined in:
app/controllers/timelog_controller.rb

Overview

redMine - project management software Copyright (C) 2006-2007 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.

Instance Method Summary (collapse)

Methods included from CustomFieldsHelper

#custom_field_formats_for_select, #custom_field_label_tag, #custom_field_tag, #custom_field_tag_with_label, #custom_fields_tabs, #format_value, #show_value

Methods included from TimelogHelper

#activity_collection_for_select_options, #entries_to_csv, #format_criteria_value, #options_for_period_select, #render_timelog_breadcrumb, #report_criteria_to_csv, #report_to_csv, #select_hours, #sum_hours, #to_utf8

Methods included from ApplicationHelper

#accesskey, #authoring, #authorize_for, #avatar, #back_url_hidden_field_tag, #breadcrumb, #calendar_for, #check_all_links, #content_for, #context_menu_link, #current_role, #due_date_distance_in_words, #format_activity_day, #format_activity_description, #format_activity_title, #has_content?, #html_hours, #html_title, #image_to_function, #include_calendar_headers_tags, #label_tag_for, #labelled_tabular_form_for, #lang_options_for_select, #link_to_attachment, #link_to_if_authorized, #link_to_issue, #link_to_remote_if_authorized, #link_to_user, #other_formats_links, #page_header_title, #pagination_links_full, #path_to_stylesheet, #per_page_links, #progress_bar, #project_nested_ul, #project_tree, #project_tree_options_for_select, #prompt_to_remote, #render_flash_messages, #render_page_hierarchy, #render_project_jump_box, #reorder_links, #simple_format_without_paragraph, #stylesheet_path, #syntax_highlight, #textilizable, #to_path_param, #toggle_link, #truncate_single_line

Methods included from Redmine::I18n

#current_language, #day_name, #find_language, #format_date, #format_time, included, #l, #l_hours, #l_or_humanize, #ll, #month_name, #set_language_if_valid, #valid_languages

Methods included from Redmine::WikiFormatting::Macros::Definitions

#exec_macro, #extract_macro_options

Methods included from SortHelper

#sort_clause, #sort_clear, #sort_header_tag, #sort_init, #sort_link, #sort_name, #sort_update

Methods inherited from ApplicationController

accept_key_auth, #accept_key_auth_actions, #attach_files, #authorize, #check_if_login_required, #check_project_privacy, #current_role, #deny_access, #filename_for_content_disposition, #find_current_user, #logged_user=, #parse_qvalues, #per_page_option, #redirect_back_or_default, #render_403, #render_404, #render_error, #render_feed, #require_admin, #require_login, #set_localization, #user_setup

Methods included from Redmine::MenuManager::MenuController

#current_menu_item, included, #menu_items, #redirect_to_project_menu_item

Instance Method Details

- (Object) destroy



207
208
209
210
211
212
213
214
215
# File 'app/controllers/timelog_controller.rb', line 207

def destroy
  render_404 and return unless @time_entry
  render_403 and return unless @time_entry.editable_by?(User.current)
  @time_entry.destroy
  flash[:notice] = l(:notice_successful_delete)
  redirect_to :back
rescue ::ActionController::RedirectBackError
  redirect_to :action => 'details', :project_id => @time_entry.project
end

- (Object) details



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/controllers/timelog_controller.rb', line 139

def details
  sort_init 'spent_on', 'desc'
  sort_update 'spent_on' => 'spent_on',
              'user' => 'user_id',
              'activity' => 'activity_id',
              'project' => "#{Project.table_name}.name",
              'issue' => 'issue_id',
              'hours' => 'hours'
  
  cond = ARCondition.new
  if @project.nil?
    cond << Project.allowed_to_condition(User.current, :view_time_entries)
  elsif @issue.nil?
    cond << @project.project_condition(Setting.display_subprojects_issues?)
  else
    cond << ["#{TimeEntry.table_name}.issue_id = ?", @issue.id]
  end
  
  retrieve_date_range
  cond << ['spent_on BETWEEN ? AND ?', @from, @to]

  TimeEntry.visible_by(User.current) do
    respond_to do |format|
      format.html {
        # Paginate results
        @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
        @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
        @entries = TimeEntry.find(:all, 
                                  :include => [:project, :activity, :user, {:issue => :tracker}],
                                  :conditions => cond.conditions,
                                  :order => sort_clause,
                                  :limit  =>  @entry_pages.items_per_page,
                                  :offset =>  @entry_pages.current.offset)
        @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f

        render :layout => !request.xhr?
      }
      format.atom {
        entries = TimeEntry.find(:all,
                                 :include => [:project, :activity, :user, {:issue => :tracker}],
                                 :conditions => cond.conditions,
                                 :order => "#{TimeEntry.table_name}.created_on DESC",
                                 :limit => Setting.feeds_limit.to_i)
        render_feed(entries, :title => l(:label_spent_time))
      }
      format.csv {
        # Export all entries
        @entries = TimeEntry.find(:all, 
                                  :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
                                  :conditions => cond.conditions,
                                  :order => sort_clause)
        send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
      }
    end
  end
end

- (Object) edit



196
197
198
199
200
201
202
203
204
205
# File 'app/controllers/timelog_controller.rb', line 196

def edit
  render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
  @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
  @time_entry.attributes = params[:time_entry]
  if request.post? and @time_entry.save
    flash[:notice] = l(:notice_successful_update)
    redirect_back_or_default :action => 'details', :project_id => @time_entry.project
    return
  end    
end

- (Object) report



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/controllers/timelog_controller.rb', line 32

def report
  @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
                                        :klass => Project,
                                        :label => :label_project},
                           'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
                                        :klass => Version,
                                        :label => :label_version},
                           'category' => {:sql => "#{Issue.table_name}.category_id",
                                          :klass => IssueCategory,
                                          :label => :field_category},
                           'member' => {:sql => "#{TimeEntry.table_name}.user_id",
                                       :klass => User,
                                       :label => :label_member},
                           'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
                                        :klass => Tracker,
                                        :label => :label_tracker},
                           'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
                                         :klass => Enumeration,
                                         :label => :label_activity},
                           'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
                                       :klass => Issue,
                                       :label => :label_issue}
                         }
  
  # Add list and boolean custom fields as available criterias
  custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
  custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
    @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = #{Issue.table_name}.id)",
                                           :format => cf.field_format,
                                           :label => cf.name}
  end if @project
  
  # Add list and boolean time entry custom fields
  TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
    @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'TimeEntry' AND c.customized_id = #{TimeEntry.table_name}.id)",
                                           :format => cf.field_format,
                                           :label => cf.name}
  end
  
  @criterias = params[:criterias] || []
  @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
  @criterias.uniq!
  @criterias = @criterias[0,3]
  
  @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
  
  retrieve_date_range
  
  unless @criterias.empty?
    sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
    sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
    
    sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
    sql << " FROM #{TimeEntry.table_name}"
    sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
    sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
    sql << " WHERE"
    sql << " (%s) AND" % @project.project_condition(Setting.display_subprojects_issues?) if @project
    sql << " (%s) AND" % Project.allowed_to_condition(User.current, :view_time_entries)
    sql << " (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)]
    sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
    
    @hours = ActiveRecord::Base.connection.select_all(sql)
    
    @hours.each do |row|
      case @columns
      when 'year'
        row['year'] = row['tyear']
      when 'month'
        row['month'] = "#{row['tyear']}-#{row['tmonth']}"
      when 'week'
        row['week'] = "#{row['tyear']}-#{row['tweek']}"
      when 'day'
        row['day'] = "#{row['spent_on']}"
      end
    end
    
    @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
    
    @periods = []
    # Date#at_beginning_of_ not supported in Rails 1.2.x
    date_from = @from.to_time
    # 100 columns max
    while date_from <= @to.to_time && @periods.length < 100
      case @columns
      when 'year'
        @periods << "#{date_from.year}"
        date_from = (date_from + 1.year).at_beginning_of_year
      when 'month'
        @periods << "#{date_from.year}-#{date_from.month}"
        date_from = (date_from + 1.month).at_beginning_of_month
      when 'week'
        @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
        date_from = (date_from + 7.day).at_beginning_of_week
      when 'day'
        @periods << "#{date_from.to_date}"
        date_from = date_from + 1.day
      end
    end
  end
  
  respond_to do |format|
    format.html { render :layout => !request.xhr? }
    format.csv  { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') }
  end
end