You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/tasks/exercise_publish_task.rb

145 lines
7.4 KiB

6 years ago
class ExercisePublishTask
include ExercisesHelper
include GitHelper
def publish
Rails.logger.info("log--------------------------------exercise_publish start")
puts "--------------------------------exercise_publish start"
exercises = Exercise.where("publish_time is not null and exercise_status = 1 and publish_time <=?",Time.now)
exercises.each do |exercise|
exercise.update_column('exercise_status', 2)
course = exercise.course
tid_str = ""
course.teachers.find_each do |member|
tid_str += "," if tid_str != ""
tid_str += "(#{member.user_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
if exercise.unified_setting
course.student.find_each do |student|
tid_str += "," if tid_str != ""
tid_str += "(#{student.student_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
end
if tid_str != ""
tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str
ActiveRecord::Base.connection.execute tid_sql
end
if exercise.exercise_users.count == 0
str = ""
course.students.find_each do |student|
str += "," if str != ""
str += "(#{student.user_id}, #{exercise.id}, 0, '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
if str != ""
sql = "insert into exercise_users (user_id, exercise_id, commit_status, created_at, updated_at) values" + str
ActiveRecord::Base.connection.execute sql
end
end
if exercise.course_acts.size == 0
exercise.course_acts << CourseActivity.new(:user_id => exercise.user_id,:course_id => exercise.course_id)
end
end
# 分组设置发布时间的测验
exercise_group_settings = ExerciseGroupSetting.where("publish_time < ? and publish_time > ?", Time.now + 1800, Time.now - 1800)
exercise_group_settings.each do |exercise_group|
exercise = exercise_group.exercise
if exercise.present?
course = exercise.course
exercise.update_attributes(:exercise_status => 2) if exercise.exercise_status == 1
tid_str = ""
members = course.students.where(:course_group_id => exercise_group.course_group_id)
members.find_each do |member|
tid_str += "," if tid_str != ""
tid_str += "(#{member.user_id},#{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
if tid_str != ""
tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str
ActiveRecord::Base.connection.execute tid_sql
end
end
end
Rails.logger.info("log--------------------------------exercise_publish end")
puts "--------------------------------exercise_publish end"
end
def end
exercises = Exercise.where("end_time <= ? and exercise_status = ?",Time.now,2)
6 years ago
exercises.each do |exercise|
course = exercise.course
ex_type = exercise.exercise_questions.pluck(:question_type).uniq
exercise.update_attribute('exercise_status', 3)
6 years ago
exercise.exercise_users.each do |exercise_user|
if exercise_user.commit_status == 0 && !exercise_user.start_at.nil?
# exercise_user.update_attributes(:commit_status => 1, :end_at => Time.now, :status => true)
6 years ago
s_score = calculate_student_score(exercise, exercise_user.user)[:total_score]
if ex_type.include?(4) #是否包含主观题
subjective_score = exercise_user.subjective_score
else
subjective_score = -1.0
6 years ago
end
# subjective_score = exercise_user.subjective_score
total_score_subjective_score = subjective_score < 0.0 ? 0.0 : subjective_score
total_score = s_score + total_score_subjective_score
commit_option = {
:status => 1,
:commit_status => 1,
:end_at => Time.now,
:objective_score => s_score,
:score => total_score,
:subjective_score => subjective_score
}
exercise_user.update_attributes(commit_option)
# if exercise_user.user.exercise_answers.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty?
# exercise_user.update_attributes(:subjective_score => 0)
# end
6 years ago
end
end
end
all_exercises = Exercise.where("end_time > ? and exercise_status = ?",Time.now,2)
6 years ago
exercise_ids = all_exercises.blank? ? "(-1)" : "(" + all_exercises.map(&:id).join(",") + ")"
ex_group_settings = ExerciseGroupSetting.where("end_time <= '#{Time.now}' and exercise_id in #{exercise_ids}")
ex_group_settings.each do |exercise_setting|
6 years ago
exercise = exercise_setting.exercise
ex_types = exercise.exercise_questions.pluck(:question_type).uniq
6 years ago
users = exercise.course.students.where(:course_group_id => exercise_setting.course_group_id)
exercise_users = exercise.exercise_users.where(:user_id => users.map(&:user_id))
exercise_users.each do |exercise_user|
if exercise_user.commit_status == 0 && !exercise_user.start_at.nil?
if ex_types.include?(4) #是否包含主观题
subjective_score = exercise_user.subjective_score
else
subjective_score = -1.0
6 years ago
end
s_score = calculate_student_score(exercise, exercise_user.user)[:total_score]
# subjective_score = exercise_user.subjective_score
total_score_subjective_score = subjective_score < 0.0 ? 0.0 : subjective_score
total_score = s_score + total_score_subjective_score
commit_option = {
:status => 1,
:commit_status => 1,
:end_at => Time.now,
:objective_score => s_score,
:score => total_score,
:subjective_score => subjective_score
}
exercise_user.update_attributes(commit_option)
# exercise_user.update_attributes(:commit_status => 1, :end_at => Time.now, :status => true)
# s_score = calculate_student_score(exercise, exercise_user.user)[:total_score]
# exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (exercise_user.subjective_score && exercise_user.subjective_score > 0 ? exercise_user.subjective_score : 0)))
# if exercise_user.user.exercise_answers.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty?
# exercise_user.update_attributes(:subjective_score => 0.0)
# end
6 years ago
end
end
end
Rails.logger.info("log--------------------------------exercise_end completed")
end
end