@trackers = @project.trackers
@open_issues_by_tracker = Issue.count(:group => :tracker, :joins => "INNER JOIN #{IssueStatus.table_name} ON #{IssueStatus.table_name}.id = #{Issue.table_name}.status_id", :conditions => ["project_id=? and #{IssueStatus.table_name}.is_closed=?", @project.id, false])
@total_issues_by_tracker = Issue.count(:group => :tracker, :conditions => ["project_id=?", @project.id])
- @total_hours = @project.time_entries.sum(:hours)
+ TimeEntry.visible_by(User.current) do
+ @total_hours = TimeEntry.sum(:hours,
+ :include => :project,
+ :conditions => ["(#{Project.table_name}.id = ? OR #{Project.table_name}.parent_id = ?)", @project.id, @project.id]).to_f
+ end
@key = User.current.rss_key
end
include TimelogHelper
def report
- @available_criterias = { 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
- :values => @project.versions,
+ @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
+ :klass => Project,
+ :label => :label_project},
+ 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
+ :klass => Version,
:label => :label_version},
'category' => {:sql => "#{Issue.table_name}.category_id",
- :values => @project.issue_categories,
+ :klass => IssueCategory,
:label => :field_category},
'member' => {:sql => "#{TimeEntry.table_name}.user_id",
- :values => @project.users,
+ :klass => User,
:label => :label_member},
'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
- :values => Tracker.find(:all),
+ :klass => Tracker,
:label => :label_tracker},
'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
- :values => Enumeration::get_values('ACTI'),
+ :klass => Enumeration,
:label => :label_activity}
}
@criterias = params[:criterias] || []
@criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
@criterias.uniq!
+ @criterias = @criterias[0,3]
@columns = (params[:period] && %w(year month week).include?(params[:period])) ? params[:period] : 'month'
sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
sql = "SELECT #{sql_select}, tyear, tmonth, tweek, SUM(hours) AS hours"
- sql << " FROM #{TimeEntry.table_name} LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
- sql << " WHERE #{TimeEntry.table_name}.project_id = %s" % @project.id
+ sql << " FROM #{TimeEntry.table_name}"
+ sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
+ sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
+ sql << " WHERE (#{Project.table_name}.id = %s OR #{Project.table_name}.parent_id = %s)" % [@project.id, @project.id]
+ sql << " AND (%s)" % Project.allowed_to_condition(User.current, :view_time_entries)
sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@date_from.to_time), ActiveRecord::Base.connection.quoted_date(@date_to.to_time)]
sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek"
row['week'] = "#{row['tyear']}-#{row['tweek']}"
end
end
+
+ @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
end
@periods = []
@from, @to = @to, @from if @from && @to && @from > @to
- conditions = nil
+ cond = ARCondition.new
+ cond << (@issue.nil? ? ["(#{Project.table_name}.id = ? OR #{Project.table_name}.parent_id = ?)", @project.id, @project.id] :
+ ["#{TimeEntry.table_name}.issue_id = ?", @issue.id])
+
if @from
if @to
- conditions = ['spent_on BETWEEN ? AND ?', @from, @to]
+ cond << ['spent_on BETWEEN ? AND ?', @from, @to]
else
- conditions = ['spent_on >= ?', @from]
+ cond << ['spent_on >= ?', @from]
end
elsif @to
- conditions = ['spent_on <= ?', @to]
+ cond << ['spent_on <= ?', @to]
end
-
- @owner_id = User.current.id
- respond_to do |format|
- format.html {
- # Paginate results
- @entry_count = (@issue ? @issue : @project).time_entries.count(:conditions => conditions)
- @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
- @entries = (@issue ? @issue : @project).time_entries.find(:all,
- :include => [:activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
- :conditions => conditions,
- :order => sort_clause,
- :limit => @entry_pages.items_per_page,
- :offset => @entry_pages.current.offset)
- @total_hours = (@issue ? @issue : @project).time_entries.sum(:hours, :conditions => conditions).to_f
- render :layout => !request.xhr?
- }
- format.csv {
- # Export all entries
- @entries = (@issue ? @issue : @project).time_entries.find(:all,
- :include => [:activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
- :conditions => conditions,
- :order => sort_clause)
- send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
- }
+ TimeEntry.visible_by(User.current) do
+ respond_to do |format|
+ format.html {
+ # Paginate results
+ @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
+ @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
+ @entries = TimeEntry.find(:all,
+ :include => [:project, :activity, :user, {:issue => :tracker}],
+ :conditions => cond.conditions,
+ :order => sort_clause,
+ :limit => @entry_pages.items_per_page,
+ :offset => @entry_pages.current.offset)
+ @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
+ render :layout => !request.xhr?
+ }
+ format.csv {
+ # Export all entries
+ @entries = TimeEntry.find(:all,
+ :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
+ :conditions => cond.conditions,
+ :order => sort_clause)
+ send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
+ }
+ end
end
end
module TimelogHelper
def select_hours(data, criteria, value)
- data.select {|row| row[criteria] == value.to_s}
+ data.select {|row| row[criteria] == value}
end
def sum_hours(data)
headers = [l(:field_spent_on),
l(:field_user),
l(:field_activity),
+ l(:field_project),
l(:field_issue),
l(:field_tracker),
l(:field_subject),
fields = [l_date(entry.spent_on),
entry.user,
entry.activity,
+ entry.project,
(entry.issue ? entry.issue.id : nil),
(entry.issue ? entry.issue.tracker : nil),
(entry.issue ? entry.issue.subject : nil),
end
end
+ def self.allowed_to_condition(user, permission)
+ statements = []
+ active_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
+ if user.admin?
+ # no restriction
+ elsif user.logged?
+ statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission)
+ allowed_project_ids = user.memberships.select {|m| m.role.allowed_to?(permission)}.collect {|m| m.project_id}
+ statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})"
+ else
+ statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.anonymous.allowed_to?(permission)
+ end
+ statements.empty? ? active_statement : "(#{active_statement} AND (#{statements.join(' OR ')}))"
+ end
+
def self.find(*args)
if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
project = find_by_identifier(*args)
def editable_by?(usr)
usr == self.user
end
+
+ def self.visible_by(usr)
+ with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
+ yield
+ end
+ end
end
<%= sort_header_tag('spent_on', :caption => l(:label_date), :default_order => 'desc') %>
<%= sort_header_tag('user_id', :caption => l(:label_member)) %>
<%= sort_header_tag('activity_id', :caption => l(:label_activity)) %>
+<%= sort_header_tag("#{Project.table_name}.name", :caption => l(:label_project)) %>
<%= sort_header_tag('issue_id', :caption => l(:label_issue), :default_order => 'desc') %>
<th><%= l(:field_comments) %></th>
<%= sort_header_tag('hours', :caption => l(:field_hours)) %>
<% entries.each do |entry| -%>
<tr class="time-entry <%= cycle("odd", "even") %>">
<td class="spent_on"><%= format_date(entry.spent_on) %></td>
-<td class="user"><%= entry.user.name %></td>
-<td class="activity"><%= entry.activity.name %></td>
+<td class="user"><%=h entry.user %></td>
+<td class="activity"><%=h entry.activity %></td>
+<td class="project"><%=h entry.project %></td>
<td class="subject">
- <% if entry.issue -%>
- <div class="tooltip"><%= link_to_issue entry.issue %>: <%= h(truncate(entry.issue.subject, 50)) -%>
- <span class="tip"><%= render_issue_tooltip entry.issue %></span>
- </div>
- <% end -%>
+<% if entry.issue -%>
+<%= link_to_issue entry.issue %>: <%= h(truncate(entry.issue.subject, 50)) -%>
+<% end -%>
</td>
<td class="comments"><%=h entry.comments %></td>
-<td class="hours"><%= entry.hours %></td>
+<td class="hours"><%= html_hours("%.2f" % entry.hours) %></td>
<td align="center"><%= link_to_if_authorized(l(:button_edit),
{:controller => 'timelog', :action => 'edit', :id => entry},
:class => 'icon icon-edit') if entry.editable_by?(User.current) %></td>
-<% @available_criterias[criterias[level]][:values].each do |value| %>
-<tr class="<%= cycle('odd', 'even') if criterias.length < level + 2 %>">
+<% @hours.collect {|h| h[criterias[level]]}.uniq.each do |value| %>
+<% hours_for_value = select_hours(hours, criterias[level], value) -%>
+<% next if hours_for_value.empty? -%>
+<tr class="<%= cycle('odd', 'even') %> <%= 'last-level' unless criterias.length > level+1 %>">
<%= '<td></td>' * level %>
-<td><%= value.name %></td>
-<%= '<td></td>' * (criterias.length - level - 1) %>
-<% hours_for_value = select_hours(hours, criterias[level], value.id) %>
- <% @periods.each do |period| %>
+<td><%= value.nil? ? l(:label_none) : @available_criterias[criterias[level]][:klass].find_by_id(value) %></td>
+<%= '<td></td>' * (criterias.length - level - 1) -%>
+ <% @periods.each do |period| -%>
<% sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s)) %>
- <td align="center"><%= sum > 0 ? "%.2f" % sum : "-" %></td>
- <% end %>
+ <td class="hours"><%= html_hours("%.2f" % sum) if sum > 0 %></td>
+ <% end -%>
</tr>
-<% if criterias.length > level+1 %>
+<% if criterias.length > level+1 -%>
<%= render(:partial => 'report_criteria', :locals => {:criterias => criterias, :hours => hours_for_value, :level => (level + 1)}) %>
-<% end %>
+<% end -%>
<% end %>
-<% reset_cycle %>
<div class="contextual">\r
+<%= link_to(l(:label_report), {:controller => 'timelog', :action => 'report', :project_id => @project}, :class => 'icon icon-report') %>\r
<%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'edit', :project_id => @project, :issue_id => @issue}, :class => 'icon icon-time' %>\r
</div>\r
\r
<%= text_field_tag 'from', @from, :size => 10, :onfocus => '$("period_type_2").checked = true;' %> <%= calendar_for('from') %>\r
<%= l(:label_date_to) %>\r
<%= text_field_tag 'to', @to, :size => 10, :onfocus => '$("period_type_2").checked = true;' %> <%= calendar_for('to') %>\r
-<%= submit_tag l(:button_submit), :name => nil, :onclick => '$("period_type_2").checked = true;' %>\r
+<%= submit_tag l(:button_apply), :name => nil, :onclick => '$("period_type_2").checked = true;' %>\r
</p>\r
</fieldset>\r
<% end %>\r
+<div class="contextual">
+<%= link_to(l(:label_details), {:controller => 'timelog', :action => 'details', :project_id => @project}, :class => 'icon icon-details') %>
+<%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'edit', :project_id => @project, :issue_id => @issue}, :class => 'icon icon-time' %>
+</div>
+
<h2><%= l(:label_spent_time) %></h2>
<% form_remote_tag(:url => {:project_id => @project}, :update => 'content') do %>
<% @criterias.each do |criteria| %>
<%= hidden_field_tag 'criterias[]', criteria %>
<% end %>
+ <fieldset><legend><%= l(:label_date_range) %></legend>
<p>
- <%= l(:label_date_from) %>: <%= text_field_tag 'date_from', @date_from, :size => 10 %><%= calendar_for('date_from') %>
-
- <%= l(:label_date_to) %>: <%= text_field_tag 'date_to', @date_to, :size => 10 %><%= calendar_for('date_to') %>
-
- <%= l(:label_details) %>:
+ <%= l(:label_date_from) %>
+ <%= text_field_tag 'date_from', @date_from, :size => 10 %><%= calendar_for('date_from') %>
+ <%= l(:label_date_to) %>
+ <%= text_field_tag 'date_to', @date_to, :size => 10 %><%= calendar_for('date_to') %>
+ <%= l(:label_details) %>
<%= select_tag 'period', options_for_select([[l(:label_year), 'year'],
[l(:label_month), 'month'],
[l(:label_week), 'week']], @columns) %>
<%= submit_tag l(:button_apply) %>
- <%= link_to_remote l(:button_clear), {:url => {:project_id => @project}, :update => 'content'}, :class => 'icon icon-reload' %>
</p>
+ </fieldset>
- <% if @criterias.length < 3 %>
- <p><%= l(:button_add) %>: <%= select_tag('criterias[]', options_for_select([[]] + (@available_criterias.keys - @criterias).collect{|k| [l(@available_criterias[k][:label]), k]}), :onchange => "this.form.onsubmit();") %></p>
- <% end %>
+ <p><%= l(:button_add) %>: <%= select_tag('criterias[]', options_for_select([[]] + (@available_criterias.keys - @criterias).collect{|k| [l(@available_criterias[k][:label]), k]}),
+ :onchange => "this.form.onsubmit();",
+ :style => 'width: 200px',
+ :disabled => (@criterias.length >= 3)) %>
+ <%= link_to_remote l(:button_clear), {:url => {:project_id => @project, :date_from => @date_from, :date_to => @date_to, :period => @columns}, :update => 'content'},
+ :class => 'icon icon-reload' %></p>
-<br />
+<% unless @criterias.empty? %>
+<div class="total-hours">
+<p><%= l(:label_total) %>: <%= html_hours(lwr(:label_f_hour, @total_hours)) %></p>
+</div>
-<% unless @criterias.empty? %>
-<table class="list">
+<% unless @hours.empty? %>
+<table class="list" id="time-report">
<thead>
<tr>
<% @criterias.each do |criteria| %>
<% end %>
</tr>
</thead>
-
<tbody>
<%= render :partial => 'report_criteria', :locals => {:criterias => @criterias, :hours => @hours, :level => 0} %>
+ <tr class="total">
+ <td><%= l(:label_total) %></td>
+ <%= '<td></td>' * (@criterias.size - 1) %>
+ <% @periods.each do |period| -%>
+ <% sum = sum_hours(select_hours(@hours, @columns, period.to_s)) %>
+ <td class="hours"><%= html_hours("%.2f" % sum) if sum > 0 %></td>
+ <% end -%>
+ </tr>
</tbody>
</table>
<% end %>
<% end %>
+<% end %>
<% content_for :header_tags do %>
<%= javascript_include_tag 'calendar/calendar' %>
--- /dev/null
+# redMine - project management software
+# Copyright (C) 2006-2008 Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+class ARCondition
+ attr_reader :conditions
+
+ def initialize(condition=nil)
+ @conditions = ['1=1']
+ @conditions.add(condition) if condition
+ end
+
+ def add(condition)
+ if condition.is_a?(Array)
+ @conditions.first << " AND (#{condition.first})"
+ @conditions += condition[1..-1]
+ elsif condition.is_a?(String)
+ @conditions.first << " AND (#{condition})"
+ else
+ raise "Unsupported #{condition.class} condition: #{condition}"
+ end
+ self
+ end
+
+ def <<(condition)
+ add(condition)
+ end
+end
tr.time-entry { text-align: center; white-space: nowrap; }
tr.time-entry td.subject, tr.time-entry td.comments { text-align: left; }
-tr.time-entry td.hours { text-align: right; font-weight: bold; padding-right: 0.6em; }
+tr.time-entry td.hours { text-align: right; font-weight: bold; padding-right: 0.5em; }
+tr.time-entry .hours-dec { font-size: 0.9em; }
table.list tbody tr:hover { background-color:#ffffdd; }
table td {padding:2px;}
div#roadmap .wiki h1 { font-size: 120%; }
div#roadmap .wiki h2 { font-size: 110%; }
+table#time-report td.hours { text-align: right; padding-right: 0.5em; }
+table#time-report tbody tr { font-style: italic; color: #777; }
+table#time-report tbody tr.last-level { font-style: normal; color: #555; }
+table#time-report tbody tr.total { font-style: normal; font-weight: bold; color: #555; background-color:#EEEEEE; }
+table#time-report .hours-dec { font-size: 0.9em; }
+
div.total-hours { text-align: left; font-size: 110%; font-weight: bold; }
div.total-hours span.hours-int { font-size: 120%; }
.icon-lock { background-image: url(../images/locked.png); }
.icon-unlock { background-image: url(../images/unlock.png); }
.icon-checked { background-image: url(../images/true.png); }
+.icon-details { background-image: url(../images/zoom_in.png); }
+.icon-report { background-image: url(../images/report.png); }
.icon22-projects { background-image: url(../images/22x22/projects.png); }
.icon22-users { background-image: url(../images/22x22/users.png); }
project_id: 1\r
comments: My hours\r
updated_on: 2007-03-23 12:54:18 +01:00\r
- activity_id: 8\r
+ activity_id: 9\r
spent_on: 2007-03-23\r
issue_id: 1\r
id: 1\r
project_id: 1\r
comments: ""\r
updated_on: 2007-03-23 14:11:04 +01:00\r
- activity_id: 8\r
+ activity_id: 9\r
spent_on: 2007-03-12\r
issue_id: 1\r
id: 2\r
project_id: 1\r
comments: ""\r
updated_on: 2007-04-21 12:20:48 +02:00\r
- activity_id: 8\r
+ activity_id: 9\r
spent_on: 2007-04-21\r
issue_id: 2\r
id: 3\r
hours: 1.0\r
user_id: 1\r
tyear: 2007\r
+time_entries_004: \r
+ created_on: 2007-04-22 12:20:48 +02:00\r
+ tweek: 16\r
+ tmonth: 4\r
+ project_id: 3\r
+ comments: Time spent on a subproject\r
+ updated_on: 2007-04-22 12:20:48 +02:00\r
+ activity_id: 10\r
+ spent_on: 2007-04-22\r
+ issue_id: \r
+ id: 4\r
+ hours: 7.65\r
+ user_id: 1\r
+ tyear: 2007\r
+
\ No newline at end of file
end
def test_report_one_criteria
- get :report, :project_id => 1, :period => "month", :date_from => "2007-01-01", :date_to => "2007-12-31", :criterias => ["member"]
+ get :report, :project_id => 1, :period => 'week', :date_from => "2007-04-01", :date_to => "2007-04-30", :criterias => ['project']
assert_response :success
assert_template 'report'
- assert_not_nil assigns(:hours)
- end
+ assert_not_nil assigns(:total_hours)
+ assert_equal "8.65", "%.2f" % assigns(:total_hours)
+ end
def test_report_two_criterias
- get :report, :project_id => 1, :period => "week", :date_from => "2007-01-01", :date_to => "2007-12-31", :criterias => ["member", "activity"]
+ get :report, :project_id => 1, :period => 'month', :date_from => "2007-01-01", :date_to => "2007-12-31", :criterias => ["member", "activity"]
assert_response :success
assert_template 'report'
- assert_not_nil assigns(:hours)
+ assert_not_nil assigns(:total_hours)
+ assert_equal "162.90", "%.2f" % assigns(:total_hours)
end
+ def test_report_one_criteria_no_result
+ get :report, :project_id => 1, :period => 'week', :date_from => "1998-04-01", :date_to => "1998-04-30", :criterias => ['project']
+ assert_response :success
+ assert_template 'report'
+ assert_not_nil assigns(:total_hours)
+ assert_equal "0.00", "%.2f" % assigns(:total_hours)
+ end
+
def test_details_at_project_level
get :details, :project_id => 1
assert_response :success
assert_template 'details'
assert_not_nil assigns(:entries)
- assert_equal 3, assigns(:entries).size
+ assert_equal 4, assigns(:entries).size
+ # project and subproject
+ assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
assert_not_nil assigns(:total_hours)
- assert_equal 155.25, assigns(:total_hours)
+ assert_equal "162.90", "%.2f" % assigns(:total_hours)
# display all time by default
assert_nil assigns(:from)
assert_nil assigns(:to)
assert_response :success
assert_template 'details'
assert_not_nil assigns(:entries)
- assert_equal 2, assigns(:entries).size
+ assert_equal 3, assigns(:entries).size
assert_not_nil assigns(:total_hours)
- assert_equal 5.25, assigns(:total_hours)
+ assert_equal "12.90", "%.2f" % assigns(:total_hours)
assert_equal '2007-03-20'.to_date, assigns(:from)
assert_equal '2007-04-30'.to_date, assigns(:to)
end