学生端视频统计

courseware
anke1460 5 years ago
parent 609876f16e
commit 094880e810

@ -25,7 +25,7 @@ class CourseVideosController < ApplicationController
course_video = CourseVideo.find(@video.id) course_video = CourseVideo.find(@video.id)
@watch_course_videos = course_video.watch_course_videos.joins(" @watch_course_videos = course_video.watch_course_videos.joins("
JOIN watch_video_histories ON watch_video_histories.watch_course_video_id = watch_course_videos.id JOIN watch_video_histories ON watch_video_histories.watch_course_video_id = watch_course_videos.id AND watch_video_histories.end_at IS NOT NULL
JOIN course_members ON course_members.user_id = watch_course_videos.user_id AND course_members.course_id = #{@course.id} AND role = 4 JOIN course_members ON course_members.user_id = watch_course_videos.user_id AND course_members.course_id = #{@course.id} AND role = 4
").group("watch_video_histories.watch_course_video_id").where("watch_course_videos.end_at IS NOT NULL").select("watch_course_videos.id") ").group("watch_video_histories.watch_course_video_id").where("watch_course_videos.end_at IS NOT NULL").select("watch_course_videos.id")
@ -35,7 +35,7 @@ class CourseVideosController < ApplicationController
@watch_course_videos = @watch_course_videos.where("course_members.course_group_id = ?", params[:group_id]) @watch_course_videos = @watch_course_videos.where("course_members.course_group_id = ?", params[:group_id])
end end
@watch_course_videos = @watch_course_videos.select("count(watch_video_histories.id) AS freq, watch_course_videos.*") @watch_course_videos = @watch_course_videos.select("count(watch_course_videos.course_video_id) AS freq, watch_course_videos.*")
if params[:order].present? if params[:order].present?
key = params[:order].split("-") key = params[:order].split("-")

