Skip to content

Commit 00a0633

Browse files
committed
Added tracker filtering
1 parent 03c7479 commit 00a0633

File tree

7 files changed

+111
-30
lines changed

7 files changed

+111
-30
lines changed

app/helpers/timesheet_helper.rb

+7
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ def group_options(timesheet)
9191
options_from_collection_for_select(available_groups, :id, :name, :selected =>timesheet.groups)
9292
end
9393

94+
def tracker_options(timesheet)
95+
available_trackers = Tracker.all
96+
selected_trackers = timesheet.trackers
97+
selected_trackers = available_trackers.collect{|g| g.id} if selected_trackers.blank?
98+
options_from_collection_for_select(available_trackers, :id, :name, :selected =>timesheet.trackers)
99+
end
100+
94101
def user_options(timesheet)
95102
available_users = Timesheet.viewable_users.sort { |a,b| a.to_s.downcase <=> b.to_s.downcase }
96103
selected_users = timesheet.users

app/models/timesheet.rb

+89-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class Timesheet
2-
attr_accessor :date_from, :date_to, :projects, :activities, :users, :groups, :allowed_projects, :period, :period_type
2+
attr_accessor :date_from, :date_to, :projects, :activities, :users, :groups, :trackers,
3+
:allowed_projects, :period, :period_type
34

45
# Time entries on the Timesheet in the form of:
56
# project.name => {:logs => [time entries], :users => [users shown in logs] }
@@ -18,6 +19,7 @@ class Timesheet
1819
:user => 'User',
1920
:issue => 'Issue',
2021
:group => 'Group',
22+
:tracker => 'Tracker',
2123
:date => 'Date'
2224
}
2325

@@ -32,7 +34,8 @@ def initialize(options = { })
3234
self.potential_time_entry_ids = options[:potential_time_entry_ids] || [ ]
3335
self.allowed_projects = options[:allowed_projects] || [ ]
3436
self.groups = [ ]
35-
37+
self.trackers = [ ]
38+
3639
unless options[:activities].nil?
3740
self.activities = options[:activities].collect do |activity_id|
3841
# Include project-overridden activities
@@ -64,7 +67,16 @@ def initialize(options = { })
6467
self.groups= Group.all
6568
end
6669
end
67-
70+
71+
unless options[:trackers].nil?
72+
self.trackers = options[:trackers].collect do |tracker_id|
73+
tracker = Tracker.find(tracker_id)
74+
tracker.id if tracker
75+
end.flatten.uniq.compact
76+
else
77+
self.trackers = Tracker.all.collect { |a| a.id.to_i }
78+
end
79+
6880
if !options[:sort].nil? && options[:sort].respond_to?(:to_sym) && ValidSortOptions.keys.include?(options[:sort].to_sym)
6981
self.sort = options[:sort].to_sym
7082
else
@@ -95,6 +107,8 @@ def fetch_time_entries
95107
fetch_time_entries_by_issue
96108
when :group
97109
fetch_time_entries_by_group
110+
when :tracker
111+
fetch_time_entries_by_tracker
98112
when :date
99113
fetch_time_entries_by_date
100114
else
@@ -201,6 +215,7 @@ def csv_header
201215
'#',
202216
l(:label_date),
203217
l(:label_member),
218+
l(:label_tracker),
204219
l(:label_activity),
205220
l(:label_project),
206221
l(:label_version),
@@ -218,6 +233,7 @@ def time_entry_to_csv(time_entry)
218233
time_entry.id,
219234
time_entry.spent_on,
220235
time_entry.user.name,
236+
time_entry.issue.tracker.name,
221237
time_entry.activity.name,
222238
time_entry.project,
223239
(time_entry.issue.fixed_version if time_entry.issue),
@@ -232,24 +248,26 @@ def time_entry_to_csv(time_entry)
232248

