From 0ae9a2383977b1e17351e616505989b291a148df Mon Sep 17 00:00:00 2001 From: ferro96 Date: Tue, 17 Dec 2024 09:41:55 -0600 Subject: [PATCH] Added logic to display analytics on course --- app/controllers/courses_controller.rb | 5 ++++- app/views/courses/_course.html.haml | 6 +++++- app/views/courses/analytics.html.haml | 30 +++++++++++++++++++++++++++ config/routes.rb | 1 + 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 app/views/courses/analytics.html.haml diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index e4b7546..c36c43f 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -1,6 +1,6 @@ class CoursesController < ApplicationController skip_before_action :authenticate_user!, only: [ :show ] - before_action :set_course, only: %i[ show edit update destroy approve unapprove ] + before_action :set_course, only: %i[ show edit update destroy approve unapprove analytics ] # GET /courses or /courses.json def index @@ -118,6 +118,9 @@ def unapproved render "index" end + def analytics + authorize @course, :owner? + end def destroy authorize @course diff --git a/app/views/courses/_course.html.haml b/app/views/courses/_course.html.haml index ff092c0..d0d83de 100644 --- a/app/views/courses/_course.html.haml +++ b/app/views/courses/_course.html.haml @@ -52,10 +52,14 @@ - if policy(course).edit? .card-footer - if policy(course).owner? - = link_to "Add lesson", new_course_lesson_path(course, @lesson), class: "btn btn-sm btn-primary" + = link_to "Add lesson", new_course_lesson_path(course, @lesson), class: 'btn btn-sm btn-primary' = link_to 'Edit', edit_course_path(course), class: 'btn btn-sm btn-warning' -# - if current_user && policy(course).destroy? = link_to 'Destroy', course, data: { turbo_method: :delete, confirm: 'Are you sure?' }, class: 'btn btn-sm btn-danger' + = link_to analytics_course_path(course), class: 'btn btn-sm btn-info' do + .fa.fa-chart-bar + Analytics + - if policy(course).destroy? .card-footer - if course.published? diff --git a/app/views/courses/analytics.html.haml b/app/views/courses/analytics.html.haml new file mode 100644 index 0000000..ddce095 --- /dev/null +++ b/app/views/courses/analytics.html.haml @@ -0,0 +1,30 @@ +-content_for :title do + E-learning + \| + = @course.title + Analytics + += link_to 'Courses', courses_path +\/ += link_to course_path(@course) do + .fa.fa-graduation-cap + = @course.title +\/ += link_to analytics_course_path(@course) do + .fa.fa-chart-bar + Analytics + +%h4.text-center + Analytics for #{@course.title} + +Enrollments per day, quantity += line_chart @course.enrollments.group_by_day(:created_at).count + +Enrollments per day, revenue += line_chart @course.enrollments.group_by_day(:created_at).sum(:price) + +Unique user views(how many unique users viewed the lesson) += bar_chart @course.user_lessons.joins(:lesson).group(:'lessons.title').count + +Impressions per lesson (how many times the lesson was opened) += bar_chart @course.user_lessons.joins(:lesson).group(:'lessons.title').sum(:impression) diff --git a/config/routes.rb b/config/routes.rb index e13093e..55c2c09 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,6 +13,7 @@ resources :courses do get :purchased, :pending_review, :created, :unapproved, on: :collection member do + get :analytics patch :approve patch :unapprove end