lecture-attendance-manager/app/controllers/attendance_tracker_controller.rb

51 lines
1.4 KiB
Ruby

class AttendanceTrackerController < ApplicationController
before_action :refresh_toggl
def index
@courses = Course.active.sort_by(&:title)
@current_lecture = get_current_lecture
end
def overview
@courses = Course.active.sort_by(&:title)
@current_lecture = get_current_lecture
end
def today
@courses = Course.active.sort_by(&:title)
@date = Date.today
@current_lecture = get_current_lecture
@lectures = @courses.flat_map { |course| course.lectures.filter { |a| a.start_time.today? } }.sort_by { |l| l.start_time }
end
def course_focus
@course = Course.find(params[:id])
@current_lecture = get_current_lecture
end
private
def get_current_lecture
current_time_entry = Toggl.current_time_entry
return nil if current_time_entry.nil?
lecture = Lecture
.joins(:course)
.find_by(title: current_time_entry['description'], course: { toggl_project: current_time_entry['project_id'] })
return lecture if lecture.present?
start_time = Time.new(current_time_entry['start'])
lecture = Lecture.joins(:course)
.find_by(start_time: (start_time - 10.minutes)..(start_time + 10.minutes), course: { toggl_project: current_time_entry['project_id'] })
return lecture if lecture.present?
nil
end
def refresh_toggl
ScrapeTogglJob.perform_later
end
end