233249
# Array of users to find
234250
# String of extra conditions to add onto the query (AND)
235-
def conditions(users, extra_conditions=nil)
251+
def conditions(users, trackers, extra_conditions=nil)
236252
if self.potential_time_entry_ids.empty?
237253
if self.date_from.present? && self.date_to.present?
238-
conditions = ["spent_on >= (:from) AND spent_on <= (:to) AND #{TimeEntry.table_name}.project_id IN (:projects) AND user_id IN (:users) AND activity_id IN (:activities)",
254+
conditions = ["spent_on >= (:from) AND spent_on <= (:to) AND #{TimeEntry.table_name}.project_id IN (:projects) AND user_id IN (:users) AND activity_id IN (:activities) AND tracker_id IN (:trackers)",
239255
{
240256
:from => self.date_from,
241257
:to => self.date_to,
242258
:projects => self.projects,
243259
:activities => self.activities,
244260
:groups => self.groups,
261+
:trackers => trackers,
245262
:users => users
246263
}]
247264
else # All time
248-
conditions = ["#{TimeEntry.table_name}.project_id IN (:projects) AND user_id IN (:users) AND activity_id IN (:activities)",
265+
conditions = ["#{TimeEntry.table_name}.project_id IN (:projects) AND user_id IN (:users) AND activity_id IN (:activities) AND tracker_id IN (:trackers)",
249266
{
250267
:projects => self.projects,
251268
:activities => self.activities,
252269
:groups => self.groups,
270+
:trackers => trackers,
253271
:users => users
254272
}]
255273
end
@@ -279,46 +297,65 @@ def includes
279297

280298

281299
def time_entries_for_all_users(project)
282-
return project.time_entries.includes(self.includes).
283-
where(self.conditions(self.users)).
300+
return project.time_entries.includes(self.includes + [{:issue => [:tracker]}]).
301+
joins(:issue).
302+
where(self.conditions(self.users, self.trackers)).
284303
order('spent_on ASC')
285304
end
286-
305+
287306
def time_entries_for_all_users_in_group(group)
288-
return TimeEntry.includes(self.includes).
289-
where(self.conditions(group.user_ids)).
307+
return TimeEntry.includes(self.includes + [{:issue => [:tracker]}]).
308+
joins(:issue).
309+
where(self.conditions(group.user_ids, self.trackers)).
290310
order('spent_on ASC')
291-
end
292-
311+
end
312+
313+
def time_entries_for_all_users_in_tracker(tracker)
314+
return TimeEntry.includes(self.includes + [{:issue => [:tracker]}]).
315+
joins(:issue).
316+
where(self.conditions(self.users, tracker)).
317+
order('spent_on ASC')
318+
end
319+
293320
def time_entries_for_current_user(project)
294321
return project.time_entries.
295322
includes(self.includes + [:activity, :user, {:issue => [:tracker, :assigned_to, :priority]}]).
296-
where(self.conditions(User.current.id)).
323+
joins(:issue).
324+
where(self.conditions(User.current.id, self.trackers)).
325+
order('spent_on ASC')
326+
end
327+
328+
def time_entries_for_current_user_in_tracker(tracker)
329+
return TimeEntry.includes(self.includes + [:activity, :user, {:issue => [:tracker, :assigned_to, :priority]}]).
330+
joins(:issue).
331+
where(self.conditions(User.current.id, tracker)).
297332
order('spent_on ASC')
298333
end
299334

300335
def issue_time_entries_for_all_users(issue)
301336
return issue.time_entries.includes(self.includes + [:activity, :user]).
302-
where(self.conditions(self.users)).
337+
joins(:issue).
338+
where(self.conditions(self.users, self.trackers)).
303339
order('spent_on ASC')
304340
end
305341

306342
def issue_time_entries_for_current_user(issue)
307343
return issue.time_entries.includes(self.includes + [:activity, :user]).
308-
where(self.conditions(User.current.id)).
344+
joins(:issue).
345+
where(self.conditions(User.current.id, self.trackers)).
309346
order('spent_on ASC')
310347
end
311348

312-
def time_entries_for_user(user, options={})
349+
def time_entries_for_user(user, trackers, options={})
313350
extra_conditions = options.delete(:conditions)
314351

315352
return TimeEntry.includes(self.includes).
316-
where(self.conditions([user], extra_conditions)).
353+
joins(:issue).
354+
where(self.conditions([user], trackers, extra_conditions)).
317355
order('spent_on ASC')
318356
end
319357

320358
def fetch_time_entries_by_project
321-
puts self.projects
322359
self.projects.each do |project|
323360
logs = []
324361
users = []
@@ -372,20 +409,20 @@ def fetch_time_entries_by_group
372409
end
373410
end
374411
end
375-
412+
376413
def fetch_time_entries_by_user
377414
self.users.each do |user_id|
378415
logs = []
379416
if User.current.admin?
380417
# Administrators can see all time entries
381-
logs = time_entries_for_user(user_id)
418+
logs = time_entries_for_user(user_id, self.trackers)
382419
elsif User.current.id == user_id
383420
# Users can see their own their time entries
384-
logs = time_entries_for_user(user_id)
421+
logs = time_entries_for_user(user_id, self.trackers)
385422
elsif User.current.allowed_to_on_single_potentially_archived_project?(:see_project_timesheets, nil, :global => true)
386423
# User can see project timesheets in at least once place, so
387424
# fetch the user timelogs for those projects
388-
logs = time_entries_for_user(user_id, :conditions => Project.allowed_to_condition(User.current, :see_project_timesheets))
425+
logs = time_entries_for_user(user_id, self.trackers, :conditions => Project.allowed_to_condition(User.current, :see_project_timesheets))
389426
else
390427
# Rest can see nothing
391428
end
@@ -397,7 +434,34 @@ def fetch_time_entries_by_user
397434
end
398435
end
399436

400-
437+
438+
def fetch_time_entries_by_tracker
439+
trackers = Tracker.where(:id => self.trackers)
440+
trackers.each do |tracker|
441+
logs = []
442+
users = []
443+
if User.current.admin?
444+
# Administrators can see all time entries
445+
logs = time_entries_for_all_users_in_tracker(tracker.id)
446+
else
447+
# Users with the Role and correct permission can see all time entries
448+
logs1 = time_entries_for_all_users_in_tracker(tracker.id).select do |te|
449+
project = Project.find(te.project_id)
450+
User.current.allowed_to_on_single_potentially_archived_project?(:see_project_timesheets, project)
451+
end
452+
# Users with permission to see their time entries
453+
logs2 = time_entries_for_current_user_in_tracker(tracker.id).select do |te|
454+
project = Project.find(te.project_id)
455+
User.current.allowed_to_on_single_potentially_archived_project?(:view_time_entries, project)
456+
end
457+
logs = logs1 + logs2
458+
end
459+
unless logs.empty?
460+
users = logs.collect(&:user).uniq.sort
461+
self.time_entries[tracker.name] = { :logs => logs, :users => users }
462+
end
463+
end
464+
end
401465

402466

403467
# project => { :users => [users shown in logs],
@@ -454,7 +518,7 @@ def fetch_time_entries_by_date
454518
logs = []
455519

456520
# extra_conditions = 'GROUP_BY spent_on'
457-
logs=TimeEntry.includes(self.includes).where(self.conditions(self.users))
521+
logs=TimeEntry.includes(self.includes).joins(:issue).where(self.conditions(self.users, self.trackers))
458522

459523

460524
unless logs.empty?

app/views/timesheet/_form.html.erb

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
<%= select_tag 'timesheet[groups][]', group_options(@timesheet), { :multiple => true, :size => @list_size} %>
4545
</div>
4646
<% end %>
47+
<div class="timesheet-trackers">
48+
<label for="timesheet_trackers_" class="select-all"><%= l(:label_tracker_plural)%></label>
49+
<%= select_tag 'timesheet[trackers][]', tracker_options(@timesheet), { :multiple => true, :size => @list_size} %>
50+
</div>
4751
<%= call_hook(:plugin_timesheet_view_timesheet_form, { :timesheet => @timesheet, :params => params, :list_size => @list_size }) %>
4852
<div class="form-actions">
4953
<%= submit_tag l(:button_apply),:class => 'button-small' -%>

app/views/timesheet/_time_entry.html.erb

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<td align="center"><%= check_box_tag 'ids[]', time_entry.id, false, { :class => 'checkbox' } %></td>
33
<td align="center"><%= format_date(time_entry.spent_on) %></td>
44
<td align="center"><%= time_entry.user.name %></td>
5+
<td align="center"><%= time_entry.issue.tracker.name %></td>
56
<td align="center"><%= time_entry.activity.name %></td>
67
<td align="center"><%= time_entry.project.name %></td>
78
<td align="center"><%= time_entry.issue.fixed_version if time_entry.issue %></td>

app/views/timesheet/_timesheet_group.html.erb

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
</th>
88
<th width="8%"><%= l(:label_date) %></th>
99
<th width="10%"><%= l(:label_member) %></th>
10-
<th width="15%"><%= l(:label_activity) %></th>
11-
<th width="15%"><%= l(:label_project) %></th>
10+
<th width="10%"><%= l(:label_tracker) %></th>
11+
<th width="10%"><%= l(:label_activity) %></th>
12+
<th width="10%"><%= l(:label_project) %></th>
1213
<th width="15%"><%= l(:label_version) %></th>
1314
<th width="10%"><%= l(:label_issue) %></th>
1415
<th width="25%"><%= l(:field_comments) %></th>

app/views/timesheet/report.html.erb

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
<% when :group %>
2424
<h3><%= h entryname -%> (<%= h number_with_precision(@total[entryname], :precision => @precision) %> <%= h(l(:field_hours)) -%>)</h3>
2525
<%= render :partial => 'timesheet_group', :locals => {:entry => entry, :name => entryname, :total => @total[entryname] } %>
26+
<% when :tracker %>
27+
<h3><%= h entryname -%> (<%= h number_with_precision(@total[entryname], :precision => @precision) %> <%= h(l(:field_hours)) -%>)</h3>
28+
<%= render :partial => 'timesheet_group', :locals => {:entry => entry, :name => entryname, :total => @total[entryname] } %>
2629
<% when :date %>
2730
<%#*entry[:logs].each do |log|%>
28-
<% logger.debug "#{'*'*100}\n ENTRYNAME > #{entryname.to_yaml} ENTRY #{entry[:logs][0].id}\n#{'*'*100}"%>
31+
<% logger.debug "#{'*'*100}\n ENTRYNAME > #{entryname.to_yaml} ENTRY #{entry[:logs][0].id}\n#{'*'*100}"%>
2932

3033
<h3><%= toggle_issue_arrows_date(entryname) %><%= h entryname -%> (<%= h number_with_precision(@total[entryname], :precision => @precision) %> <%= h(l(:field_hours)) -%>)</h3>
3134

@@ -35,7 +38,7 @@
3538
<%#*end%>
3639
<% else %>
3740
<%# Default to :project %>
38-
<h3><%= h entryname -%> (<%= h number_with_precision(@total[entryname], :precision => @precision) %> <%= h(l(:field_hours)) -%>) <%= showing_users(entry[:users]) %></h3>
41+
<h3><%= h entryname -%> (<%= h number_with_precision(@total[entryname], :precision => @precision) %> <%= h(l(:field_hours)) -%>) <%= showing_users(entry[:users]) %></h3>
3942
<%= render :partial => 'timesheet_group', :locals => {:entry => entry, :name => entryname, :total => @total[entryname] } %>
4043
<% end
4144
end # each

assets/stylesheets/timesheet.css

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ div#timesheet-form p { padding:0px 10px; float:left; clear:none;}
2424
#timesheet-form .timesheet-projects select,
2525
#timesheet-form .timesheet-activities select,
2626
#timesheet-form .timesheet-users select,
27-
#timesheet-form .timesheet-groups select {
27+
#timesheet-form .timesheet-groups select,
28+
#timesheet-form .timesheet-trackers select {
2829
height: 300px;
2930
min-width: 200px;
3031
border: 1px solid #ddd;

0 commit comments

Comments
 (0)