diff --git a/app/controllers/api/v0/statistics_controller.rb b/app/controllers/api/v0/statistics_controller.rb index c533cd2..57c2e43 100644 --- a/app/controllers/api/v0/statistics_controller.rb +++ b/app/controllers/api/v0/statistics_controller.rb @@ -84,6 +84,47 @@ end end + # /api/v0/statistics/created_plans + # Returns the number of created plans within the user's org for the data start_date and end_date specified + def created_plans + raise Pundit::NotAuthorizedError unless Api::V0::StatisticsPolicy.new(@user, :statistics).plans? + + roles = Role.where("#{Role.creator_condition} OR #{Role.administrator_condition}") + + users = User.unscoped + if @user.can_super_admin? && params[:org_id].present? + users = users.where(org_id: params[:org_id]) + else + users = users.where(org_id: @user.org_id) + end + + plans = Plan.all + if params[:range_dates].present? + r = {} + params[:range_dates].each_pair do |k, v| + range_date_plans = plans + .where('plans.created_at >=?', v['start_date']) + .where('plans.created_at <=?', v['end_date']) + r[k] = roles.joins(:user, :plan).merge(users).merge(range_date_plans).select(:plan_id).distinct.count + end + respond_to do |format| + format.json { render(json: r.to_json) } + format.csv { + send_data(CSV.generate do |csv| + csv << [_('Month'), _('No. Plans')] + total = 0 + r.each_pair{ |k,v| csv << [k,v]; total+=v } + csv << [_('Total'), total] + end, filename: "#{_('plans')}.csv") } + end + else + plans = plans.where('plans.created_at >= ?', Date.parse(params[:start_date])) if params[:start_date].present? + plans = plans.where('plans.created_at <= ?', Date.parse(params[:end_date])) if params[:end_date].present? + count = roles.joins(:user, :plan).merge(users).merge(plans).select(:plan_id).distinct.count + render(json: { created_plans: count }) + end + end + ## # GET # @return the number of DMPs using the specified template between the optional specified dates diff --git a/app/views/usage/index.html.erb b/app/views/usage/index.html.erb index 122a71d..26ef8ef 100644 --- a/app/views/usage/index.html.erb +++ b/app/views/usage/index.html.erb @@ -16,7 +16,7 @@ <%= select_tag('topic', options_for_select( [ [_('Users'), 'users', { 'data-url': users_joined_api_v0_statistics_path }], - [_('Completed Plans'), 'completed_plans', { 'data-url': completed_plans_api_v0_statistics_path }] + [_('Plans'), 'plans', { 'data-url': created_plans_api_v0_statistics_path }] ]), class: 'form-control') %> @@ -62,7 +62,7 @@