From 200842ba5e753f342940d7df0f1cc342ae8485d4 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Wed, 27 Feb 2008 20:50:19 +0000 Subject: [PATCH] Propagates time tracking to the parent project (closes #433). Time report enhancements. git-svn-id: http://redmine.rubyforge.org/svn/trunk@1176 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/projects_controller.rb | 6 +- app/controllers/timelog_controller.rb | 84 ++++++++++++--------- app/helpers/timelog_helper.rb | 4 +- app/models/project.rb | 15 ++++ app/models/time_entry.rb | 6 ++ app/views/timelog/_list.rhtml | 16 ++-- app/views/timelog/_report_criteria.rhtml | 22 +++--- app/views/timelog/details.rhtml | 3 +- app/views/timelog/report.rhtml | 46 +++++++---- lib/ar_condition.rb | 41 ++++++++++ public/images/report.png | Bin 0 -> 1152 bytes public/stylesheets/application.css | 11 ++- test/fixtures/time_entries.yml | 21 +++++- test/functional/timelog_controller_test.rb | 30 +++++--- 14 files changed, 221 insertions(+), 84 deletions(-) create mode 100644 lib/ar_condition.rb create mode 100644 public/images/report.png diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index a3eac71ab..39eb33b99 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -90,7 +90,11 @@ class ProjectsController < ApplicationController @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 diff --git a/app/controllers/timelog_controller.rb b/app/controllers/timelog_controller.rb index 6ce86fd50..d672ac577 100644 --- a/app/controllers/timelog_controller.rb +++ b/app/controllers/timelog_controller.rb @@ -26,26 +26,30 @@ class TimelogController < ApplicationController 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' @@ -63,8 +67,11 @@ class TimelogController < ApplicationController 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" @@ -80,6 +87,8 @@ class TimelogController < ApplicationController row['week'] = "#{row['tyear']}-#{row['tweek']}" end end + + @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f} end @periods = [] @@ -147,41 +156,44 @@ class TimelogController < ApplicationController @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 diff --git a/app/helpers/timelog_helper.rb b/app/helpers/timelog_helper.rb index 2af748f6f..05b55907c 100644 --- a/app/helpers/timelog_helper.rb +++ b/app/helpers/timelog_helper.rb @@ -17,7 +17,7 @@ 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) @@ -50,6 +50,7 @@ module TimelogHelper headers = [l(:field_spent_on), l(:field_user), l(:field_activity), + l(:field_project), l(:field_issue), l(:field_tracker), l(:field_subject), @@ -62,6 +63,7 @@ module TimelogHelper 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), diff --git a/app/models/project.rb b/app/models/project.rb index 73a8d6404..8f1dada1d 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -110,6 +110,21 @@ class Project < ActiveRecord::Base 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) diff --git a/app/models/time_entry.rb b/app/models/time_entry.rb index 04df5233f..0f8f62889 100644 --- a/app/models/time_entry.rb +++ b/app/models/time_entry.rb @@ -52,4 +52,10 @@ class TimeEntry < ActiveRecord::Base 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 diff --git a/app/views/timelog/_list.rhtml b/app/views/timelog/_list.rhtml index 929d8f7dd..ae5b6376a 100644 --- a/app/views/timelog/_list.rhtml +++ b/app/views/timelog/_list.rhtml @@ -3,6 +3,7 @@ <%= 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') %> <%= l(:field_comments) %> <%= sort_header_tag('hours', :caption => l(:field_hours)) %> @@ -12,17 +13,16 @@ <% entries.each do |entry| -%> "> <%= format_date(entry.spent_on) %> -<%= entry.user.name %> -<%= entry.activity.name %> +<%=h entry.user %> +<%=h entry.activity %> +<%=h entry.project %> - <% if entry.issue -%> -
<%= link_to_issue entry.issue %>: <%= h(truncate(entry.issue.subject, 50)) -%> - <%= render_issue_tooltip entry.issue %> -
- <% end -%> +<% if entry.issue -%> +<%= link_to_issue entry.issue %>: <%= h(truncate(entry.issue.subject, 50)) -%> +<% end -%> <%=h entry.comments %> -<%= entry.hours %> +<%= html_hours("%.2f" % entry.hours) %> <%= link_to_if_authorized(l(:button_edit), {:controller => 'timelog', :action => 'edit', :id => entry}, :class => 'icon icon-edit') if entry.editable_by?(User.current) %> diff --git a/app/views/timelog/_report_criteria.rhtml b/app/views/timelog/_report_criteria.rhtml index 0b7e91cfd..b048c874a 100644 --- a/app/views/timelog/_report_criteria.rhtml +++ b/app/views/timelog/_report_criteria.rhtml @@ -1,17 +1,17 @@ -<% @available_criterias[criterias[level]][:values].each do |value| %> - +<% @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? -%> + <%= '' * level %> -<%= value.name %> -<%= '' * (criterias.length - level - 1) %> -<% hours_for_value = select_hours(hours, criterias[level], value.id) %> - <% @periods.each do |period| %> +<%= value.nil? ? l(:label_none) : @available_criterias[criterias[level]][:klass].find_by_id(value) %> +<%= '' * (criterias.length - level - 1) -%> + <% @periods.each do |period| -%> <% sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s)) %> - <%= sum > 0 ? "%.2f" % sum : "-" %> - <% end %> + <%= html_hours("%.2f" % sum) if sum > 0 %> + <% end -%> -<% 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 %> diff --git a/app/views/timelog/details.rhtml b/app/views/timelog/details.rhtml index 0e128f6ae..802090b7a 100644 --- a/app/views/timelog/details.rhtml +++ b/app/views/timelog/details.rhtml @@ -1,4 +1,5 @@
+<%= link_to(l(:label_report), {:controller => 'timelog', :action => 'report', :project_id => @project}, :class => 'icon icon-report') %> <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'edit', :project_id => @project, :issue_id => @issue}, :class => 'icon icon-time' %>
@@ -25,7 +26,7 @@ <%= text_field_tag 'from', @from, :size => 10, :onfocus => '$("period_type_2").checked = true;' %> <%= calendar_for('from') %> <%= l(:label_date_to) %> <%= text_field_tag 'to', @to, :size => 10, :onfocus => '$("period_type_2").checked = true;' %> <%= calendar_for('to') %> -<%= submit_tag l(:button_submit), :name => nil, :onclick => '$("period_type_2").checked = true;' %> +<%= submit_tag l(:button_apply), :name => nil, :onclick => '$("period_type_2").checked = true;' %>

