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.
This commit is contained in:
Joshua Coles 2023-10-06 22:38:48 +01:00
parent e6dcd8e6ec
commit a0918ce011
3 changed files with 46 additions and 11 deletions

View File

@ -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

View File

@ -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

View File

@ -22,7 +22,19 @@
<tbody class="divide-y divide-gray-200 bg-white">
<% course.lectures.order(:start_time).group_by { |lecture| lecture.week_number }.each do |(week_number, lectures)| %>
<tr class="border-t border-gray-200">
<th colspan="4" scope="colgroup" class="bg-gray-50 py-2 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6">Week <%= week_number %></th>
<th colspan="5" scope="colgroup" class="bg-gray-50 py-2 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6">
<div class="flex flex-row justify-between">
<div class="">
Week <%= week_number %>
</div>
<div>
<%# 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) %>)
</div>
</div>
</th>
</tr>
<% lectures.reject(&:cancelled).each do |lecture| %>
@ -72,17 +84,19 @@
</div>
<template data-popover-target="content">
<div class="absolute p-1 w-max whitespace-normal break-words rounded-lg border border-blue-gray-50 bg-white text-blue-gray-500 shadow-lg shadow-blue-gray-500/10 focus:outline-none" data-popover-target="card">
<div>
Preparation: <%= lecture.total_preparation_time.in_minutes.round %> minutes
<% unless lecture.tracked_time_entries.empty? %>
<div class="absolute p-1 w-max whitespace-normal break-words rounded-lg border border-blue-gray-50 bg-white text-blue-gray-500 shadow-lg shadow-blue-gray-500/10 focus:outline-none" data-popover-target="card">
<div>
Preparation: <%= humanise_duration(lecture.total_preparation_time) %>
</div>
<div>
In lecture: <%= humanise_duration(lecture.total_attendance_time) %>
</div>
<div>
Review: <%= humanise_duration(lecture.total_review_time) %>
</div>
</div>
<div>
In lecture: <%= lecture.total_attendance_time.in_minutes.round %> minutes
</div>
<div>
Review: <%= lecture.total_review_time.in_minutes.round %> minutes
</div>
</div>
<% end %>
</template>
</td>