From 54d3338afef473c4182b183bcaceda31eb7632e2 Mon Sep 17 00:00:00 2001 From: anke1460 Date: Fri, 13 Mar 2020 19:08:14 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E5=A0=82=E8=A7=86=E9=A2=91=E6=97=B6?= =?UTF-8?q?=E9=95=BF=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/courses_controller.rb | 21 +++++++++++++++++-- app/services/create_watch_video_service.rb | 5 ++++- app/views/courses/course_videos.json.jbuilder | 2 ++ .../watch_video_histories.json.jbuilder | 10 +++++++++ config/routes.rb | 1 + ...total_duration_to_watch_course_duration.rb | 8 +++++++ 6 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 app/views/courses/watch_video_histories.json.jbuilder create mode 100644 db/migrate/20200313104522_add_total_duration_to_watch_course_duration.rb diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 124fc1094..2c1a78b86 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -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. diff --git a/app/services/create_watch_video_service.rb b/app/services/create_watch_video_service.rb index 28cf36838..f2d360638 100644 --- a/app/services/create_watch_video_service.rb +++ b/app/services/create_watch_video_service.rb @@ -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 diff --git a/app/views/courses/course_videos.json.jbuilder b/app/views/courses/course_videos.json.jbuilder index bee5e89cb..2e557828b 100644 --- a/app/views/courses/course_videos.json.jbuilder +++ b/app/views/courses/course_videos.json.jbuilder @@ -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 diff --git a/app/views/courses/watch_video_histories.json.jbuilder b/app/views/courses/watch_video_histories.json.jbuilder new file mode 100644 index 000000000..4e7a4b174 --- /dev/null +++ b/app/views/courses/watch_video_histories.json.jbuilder @@ -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 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 6c3efcfe8..881324e61 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20200313104522_add_total_duration_to_watch_course_duration.rb b/db/migrate/20200313104522_add_total_duration_to_watch_course_duration.rb new file mode 100644 index 000000000..400e52ac8 --- /dev/null +++ b/db/migrate/20200313104522_add_total_duration_to_watch_course_duration.rb @@ -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