<% end %> diff --git a/app/views/timelog/report.rhtml b/app/views/timelog/report.rhtml index 4fabfe3dd..8fc15a3b4 100644 --- a/app/views/timelog/report.rhtml +++ b/app/views/timelog/report.rhtml @@ -1,31 +1,43 @@ +
+<%= 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' %> +
+

<%= l(:label_spent_time) %>

<% form_remote_tag(:url => {:project_id => @project}, :update => 'content') do %> <% @criterias.each do |criteria| %> <%= hidden_field_tag 'criterias[]', criteria %> <% end %> +
<%= l(:label_date_range) %>

- <%= 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' %>

+
- <% if @criterias.length < 3 %> -

<%= 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();") %>

- <% end %> +

<%= 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' %>

-
+<% unless @criterias.empty? %> +
+

<%= l(:label_total) %>: <%= html_hours(lwr(:label_f_hour, @total_hours)) %>

+
-<% unless @criterias.empty? %> - +<% unless @hours.empty? %> +
<% @criterias.each do |criteria| %> @@ -36,13 +48,21 @@ <% end %> - <%= render :partial => 'report_criteria', :locals => {:criterias => @criterias, :hours => @hours, :level => 0} %> + + + <%= '' * (@criterias.size - 1) %> + <% @periods.each do |period| -%> + <% sum = sum_hours(select_hours(@hours, @columns, period.to_s)) %> + + <% end -%> +
<%= l(:label_total) %><%= html_hours("%.2f" % sum) if sum > 0 %>
<% end %> <% end %> +<% end %> <% content_for :header_tags do %> <%= javascript_include_tag 'calendar/calendar' %> diff --git a/lib/ar_condition.rb b/lib/ar_condition.rb new file mode 100644 index 000000000..b08e90761 --- /dev/null +++ b/lib/ar_condition.rb @@ -0,0 +1,41 @@ +# 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 diff --git a/public/images/report.png b/public/images/report.png new file mode 100644 index 0000000000000000000000000000000000000000..05386ac4f44c052af3a0fe6426d418b133cdde7f GIT binary patch literal 1152 zcmeHE?@Lor7(S+@n_8Oo!y=m$C`G!NU^4<;TRGDyQ$$CFB6H}p!i`9-MA=;#EGrmk zwhvj}4@Fn8I)up0EIEDhf*(2!KOCu`TaXX2Vy6Ogp4nPEwKoqw{R8N=(a*IfiR8fLd!y08oV_0JmNg}cp zAz9aT1ydPOK_Z&s(V$J0{fbHyl|q$@GNF_b*`h%=maL4(lrcsmio+0!1X`IYP~=yT zO4T-o0~B)%PsWyf^jD#5$G{vnbQ3>(! zx{5?b0!)Mc2!=?Z*hADRf^1l^MUcRMBk)iH{wR(rGy=6S)VLcPXTt&Rz=OsECpaYp z-L862{yo;tFVjwYMZ=ZF(7bck^}5|?btn=@nRxVL9cQ*P$+2Y;;(_f4LaODDRv zK0ewxh_cQ(4i4PUD1YN?zWIzLug~5))eD|3lvKYOIrMsQU)TtqH!iGo*0??!@}9gq z|8lBtw8=K+&F 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) @@ -68,9 +80,9 @@ class TimelogControllerTest < Test::Unit::TestCase 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 -- 2.39.5