Skip to content

Commit

Permalink
Merge pull request #374 from Mariam05/LTI-415-pagination-with-totalEl…
Browse files Browse the repository at this point in the history
…ements

LTI-415: Make pagination backwards compatible
  • Loading branch information
ritzalam authored Dec 11, 2024
2 parents 2feb274 + 7ea72c4 commit 3938903
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
41 changes: 25 additions & 16 deletions app/controllers/concerns/bbb_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,31 +125,40 @@ def meeting_running?

# Fetches all recordings for a room.
def recordings(page = 1)
res = Rails.cache.fetch("rooms/#{@chosen_room.handler}/#{RECORDINGS_KEY}", expires_in: Rails.configuration.cache_expires_in_minutes.minutes) if Rails.configuration.cache_enabled
offset = (page.to_i - 1) * RECORDINGS_PER_PAGE # The offset is an index that starts at 0.
res ||= bbb.get_recordings(meetingID: @chosen_room.handler, offset: offset, limit: RECORDINGS_PER_PAGE) # offset and limit are for pagination purposes
res = Rails.cache.fetch("rooms/#{@chosen_room.handler}/#{RECORDINGS_KEY}/page#{page}", expires_in: Rails.configuration.cache_expires_in_minutes.minutes) if Rails.configuration.cache_enabled

if res
logger.debug("Pulled recordings from cache: #{res}")
else
offset = (page.to_i - 1) * RECORDINGS_PER_PAGE # The offset is an index that starts at 0.
logger.debug('Could not find recordings in cache. Pulling them from BBB: ')
res ||= bbb.get_recordings(meetingID: @chosen_room.handler, offset: offset, limit: RECORDINGS_PER_PAGE) # offset and limit are for pagination purposes
end

@num_of_recs = res[:totalElements]&.to_i
recordings_formatted(res)
end

def paginate?
@num_of_recs ? @num_of_recs > RECORDINGS_PER_PAGE : false
end
return true if @num_of_recs && @num_of_recs > RECORDINGS_PER_PAGE

# returns the amount of pages for recordings
def pages_count
@num_of_recs ? (@num_of_recs.to_f / RECORDINGS_PER_PAGE).ceil : 1
recordings_count > RECORDINGS_PER_PAGE
end

# on the last page, we don't want recordings that were in the second-to-last page to show
def recordings_limit(page)
page_int = page.to_i
num_of_recs = recordings_count
recordings_overflow = num_of_recs - page_int * RECORDINGS_PER_PAGE
# if offset is > 0 and there are less recordings than the current page * recordings per page, then we'll need to pass a limit that's less than what's defined in RECORDINGS_PER_PAGE
return recordings_overflow if page_int.positive? && recordings_overflow < RECORDINGS_PER_PAGE
# This method is used if #@num_of_recs is null, which means that totalElements is not present in the BBB response.
# Pull the rest of the recordings to see if we need to paginate.
# Set offset to 8 because we know that at least one page was already pulled.
# Set the limit to the max number of recordings that can be pulled.
def recordings_count
res_count = Rails.cache.fetch("rooms/#{@chosen_room.handler}/#{RECORDINGS_KEY}/remaining", expires_in: Rails.configuration.cache_expires_in_minutes.minutes) if Rails.configuration.cache_enabled
res_count ||= bbb.get_recordings(meetingID: @chosen_room.handler, offset: RECORDINGS_PER_PAGE, limit: 100)[:recordings].length
@num_of_recs = res_count + RECORDINGS_PER_PAGE # We add RECORDINGS_PER_PAGE because we set the offset to 1
@num_of_recs
end

RECORDINGS_PER_PAGE
# returns the amount of pages for recordings
def pages_count
(@num_of_recs.to_f / RECORDINGS_PER_PAGE).ceil
end

# Fetch an individual recording
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/rooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ def recording_delete
def individual_recording
rec = recording(params[:record_id])
formats_arr = rec[:playback][:format]
formats_arr = [formats_arr] unless formats_arr.is_a?(Array) # for when BBB returns just one format

format_obj = formats_arr.find { |i| i[:type] == params[:format] }

playback_url = format_obj[:url]
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_room.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@
</div>


<% numOfPages = pages_count %>
<% if paginate? %>
<% numOfPages = pages_count %>
<div class="flex justify-center items-center mb-5" id="pagination-container" data-current-page="<%= @page %>" data-total-pages="<%= numOfPages %>">
<nav aria-label="Page navigation example">
<ul class="flex items-center -space-x-px h-8 text-sm">
Expand Down

0 comments on commit 3938903

Please sign in to comment.