educoder/app/services/subjects/course_used_info_service.rb

41 lines
1.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

class Subjects::CourseUsedInfoService < ApplicationService
attr_reader :subject
def initialize(subject)
@subject = subject
@shixun_ids = subject.shixuns.pluck(:id)
end
def call
return if subject.blank?
homework_commons =
HomeworkCommon.joins(:homework_commons_shixun)
.where(homework_type: %i[practice])
.where(homework_commons_shixuns: {shixun_id: @shixun_ids})
course_ids = homework_commons.pluck(:course_id)
homework_common_ids = homework_commons.pluck("homework_commons.id")
schools = School.joins(courses: [:course_members, :homework_commons])
# name将该课程使用到课堂的单位
# course_count: 将该课程使用到课堂的数量
# student_count: 课堂的学生总数(去掉重复)
# choice_shixun_num: 选用该课程实训的个数(去重)
# choice_shixun_frequency: 选用该课程实训的次数
course_info =
schools.map do |school|
name = school.name
course_count = school.where(courses: {id: course_ids}).count
student_count = school.where(course_members: {role: 4}).select("course_members.user_id").distinct.size
homework_commons = school.where(homework_commons: {id: homework_common_ids})
.select("homework_commons.id")
choice_shixun_num = homework_commons.distinct.size
choice_shixun_frequency = homework_commons.size
{name: name, course_count: course_count, student_count: student_count, choice_shixun_num: choice_shixun_num,
choice_shixun_frequency: choice_shixun_frequency}
end
# 默认按照选用实训的次数作为排序
course_info.sort{|x,y| y['choice_shixun_frequency'] <=> x['choice_shixun_frequency']}
end
end