##// END OF EJS Templates
Merged r3009 from trunk....
Eric Davis -
r2932:3d926660ba01
parent child
Show More
@@ -1,293 +1,293
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class TimelogController < ApplicationController
18 class TimelogController < ApplicationController
19 menu_item :issues
19 menu_item :issues
20 before_filter :find_project, :authorize, :only => [:edit, :destroy]
20 before_filter :find_project, :authorize, :only => [:edit, :destroy]
21 before_filter :find_optional_project, :only => [:report, :details]
21 before_filter :find_optional_project, :only => [:report, :details]
22
22
23 verify :method => :post, :only => :destroy, :redirect_to => { :action => :details }
23 verify :method => :post, :only => :destroy, :redirect_to => { :action => :details }
24
24
25 helper :sort
25 helper :sort
26 include SortHelper
26 include SortHelper
27 helper :issues
27 helper :issues
28 include TimelogHelper
28 include TimelogHelper
29 helper :custom_fields
29 helper :custom_fields
30 include CustomFieldsHelper
30 include CustomFieldsHelper
31
31
32 def report
32 def report
33 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
33 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
34 :klass => Project,
34 :klass => Project,
35 :label => :label_project},
35 :label => :label_project},
36 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
36 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
37 :klass => Version,
37 :klass => Version,
38 :label => :label_version},
38 :label => :label_version},
39 'category' => {:sql => "#{Issue.table_name}.category_id",
39 'category' => {:sql => "#{Issue.table_name}.category_id",
40 :klass => IssueCategory,
40 :klass => IssueCategory,
41 :label => :field_category},
41 :label => :field_category},
42 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
42 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
43 :klass => User,
43 :klass => User,
44 :label => :label_member},
44 :label => :label_member},
45 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
45 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
46 :klass => Tracker,
46 :klass => Tracker,
47 :label => :label_tracker},
47 :label => :label_tracker},
48 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
48 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
49 :klass => Enumeration,
49 :klass => Enumeration,
50 :label => :label_activity},
50 :label => :label_activity},
51 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
51 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
52 :klass => Issue,
52 :klass => Issue,
53 :label => :label_issue}
53 :label => :label_issue}
54 }
54 }
55
55
56 # Add list and boolean custom fields as available criterias
56 # Add list and boolean custom fields as available criterias
57 custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
57 custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
58 custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
58 custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
59 @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)",
59 @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)",
60 :format => cf.field_format,
60 :format => cf.field_format,
61 :label => cf.name}
61 :label => cf.name}
62 end if @project
62 end if @project
63
63
64 # Add list and boolean time entry custom fields
64 # Add list and boolean time entry custom fields
65 TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
65 TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
66 @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)",
66 @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)",
67 :format => cf.field_format,
67 :format => cf.field_format,
68 :label => cf.name}
68 :label => cf.name}
69 end
69 end
70
70
71 @criterias = params[:criterias] || []
71 @criterias = params[:criterias] || []
72 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
72 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
73 @criterias.uniq!
73 @criterias.uniq!
74 @criterias = @criterias[0,3]
74 @criterias = @criterias[0,3]
75
75
76 @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
76 @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
77
77
78 retrieve_date_range
78 retrieve_date_range
79
79
80 unless @criterias.empty?
80 unless @criterias.empty?
81 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
81 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
82 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
82 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
83
83
84 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
84 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
85 sql << " FROM #{TimeEntry.table_name}"
85 sql << " FROM #{TimeEntry.table_name}"
86 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
86 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
87 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
87 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
88 sql << " WHERE"
88 sql << " WHERE"
89 sql << " (%s) AND" % @project.project_condition(Setting.display_subprojects_issues?) if @project
89 sql << " (%s) AND" % @project.project_condition(Setting.display_subprojects_issues?) if @project
90 sql << " (%s) AND" % Project.allowed_to_condition(User.current, :view_time_entries)
90 sql << " (%s) AND" % Project.allowed_to_condition(User.current, :view_time_entries)
91 sql << " (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)]
91 sql << " (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from), ActiveRecord::Base.connection.quoted_date(@to)]
92 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
92 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
93
93
94 @hours = ActiveRecord::Base.connection.select_all(sql)
94 @hours = ActiveRecord::Base.connection.select_all(sql)
95
95
96 @hours.each do |row|
96 @hours.each do |row|
97 case @columns
97 case @columns
98 when 'year'
98 when 'year'
99 row['year'] = row['tyear']
99 row['year'] = row['tyear']
100 when 'month'
100 when 'month'
101 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
101 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
102 when 'week'
102 when 'week'
103 row['week'] = "#{row['tyear']}-#{row['tweek']}"
103 row['week'] = "#{row['tyear']}-#{row['tweek']}"
104 when 'day'
104 when 'day'
105 row['day'] = "#{row['spent_on']}"
105 row['day'] = "#{row['spent_on']}"
106 end
106 end
107 end
107 end
108
108
109 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
109 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
110
110
111 @periods = []
111 @periods = []
112 # Date#at_beginning_of_ not supported in Rails 1.2.x
112 # Date#at_beginning_of_ not supported in Rails 1.2.x
113 date_from = @from.to_time
113 date_from = @from.to_time
114 # 100 columns max
114 # 100 columns max
115 while date_from <= @to.to_time && @periods.length < 100
115 while date_from <= @to.to_time && @periods.length < 100
116 case @columns
116 case @columns
117 when 'year'
117 when 'year'
118 @periods << "#{date_from.year}"
118 @periods << "#{date_from.year}"
119 date_from = (date_from + 1.year).at_beginning_of_year
119 date_from = (date_from + 1.year).at_beginning_of_year
120 when 'month'
120 when 'month'
121 @periods << "#{date_from.year}-#{date_from.month}"
121 @periods << "#{date_from.year}-#{date_from.month}"
122 date_from = (date_from + 1.month).at_beginning_of_month
122 date_from = (date_from + 1.month).at_beginning_of_month
123 when 'week'
123 when 'week'
124 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
124 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
125 date_from = (date_from + 7.day).at_beginning_of_week
125 date_from = (date_from + 7.day).at_beginning_of_week
126 when 'day'
126 when 'day'
127 @periods << "#{date_from.to_date}"
127 @periods << "#{date_from.to_date}"
128 date_from = date_from + 1.day
128 date_from = date_from + 1.day
129 end
129 end
130 end
130 end
131 end
131 end
132
132
133 respond_to do |format|
133 respond_to do |format|
134 format.html { render :layout => !request.xhr? }
134 format.html { render :layout => !request.xhr? }
135 format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') }
135 format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') }
136 end
136 end
137 end
137 end
138
138
139 def details
139 def details
140 sort_init 'spent_on', 'desc'
140 sort_init 'spent_on', 'desc'
141 sort_update 'spent_on' => 'spent_on',
141 sort_update 'spent_on' => 'spent_on',
142 'user' => 'user_id',
142 'user' => 'user_id',
143 'activity' => 'activity_id',
143 'activity' => 'activity_id',
144 'project' => "#{Project.table_name}.name",
144 'project' => "#{Project.table_name}.name",
145 'issue' => 'issue_id',
145 'issue' => 'issue_id',
146 'hours' => 'hours'
146 'hours' => 'hours'
147
147
148 cond = ARCondition.new
148 cond = ARCondition.new
149 if @project.nil?
149 if @project.nil?
150 cond << Project.allowed_to_condition(User.current, :view_time_entries)
150 cond << Project.allowed_to_condition(User.current, :view_time_entries)
151 elsif @issue.nil?
151 elsif @issue.nil?
152 cond << @project.project_condition(Setting.display_subprojects_issues?)
152 cond << @project.project_condition(Setting.display_subprojects_issues?)
153 else
153 else
154 cond << ["#{TimeEntry.table_name}.issue_id = ?", @issue.id]
154 cond << ["#{TimeEntry.table_name}.issue_id = ?", @issue.id]
155 end
155 end
156
156
157 retrieve_date_range
157 retrieve_date_range
158 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
158 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
159
159
160 TimeEntry.visible_by(User.current) do
160 TimeEntry.visible_by(User.current) do
161 respond_to do |format|
161 respond_to do |format|
162 format.html {
162 format.html {
163 # Paginate results
163 # Paginate results
164 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
164 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
165 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
165 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
166 @entries = TimeEntry.find(:all,
166 @entries = TimeEntry.find(:all,
167 :include => [:project, :activity, :user, {:issue => :tracker}],
167 :include => [:project, :activity, :user, {:issue => :tracker}],
168 :conditions => cond.conditions,
168 :conditions => cond.conditions,
169 :order => sort_clause,
169 :order => sort_clause,
170 :limit => @entry_pages.items_per_page,
170 :limit => @entry_pages.items_per_page,
171 :offset => @entry_pages.current.offset)
171 :offset => @entry_pages.current.offset)
172 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
172 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
173
173
174 render :layout => !request.xhr?
174 render :layout => !request.xhr?
175 }
175 }
176 format.atom {
176 format.atom {
177 entries = TimeEntry.find(:all,
177 entries = TimeEntry.find(:all,
178 :include => [:project, :activity, :user, {:issue => :tracker}],
178 :include => [:project, :activity, :user, {:issue => :tracker}],
179 :conditions => cond.conditions,
179 :conditions => cond.conditions,
180 :order => "#{TimeEntry.table_name}.created_on DESC",
180 :order => "#{TimeEntry.table_name}.created_on DESC",
181 :limit => Setting.feeds_limit.to_i)
181 :limit => Setting.feeds_limit.to_i)
182 render_feed(entries, :title => l(:label_spent_time))
182 render_feed(entries, :title => l(:label_spent_time))
183 }
183 }
184 format.csv {
184 format.csv {
185 # Export all entries
185 # Export all entries
186 @entries = TimeEntry.find(:all,
186 @entries = TimeEntry.find(:all,
187 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
187 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
188 :conditions => cond.conditions,
188 :conditions => cond.conditions,
189 :order => sort_clause)
189 :order => sort_clause)
190 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
190 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
191 }
191 }
192 end
192 end
193 end
193 end
194 end
194 end
195
195
196 def edit
196 def edit
197 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
197 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
198 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
198 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
199 @time_entry.attributes = params[:time_entry]
199 @time_entry.attributes = params[:time_entry]
200
200
201 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
201 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
202
202
203 if request.post? and @time_entry.save
203 if request.post? and @time_entry.save
204 flash[:notice] = l(:notice_successful_update)
204 flash[:notice] = l(:notice_successful_update)
205 redirect_back_or_default :action => 'details', :project_id => @time_entry.project
205 redirect_back_or_default :action => 'details', :project_id => @time_entry.project
206 return
206 return
207 end
207 end
208 end
208 end
209
209
210 def destroy
210 def destroy
211 render_404 and return unless @time_entry
211 render_404 and return unless @time_entry
212 render_403 and return unless @time_entry.editable_by?(User.current)
212 render_403 and return unless @time_entry.editable_by?(User.current)
213 @time_entry.destroy
213 @time_entry.destroy
214 flash[:notice] = l(:notice_successful_delete)
214 flash[:notice] = l(:notice_successful_delete)
215 redirect_to :back
215 redirect_to :back
216 rescue ::ActionController::RedirectBackError
216 rescue ::ActionController::RedirectBackError
217 redirect_to :action => 'details', :project_id => @time_entry.project
217 redirect_to :action => 'details', :project_id => @time_entry.project
218 end
218 end
219
219
220 private
220 private
221 def find_project
221 def find_project
222 if params[:id]
222 if params[:id]
223 @time_entry = TimeEntry.find(params[:id])
223 @time_entry = TimeEntry.find(params[:id])
224 @project = @time_entry.project
224 @project = @time_entry.project
225 elsif params[:issue_id]
225 elsif params[:issue_id]
226 @issue = Issue.find(params[:issue_id])
226 @issue = Issue.find(params[:issue_id])
227 @project = @issue.project
227 @project = @issue.project
228 elsif params[:project_id]
228 elsif params[:project_id]
229 @project = Project.find(params[:project_id])
229 @project = Project.find(params[:project_id])
230 else
230 else
231 render_404
231 render_404
232 return false
232 return false
233 end
233 end
234 rescue ActiveRecord::RecordNotFound
234 rescue ActiveRecord::RecordNotFound
235 render_404
235 render_404
236 end
236 end
237
237
238 def find_optional_project
238 def find_optional_project
239 if !params[:issue_id].blank?
239 if !params[:issue_id].blank?
240 @issue = Issue.find(params[:issue_id])
240 @issue = Issue.find(params[:issue_id])
241 @project = @issue.project
241 @project = @issue.project
242 elsif !params[:project_id].blank?
242 elsif !params[:project_id].blank?
243 @project = Project.find(params[:project_id])
243 @project = Project.find(params[:project_id])
244 end
244 end
245 deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true)
245 deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true)
246 end
246 end
247
247
248 # Retrieves the date range based on predefined ranges or specific from/to param dates
248 # Retrieves the date range based on predefined ranges or specific from/to param dates
249 def retrieve_date_range
249 def retrieve_date_range
250 @free_period = false
250 @free_period = false
251 @from, @to = nil, nil
251 @from, @to = nil, nil
252
252
253 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
253 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
254 case params[:period].to_s
254 case params[:period].to_s
255 when 'today'
255 when 'today'
256 @from = @to = Date.today
256 @from = @to = Date.today
257 when 'yesterday'
257 when 'yesterday'
258 @from = @to = Date.today - 1
258 @from = @to = Date.today - 1
259 when 'current_week'
259 when 'current_week'
260 @from = Date.today - (Date.today.cwday - 1)%7
260 @from = Date.today - (Date.today.cwday - 1)%7
261 @to = @from + 6
261 @to = @from + 6
262 when 'last_week'
262 when 'last_week'
263 @from = Date.today - 7 - (Date.today.cwday - 1)%7
263 @from = Date.today - 7 - (Date.today.cwday - 1)%7
264 @to = @from + 6
264 @to = @from + 6
265 when '7_days'
265 when '7_days'
266 @from = Date.today - 7
266 @from = Date.today - 7
267 @to = Date.today
267 @to = Date.today
268 when 'current_month'
268 when 'current_month'
269 @from = Date.civil(Date.today.year, Date.today.month, 1)
269 @from = Date.civil(Date.today.year, Date.today.month, 1)
270 @to = (@from >> 1) - 1
270 @to = (@from >> 1) - 1
271 when 'last_month'
271 when 'last_month'
272 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
272 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
273 @to = (@from >> 1) - 1
273 @to = (@from >> 1) - 1
274 when '30_days'
274 when '30_days'
275 @from = Date.today - 30
275 @from = Date.today - 30
276 @to = Date.today
276 @to = Date.today
277 when 'current_year'
277 when 'current_year'
278 @from = Date.civil(Date.today.year, 1, 1)
278 @from = Date.civil(Date.today.year, 1, 1)
279 @to = Date.civil(Date.today.year, 12, 31)
279 @to = Date.civil(Date.today.year, 12, 31)
280 end
280 end
281 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
281 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
282 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
282 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
283 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
283 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
284 @free_period = true
284 @free_period = true
285 else
285 else
286 # default
286 # default
287 end
287 end
288
288
289 @from, @to = @to, @from if @from && @to && @from > @to
289 @from, @to = @to, @from if @from && @to && @from > @to
290 @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today) - 1
290 @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today) - 1
291 @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today)
291 @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today)
292 end
292 end
293 end
293 end
@@ -1,278 +1,294
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'timelog_controller'
19 require 'timelog_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class TimelogController; def rescue_action(e) raise e end; end
22 class TimelogController; def rescue_action(e) raise e end; end
23
23
24 class TimelogControllerTest < Test::Unit::TestCase
24 class TimelogControllerTest < Test::Unit::TestCase
25 fixtures :projects, :enabled_modules, :roles, :members, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses, :custom_fields, :custom_values
25 fixtures :projects, :enabled_modules, :roles, :members, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses, :custom_fields, :custom_values
26
26
27 def setup
27 def setup
28 @controller = TimelogController.new
28 @controller = TimelogController.new
29 @request = ActionController::TestRequest.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
30 @response = ActionController::TestResponse.new
31 end
31 end
32
32
33 def test_get_edit
33 def test_get_edit
34 @request.session[:user_id] = 3
34 @request.session[:user_id] = 3
35 get :edit, :project_id => 1
35 get :edit, :project_id => 1
36 assert_response :success
36 assert_response :success
37 assert_template 'edit'
37 assert_template 'edit'
38 # Default activity selected
38 # Default activity selected
39 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
39 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
40 :content => 'Development'
40 :content => 'Development'
41 end
41 end
42
42
43 def test_post_edit
43 def test_post_edit
44 @request.session[:user_id] = 3
44 @request.session[:user_id] = 3
45 post :edit, :project_id => 1,
45 post :edit, :project_id => 1,
46 :time_entry => {:comments => 'Some work on TimelogControllerTest',
46 :time_entry => {:comments => 'Some work on TimelogControllerTest',
47 # Not the default activity
47 # Not the default activity
48 :activity_id => '11',
48 :activity_id => '11',
49 :spent_on => '2008-03-14',
49 :spent_on => '2008-03-14',
50 :issue_id => '1',
50 :issue_id => '1',
51 :hours => '7.3'}
51 :hours => '7.3'}
52 assert_redirected_to 'projects/ecookbook/timelog/details'
52 assert_redirected_to 'projects/ecookbook/timelog/details'
53
53
54 i = Issue.find(1)
54 i = Issue.find(1)
55 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
55 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
56 assert_not_nil t
56 assert_not_nil t
57 assert_equal 11, t.activity_id
57 assert_equal 11, t.activity_id
58 assert_equal 7.3, t.hours
58 assert_equal 7.3, t.hours
59 assert_equal 3, t.user_id
59 assert_equal 3, t.user_id
60 assert_equal i, t.issue
60 assert_equal i, t.issue
61 assert_equal i.project, t.project
61 assert_equal i.project, t.project
62 end
62 end
63
63
64 def test_update
64 def test_update
65 entry = TimeEntry.find(1)
65 entry = TimeEntry.find(1)
66 assert_equal 1, entry.issue_id
66 assert_equal 1, entry.issue_id
67 assert_equal 2, entry.user_id
67 assert_equal 2, entry.user_id
68
68
69 @request.session[:user_id] = 1
69 @request.session[:user_id] = 1
70 post :edit, :id => 1,
70 post :edit, :id => 1,
71 :time_entry => {:issue_id => '2',
71 :time_entry => {:issue_id => '2',
72 :hours => '8'}
72 :hours => '8'}
73 assert_redirected_to 'projects/ecookbook/timelog/details'
73 assert_redirected_to 'projects/ecookbook/timelog/details'
74 entry.reload
74 entry.reload
75
75
76 assert_equal 8, entry.hours
76 assert_equal 8, entry.hours
77 assert_equal 2, entry.issue_id
77 assert_equal 2, entry.issue_id
78 assert_equal 2, entry.user_id
78 assert_equal 2, entry.user_id
79 end
79 end
80
80
81 def test_destroy
81 def test_destroy
82 @request.session[:user_id] = 2
82 @request.session[:user_id] = 2
83 post :destroy, :id => 1
83 post :destroy, :id => 1
84 assert_redirected_to 'projects/ecookbook/timelog/details'
84 assert_redirected_to 'projects/ecookbook/timelog/details'
85 assert_nil TimeEntry.find_by_id(1)
85 assert_nil TimeEntry.find_by_id(1)
86 end
86 end
87
87
88 def test_report_no_criteria
88 def test_report_no_criteria
89 get :report, :project_id => 1
89 get :report, :project_id => 1
90 assert_response :success
90 assert_response :success
91 assert_template 'report'
91 assert_template 'report'
92 end
92 end
93
93
94 def test_report_all_projects
94 def test_report_all_projects
95 get :report
95 get :report
96 assert_response :success
96 assert_response :success
97 assert_template 'report'
97 assert_template 'report'
98 end
98 end
99
99
100 def test_report_all_projects_denied
100 def test_report_all_projects_denied
101 r = Role.anonymous
101 r = Role.anonymous
102 r.permissions.delete(:view_time_entries)
102 r.permissions.delete(:view_time_entries)
103 r.permissions_will_change!
103 r.permissions_will_change!
104 r.save
104 r.save
105 get :report
105 get :report
106 assert_redirected_to '/account/login'
106 assert_redirected_to '/account/login'
107 end
107 end
108
108
109 def test_report_all_projects_one_criteria
109 def test_report_all_projects_one_criteria
110 get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
110 get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
111 assert_response :success
111 assert_response :success
112 assert_template 'report'
112 assert_template 'report'
113 assert_not_nil assigns(:total_hours)
113 assert_not_nil assigns(:total_hours)
114 assert_equal "8.65", "%.2f" % assigns(:total_hours)
114 assert_equal "8.65", "%.2f" % assigns(:total_hours)
115 end
115 end
116
116
117 def test_report_all_time
117 def test_report_all_time
118 get :report, :project_id => 1, :criterias => ['project', 'issue']
118 get :report, :project_id => 1, :criterias => ['project', 'issue']
119 assert_response :success
119 assert_response :success
120 assert_template 'report'
120 assert_template 'report'
121 assert_not_nil assigns(:total_hours)
121 assert_not_nil assigns(:total_hours)
122 assert_equal "162.90", "%.2f" % assigns(:total_hours)
122 assert_equal "162.90", "%.2f" % assigns(:total_hours)
123 end
123 end
124
124
125 def test_report_all_time_by_day
125 def test_report_all_time_by_day
126 get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day'
126 get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day'
127 assert_response :success
127 assert_response :success
128 assert_template 'report'
128 assert_template 'report'
129 assert_not_nil assigns(:total_hours)
129 assert_not_nil assigns(:total_hours)
130 assert_equal "162.90", "%.2f" % assigns(:total_hours)
130 assert_equal "162.90", "%.2f" % assigns(:total_hours)
131 assert_tag :tag => 'th', :content => '2007-03-12'
131 assert_tag :tag => 'th', :content => '2007-03-12'
132 end
132 end
133
133
134 def test_report_one_criteria
134 def test_report_one_criteria
135 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
135 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
136 assert_response :success
136 assert_response :success
137 assert_template 'report'
137 assert_template 'report'
138 assert_not_nil assigns(:total_hours)
138 assert_not_nil assigns(:total_hours)
139 assert_equal "8.65", "%.2f" % assigns(:total_hours)
139 assert_equal "8.65", "%.2f" % assigns(:total_hours)
140 end
140 end
141
141
142 def test_report_two_criterias
142 def test_report_two_criterias
143 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
143 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
144 assert_response :success
144 assert_response :success
145 assert_template 'report'
145 assert_template 'report'
146 assert_not_nil assigns(:total_hours)
146 assert_not_nil assigns(:total_hours)
147 assert_equal "162.90", "%.2f" % assigns(:total_hours)
147 assert_equal "162.90", "%.2f" % assigns(:total_hours)
148 end
148 end
149
149
150 def test_report_one_day
151 get :report, :project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criterias => ["member", "activity"]
152 assert_response :success
153 assert_template 'report'
154 assert_not_nil assigns(:total_hours)
155 assert_equal "4.25", "%.2f" % assigns(:total_hours)
156 end
157
150 def test_report_custom_field_criteria
158 def test_report_custom_field_criteria
151 get :report, :project_id => 1, :criterias => ['project', 'cf_1']
159 get :report, :project_id => 1, :criterias => ['project', 'cf_1']
152 assert_response :success
160 assert_response :success
153 assert_template 'report'
161 assert_template 'report'
154 assert_not_nil assigns(:total_hours)
162 assert_not_nil assigns(:total_hours)
155 assert_not_nil assigns(:criterias)
163 assert_not_nil assigns(:criterias)
156 assert_equal 2, assigns(:criterias).size
164 assert_equal 2, assigns(:criterias).size
157 assert_equal "162.90", "%.2f" % assigns(:total_hours)
165 assert_equal "162.90", "%.2f" % assigns(:total_hours)
158 # Custom field column
166 # Custom field column
159 assert_tag :tag => 'th', :content => 'Database'
167 assert_tag :tag => 'th', :content => 'Database'
160 # Custom field row
168 # Custom field row
161 assert_tag :tag => 'td', :content => 'MySQL',
169 assert_tag :tag => 'td', :content => 'MySQL',
162 :sibling => { :tag => 'td', :attributes => { :class => 'hours' },
170 :sibling => { :tag => 'td', :attributes => { :class => 'hours' },
163 :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
171 :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
164 :content => '1' }}
172 :content => '1' }}
165 end
173 end
166
174
167 def test_report_one_criteria_no_result
175 def test_report_one_criteria_no_result
168 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
176 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
169 assert_response :success
177 assert_response :success
170 assert_template 'report'
178 assert_template 'report'
171 assert_not_nil assigns(:total_hours)
179 assert_not_nil assigns(:total_hours)
172 assert_equal "0.00", "%.2f" % assigns(:total_hours)
180 assert_equal "0.00", "%.2f" % assigns(:total_hours)
173 end
181 end
174
182
175 def test_report_all_projects_csv_export
183 def test_report_all_projects_csv_export
176 get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
184 get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
177 assert_response :success
185 assert_response :success
178 assert_equal 'text/csv', @response.content_type
186 assert_equal 'text/csv', @response.content_type
179 lines = @response.body.chomp.split("\n")
187 lines = @response.body.chomp.split("\n")
180 # Headers
188 # Headers
181 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
189 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
182 # Total row
190 # Total row
183 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
191 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
184 end
192 end
185
193
186 def test_report_csv_export
194 def test_report_csv_export
187 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
195 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
188 assert_response :success
196 assert_response :success
189 assert_equal 'text/csv', @response.content_type
197 assert_equal 'text/csv', @response.content_type
190 lines = @response.body.chomp.split("\n")
198 lines = @response.body.chomp.split("\n")
191 # Headers
199 # Headers
192 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
200 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
193 # Total row
201 # Total row
194 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
202 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
195 end
203 end
196
204
197 def test_details_all_projects
205 def test_details_all_projects
198 get :details
206 get :details
199 assert_response :success
207 assert_response :success
200 assert_template 'details'
208 assert_template 'details'
201 assert_not_nil assigns(:total_hours)
209 assert_not_nil assigns(:total_hours)
202 assert_equal "162.90", "%.2f" % assigns(:total_hours)
210 assert_equal "162.90", "%.2f" % assigns(:total_hours)
203 end
211 end
204
212
205 def test_details_at_project_level
213 def test_details_at_project_level
206 get :details, :project_id => 1
214 get :details, :project_id => 1
207 assert_response :success
215 assert_response :success
208 assert_template 'details'
216 assert_template 'details'
209 assert_not_nil assigns(:entries)
217 assert_not_nil assigns(:entries)
210 assert_equal 4, assigns(:entries).size
218 assert_equal 4, assigns(:entries).size
211 # project and subproject
219 # project and subproject
212 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
220 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
213 assert_not_nil assigns(:total_hours)
221 assert_not_nil assigns(:total_hours)
214 assert_equal "162.90", "%.2f" % assigns(:total_hours)
222 assert_equal "162.90", "%.2f" % assigns(:total_hours)
215 # display all time by default
223 # display all time by default
216 assert_equal '2007-03-11'.to_date, assigns(:from)
224 assert_equal '2007-03-11'.to_date, assigns(:from)
217 assert_equal '2007-04-22'.to_date, assigns(:to)
225 assert_equal '2007-04-22'.to_date, assigns(:to)
218 end
226 end
219
227
220 def test_details_at_project_level_with_date_range
228 def test_details_at_project_level_with_date_range
221 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
229 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
222 assert_response :success
230 assert_response :success
223 assert_template 'details'
231 assert_template 'details'
224 assert_not_nil assigns(:entries)
232 assert_not_nil assigns(:entries)
225 assert_equal 3, assigns(:entries).size
233 assert_equal 3, assigns(:entries).size
226 assert_not_nil assigns(:total_hours)
234 assert_not_nil assigns(:total_hours)
227 assert_equal "12.90", "%.2f" % assigns(:total_hours)
235 assert_equal "12.90", "%.2f" % assigns(:total_hours)
228 assert_equal '2007-03-20'.to_date, assigns(:from)
236 assert_equal '2007-03-20'.to_date, assigns(:from)
229 assert_equal '2007-04-30'.to_date, assigns(:to)
237 assert_equal '2007-04-30'.to_date, assigns(:to)
230 end
238 end
231
239
232 def test_details_at_project_level_with_period
240 def test_details_at_project_level_with_period
233 get :details, :project_id => 1, :period => '7_days'
241 get :details, :project_id => 1, :period => '7_days'
234 assert_response :success
242 assert_response :success
235 assert_template 'details'
243 assert_template 'details'
236 assert_not_nil assigns(:entries)
244 assert_not_nil assigns(:entries)
237 assert_not_nil assigns(:total_hours)
245 assert_not_nil assigns(:total_hours)
238 assert_equal Date.today - 7, assigns(:from)
246 assert_equal Date.today - 7, assigns(:from)
239 assert_equal Date.today, assigns(:to)
247 assert_equal Date.today, assigns(:to)
240 end
248 end
249
250 def test_details_one_day
251 get :details, :project_id => 1, :from => "2007-03-23", :to => "2007-03-23"
252 assert_response :success
253 assert_template 'details'
254 assert_not_nil assigns(:total_hours)
255 assert_equal "4.25", "%.2f" % assigns(:total_hours)
256 end
241
257
242 def test_details_at_issue_level
258 def test_details_at_issue_level
243 get :details, :issue_id => 1
259 get :details, :issue_id => 1
244 assert_response :success
260 assert_response :success
245 assert_template 'details'
261 assert_template 'details'
246 assert_not_nil assigns(:entries)
262 assert_not_nil assigns(:entries)
247 assert_equal 2, assigns(:entries).size
263 assert_equal 2, assigns(:entries).size
248 assert_not_nil assigns(:total_hours)
264 assert_not_nil assigns(:total_hours)
249 assert_equal 154.25, assigns(:total_hours)
265 assert_equal 154.25, assigns(:total_hours)
250 # display all time by default
266 # display all time by default
251 assert_equal '2007-03-11'.to_date, assigns(:from)
267 assert_equal '2007-03-11'.to_date, assigns(:from)
252 assert_equal '2007-04-22'.to_date, assigns(:to)
268 assert_equal '2007-04-22'.to_date, assigns(:to)
253 end
269 end
254
270
255 def test_details_atom_feed
271 def test_details_atom_feed
256 get :details, :project_id => 1, :format => 'atom'
272 get :details, :project_id => 1, :format => 'atom'
257 assert_response :success
273 assert_response :success
258 assert_equal 'application/atom+xml', @response.content_type
274 assert_equal 'application/atom+xml', @response.content_type
259 assert_not_nil assigns(:items)
275 assert_not_nil assigns(:items)
260 assert assigns(:items).first.is_a?(TimeEntry)
276 assert assigns(:items).first.is_a?(TimeEntry)
261 end
277 end
262
278
263 def test_details_all_projects_csv_export
279 def test_details_all_projects_csv_export
264 get :details, :format => 'csv'
280 get :details, :format => 'csv'
265 assert_response :success
281 assert_response :success
266 assert_equal 'text/csv', @response.content_type
282 assert_equal 'text/csv', @response.content_type
267 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
283 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
268 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
284 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
269 end
285 end
270
286
271 def test_details_csv_export
287 def test_details_csv_export
272 get :details, :project_id => 1, :format => 'csv'
288 get :details, :project_id => 1, :format => 'csv'
273 assert_response :success
289 assert_response :success
274 assert_equal 'text/csv', @response.content_type
290 assert_equal 'text/csv', @response.content_type
275 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
291 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
276 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
292 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
277 end
293 end
278 end
294 end
General Comments 0
You need to be logged in to leave comments. Login now