diff --git a/app/jobs/scrape_panopto_job.rb b/app/jobs/scrape_panopto_job.rb index 8887c16..cb9fc4b 100644 --- a/app/jobs/scrape_panopto_job.rb +++ b/app/jobs/scrape_panopto_job.rb @@ -13,18 +13,17 @@ class ScrapePanoptoJob < ApplicationJob def scrape_course(course) panopto_folder_ids = course.panopto_folders panopto_folder_ids.each do |folder_id| - lectures_data = Panopto::list_folder folder_id - attributes = lectures_data.map do |lecture_data| + recording_data = Panopto::list_folder folder_id + attributes = recording_data.map do |lecture_data| { title: lecture_data['title'], start_time: Time.new(lecture_data['start_time']), - recording_id: lecture_data['panopto_delivery_id'], + recording_uuid: lecture_data['panopto_delivery_id'], course_id: course.id, - status: :undetermined } end - Lecture.insert_all(attributes, unique_by: :recording_id) + Recording.insert_all(attributes, unique_by: :recording_uuid) end end end diff --git a/app/models/lecture.rb b/app/models/lecture.rb index 7e36de5..ee5d000 100644 --- a/app/models/lecture.rb +++ b/app/models/lecture.rb @@ -1,6 +1,7 @@ class Lecture < ApplicationRecord belongs_to :course has_one :attendance + belongs_to :recording enum :status, [ :undetermined, @@ -12,26 +13,10 @@ class Lecture < ApplicationRecord ], default: :undetermined def week_number - ((start_time.beginning_of_week - Time.new('2023-10-02')) / 1.week).floor + ((start_time.beginning_of_week - START_OF_YEAR_5_SEMESTER_1) / 1.week).floor end def live_video_url nil end - - def recording_url - return nil if recording_id.nil? - - "https://uniofbath.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=#{recording_id}" - end - - def nice_title - if course.title == "General Relativity" - regex = /.+L(\d+).*/ - lecture_number = self.title.match(regex)[1].to_i - return "Lecture #{lecture_number}" - end - - title - end end diff --git a/app/models/recording.rb b/app/models/recording.rb index 4ffcd7c..dac26ae 100644 --- a/app/models/recording.rb +++ b/app/models/recording.rb @@ -1,3 +1,20 @@ +# This is a Foreign Object Reference Table, keyed to the external data source by #recording_uuid class Recording < ApplicationRecord belongs_to :course + + def recording_url + return nil if recording_uuid.nil? + + "https://uniofbath.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=#{recording_uuid}" + end + + def nice_title + if course.title == "General Relativity" + regex = /.+L(\d+).*/ + lecture_number = self.title.match(regex)[1].to_i + return "Lecture #{lecture_number}" + end + + title + end end diff --git a/app/views/attendance_tracker/index.html.erb b/app/views/attendance_tracker/index.html.erb index 01a3ff3..14dec1b 100644 --- a/app/views/attendance_tracker/index.html.erb +++ b/app/views/attendance_tracker/index.html.erb @@ -28,7 +28,7 @@ <% style = if lecture.start_time.future? then 'background: repeating-linear-gradient(45deg, #f3f4f6, #f3f4f6 10px, white 10px, white 20px);' else '' end %> - <%= lecture.nice_title %> + <%= lecture.recording.nice_title %> <%= lecture.start_time.to_fs(:dmy) %> @@ -59,7 +59,7 @@ Start <% else %> - <%= link_to "Open recording", lecture.recording_url %> + <%= link_to "Open recording", lecture.recording.recording_url %> <% end %> <% end %> diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb new file mode 100644 index 0000000..44c492e --- /dev/null +++ b/config/initializers/constants.rb @@ -0,0 +1 @@ +START_OF_YEAR_5_SEMESTER_1 = Time.new('2023-10-02') diff --git a/db/migrate/20231001161500_create_recordings.rb b/db/migrate/20231001161500_create_recordings.rb index ea0f42e..0ef718f 100644 --- a/db/migrate/20231001161500_create_recordings.rb +++ b/db/migrate/20231001161500_create_recordings.rb @@ -8,5 +8,7 @@ class CreateRecordings < ActiveRecord::Migration[7.0] t.timestamps end + + add_index :recordings, :recording_uuid, unique: true end end diff --git a/db/migrate/20231001163055_add_recording_id_to_lecture.rb b/db/migrate/20231001163055_add_recording_id_to_lecture.rb new file mode 100644 index 0000000..518be88 --- /dev/null +++ b/db/migrate/20231001163055_add_recording_id_to_lecture.rb @@ -0,0 +1,6 @@ +class AddRecordingIdToLecture < ActiveRecord::Migration[7.0] + def change + remove_column :lectures, :recording_id + add_belongs_to :lectures, :recording + end +end diff --git a/db/schema.rb b/db/schema.rb index b575408..2d6c9c6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_10_01_161500) do +ActiveRecord::Schema[7.0].define(version: 2023_10_01_163055) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -36,12 +36,12 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_01_161500) do t.string "title", null: false t.datetime "start_time", null: false t.integer "status", default: 0, null: false - t.string "recording_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.bigint "course_id" + t.bigint "recording_id" t.index ["course_id"], name: "index_lectures_on_course_id" - t.index ["recording_id"], name: "index_lectures_on_recording_id", unique: true + t.index ["recording_id"], name: "index_lectures_on_recording_id" end create_table "recordings", force: :cascade do |t| @@ -52,6 +52,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_01_161500) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["course_id"], name: "index_recordings_on_course_id" + t.index ["recording_uuid"], name: "index_recordings_on_recording_uuid", unique: true end add_foreign_key "attendances", "lectures"