diff --git a/app/controllers/course_videos_controller.rb b/app/controllers/course_videos_controller.rb index fe01c6226..3a0b09b4e 100644 --- a/app/controllers/course_videos_controller.rb +++ b/app/controllers/course_videos_controller.rb @@ -25,7 +25,7 @@ class CourseVideosController < ApplicationController course_video = CourseVideo.find(@video.id) @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 ").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]) 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? key = params[:order].split("-") diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index f6868cfa2..4fc8d1350 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -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_histories.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_histories.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,24 +1542,32 @@ 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") - @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) + 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 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, - begin_at: '2020-03-13 24:00' + freq: @frequencies, + num: @num, + begin_at: '2020-03-13 24:00' } end diff --git a/app/views/courses/own_watch_histories.json.jbuilder b/app/views/courses/own_watch_histories.json.jbuilder index cc3b31eae..c3b8b8ffa 100644 --- a/app/views/courses/own_watch_histories.json.jbuilder +++ b/app/views/courses/own_watch_histories.json.jbuilder @@ -1,7 +1,7 @@ json.data do json.array! @videos.each do |d| 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.total_duration d.total_duration.round(0) json.freq d['freq']