class CreateCourseHomeworkStatistics < ActiveRecord::Migration def change create_table :course_homework_statistics do |t| t.references :user t.references :course t.integer :committed_work_num, :default => 0 t.integer :un_commit_work_num, :default => 0 t.integer :late_commit_work_num, :default => 0 t.integer :absence_evaluation_work_num, :default => 0 t.integer :un_evaluation_work_num, :default => 0 t.integer :appeal_num, :default => 0 t.float :average_score, :default => 0 t.float :total_score, :default => 0 t.timestamps end count =Course.all.count / 30 + 2 transaction do for i in 1 ... count do i Course.page(i).per(30).each do |course| hw_count = course.homework_commons.includes(:homework_detail_manual).where("homework_detail_manuals.comment_status > 0").count homework_ids = course.homework_commons.empty? ? "(-1)" : "(" + course.homework_commons.map{|hw| hw.id}.join(",") + ")" student_works = StudentWork.where("homework_common_id in #{homework_ids} and work_status !=0") is_eva_homeworks = course.homework_commons.includes(:homework_detail_manual).where("homework_commons.anonymous_comment = 0 and homework_detail_manuals.comment_status = 2") is_eva_student_works = StudentWork.where(:homework_common_id => is_eva_homeworks.map{|hw| hw.id}) has_eva_homeworks = course.homework_commons.includes(:homework_detail_manual).where("homework_commons.anonymous_comment = 0 and homework_detail_manuals.comment_status = 3") has_eva_student_works = StudentWork.where(:homework_common_id => has_eva_homeworks.map{|hw| hw.id}) course.student.each do |student| user = student.student if user committed_work_num = user.student_works.where("homework_common_id in #{homework_ids} and work_status != 0").count un_commit_work_num = (hw_count - committed_work_num) < 0 ? 0 : (hw_count - committed_work_num) late_commit_work_num = user.student_works.where("homework_common_id in #{homework_ids} and work_status = 2").count absence_evaluation_work_num = user.student_works_evaluation_distributions.where(:student_work_id => has_eva_student_works.map(&:id)).count - user.student_works_scores.where(:reviewer_role => 3, :student_work_id => has_eva_student_works.map(&:id)).group_by(&:student_work_id).count absence_evaluation_work_num = absence_evaluation_work_num < 0 ? 0 : absence_evaluation_work_num un_evaluation_work_num = user.student_works_evaluation_distributions.where(:student_work_id => is_eva_student_works.map(&:id)).count - user.student_works_scores.where(:reviewer_role => 3, :student_work_id => is_eva_student_works.map(&:id)).group_by(&:student_work_id).count un_evaluation_work_num = un_evaluation_work_num < 0 ? 0 : un_evaluation_work_num appeal_num = user.student_works_scores.where(:student_work_id => student_works.map(&:id), :appeal_status => 3).count average_score = user.student_works.where(:id => student_works.map(&:id)).select("AVG(student_works.work_score) as score").first ? user.student_works.where(:id => student_works.map(&:id)).select("AVG(student_works.work_score) as score").first.score : 0 total_score = user.student_works.where(:id => student_works.map(&:id)).select("SUM(student_works.work_score) as score").first ? user.student_works.where(:id => student_works.map(&:id)).select("SUM(student_works.work_score) as score").first.score : 0 CourseHomeworkStatistics.create(:course_id => course.id, :user_id => user.id, :committed_work_num => committed_work_num, :un_commit_work_num => un_commit_work_num, :late_commit_work_num => late_commit_work_num, :absence_evaluation_work_num => absence_evaluation_work_num, :un_evaluation_work_num => un_evaluation_work_num, :appeal_num => appeal_num, :average_score => average_score, :total_score => total_score) end end end end end end end