课堂视频时长统计

PCqiandao
anke1460 5 years ago
parent 50abaefa7f
commit 54d3338afe

@ -30,7 +30,7 @@ class CoursesController < ApplicationController
:informs, :update_informs, :online_learning, :update_task_position, :tasks_list,
:join_excellent_course, :export_couser_info, :export_member_act_score, :new_informs,
:delete_informs, :change_member_role, :course_groups, :join_course_group, :statistics,
:work_score, :act_score, :calculate_all_shixun_scores, :move_to_category]
:work_score, :act_score, :calculate_all_shixun_scores, :move_to_category, :watch_video_histories]
before_action :user_course_identity, except: [:join_excellent_course, :index, :create, :new, :apply_to_join_course,
:search_course_list, :get_historical_course_students, :mine, :search_slim, :board_list]
before_action :teacher_allowed, only: [:update, :destroy, :settings, :search_teacher_candidate,
@ -114,7 +114,7 @@ class CoursesController < ApplicationController
#sql = "left join videos on videos.id=course_videos.video_id AND (videos.transcoded=1 OR videos.user_id = #{current_user.id})"
#@videos = paginate videos.joins(sql).includes(video: [user: :user_extension], user: :user_extension)
videos = videos.includes(video: [user: :user_extension], user: :user_extension)
videos = videos.includes(:watch_course_videos, video: [user: :user_extension],user: :user_extension)
videos = videos.where(videos: {transcoded: true})
.or(videos.where(videos: {user_id: current_user.id}))
.or(videos.where(course_videos: {is_link: true}))
@ -1481,6 +1481,23 @@ class CoursesController < ApplicationController
end
end
def watch_video_histories
@videos = CourseVideo.find_by_sql("
SELECT course_videos.id, videos.user_id, videos.title, IFNULL(hisotries.time,0) AS time, IFNULL(hisotries.num,0) AS num
FROM course_videos
JOIN videos ON course_videos.course_id = #{@course.id} AND videos.id = course_videos.video_id
LEFT JOIN (
SELECT watch_course_videos.course_video_id, sum(watch_course_videos.total_duration) AS time, count(watch_course_videos.course_video_id) AS num
FROM watch_course_videos
JOIN course_videos ON course_videos.id = watch_course_videos.course_video_id
WHERE course_videos.course_id = #{@course.id}
GROUP BY watch_course_videos.course_video_id
) AS hisotries ON hisotries.course_video_id = course_videos.id
")
@count = @videos.count
@videos = paginate @videos
end
private
# Use callbacks to share common setup or constraints between actions.

@ -28,13 +28,16 @@ class CreateWatchVideoService < ApplicationService
watch_course_video = watch_video_history.watch_course_video
watch_course_video.total_duration = watch_course_video.watch_video_histories.sum(:total_duration)
watch_course_video.end_at = current_time
watch_course_video.save!
if watch_course_video.present? && !watch_course_video.is_finished && watch_course_video.watch_duration < params[:watch_duration].to_f
# 更新课程视频的时长及是否看完状态
watch_course_video.watch_duration = params[:watch_duration]
if params[:ed].present?
watch_course_video.is_finished = watch_course_video.watch_video_histories.sum(:total_duration) >= watch_course_video.duration
end
watch_course_video.end_at = current_time
watch_course_video.save!
end
end

@ -10,6 +10,8 @@ json.videos @videos do |video|
json.user_login user&.login
else
json.partial! 'users/videos/video', locals: { video: video.video }
json.total_time video.watch_course_videos.sum(:total_duration)
json.people_num video.watch_course_videos.count(:user_id)
end
end

@ -0,0 +1,10 @@
json.videos do
json.array! @videos do |v|
json.id v.id
json.title v.title
json.user_name v.user&.real_name
json.people_num v.num
json.total_time v.time
end
end
json.count @count

@ -531,6 +531,7 @@ Rails.application.routes.draw do
post :inform_up
post :inform_down
get :calculate_all_shixun_scores
get :watch_video_histories
end
collection do

@ -0,0 +1,8 @@
class AddTotalDurationToWatchCourseDuration < ActiveRecord::Migration[5.2]
def change
add_column :watch_course_videos, :total_duration, :float, default: 0
WatchVideoHistory.where("created_at < '2020-03-14 00:00:00'").each do |d|
d.watch_course_video.increment!(:total_duration, d.total_duration) if d.watch_course_video.present?
end
end
end
Loading…
Cancel
Save