Project

General

Profile

Feature #3848 » log-time-for-other-users.diff

Gary Watson, 2013-07-08 23:12

View differences:

app/controllers/timelog_controller.rb
end
def create
@time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
# Check if current user has permissions to edit time entries
if request.post? and User.current.allowed_to?(:edit_time_entries, @project)
user = User.find(params[:time_entry][:user_id])
else
user = User.current
end
@time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => user, :spent_on => user.today)
@time_entry.safe_attributes = params[:time_entry]
call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
app/helpers/timelog_helper.rb
activities.each { |a| collection << [a.name, a.id] }
collection
end
# Returns a collection of users for a select field.
def user_collection_for_select_options(project, selected = nil)
collection = project.members.map{|member| member.user }
collection.keep_if{|user| user.allowed_to?(:log_time, project)}
s = ''
s << content_tag('option', "<< #{l(:label_me)} >>", :value => User.current.id) if User.current.admin? || collection.include?(User.current)
collection.sort.each do |element|
selected_attribute = ' selected="selected"' if option_value_selected?(element, selected)
s << %(<option value="#{element.id}"#{selected_attribute}>#{h element.name}</option>)
end
s.html_safe
end
def select_hours(data, criteria, value)
if value.to_s.empty?
app/models/issue.rb
@time_entry = existing_time_entry || TimeEntry.new
@time_entry.project = project
@time_entry.issue = self
@time_entry.user = User.current
@time_entry.spent_on = User.current.today
@time_entry.attributes = params[:time_entry]
self.time_entries << @time_entry
@time_entry.user ||= User.current # For some unknown reason, if User.current override @time_entry.user when it's already set,
@time_entry.spent_on ||= User.current.today
@time_entry.attributes = params[:time_entry] # and although this instruction will revert the user to that in params[:time_entry][:user_id]
self.time_entries << @time_entry # This instruction will force @time_entry.user to be User.current, independently of what it has already set. Or at least that happened to me.
end
# TODO: Rename hook
app/models/time_entry.rb
belongs_to :user
belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id'
attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
attr_protected :project_id, :tyear, :tmonth, :tweek
acts_as_customizable
acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
......
}
safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
safe_attributes 'user_id', :if => lambda{ |time_entry, user| user.allowed_to?(:edit_time_entries, time_entry.project) }
def initialize(attributes=nil, *args)
super
app/views/issues/_edit.html.erb
<div class="splitcontentright">
<p><%= time_entry.select :activity_id, activity_collection_for_select_options %></p>
</div>
<% if User.current.allowed_to?(:edit_time_entries, @project) %>
<p> <%= time_entry.select :user_id, user_collection_for_select_options(@project, @time_entry.user), :required => true %></p>
<% end %>
<p><%= time_entry.text_field :comments, :size => 60 %></p>
<% @time_entry.custom_field_values.each do |value| %>
<p><%= custom_field_tag_with_label :time_entry, value %></p>
app/views/timelog/_form.html.erb
</p>
<p><%= f.text_field :spent_on, :size => 10, :required => true %><%= calendar_for('time_entry_spent_on') %></p>
<p><%= f.text_field :hours, :size => 6, :required => true %></p>
<% if User.current.allowed_to?(:edit_time_entries, @project) %>
<p> <%= f.select :user_id, user_collection_for_select_options(@project, @time_entry.user), :required => true %></p>
<% end %>
<p><%= f.text_field :comments, :size => 100, :maxlength => 255 %></p>
<p><%= f.select :activity_id, activity_collection_for_select_options(@time_entry), :required => true %></p>
<% @time_entry.custom_field_values.each do |value| %>
(12-12/21)