diff --git a/app/models/myshixun.rb b/app/models/myshixun.rb index 1a89c755e..54d068be6 100644 --- a/app/models/myshixun.rb +++ b/app/models/myshixun.rb @@ -5,6 +5,7 @@ class Myshixun < ApplicationRecord has_one :shixun_modify, :dependent => :destroy belongs_to :user + belongs_to :user_extension belongs_to :shixun, counter_cache: true has_one :last_executable_task, -> { where(status: [0, 1]).reorder(created_at: :asc) }, class_name: 'Game' diff --git a/app/services/subjects/data_statistic_service.rb b/app/services/subjects/data_statistic_service.rb index 29696cf15..4ab7488c6 100644 --- a/app/services/subjects/data_statistic_service.rb +++ b/app/services/subjects/data_statistic_service.rb @@ -3,15 +3,20 @@ class Subjects::DataStatisticService < ApplicationService def initialize(subject, user) @subject = subject @user = user - @shixun_ids = subject.shixuns.pluck(:id) + @shixuns = subject.shixuns + @shixun_ids = @shixuns.pluck(:id) end - # 学习总人数 + # 学习总人数: + # 文案解释:学习该课程的全部人数(学习总人数=课堂学习人数+自主学习人数) + # 开发备注:只要点击该课程的任何一个实训(生成了tpi),都算一个学习人数;(去重,一个人数计算1次) def study_count - @shixuns.includes(:myshixuns).map{|shixun| shixun.myshixuns.pluck(:user_id)}.uniq + @shixuns.joins(:myshixuns).select("myshixuns.user_id").distinct.size end - # 课堂学习人数 + # 课堂学习人数: + # 文案解释:通过课堂学习该课程的人数 + # 开发备注:只要通过课堂进入,并点击了实训(生成了tpi),则算一个可以学习人数;(去重,一个人数计算1次) def course_study_count # 实训作业 sw_user_ids = StudentWork.where.not(myshixun_id: nil).joins(homework_common: :homework_commons_shixuns) @@ -19,6 +24,50 @@ class Subjects::DataStatisticService < ApplicationService # 试卷的实训题 esa_user_ids = ExerciseShixunAnswer.joins(exercise_shixun_challenge: :shixun) .where(shixuns: {id: @shixun_ids}).pluck("exercise_shixun_answers.user_id") + (sw_user_ids + esa_user_ids).uniq.size end + # 自主学习人数: + # 文案解释:通过自主学习该课程的人数 + # 开发备注:非课程进入,生成了实训的tpi(去重。一个人数计算1次 + def initiative_study + study_count - course_study_count + end + + # 通关总人数: + # 文案解释: + # 通关该课程所有实训的人数(去重。一个人数计算1次) + def passed_count + @shixuns.includes(:myshixuns).where(myshixuns: {status: 1}).select("myshixuns.user_id").distinct.size + end + + # 使用课堂数: + # 文案解释:使用该课程的课堂数量 + # 开发备注:课堂加入该课程的实训到自己课堂,则算一个使用课堂数。(去重,一个课堂计算1次) + def course_used_count + hc_course_ids = + HomeworkCommon.joins(:homework_commons_shixun) + .where(homework_type: %i[practice]) + .where(homework_commons_shixuns: {shixun_id: @shixun_ids}).pluck("homework_commons.course_id").uniq + ex_course_ids = + Exercise.joins(:exercise_questions) + .where(exercise_questions: {question_type: 5, shixun_id: @shixun_ids}).pluck("exercises.course_id").uniq + + (hc_course_ids + ex_course_ids).uniq.size + end + + # 使用单位数: + # 文案解释:使用该课程的单位数量(包括自主学习者所在单位) + # 开发备注:凡事该单位有使用该课程的实训(自主学习+课堂使用)都算;(去重,一个单位计算一次) + def school_used_count + school_ids.size + end + + + private + def school_ids + @shixuns.joins(myshixuns: :user_extension).select("user_extensions.school_id").distinct + end + + end