Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/jobs/featured_quiz_mail_scheduler_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions app/mailers/welcome_user_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class WelcomeUserMailer < ApplicationMailer

def welcome_user_mail
@user = params[:user]
mail(to: @user.email, subject: 'Welcome new user!')
end

end
2 changes: 2 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 6 additions & 12 deletions app/services/quizathon/stats_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions app/views/admin/stats/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<br />
<ul class="list-group">
<b>Quizzes :</b>
<% @stats_manager.quizzes_of_highest_scoring_participant.each do |quiz| %>
<% @stats_manager.highest_scoring_participant&.quizzes.each do |quiz| %>
<li class="list-group-item"><%= quiz.title %></li>
<% end %>
</ul>
Expand All @@ -37,11 +37,11 @@
<th class="text-center">Participants count</th>
<th class="text-center">Revenue generated(Rs.)</th>
</tr>
<% @stats_manager.quiz_users_count_hash.each do |quiz, users_count| %>
<% @stats_manager.quizzes_with_participants_count.each do |quiz| %>
<tr>
<td class="text-center"><%= quiz.title %></td>
<td class="text-center"><%= users_count %></td>
<td class="text-center"><%= quiz&.payments&.size.to_i * 5 %></td>
<td class="text-center"><%= quiz.users_count %></td>
<td class="text-center"><%= quiz.payments.size * 5 %></td>
</tr>
<% end %>
</table>
Expand Down