Merge pull request #2962 from techAPJ/patch-1

FEATURE: add custom user fields in user csv export
This commit is contained in:
Régis Hanol 2014-11-13 11:11:02 +01:00
commit 4f84a3620e

View file

@ -4,6 +4,9 @@ require_dependency 'system_message'
module Jobs module Jobs
class ExportCsvFile < Jobs::Base class ExportCsvFile < Jobs::Base
CSV_USER_ATTRS = ['id','name','username','email','title','created_at','trust_level','active','admin','moderator','ip_address']
CSV_USER_STATS = ['topics_entered','posts_read_count','time_read','topic_count','post_count','likes_given','likes_received']
sidekiq_options retry: false sidekiq_options retry: false
attr_accessor :current_user attr_accessor :current_user
@ -43,7 +46,7 @@ module Jobs
private private
def get_group_names(user) def get_group_names(user)
group_names = [] group_names = Array.new
groups = user.groups groups = user.groups
groups.each do |group| groups.each do |group|
group_names.push(group.name) group_names.push(group.name)
@ -52,21 +55,39 @@ module Jobs
end end
def get_user_fields(user) def get_user_fields(user)
csv_user_attrs = ['id','name','username','email','title','created_at'] user_array = Array.new
csv_user_stats = ['topics_entered','posts_read_count','time_read','topic_count','post_count','likes_given','likes_received']
user_array = []
csv_user_attrs.each do |attr| CSV_USER_ATTRS.each do |attr|
user_array.push(user.attributes[attr]) user_array.push(user.attributes[attr])
end end
csv_user_stats.each do |stat| CSV_USER_STATS.each do |stat|
user_array.push(user.user_stat.attributes[stat]) user_array.push(user.user_stat.attributes[stat])
end end
if user.user_fields.present?
user.user_fields.each do |custom_field|
user_array.push(custom_field[1])
end
end
return user_array return user_array
end end
def get_header
header_array = CSV_USER_ATTRS + CSV_USER_STATS
user_custom_fields = UserField.all
if user_custom_fields.present?
user_custom_fields.each do |custom_field|
header_array.push("#{custom_field.name} (custom user field)")
end
end
header_array.push("group_names")
return header_array
end
def set_file_path def set_file_path
@file_name = "export_#{SecureRandom.hex(4)}.csv" @file_name = "export_#{SecureRandom.hex(4)}.csv"
# ensure directory exists # ensure directory exists
@ -77,6 +98,7 @@ module Jobs
def write_csv_file(data) def write_csv_file(data)
# write to CSV file # write to CSV file
CSV.open(File.expand_path("#{ExportCsv.base_directory}/#{@file_name}", __FILE__), "w") do |csv| CSV.open(File.expand_path("#{ExportCsv.base_directory}/#{@file_name}", __FILE__), "w") do |csv|
csv << get_header
data.each do |value| data.each do |value|
csv << value csv << value
end end