Patch #3803
openShow custom user fields in csv export of time entry reports
0%
Description
Ok, fist of all, this was my first morning of ruby hacking, so code quality = 0. But it does what i need for now, and i wanted to share it.
The idea is as follows, i use these csv's to get my data in categories and per user into excel and do analysis on them. The problem is that not all users work at the same rate, and that is kinda vital to my analysis. I also don't want to copy paste in excel until i have a "rate" column for every user in every category in every product. The Rate plugin did,'t integrate with the normal time tracking export so i changed some core code by myself.
So i created a custom field on user (Rate) and i added some "if" statements in report_ ... _to_csv in the timeloghelper.rb. If "Member" is in the criteria, i add the custom fields of user just behind the Member column. This way i would have a colum "Member" and a column "Rate" (or any other custom user field i make.
To make it work i changed these to methods like so:
Index: timelog_helper.rb
===================================================================
--- timelog_helper.rb (revision 2847)
+++ timelog_helper.rb (working copy)
@@ -68,6 +68,8 @@
ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
decimal_separator = l(:general_csv_decimal_separator)
custom_fields = TimeEntryCustomField.find(:all)
+ #custom_user_fields = UserCustomField.find(:all)
+ #raise custom_user_fields.inspect
export = StringIO.new
CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
# csv header fields
@@ -115,6 +117,11 @@
CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
# Column headers
headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
+ #logger.info{headers.inspect}
+ if headers.include?("Member")
+ custom_user_fields = UserCustomField.find(:all)
+ headers += custom_user_fields.collect(&:name)
+ end
headers += periods
headers << l(:label_total)
csv << headers.collect {|c| to_utf8(c) }
@@ -141,6 +148,14 @@
next if hours_for_value.empty?
row = [''] * level
row << to_utf8(format_criteria_value(criterias[level], value))
+ #logger.info criterias.inspect
+ if (criterias[level] == "member")
+ k = @available_criterias[criterias[level]][:klass]
+ user = k.find_by_id(value.to_i)
+ custom_user_fields = UserCustomField.find(:all)
+ #logger.info user.inspect
+ row += custom_user_fields.collect {|f| show_value(user.custom_value_for(f)) }
+ end
row += [''] * (criterias.length - level - 1)
total = 0
periods.each do |period|
Updated by Roderik van der Veer about 16 years ago
Sorry, the total rows didn't move up if custom fields were added, the correct patch:
Index: timelog_helper.rb
===================================================================
--- timelog_helper.rb (revision 2847)
+++ timelog_helper.rb (working copy)
@@ -68,6 +68,8 @@
ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
decimal_separator = l(:general_csv_decimal_separator)
custom_fields = TimeEntryCustomField.find(:all)
+ #custom_user_fields = UserCustomField.find(:all)
+ #raise custom_user_fields.inspect
export = StringIO.new
CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
# csv header fields
@@ -115,6 +117,11 @@
CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
# Column headers
headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
+ #logger.info{headers.inspect}
+ if headers.include?("Member")
+ custom_user_fields = UserCustomField.find(:all)
+ headers += custom_user_fields.collect(&:name)
+ end
headers += periods
headers << l(:label_total)
csv << headers.collect {|c| to_utf8(c) }
@@ -141,7 +148,15 @@
next if hours_for_value.empty?
row = [''] * level
row << to_utf8(format_criteria_value(criterias[level], value))
- row += [''] * (criterias.length - level - 1)
+ #logger.info criterias.inspect
+ custom_user_fields = UserCustomField.find(:all)
+ addedamount = custom_user_fields.size
+ if (criterias[level] == "member")
+ k = @available_criterias[criterias[level]][:klass]
+ user = k.find_by_id(value.to_i)
+ row += custom_user_fields.collect {|f| show_value(user.custom_value_for(f)) }
+ end
+ row += [''] * (criterias.length + addedamount - level - 1)
total = 0
periods.each do |period|
sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s))
Updated by Roderik van der Veer about 16 years ago
another fix, sorry for spamming
Index: timelog_helper.rb
===================================================================
--- timelog_helper.rb (revision 2847)
+++ timelog_helper.rb (working copy)
@@ -115,13 +115,17 @@
CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
# Column headers
headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
+ custom_user_fields = UserCustomField.find(:all)
+ if headers.include?("Member")
+ headers += custom_user_fields.collect(&:name)
+ end
headers += periods
headers << l(:label_total)
csv << headers.collect {|c| to_utf8(c) }
# Content
report_criteria_to_csv(csv, criterias, periods, hours)
# Total row
- row = [ l(:label_total) ] + [''] * (criterias.size - 1)
+ row = [ l(:label_total) ] + [''] * (criterias.size + custom_user_fields.size - 1)
total = 0
periods.each do |period|
sum = sum_hours(select_hours(hours, @columns, period.to_s))
@@ -141,7 +145,15 @@
next if hours_for_value.empty?
row = [''] * level
row << to_utf8(format_criteria_value(criterias[level], value))
- row += [''] * (criterias.length - level - 1)
+ custom_user_fields = UserCustomField.find(:all)
+ addedamount = custom_user_fields.size
+ if (criterias[level] == "member")
+ k = @available_criterias[criterias[level]][:klass]
+ user = k.find_by_id(value.to_i)
+ row += custom_user_fields.collect {|f| show_value(user.custom_value_for(f)) }
+ addedamount = 0;
+ end
+ row += [''] * (criterias.length + addedamount - level - 1)
total = 0
periods.each do |period|
sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s))
Updated by Roderik van der Veer about 16 years ago
and hopefully the last one
Index: timelog_helper.rb
===================================================================
--- timelog_helper.rb (revision 2847)
+++ timelog_helper.rb (working copy)
@@ -115,13 +115,19 @@
CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
# Column headers
headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
+ custom_user_fields = UserCustomField.find(:all)
+ addedamount = 0
+ if headers.include?("Member")
+ headers += custom_user_fields.collect(&:name)
+ addedamount = custom_user_fields.size
+ end
headers += periods
headers << l(:label_total)
csv << headers.collect {|c| to_utf8(c) }
# Content
report_criteria_to_csv(csv, criterias, periods, hours)
# Total row
- row = [ l(:label_total) ] + [''] * (criterias.size - 1)
+ row = [ l(:label_total) ] + [''] * (criterias.size + addedamount - 1)
total = 0
periods.each do |period|
sum = sum_hours(select_hours(hours, @columns, period.to_s))
@@ -141,7 +147,18 @@
next if hours_for_value.empty?
row = [''] * level
row << to_utf8(format_criteria_value(criterias[level], value))
- row += [''] * (criterias.length - level - 1)
+ custom_user_fields = UserCustomField.find(:all)
+ addedamount = 0
+ if criterias.include?("member")
+ addedamount = custom_user_fields.size
+ end
+ if (criterias[level] == "member")
+ k = @available_criterias[criterias[level]][:klass]
+ user = k.find_by_id(value.to_i)
+ row += custom_user_fields.collect {|f| show_value(user.custom_value_for(f)) }
+ addedamount = 0;
+ end
+ row += [''] * (criterias.length + addedamount - level - 1)
total = 0
periods.each do |period|
sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s))