@ -1491,10 +1491,11 @@ class CoursesController < ApplicationController
@videos = CourseVideo . joins ( "
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
SELECT watch_course_videos . course_video_id , SUM ( watch_course_videos . total_duration ) AS time , COUNT ( watch_ video_historie s. watch_ course_video_id) AS num
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_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}
GROUP BY watch_course_videos . course_video_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 watch_course_videos ON course_videos . id = watch_course_videos . course_video_id AND watch_course_videos . user_id = #{current_user.id}
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_historie s. watch_ course_video_id) AS num
FROM watch_course_videos
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 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
GROUP BY watch_course_videos . course_video_id
) AS hisotries ON hisotries . course_video_id = course_videos . id " ).select( " course_videos . id " )
@count = @videos . count
@videos = @videos . includes ( video : :user )
if params [ :order ] . present?
key = params [ :order ] . split ( " - " )
if [ " freq " , 'total_duration' ] . include? ( key . first ) && [ " desc " , " asc " ] . include? ( key . last )
@ -1538,23 +1542,31 @@ class CoursesController < ApplicationController
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
end
# 课堂视频的统计总览
def watch_statics
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 " )
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 " )
if params [ :all ] . present?
return normal_status ( 403 , " 你没有权限操作 " ) unless current_user . teacher_of_course? ( @course )
@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 " ) . count ( :id )
@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 : {
total_duration : @total_duration ,
freq : @frequencies ,
people_num : @people_num ,
num: @ num,
begin_at : '2020-03-13 24:00'
}
end