diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2008-02-27 20:50:19 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2008-02-27 20:50:19 +0000 |
commit | 200842ba5e753f342940d7df0f1cc342ae8485d4 (patch) | |
tree | 50ceded1ab5f0472b3896a07a330b8304521a84d /app | |
parent | 421d4203113a3cefdcc6998a9b9cc92ddbb27321 (diff) | |
download | redmine-200842ba5e753f342940d7df0f1cc342ae8485d4.tar.gz redmine-200842ba5e753f342940d7df0f1cc342ae8485d4.zip |
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
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/projects_controller.rb | 6 | ||||
-rw-r--r-- | app/controllers/timelog_controller.rb | 84 | ||||
-rw-r--r-- | app/helpers/timelog_helper.rb | 4 | ||||
-rw-r--r-- | app/models/project.rb | 15 | ||||
-rw-r--r-- | app/models/time_entry.rb | 6 | ||||
-rw-r--r-- | app/views/timelog/_list.rhtml | 16 | ||||
-rw-r--r-- | app/views/timelog/_report_criteria.rhtml | 22 | ||||
-rw-r--r-- | app/views/timelog/details.rhtml | 3 | ||||
-rw-r--r-- | app/views/timelog/report.rhtml | 46 |
9 files changed, 131 insertions, 71 deletions
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') %> <th><%= l(:field_comments) %></th> <%= sort_header_tag('hours', :caption => l(:field_hours)) %> @@ -12,17 +13,16 @@ <% 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> 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| %> -<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 %> 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 @@ <div class="contextual">
+<%= 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' %>
</div>
@@ -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;' %>
</p>
</fieldset>
<% 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 @@ +<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| %> @@ -36,13 +48,21 @@ <% 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' %> |