From a0918ce011a7e25e57eba1266f5263a2040c6016 Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Fri, 6 Oct 2023 22:38:48 +0100 Subject: [PATCH] Add total lecture time and improve time representation In the attendance tracker view, the total times for preparation, in lecture, and review have been humanized for better readability. Also, total time spent on lectures for each week has been included for a quicker overview. A helper method 'humanise_duration' has been added in application helper to convert time into a more readable format. Changes have been done in 'lecture' model for calculation of total time. --- app/helpers/application_helper.rb | 17 ++++++++++ app/models/lecture.rb | 4 +++ app/views/attendance_tracker/index.html.erb | 36 ++++++++++++++------- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be79..e2f9d7a 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,19 @@ module ApplicationHelper + def humanise_duration(duration) + raise ArgumentError, "Duration must be a number" unless duration.is_a? ActiveSupport::Duration + + seconds = duration.in_seconds + minutes = seconds / 60 + hours = minutes / 60 + + remaining_minutes = minutes % 60 + + if hours > 0 + "#{hours.floor}h #{remaining_minutes.floor}m" + elsif minutes > 0 + "#{remaining_minutes.floor} minutes" + else + "#{seconds}s" + end + end end diff --git a/app/models/lecture.rb b/app/models/lecture.rb index f761a67..92e6a9a 100644 --- a/app/models/lecture.rb +++ b/app/models/lecture.rb @@ -25,6 +25,10 @@ class Lecture < ApplicationRecord tracked_time_entries.preparation.sum(&:duration).seconds end + def total_overall_time + tracked_time_entries.sum(&:duration).seconds + end + def total_attendance_time tracked_time_entries.where(kind: [:concurrent, :catchup]).sum(&:duration).seconds end diff --git a/app/views/attendance_tracker/index.html.erb b/app/views/attendance_tracker/index.html.erb index d0b9079..c841344 100644 --- a/app/views/attendance_tracker/index.html.erb +++ b/app/views/attendance_tracker/index.html.erb @@ -22,7 +22,19 @@ <% course.lectures.order(:start_time).group_by { |lecture| lecture.week_number }.each do |(week_number, lectures)| %> - Week <%= week_number %> + +
+
+ Week <%= week_number %> +
+ +
+ <%# TODO: Improve this figure, atm it is for lectures in week, not time spent in week (ie + prepping for lecture next week will count in next week not current week) %> + (Total <%= humanise_duration(lectures.sum { |lecture| lecture.total_overall_time }.seconds) %>) +
+
+ <% lectures.reject(&:cancelled).each do |lecture| %> @@ -72,17 +84,19 @@