diff --git a/app/controllers/exercise_collections_controller.rb b/app/controllers/exercise_collections_controller.rb index 4861a062..6d239d9c 100644 --- a/app/controllers/exercise_collections_controller.rb +++ b/app/controllers/exercise_collections_controller.rb @@ -1,7 +1,8 @@ class ExerciseCollectionsController < ApplicationController include CommonBehavior + include TimeHelper - before_action :set_exercise_collection, only: [:show, :edit, :update, :destroy] + before_action :set_exercise_collection, only: [:show, :edit, :update, :destroy, :statistics] def index @exercise_collections = ExerciseCollection.all.paginate(:page => params[:page]) @@ -34,6 +35,14 @@ class ExerciseCollectionsController < ApplicationController update_and_respond(object: @exercise_collection, params: exercise_collection_params) end + def statistics + @working_times = {} + @exercise_collection.exercises.each do |exercise| + @working_times[exercise.id] = time_to_f exercise.average_working_time + end + @average = @working_times.values.reduce(:+) / @working_times.size + end + private def set_exercise_collection diff --git a/app/policies/exercise_collection_policy.rb b/app/policies/exercise_collection_policy.rb index ff150290..3d6b725e 100644 --- a/app/policies/exercise_collection_policy.rb +++ b/app/policies/exercise_collection_policy.rb @@ -1,3 +1,7 @@ class ExerciseCollectionPolicy < AdminOnlyPolicy + def statistics? + admin? + end + end diff --git a/app/views/exercise_collections/index.html.slim b/app/views/exercise_collections/index.html.slim index 75a9d011..e0e8ebbc 100644 --- a/app/views/exercise_collections/index.html.slim +++ b/app/views/exercise_collections/index.html.slim @@ -8,7 +8,7 @@ h1 = ExerciseCollection.model_name.human(count: 2) th = t('activerecord.attributes.exercise_collections.name') th = t('activerecord.attributes.exercise_collections.updated_at') th = t('activerecord.attributes.exercise_collections.exercises') - th colspan=3 = t('shared.actions') + th colspan=4 = t('shared.actions') tbody - @exercise_collections.each do |collection| tr @@ -18,6 +18,7 @@ h1 = ExerciseCollection.model_name.human(count: 2) td = collection.exercises.size td = link_to(t('shared.show'), collection) td = link_to(t('shared.edit'), edit_exercise_collection_path(collection)) + td = link_to(t('shared.statistics'), statistics_exercise_collection_path(collection)) td = link_to(t('shared.destroy'), collection, data: {confirm: t('shared.confirm_destroy')}, method: :delete) = render('shared/pagination', collection: @exercise_collections) diff --git a/app/views/exercise_collections/statistics.html.slim b/app/views/exercise_collections/statistics.html.slim new file mode 100644 index 00000000..9ca7cc78 --- /dev/null +++ b/app/views/exercise_collections/statistics.html.slim @@ -0,0 +1,6 @@ +h1 = @exercise_collection + += row(label: 'exercise_collections.name', value: @exercise_collection.name) += row(label: 'exercise_collections.updated_at', value: @exercise_collection.updated_at) += row(label: 'exercise_collections.exercises', value: @exercise_collection.exercises.count) += row(label: 'exercises.statistics.average_worktime', value: @average.round(3).to_s + 's') diff --git a/config/routes.rb b/config/routes.rb index 21b1b719..399bee40 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -84,7 +84,11 @@ Rails.application.routes.draw do end end - resources :exercise_collections + resources :exercise_collections do + member do + get :statistics + end + end resources :proxy_exercises do member do