Allow filtering external user statistics by tag

This commit is contained in:
Maximilian Grundke
2018-07-20 12:57:57 +02:00
parent 39f1d3ec3a
commit bc33d9211f
2 changed files with 17 additions and 13 deletions

View File

@ -14,12 +14,12 @@ class ExternalUsersController < ApplicationController
authorize! authorize!
end end
def working_time_query def working_time_query(tag=nil)
""" """
SELECT user_id, SELECT user_id,
exercise_id, bar.exercise_id,
max(score) as maximum_score, max(score) as maximum_score,
count(id) as runs, count(bar.id) as runs,
sum(working_time_new) AS working_time sum(working_time_new) AS working_time
FROM FROM
(SELECT user_id, (SELECT user_id,
@ -42,9 +42,12 @@ class ExternalUsersController < ApplicationController
AND user_type = 'ExternalUser' AND user_type = 'ExternalUser'
GROUP BY exercise_id, GROUP BY exercise_id,
user_id, user_id,
id) AS foo) AS bar id
) AS foo
) AS bar
#{tag.nil? ? '' : ' JOIN exercise_tags et ON et.exercise_id = bar.exercise_id AND et.tag_id = ' + tag + ' '}
GROUP BY user_id, GROUP BY user_id,
exercise_id; bar.exercise_id;
""" """
end end
@ -54,7 +57,7 @@ class ExternalUsersController < ApplicationController
statistics = {} statistics = {}
ActiveRecord::Base.connection.execute(working_time_query).each do |tuple| ActiveRecord::Base.connection.execute(working_time_query(params[:tag])).each do |tuple|
statistics[tuple["exercise_id"].to_i] = tuple statistics[tuple["exercise_id"].to_i] = tuple
end end

View File

@ -1,6 +1,6 @@
h1 = t('.title') h1 = t('.title')
- exercises = Exercise.where(:id => @user.submissions.group(:exercise_id).select(:exercise_id).distinct()) - exercises = Exercise.where(:id => @user.submissions.group(:exercise_id).select(:exercise_id).distinct)
.table-responsive .table-responsive
table.table.table-striped.sortable table.table.table-striped.sortable
@ -10,9 +10,10 @@ h1 = t('.title')
th.header = t(title) th.header = t(title)
tbody tbody
- exercises.each do |exercise| - exercises.each do |exercise|
- if statistics[exercise.id] then stats = statistics[exercise.id] else stats = {"working_time" => 0, "runs" => 0, "score" => 0} - if statistics[exercise.id]
tr - stats = statistics[exercise.id]
td = link_to exercise, controller: "exercises", action: "statistics", external_user_id: @user.id, id: exercise.id tr
td = stats["maximum_score"] or 0 td = link_to exercise, controller: "exercises", action: "statistics", external_user_id: @user.id, id: exercise.id
td = stats["runs"] or 0 td = stats["maximum_score"] or 0
td = stats["working_time"] or 0 td = stats["runs"] or 0
td = stats["working_time"] or 0