@ -1491,10 +1491,11 @@ class CoursesController < ApplicationController
@videos = CourseVideo.joins(" @videos = CourseVideo.joins("
JOIN videos ON course_videos.course_id = #{@course.id} AND videos.id = course_videos.video_id JOIN videos ON course_videos.course_id = #{@course.id} AND videos.id = course_videos.video_id
LEFT JOIN ( 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 SELECT watch_course_videos.course_video_id, SUM(watch_course_videos.total_duration) AS time, COUNT(watch_video_histories.watch_course_video_id) AS num
FROM watch_course_videos FROM watch_course_videos
JOIN course_videos ON course_videos.id = watch_course_videos.course_video_id AND watch_course_videos.end_at IS NOT NULL JOIN course_videos ON course_videos.id = watch_course_videos.course_video_id AND watch_course_videos.end_at IS NOT NULL
JOIN course_members ON course_members.user_id = watch_course_videos.user_id AND course_members.course_id = #{@course.id} AND role = 4 JOIN course_members ON course_members.user_id = watch_course_videos.user_id AND course_members.course_id = #{@course.id} AND role = 4
JOIN watch_video_histories ON watch_course_videos.id = watch_video_histories.watch_course_video_id AND watch_video_histories.end_at IS NOT NULL
WHERE course_videos.course_id = #{@course.id} WHERE course_videos.course_id = #{@course.id}
GROUP BY watch_course_videos.course_video_id GROUP BY watch_course_videos.course_video_id
) AS hisotries ON hisotries.course_video_id = course_videos.id").select("course_videos.id") ) AS hisotries ON hisotries.course_video_id = course_videos.id").select("course_videos.id")
@ -1521,16 +1522,19 @@ class CoursesController < ApplicationController
JOIN videos ON course_videos.course_id = #{@course.id} AND videos.id = course_videos.video_id JOIN videos ON course_videos.course_id = #{@course.id} AND videos.id = course_videos.video_id
JOIN watch_course_videos ON course_videos.id = watch_course_videos.course_video_id AND watch_course_videos.user_id = #{current_user.id} JOIN watch_course_videos ON course_videos.id = watch_course_videos.course_video_id AND watch_course_videos.user_id = #{current_user.id}
JOIN ( JOIN (
SELECT watch_course_videos.course_video_id, COUNT(watch_course_videos.course_video_id) AS num SELECT watch_course_videos.course_video_id, COUNT(watch_video_histories.watch_course_video_id) AS num
FROM watch_course_videos FROM watch_course_videos
JOIN course_videos ON course_videos.id = watch_course_videos.course_video_id JOIN course_videos ON course_videos.id = watch_course_videos.course_video_id
JOIN course_members ON course_members.user_id = watch_course_videos.user_id AND course_members.course_id = #{@course.id} AND role = 4 JOIN course_members ON course_members.user_id = watch_course_videos.user_id AND course_members.course_id = #{@course.id} AND role = 4
JOIN watch_video_histories ON watch_course_videos.id = watch_video_histories.watch_course_video_id AND watch_video_histories.end_at IS NOT NULL
WHERE course_videos.course_id = #{@course.id} AND watch_course_videos.user_id = #{current_user.id} AND watch_course_videos.end_at IS NOT NULL WHERE course_videos.course_id = #{@course.id} AND watch_course_videos.user_id = #{current_user.id} AND watch_course_videos.end_at IS NOT NULL
GROUP BY watch_course_videos.course_video_id GROUP BY watch_course_videos.course_video_id
) AS hisotries ON hisotries.course_video_id = course_videos.id").select("course_videos.id") ) AS hisotries ON hisotries.course_video_id = course_videos.id").select("course_videos.id")
@count = @videos.count @count = @videos.count
@videos = @videos.includes(video: :user)
if params[:order].present? if params[:order].present?
key = params[:order].split("-") key = params[:order].split("-")
if ["freq", 'total_duration'].include?(key.first) && ["desc", "asc"].include?(key.last) if ["freq", 'total_duration'].include?(key.first) && ["desc", "asc"].include?(key.last)
@ -1538,24 +1542,32 @@ class CoursesController < ApplicationController
end end
end end
@videos = @videos.select("course_videos.id, watch_course_videos.start_at, watch_course_videos.total_duration, watch_course_videos.end_at, watch_course_videos.is_finished, videos.title, IFNULL(hisotries.num,0) AS freq") @videos = @videos.select("course_videos.id, course_videos.video_id, watch_course_videos.start_at, watch_course_videos.total_duration, watch_course_videos.end_at, watch_course_videos.is_finished, videos.title, IFNULL(hisotries.num,0) AS freq")
@videos = paginate @videos @videos = paginate @videos
end end
# 课堂视频的统计总览 # 课堂视频的统计总览
def watch_statics def watch_statics
course_videos = @course.course_videos.joins(:watch_course_videos).joins(" course_videos = @course.course_videos.joins(:watch_course_videos).joins("JOIN course_members ON course_members.user_id = watch_course_videos.user_id AND course_members.course_id = #{@course.id} AND role = 4").where("watch_course_videos.end_at IS NOT NULL")
JOIN course_members ON course_members.user_id = watch_course_videos.user_id AND course_members.course_id = #{@course.id} AND role = 4
").where("watch_course_videos.end_at IS NOT NULL") if params[:all].present?
@total_duration = course_videos.sum(:total_duration).round(0) return normal_status(403, "你没有权限操作") unless current_user.teacher_of_course?(@course)
@frequencies = course_videos.joins("JOIN watch_video_histories ON watch_course_videos.id = watch_video_histories.watch_course_video_id").count(:id) @total_duration = course_videos.sum(:total_duration).round(0)
@people_num = course_videos.count(:id) @frequencies = course_videos.joins("JOIN watch_video_histories ON watch_course_videos.id = watch_video_histories.watch_course_video_id AND watch_video_histories.end_at IS NOT NULL").count(:id)
@num = course_videos.count(:id)
else
course_videos = course_videos.where("course_members.user_id = #{current_user.id}")
@total_duration = course_videos.sum(:total_duration).round(0)
@frequencies = course_videos.joins("JOIN watch_video_histories ON watch_course_videos.id = watch_video_histories.watch_course_video_id AND watch_video_histories.end_at IS NOT NULL").count(:id)
@num = course_videos.count(:id)
end
render json: { render json: {
total_duration: @total_duration, total_duration: @total_duration,
freq: @frequencies, freq: @frequencies,
people_num: @people_num, num: @num,
begin_at: '2020-03-13 24:00' begin_at: '2020-03-13 24:00'
} }
end end

@ -1,7 +1,7 @@
json.data do json.data do
json.array! @videos.each do |d| json.array! @videos.each do |d|
json.title d.title json.title d.title
json.user_name @current_user&.real_name json.user_name d.video.user&.real_name
json.is_finished d.is_finished ? true : false json.is_finished d.is_finished ? true : false
json.total_duration d.total_duration.round(0) json.total_duration d.total_duration.round(0)
json.freq d['freq'] json.freq d['freq']

Loading…
Cancel
Save