diff --git a/app/controllers/course_videos_controller.rb b/app/controllers/course_videos_controller.rb index 090c7e29a..fc60ab26f 100644 --- a/app/controllers/course_videos_controller.rb +++ b/app/controllers/course_videos_controller.rb @@ -1,9 +1,9 @@ class CourseVideosController < ApplicationController before_action :require_login - before_action :validate_params + before_action :validate_params, except: [:watch_histories] before_action :find_course, only: [:create] - before_action :find_video, only: [:update] - before_action :teacher_allowed + before_action :find_video, only: [:update, :watch_histories] + before_action :teacher_allowed, except: [:watch_histories] def create title = params[:name].strip @@ -20,6 +20,15 @@ class CourseVideosController < ApplicationController render_ok end + def watch_histories + return normal_status(403, "你没有权限操作") if !current_user.teacher_of_course?(@course) + course_video = CourseVideo.find(@video.id) + + @watch_course_videos = course_video.watch_course_videos.includes(:user, :watch_video_histories).where("end_at IS NOT NULL") + @count = @watch_course_videos.count + @watch_course_videos = paginate @watch_course_videos + end + private def validate_params diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 13be4168b..e5c9ae0f5 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -30,7 +30,8 @@ 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, :watch_video_histories] + :work_score, :act_score, :calculate_all_shixun_scores, :move_to_category, :watch_video_histories, :watch_statics, + :own_watch_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, @@ -1482,23 +1483,58 @@ class CoursesController < ApplicationController end end + # 课堂视频观看记录总览 def watch_video_histories + return normal_status(403, "你没有权限操作") unless current_user.teacher_of_course?(@course) + @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 + 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 AND watch_course_videos.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 + ") + @count = @videos.count + @videos = paginate @videos + end + + # 学生角度观看课堂视频的记录 + def own_watch_histories + @current_user = current_user + @videos = CourseVideo.find_by_sql(" + 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.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 + 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, 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 + 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 ") @count = @videos.count @videos = paginate @videos end + # 课堂视频的统计总览 + def watch_statics + @total_duration = @course.course_videos.joins(:watch_course_videos).sum(:total_duration).round(2) + @frequencies = @course.course_videos.joins([watch_course_videos: :watch_video_histories]).count(:id) + @people_num = @course.course_videos.joins(:watch_course_videos).count(:id) + render json: { + total_duration: @total_duration, + freq: @frequencies, + people_num: @people_num + } + end + private # Use callbacks to share common setup or constraints between actions. diff --git a/app/views/course_videos/watch_histories.json.jbuilder b/app/views/course_videos/watch_histories.json.jbuilder new file mode 100644 index 000000000..4845476dc --- /dev/null +++ b/app/views/course_videos/watch_histories.json.jbuilder @@ -0,0 +1,12 @@ +json.data do + json.array! @watch_course_videos do |d| + json.user_name d.user&.real_name + json.is_finished d.is_finished ? true : false + json.total_duration d.total_duration.round(2) + json.feq d.watch_video_histories.count + json.start_at d.start_at.to_s + json.end_at d.end_at.to_s + end +end + +json.count @count \ No newline at end of file diff --git a/app/views/courses/own_watch_histories.json.jbuilder b/app/views/courses/own_watch_histories.json.jbuilder new file mode 100644 index 000000000..3c3bddc55 --- /dev/null +++ b/app/views/courses/own_watch_histories.json.jbuilder @@ -0,0 +1,13 @@ +json.data do + json.array! @videos.each do |d| + json.title d.title + json.user_name @current_user&.real_name + json.is_finished d.is_finished ? true : false + json.total_duration d.time.round(2) + json.feq d.num + json.start_at d.start_at.to_s + json.end_at d.end_at.to_s + end +end + +json.count @count \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 881324e61..047141ea8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -532,6 +532,8 @@ Rails.application.routes.draw do post :inform_down get :calculate_all_shixun_scores get :watch_video_histories + get :watch_statics + get :own_watch_histories end collection do @@ -542,7 +544,11 @@ Rails.application.routes.draw do get 'search_slim' end - resources :course_videos, only:[:create, :update], shallow: true + resources :course_videos, only:[:create, :update], shallow: true do + member do + get :watch_histories + end + end resources :course_stages, shallow: true do member do