##// END OF EJS Templates
Adds atom feed on time entries details (#1479)....
Jean-Philippe Lang -
r1546:062a2d6f5d6e
parent child
Show More
@@ -1,254 +1,262
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 layout 'base'
19 layout 'base'
20 menu_item :issues
20 menu_item :issues
21 before_filter :find_project, :authorize
21 before_filter :find_project, :authorize
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 @project.all_custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
57 @project.all_custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
58 @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM custom_values c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = issues.id)",
58 @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM custom_values c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = issues.id)",
59 :format => cf.field_format,
59 :format => cf.field_format,
60 :label => cf.name}
60 :label => cf.name}
61 end
61 end
62
62
63 @criterias = params[:criterias] || []
63 @criterias = params[:criterias] || []
64 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
64 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
65 @criterias.uniq!
65 @criterias.uniq!
66 @criterias = @criterias[0,3]
66 @criterias = @criterias[0,3]
67
67
68 @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
68 @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
69
69
70 retrieve_date_range
70 retrieve_date_range
71
71
72 unless @criterias.empty?
72 unless @criterias.empty?
73 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
73 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
74 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
74 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
75
75
76 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
76 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
77 sql << " FROM #{TimeEntry.table_name}"
77 sql << " FROM #{TimeEntry.table_name}"
78 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
78 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
79 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
79 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
80 sql << " WHERE (%s)" % @project.project_condition(Setting.display_subprojects_issues?)
80 sql << " WHERE (%s)" % @project.project_condition(Setting.display_subprojects_issues?)
81 sql << " AND (%s)" % Project.allowed_to_condition(User.current, :view_time_entries)
81 sql << " AND (%s)" % Project.allowed_to_condition(User.current, :view_time_entries)
82 sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)]
82 sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)]
83 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
83 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
84
84
85 @hours = ActiveRecord::Base.connection.select_all(sql)
85 @hours = ActiveRecord::Base.connection.select_all(sql)
86
86
87 @hours.each do |row|
87 @hours.each do |row|
88 case @columns
88 case @columns
89 when 'year'
89 when 'year'
90 row['year'] = row['tyear']
90 row['year'] = row['tyear']
91 when 'month'
91 when 'month'
92 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
92 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
93 when 'week'
93 when 'week'
94 row['week'] = "#{row['tyear']}-#{row['tweek']}"
94 row['week'] = "#{row['tyear']}-#{row['tweek']}"
95 when 'day'
95 when 'day'
96 row['day'] = "#{row['spent_on']}"
96 row['day'] = "#{row['spent_on']}"
97 end
97 end
98 end
98 end
99
99
100 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
100 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
101
101
102 @periods = []
102 @periods = []
103 # Date#at_beginning_of_ not supported in Rails 1.2.x
103 # Date#at_beginning_of_ not supported in Rails 1.2.x
104 date_from = @from.to_time
104 date_from = @from.to_time
105 # 100 columns max
105 # 100 columns max
106 while date_from <= @to.to_time && @periods.length < 100
106 while date_from <= @to.to_time && @periods.length < 100
107 case @columns
107 case @columns
108 when 'year'
108 when 'year'
109 @periods << "#{date_from.year}"
109 @periods << "#{date_from.year}"
110 date_from = (date_from + 1.year).at_beginning_of_year
110 date_from = (date_from + 1.year).at_beginning_of_year
111 when 'month'
111 when 'month'
112 @periods << "#{date_from.year}-#{date_from.month}"
112 @periods << "#{date_from.year}-#{date_from.month}"
113 date_from = (date_from + 1.month).at_beginning_of_month
113 date_from = (date_from + 1.month).at_beginning_of_month
114 when 'week'
114 when 'week'
115 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
115 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
116 date_from = (date_from + 7.day).at_beginning_of_week
116 date_from = (date_from + 7.day).at_beginning_of_week
117 when 'day'
117 when 'day'
118 @periods << "#{date_from.to_date}"
118 @periods << "#{date_from.to_date}"
119 date_from = date_from + 1.day
119 date_from = date_from + 1.day
120 end
120 end
121 end
121 end
122 end
122 end
123
123
124 respond_to do |format|
124 respond_to do |format|
125 format.html { render :layout => !request.xhr? }
125 format.html { render :layout => !request.xhr? }
126 format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') }
126 format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') }
127 end
127 end
128 end
128 end
129
129
130 def details
130 def details
131 sort_init 'spent_on', 'desc'
131 sort_init 'spent_on', 'desc'
132 sort_update
132 sort_update
133
133
134 cond = ARCondition.new
134 cond = ARCondition.new
135 cond << (@issue.nil? ? @project.project_condition(Setting.display_subprojects_issues?) :
135 cond << (@issue.nil? ? @project.project_condition(Setting.display_subprojects_issues?) :
136 ["#{TimeEntry.table_name}.issue_id = ?", @issue.id])
136 ["#{TimeEntry.table_name}.issue_id = ?", @issue.id])
137
137
138 retrieve_date_range
138 retrieve_date_range
139 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
139 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
140
140
141 TimeEntry.visible_by(User.current) do
141 TimeEntry.visible_by(User.current) do
142 respond_to do |format|
142 respond_to do |format|
143 format.html {
143 format.html {
144 # Paginate results
144 # Paginate results
145 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
145 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
146 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
146 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
147 @entries = TimeEntry.find(:all,
147 @entries = TimeEntry.find(:all,
148 :include => [:project, :activity, :user, {:issue => :tracker}],
148 :include => [:project, :activity, :user, {:issue => :tracker}],
149 :conditions => cond.conditions,
149 :conditions => cond.conditions,
150 :order => sort_clause,
150 :order => sort_clause,
151 :limit => @entry_pages.items_per_page,
151 :limit => @entry_pages.items_per_page,
152 :offset => @entry_pages.current.offset)
152 :offset => @entry_pages.current.offset)
153 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
153 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
154
154
155 render :layout => !request.xhr?
155 render :layout => !request.xhr?
156 }
156 }
157 format.atom {
158 entries = TimeEntry.find(:all,
159 :include => [:project, :activity, :user, {:issue => :tracker}],
160 :conditions => cond.conditions,
161 :order => "#{TimeEntry.table_name}.created_on DESC",
162 :limit => Setting.feeds_limit.to_i)
163 render_feed(entries, :title => l(:label_spent_time))
164 }
157 format.csv {
165 format.csv {
158 # Export all entries
166 # Export all entries
159 @entries = TimeEntry.find(:all,
167 @entries = TimeEntry.find(:all,
160 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
168 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
161 :conditions => cond.conditions,
169 :conditions => cond.conditions,
162 :order => sort_clause)
170 :order => sort_clause)
163 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
171 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
164 }
172 }
165 end
173 end
166 end
174 end
167 end
175 end
168
176
169 def edit
177 def edit
170 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
178 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
171 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
179 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
172 @time_entry.attributes = params[:time_entry]
180 @time_entry.attributes = params[:time_entry]
173 if request.post? and @time_entry.save
181 if request.post? and @time_entry.save
174 flash[:notice] = l(:notice_successful_update)
182 flash[:notice] = l(:notice_successful_update)
175 redirect_to(params[:back_url] || {:action => 'details', :project_id => @time_entry.project})
183 redirect_to(params[:back_url] || {:action => 'details', :project_id => @time_entry.project})
176 return
184 return
177 end
185 end
178 @activities = Enumeration::get_values('ACTI')
186 @activities = Enumeration::get_values('ACTI')
179 end
187 end
180
188
181 def destroy
189 def destroy
182 render_404 and return unless @time_entry
190 render_404 and return unless @time_entry
183 render_403 and return unless @time_entry.editable_by?(User.current)
191 render_403 and return unless @time_entry.editable_by?(User.current)
184 @time_entry.destroy
192 @time_entry.destroy
185 flash[:notice] = l(:notice_successful_delete)
193 flash[:notice] = l(:notice_successful_delete)
186 redirect_to :back
194 redirect_to :back
187 rescue RedirectBackError
195 rescue RedirectBackError
188 redirect_to :action => 'details', :project_id => @time_entry.project
196 redirect_to :action => 'details', :project_id => @time_entry.project
189 end
197 end
190
198
191 private
199 private
192 def find_project
200 def find_project
193 if params[:id]
201 if params[:id]
194 @time_entry = TimeEntry.find(params[:id])
202 @time_entry = TimeEntry.find(params[:id])
195 @project = @time_entry.project
203 @project = @time_entry.project
196 elsif params[:issue_id]
204 elsif params[:issue_id]
197 @issue = Issue.find(params[:issue_id])
205 @issue = Issue.find(params[:issue_id])
198 @project = @issue.project
206 @project = @issue.project
199 elsif params[:project_id]
207 elsif params[:project_id]
200 @project = Project.find(params[:project_id])
208 @project = Project.find(params[:project_id])
201 else
209 else
202 render_404
210 render_404
203 return false
211 return false
204 end
212 end
205 rescue ActiveRecord::RecordNotFound
213 rescue ActiveRecord::RecordNotFound
206 render_404
214 render_404
207 end
215 end
208
216
209 # Retrieves the date range based on predefined ranges or specific from/to param dates
217 # Retrieves the date range based on predefined ranges or specific from/to param dates
210 def retrieve_date_range
218 def retrieve_date_range
211 @free_period = false
219 @free_period = false
212 @from, @to = nil, nil
220 @from, @to = nil, nil
213
221
214 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
222 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
215 case params[:period].to_s
223 case params[:period].to_s
216 when 'today'
224 when 'today'
217 @from = @to = Date.today
225 @from = @to = Date.today
218 when 'yesterday'
226 when 'yesterday'
219 @from = @to = Date.today - 1
227 @from = @to = Date.today - 1
220 when 'current_week'
228 when 'current_week'
221 @from = Date.today - (Date.today.cwday - 1)%7
229 @from = Date.today - (Date.today.cwday - 1)%7
222 @to = @from + 6
230 @to = @from + 6
223 when 'last_week'
231 when 'last_week'
224 @from = Date.today - 7 - (Date.today.cwday - 1)%7
232 @from = Date.today - 7 - (Date.today.cwday - 1)%7
225 @to = @from + 6
233 @to = @from + 6
226 when '7_days'
234 when '7_days'
227 @from = Date.today - 7
235 @from = Date.today - 7
228 @to = Date.today
236 @to = Date.today
229 when 'current_month'
237 when 'current_month'
230 @from = Date.civil(Date.today.year, Date.today.month, 1)
238 @from = Date.civil(Date.today.year, Date.today.month, 1)
231 @to = (@from >> 1) - 1
239 @to = (@from >> 1) - 1
232 when 'last_month'
240 when 'last_month'
233 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
241 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
234 @to = (@from >> 1) - 1
242 @to = (@from >> 1) - 1
235 when '30_days'
243 when '30_days'
236 @from = Date.today - 30
244 @from = Date.today - 30
237 @to = Date.today
245 @to = Date.today
238 when 'current_year'
246 when 'current_year'
239 @from = Date.civil(Date.today.year, 1, 1)
247 @from = Date.civil(Date.today.year, 1, 1)
240 @to = Date.civil(Date.today.year, 12, 31)
248 @to = Date.civil(Date.today.year, 12, 31)
241 end
249 end
242 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
250 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
243 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
251 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
244 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
252 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
245 @free_period = true
253 @free_period = true
246 else
254 else
247 # default
255 # default
248 end
256 end
249
257
250 @from, @to = @to, @from if @from && @to && @from > @to
258 @from, @to = @to, @from if @from && @to && @from > @to
251 @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today) - 1
259 @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today) - 1
252 @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today)
260 @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today)
253 end
261 end
254 end
262 end
@@ -1,73 +1,78
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
2 # Copyright (C) 2006-2008 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 TimeEntry < ActiveRecord::Base
18 class TimeEntry < ActiveRecord::Base
19 # could have used polymorphic association
19 # could have used polymorphic association
20 # project association here allows easy loading of time entries at project level with one database trip
20 # project association here allows easy loading of time entries at project level with one database trip
21 belongs_to :project
21 belongs_to :project
22 belongs_to :issue
22 belongs_to :issue
23 belongs_to :user
23 belongs_to :user
24 belongs_to :activity, :class_name => 'Enumeration', :foreign_key => :activity_id
24 belongs_to :activity, :class_name => 'Enumeration', :foreign_key => :activity_id
25
25
26 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
26 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
27
27
28 acts_as_event :title => Proc.new {|o| "#{o.user}: #{lwr(:label_f_hour, o.hours)} (#{(o.issue || o.project).event_title})"},
29 :url => Proc.new {|o| {:controller => 'timelog', :action => 'details', :project_id => o.project}},
30 :author => :user,
31 :description => :comments
32
28 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
33 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
29 validates_numericality_of :hours, :allow_nil => true
34 validates_numericality_of :hours, :allow_nil => true
30 validates_length_of :comments, :maximum => 255
35 validates_length_of :comments, :maximum => 255
31
36
32 def after_initialize
37 def after_initialize
33 if new_record? && self.activity.nil?
38 if new_record? && self.activity.nil?
34 if default_activity = Enumeration.default('ACTI')
39 if default_activity = Enumeration.default('ACTI')
35 self.activity_id = default_activity.id
40 self.activity_id = default_activity.id
36 end
41 end
37 end
42 end
38 end
43 end
39
44
40 def before_validation
45 def before_validation
41 self.project = issue.project if issue && project.nil?
46 self.project = issue.project if issue && project.nil?
42 end
47 end
43
48
44 def validate
49 def validate
45 errors.add :hours, :activerecord_error_invalid if hours && (hours < 0 || hours >= 1000)
50 errors.add :hours, :activerecord_error_invalid if hours && (hours < 0 || hours >= 1000)
46 errors.add :project_id, :activerecord_error_invalid if project.nil?
51 errors.add :project_id, :activerecord_error_invalid if project.nil?
47 errors.add :issue_id, :activerecord_error_invalid if (issue_id && !issue) || (issue && project!=issue.project)
52 errors.add :issue_id, :activerecord_error_invalid if (issue_id && !issue) || (issue && project!=issue.project)
48 end
53 end
49
54
50 def hours=(h)
55 def hours=(h)
51 write_attribute :hours, (h.is_a?(String) ? h.to_hours : h)
56 write_attribute :hours, (h.is_a?(String) ? h.to_hours : h)
52 end
57 end
53
58
54 # tyear, tmonth, tweek assigned where setting spent_on attributes
59 # tyear, tmonth, tweek assigned where setting spent_on attributes
55 # these attributes make time aggregations easier
60 # these attributes make time aggregations easier
56 def spent_on=(date)
61 def spent_on=(date)
57 super
62 super
58 self.tyear = spent_on ? spent_on.year : nil
63 self.tyear = spent_on ? spent_on.year : nil
59 self.tmonth = spent_on ? spent_on.month : nil
64 self.tmonth = spent_on ? spent_on.month : nil
60 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
65 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
61 end
66 end
62
67
63 # Returns true if the time entry can be edited by usr, otherwise false
68 # Returns true if the time entry can be edited by usr, otherwise false
64 def editable_by?(usr)
69 def editable_by?(usr)
65 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
70 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
66 end
71 end
67
72
68 def self.visible_by(usr)
73 def self.visible_by(usr)
69 with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
74 with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
70 yield
75 yield
71 end
76 end
72 end
77 end
73 end
78 end
@@ -1,31 +1,36
1 <div class="contextual">
1 <div class="contextual">
2 <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'edit', :project_id => @project, :issue_id => @issue}, :class => 'icon icon-time' %>
2 <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'edit', :project_id => @project, :issue_id => @issue}, :class => 'icon icon-time' %>
3 </div>
3 </div>
4
4
5 <h2><%= l(:label_spent_time) %></h2>
5 <h2><%= l(:label_spent_time) %></h2>
6
6
7 <% if @issue %>
7 <% if @issue %>
8 <h3><%= link_to(@project.name, {:action => 'details', :project_id => @project}) %> / <%= link_to_issue(@issue) %></h3>
8 <h3><%= link_to(@project.name, {:action => 'details', :project_id => @project}) %> / <%= link_to_issue(@issue) %></h3>
9 <% end %>
9 <% end %>
10
10
11 <% form_remote_tag( :url => {}, :method => :get, :update => 'content' ) do %>
11 <% form_remote_tag( :url => {}, :method => :get, :update => 'content' ) do %>
12 <%= hidden_field_tag 'project_id', params[:project_id] %>
12 <%= hidden_field_tag 'project_id', params[:project_id] %>
13 <%= hidden_field_tag 'issue_id', params[:issue_id] if @issue %>
13 <%= hidden_field_tag 'issue_id', params[:issue_id] if @issue %>
14 <%= render :partial => 'date_range' %>
14 <%= render :partial => 'date_range' %>
15 <% end %>
15 <% end %>
16
16
17 <div class="total-hours">
17 <div class="total-hours">
18 <p><%= l(:label_total) %>: <%= html_hours(lwr(:label_f_hour, @total_hours)) %></p>
18 <p><%= l(:label_total) %>: <%= html_hours(lwr(:label_f_hour, @total_hours)) %></p>
19 </div>
19 </div>
20
20
21 <% unless @entries.empty? %>
21 <% unless @entries.empty? %>
22 <%= render :partial => 'list', :locals => { :entries => @entries }%>
22 <%= render :partial => 'list', :locals => { :entries => @entries }%>
23 <p class="pagination"><%= pagination_links_full @entry_pages, @entry_count %></p>
23 <p class="pagination"><%= pagination_links_full @entry_pages, @entry_count %></p>
24
24
25 <p class="other-formats">
25 <p class="other-formats">
26 <%= l(:label_export_to) %>
26 <%= l(:label_export_to) %>
27 <span><%= link_to 'Atom', {:issue_id => @issue, :format => 'atom', :key => User.current.rss_key}, :class => 'feed' %></span>
27 <span><%= link_to 'CSV', params.merge(:format => 'csv'), :class => 'csv' %></span>
28 <span><%= link_to 'CSV', params.merge(:format => 'csv'), :class => 'csv' %></span>
28 </p>
29 </p>
29 <% end %>
30 <% end %>
30
31
31 <% html_title l(:label_spent_time), l(:label_details) %>
32 <% html_title l(:label_spent_time), l(:label_details) %>
33
34 <% content_for :header_tags do %>
35 <%= auto_discovery_link_tag(:atom, {:issue_id => @issue, :format => 'atom', :key => User.current.rss_key}, :title => l(:label_spent_time)) %>
36 <% end %>
@@ -1,220 +1,228
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 destroy
81 def 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_time
94 def test_report_all_time
95 get :report, :project_id => 1, :criterias => ['project', 'issue']
95 get :report, :project_id => 1, :criterias => ['project', 'issue']
96 assert_response :success
96 assert_response :success
97 assert_template 'report'
97 assert_template 'report'
98 assert_not_nil assigns(:total_hours)
98 assert_not_nil assigns(:total_hours)
99 assert_equal "162.90", "%.2f" % assigns(:total_hours)
99 assert_equal "162.90", "%.2f" % assigns(:total_hours)
100 end
100 end
101
101
102 def test_report_all_time_by_day
102 def test_report_all_time_by_day
103 get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day'
103 get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day'
104 assert_response :success
104 assert_response :success
105 assert_template 'report'
105 assert_template 'report'
106 assert_not_nil assigns(:total_hours)
106 assert_not_nil assigns(:total_hours)
107 assert_equal "162.90", "%.2f" % assigns(:total_hours)
107 assert_equal "162.90", "%.2f" % assigns(:total_hours)
108 assert_tag :tag => 'th', :content => '2007-03-12'
108 assert_tag :tag => 'th', :content => '2007-03-12'
109 end
109 end
110
110
111 def test_report_one_criteria
111 def test_report_one_criteria
112 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
112 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
113 assert_response :success
113 assert_response :success
114 assert_template 'report'
114 assert_template 'report'
115 assert_not_nil assigns(:total_hours)
115 assert_not_nil assigns(:total_hours)
116 assert_equal "8.65", "%.2f" % assigns(:total_hours)
116 assert_equal "8.65", "%.2f" % assigns(:total_hours)
117 end
117 end
118
118
119 def test_report_two_criterias
119 def test_report_two_criterias
120 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
120 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
121 assert_response :success
121 assert_response :success
122 assert_template 'report'
122 assert_template 'report'
123 assert_not_nil assigns(:total_hours)
123 assert_not_nil assigns(:total_hours)
124 assert_equal "162.90", "%.2f" % assigns(:total_hours)
124 assert_equal "162.90", "%.2f" % assigns(:total_hours)
125 end
125 end
126
126
127 def test_report_custom_field_criteria
127 def test_report_custom_field_criteria
128 get :report, :project_id => 1, :criterias => ['project', 'cf_1']
128 get :report, :project_id => 1, :criterias => ['project', 'cf_1']
129 assert_response :success
129 assert_response :success
130 assert_template 'report'
130 assert_template 'report'
131 assert_not_nil assigns(:total_hours)
131 assert_not_nil assigns(:total_hours)
132 assert_not_nil assigns(:criterias)
132 assert_not_nil assigns(:criterias)
133 assert_equal 2, assigns(:criterias).size
133 assert_equal 2, assigns(:criterias).size
134 assert_equal "162.90", "%.2f" % assigns(:total_hours)
134 assert_equal "162.90", "%.2f" % assigns(:total_hours)
135 # Custom field column
135 # Custom field column
136 assert_tag :tag => 'th', :content => 'Database'
136 assert_tag :tag => 'th', :content => 'Database'
137 # Custom field row
137 # Custom field row
138 assert_tag :tag => 'td', :content => 'MySQL',
138 assert_tag :tag => 'td', :content => 'MySQL',
139 :sibling => { :tag => 'td', :attributes => { :class => 'hours' },
139 :sibling => { :tag => 'td', :attributes => { :class => 'hours' },
140 :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
140 :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
141 :content => '1' }}
141 :content => '1' }}
142 end
142 end
143
143
144 def test_report_one_criteria_no_result
144 def test_report_one_criteria_no_result
145 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
145 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
146 assert_response :success
146 assert_response :success
147 assert_template 'report'
147 assert_template 'report'
148 assert_not_nil assigns(:total_hours)
148 assert_not_nil assigns(:total_hours)
149 assert_equal "0.00", "%.2f" % assigns(:total_hours)
149 assert_equal "0.00", "%.2f" % assigns(:total_hours)
150 end
150 end
151
151
152 def test_report_csv_export
152 def test_report_csv_export
153 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
153 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
154 assert_response :success
154 assert_response :success
155 assert_equal 'text/csv', @response.content_type
155 assert_equal 'text/csv', @response.content_type
156 lines = @response.body.chomp.split("\n")
156 lines = @response.body.chomp.split("\n")
157 # Headers
157 # Headers
158 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
158 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
159 # Total row
159 # Total row
160 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
160 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
161 end
161 end
162
162
163 def test_details_at_project_level
163 def test_details_at_project_level
164 get :details, :project_id => 1
164 get :details, :project_id => 1
165 assert_response :success
165 assert_response :success
166 assert_template 'details'
166 assert_template 'details'
167 assert_not_nil assigns(:entries)
167 assert_not_nil assigns(:entries)
168 assert_equal 4, assigns(:entries).size
168 assert_equal 4, assigns(:entries).size
169 # project and subproject
169 # project and subproject
170 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
170 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
171 assert_not_nil assigns(:total_hours)
171 assert_not_nil assigns(:total_hours)
172 assert_equal "162.90", "%.2f" % assigns(:total_hours)
172 assert_equal "162.90", "%.2f" % assigns(:total_hours)
173 # display all time by default
173 # display all time by default
174 assert_equal '2007-03-11'.to_date, assigns(:from)
174 assert_equal '2007-03-11'.to_date, assigns(:from)
175 assert_equal '2007-04-22'.to_date, assigns(:to)
175 assert_equal '2007-04-22'.to_date, assigns(:to)
176 end
176 end
177
177
178 def test_details_at_project_level_with_date_range
178 def test_details_at_project_level_with_date_range
179 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
179 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
180 assert_response :success
180 assert_response :success
181 assert_template 'details'
181 assert_template 'details'
182 assert_not_nil assigns(:entries)
182 assert_not_nil assigns(:entries)
183 assert_equal 3, assigns(:entries).size
183 assert_equal 3, assigns(:entries).size
184 assert_not_nil assigns(:total_hours)
184 assert_not_nil assigns(:total_hours)
185 assert_equal "12.90", "%.2f" % assigns(:total_hours)
185 assert_equal "12.90", "%.2f" % assigns(:total_hours)
186 assert_equal '2007-03-20'.to_date, assigns(:from)
186 assert_equal '2007-03-20'.to_date, assigns(:from)
187 assert_equal '2007-04-30'.to_date, assigns(:to)
187 assert_equal '2007-04-30'.to_date, assigns(:to)
188 end
188 end
189
189
190 def test_details_at_project_level_with_period
190 def test_details_at_project_level_with_period
191 get :details, :project_id => 1, :period => '7_days'
191 get :details, :project_id => 1, :period => '7_days'
192 assert_response :success
192 assert_response :success
193 assert_template 'details'
193 assert_template 'details'
194 assert_not_nil assigns(:entries)
194 assert_not_nil assigns(:entries)
195 assert_not_nil assigns(:total_hours)
195 assert_not_nil assigns(:total_hours)
196 assert_equal Date.today - 7, assigns(:from)
196 assert_equal Date.today - 7, assigns(:from)
197 assert_equal Date.today, assigns(:to)
197 assert_equal Date.today, assigns(:to)
198 end
198 end
199
199
200 def test_details_at_issue_level
200 def test_details_at_issue_level
201 get :details, :issue_id => 1
201 get :details, :issue_id => 1
202 assert_response :success
202 assert_response :success
203 assert_template 'details'
203 assert_template 'details'
204 assert_not_nil assigns(:entries)
204 assert_not_nil assigns(:entries)
205 assert_equal 2, assigns(:entries).size
205 assert_equal 2, assigns(:entries).size
206 assert_not_nil assigns(:total_hours)
206 assert_not_nil assigns(:total_hours)
207 assert_equal 154.25, assigns(:total_hours)
207 assert_equal 154.25, assigns(:total_hours)
208 # display all time by default
208 # display all time by default
209 assert_equal '2007-03-11'.to_date, assigns(:from)
209 assert_equal '2007-03-11'.to_date, assigns(:from)
210 assert_equal '2007-04-22'.to_date, assigns(:to)
210 assert_equal '2007-04-22'.to_date, assigns(:to)
211 end
211 end
212
212
213 def test_details_atom_feed
214 get :details, :project_id => 1, :format => 'atom'
215 assert_response :success
216 assert_equal 'application/atom+xml', @response.content_type
217 assert_not_nil assigns(:items)
218 assert assigns(:items).first.is_a?(TimeEntry)
219 end
220
213 def test_details_csv_export
221 def test_details_csv_export
214 get :details, :project_id => 1, :format => 'csv'
222 get :details, :project_id => 1, :format => 'csv'
215 assert_response :success
223 assert_response :success
216 assert_equal 'text/csv', @response.content_type
224 assert_equal 'text/csv', @response.content_type
217 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
225 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
218 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
226 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
219 end
227 end
220 end
228 end
General Comments 0
You need to be logged in to leave comments. Login now