diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index e46078c5..27378e64 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,3 +1,5 @@ +require "csv" + module Admin class UsersController < BaseController def change_user_roles @@ -14,5 +16,47 @@ def change_user_roles render status: :unprocessable_entity, json: "roles failed to update" end end + + def export_user_info + @users = User.where(organization_id: current_organization.id) + + respond_to do |format| + format.csv { send_data users_csv(@users), filename: "#{subdomain_name}_users_#{current_date_string}.csv" } + end + end + + private + + def users_csv(users) + CSV.generate do |csv| + attributes = [] + csv << ["User Name", "User Last Name", "User Email", "User Role", "Registration Date", "Branch", "Zip Code", "Courses User has Started", "Courses User has Completed"] + + users.each do |user| + row = [] + profile = user.profile + + row << profile.try(:first_name) + row << profile.try(:last_name) + row << user.email + row << user.roles.map(&:name).map(&:capitalize).join(", ") + row << user.created_at.in_time_zone("Central Time (US & Canada)").strftime("%m-%d-%Y") + row << profile.try(:library_location).try(:name) + row << profile.try(:zip_code) + row << user.course_progresses.where(completed_at: nil).map{ |cp| cp.course.title }.join(", ") + row << user.course_progresses.where.not(completed_at: nil).map{ |cp| cp.course.title }.join(", ") + + csv << row + end + end + end + + def subdomain_name + current_organization.try(:subdomain) + end + + def current_date_string + Date.today.strftime("%m-%d-%Y") + end end end diff --git a/app/views/admin/courses/index.html.erb b/app/views/admin/courses/index.html.erb index 007ee7c0..d49c48aa 100644 --- a/app/views/admin/courses/index.html.erb +++ b/app/views/admin/courses/index.html.erb @@ -13,7 +13,6 @@ - <% (@category_ids || []).each do |category_id| %> <%= render partial: "admin/courses/sortable_list", locals: { courses: @courses.with_category(category_id), category: Category.find_by_id(category_id) } %> <% end %> diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb index f02b26c9..ea48a076 100644 --- a/app/views/admin/users/index.html.erb +++ b/app/views/admin/users/index.html.erb @@ -2,12 +2,19 @@