diff --git a/app/controllers/analytics_dashboard_controller.rb b/app/controllers/analytics_dashboard_controller.rb index 5db4f2eeaa0d984a9180569955e355ffe5309bc3..cc7546f51a7ac42268215062314e3ac22cb344eb 100644 --- a/app/controllers/analytics_dashboard_controller.rb +++ b/app/controllers/analytics_dashboard_controller.rb @@ -58,6 +58,7 @@ class AnalyticsDashboardController < ApplicationController def searches_per_tag_chart options = { + tags: params[:tags].presence, start_date: params[:start_date].presence, end_date: params[:end_date].presence, top_n: params[:top_n].presence&.to_i diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f1eb33bf8fcdb86c2fb7feabeddd7c06e1aa696d..1df2f82573222b75c574b826d02d0531c6a75c83 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -374,7 +374,8 @@ class ApplicationController < ActionController::Base path.start_with?('/assets/') || path.end_with?('.css') || path.end_with?('.js') - return true if path.start_with?('/lti/') + # Allow LTI and survey + return true if path.start_with?('/lti/') || path == '/survey' # Make available to controller that the we should not leak posts in the sidebar @prevent_sidebar = true diff --git a/app/helpers/analytics_dashboard_helper.rb b/app/helpers/analytics_dashboard_helper.rb index a2f1ef4900879c41c0799993bfbdf2f2b786e56a..2b27fc3589def03c1464d06d2021f78e4ddc1625 100644 --- a/app/helpers/analytics_dashboard_helper.rb +++ b/app/helpers/analytics_dashboard_helper.rb @@ -1,8 +1,9 @@ module AnalyticsDashboardHelper # rubocop:disable Metrics/ParameterLists - def number_of_searches_per_tag(start_date: nil, end_date: nil, top_n: 10) + def number_of_searches_per_tag(tags: nil, start_date: nil, end_date: nil, top_n: 10) top_n ||= 10 - hash = {} + searched_tag_counts = {} + searched_tag_counts_by_id = {} base = ActionLog.where(controller_name: 'search', controller_action_name: 'search') base = base.where(created_at: start_date..end_date) if start_date.present? || end_date.present? base.find_each do |result| @@ -19,25 +20,31 @@ module AnalyticsDashboardHelper value = splat[1] # Only take the 'tag' qualifiers from the string and add them to result if parameter == 'tag' - hash[value] = hash.fetch(value, 0) + 1 + searched_tag_counts[value] = searched_tag_counts.fetch(value, 0) + 1 end end end # Extract the parameters from 'include_tags' parameters - if params_hash['include_tags'].present? - valid_value = { - integer: /^\d+$/ - } - # Filter only the valid ids - check that they are integers - tags_ids = params[:include_tags]&.all? { |id| id.match? valid_value[:integer] } - Tag.where(tags_ids).select(:name).find_each do |tag| - hash[tag.name] = hash.fetch(tag.name, 0) + 1 + included_tags = params_hash['included_tags']&.grep(/\d+/, &:to_i) + if included_tags.present? + included_tags.each do |tag_id| + searched_tag_counts_by_id[tag_id] = searched_tag_counts_by_id.fetch(tag_id, 0) + 1 end end end - puts hash + + # Convert the tags by id to the other format + Tag.where(id: searched_tag_counts_by_id.keys).select(:id, :name).find_each do |tag| + searched_tag_counts[tag.name] = searched_tag_counts.fetch(tag.name, 0) + searched_tag_counts_by_id[tag.id] + end + + # Filter to only the selected tags + if tags.present? + searched_tag_counts.slice!(Tag.where(id: tags).pluck(:name)) + end + # Take top 10 keys with largest values - hash.sort_by { |_, v| -v }.first(top_n) + searched_tag_counts.sort_by { |_, v| -v }.first(top_n) end def currently_searched