diff --git a/app/jobs/featured_quiz_mail_scheduler_job.rb b/app/jobs/featured_quiz_mail_scheduler_job.rb index ad77b6c6..f493c333 100644 --- a/app/jobs/featured_quiz_mail_scheduler_job.rb +++ b/app/jobs/featured_quiz_mail_scheduler_job.rb @@ -3,7 +3,7 @@ class FeaturedQuizMailSchedulerJob < ApplicationJob queue_as :default def perform(quiz) - user_emails = User.where.not(id: QuizRunner.where(quiz_id: quiz.id).pluck(:user_id)).pluck(:email) + user_emails = User.where.not(id: QuizRunner.where(quiz_id: quiz.id).select(:user_id)).pluck(:email) FeaturedQuizMailer.with(emails: user_emails, quiz: quiz).featured_quiz_email.deliver_later if user_emails.present? end diff --git a/app/mailers/welcome_user_mailer.rb b/app/mailers/welcome_user_mailer.rb index 772b43ae..2d7c7804 100644 --- a/app/mailers/welcome_user_mailer.rb +++ b/app/mailers/welcome_user_mailer.rb @@ -1,6 +1,8 @@ class WelcomeUserMailer < ApplicationMailer + def welcome_user_mail @user = params[:user] mail(to: @user.email, subject: 'Welcome new user!') end + end diff --git a/app/models/comment.rb b/app/models/comment.rb index a578f6de..db4f5d97 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -12,6 +12,8 @@ class Comment < ApplicationRecord scope :published, -> { where(published: true) } + scope :on_quizzes, -> { where(commentable_type: 'Quiz') } + def publish update_column(:published, true) end diff --git a/app/services/quizathon/stats_manager.rb b/app/services/quizathon/stats_manager.rb index 43bb0555..e99d6adb 100644 --- a/app/services/quizathon/stats_manager.rb +++ b/app/services/quizathon/stats_manager.rb @@ -2,7 +2,7 @@ module Quizathon class StatsManager - attr_accessor :user_with_most_quiz, :highest_rated_quiz, :highest_scoring_participant, :quizzes_of_highest_scoring_participant, :commenting_user, :quiz_users_count_hash + attr_accessor :user_with_most_quiz, :highest_rated_quiz, :highest_scoring_participant, :commenting_user, :quizzes_with_participants_count def initialize(from, to) @from = from || 1.month.ago @@ -21,29 +21,23 @@ def fetch_stats private def fetch_user_with_most_quiz - user_id = QuizRunner.between(@from, @to).select(:user_id, 'count(*) AS quiz_count').group(:user_id).order(quiz_count: :desc).first.user_id - @user_with_most_quiz = User.find_by_id(user_id) + @user_with_most_quiz = QuizRunner.between(@from, @to).select(:user_id, 'count(*) AS quiz_count').group(:user_id).order(quiz_count: :desc).first.user end def fetch_highest_rated_quiz - quiz_id = Rating.select(:quiz_id, 'SUM(value) as total_rating').group(:quiz_id).order(total_rating: :desc).first.quiz_id - @highest_rated_quiz = Quiz.find_by_id(quiz_id) + @highest_rated_quiz = Rating.select(:quiz_id, 'SUM(value) as total_rating').group(:quiz_id).order(total_rating: :desc).first.quiz end def fetch_highest_scoring_participant(time_range) - user_id = QuizRunner.select(:user_id, 'SUM(score) AS total_score').group(:user_id).order(total_score: :desc).first.user_id - @highest_scoring_participant = User.find_by_id(user_id) - @quizzes_of_highest_scoring_participant = @highest_scoring_participant.quizzes.where(id: QuizRunner.where(created_at: time_range).pluck(:quiz_id)) + @highest_scoring_participant = User.includes(:quizzes).joins(:quiz_runners).select(:id, :first_name, :last_name, 'SUM(score) AS total_score').where('quiz_runners.created_at': time_range).group(:id).order(total_score: :desc).first end def fetch_commenting_user(time_range, min_no_of_quizzes_commented) - user_id = Comment.where(created_at: time_range, commentable_type: 'Quiz').select(:user_id, 'COUNT (DISTINCT commentable_id) as quizzes_count').group(:user_id).having('COUNT (DISTINCT commentable_id) >= ?', min_no_of_quizzes_commented).order(quizzes_count: :desc).first&.user_id - @commenting_user = User.find_by_id(user_id) + @commenting_user = User.joins(:comments).where(comments: {commentable_type: 'Quiz', created_at: time_range}).group(:id).having('count(distinct comments.commentable_id) >= ?', min_no_of_quizzes_commented).select('users.*, count(distinct comments.commentable_id) as total_quiz_comment').order(total_quiz_comment: :desc).first end def fetch_quizzes_with_participants_count - quiz_users_count_records = QuizRunner.select(:quiz_id, 'COUNT(user_id) AS users_count').group(:quiz_id).order(users_count: :desc) - @quiz_users_count_hash = quiz_users_count_records.map { |record| [Quiz.find_by_id(record.quiz_id), record.users_count] }.to_h + @quizzes_with_participants_count = Quiz.joins(:quiz_runners).select('quizzes.*', 'COUNT(user_id) AS users_count').group(:id).order(users_count: :desc) end end diff --git a/app/views/admin/stats/index.html.erb b/app/views/admin/stats/index.html.erb index b8c3292b..5e53c8c4 100644 --- a/app/views/admin/stats/index.html.erb +++ b/app/views/admin/stats/index.html.erb @@ -21,7 +21,7 @@
@@ -37,11 +37,11 @@ Participants count Revenue generated(Rs.) - <% @stats_manager.quiz_users_count_hash.each do |quiz, users_count| %> + <% @stats_manager.quizzes_with_participants_count.each do |quiz| %> <%= quiz.title %> - <%= users_count %> - <%= quiz&.payments&.size.to_i * 5 %> + <%= quiz.users_count %> + <%= quiz.payments.size * 5 %> <% end %>