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.
pgfqe6ch8/lib/tasks/exercise_publish.rake

212 lines
9.9 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.

#coding=utf-8
namespace :exercise_publish do
desc "publish exercise and end exercise"
def get_mulscore(question, user)
ecs = ExerciseAnswer.where("user_id =? and exercise_question_id =?", user.id, question.id)
arr = []
ecs.each do |ec|
arr << ec.exercise_choice.choice_position
end
#arr = arr.sort
str = arr.sort.join("")
return str
end
def tran_base64_decode64 str
if str.blank?
str
else
s_size = str.size % 4
if s_size != 0
str += "=" * (4 - s_size)
end
Base64.decode64(str.tr("-_", "+/")).force_encoding("utf-8")
end
end
def calculate_student_score(exercise, user)
score = 0
score1 = 0
score2 = 0
score3 = 0
score4 = 0
exercise_qustions = exercise.exercise_questions
exercise_qustions.each do |question|
if question.question_type != 5
answer = question.exercise_answers.where("#{ExerciseAnswer.table_name}.user_id = #{user.id}")
if question.question_type == 3
standard_answer =[]
question.exercise_standard_answers.each do |answer|
standard_answer << answer.answer_text.strip.downcase
end
else
standard_answer = question.exercise_standard_answers.first
end
unless answer.empty?
# 问答题有多个答案
if question.question_type == 3 && !standard_answer.empty?
if standard_answer.include?(answer.first.answer_text.strip.downcase)
score1 = score1+ question.question_score unless question.question_score.nil?
end
elsif question.question_type == 1 && !standard_answer.nil?
if answer.first.exercise_choice.choice_position == standard_answer.exercise_choice_id
score2 = score2 + question.question_score unless question.question_score.nil?
end
elsif question.question_type == 2 && !standard_answer.nil?
arr = get_mulscore(question, user)
if arr.to_i == standard_answer.exercise_choice_id
score3 = score3 + question.question_score unless question.question_score.nil?
end
end
end
else
question.exercise_shixun_challenges.each do |exercise_cha|
game = Game.where(:user_id => user.id, :challenge_id => exercise_cha.challenge_id).first
if game.present?
exercise_cha_score = 0
answer_status = 0
cha_path = exercise_cha.challenge.path.present? ? exercise_cha.challenge.path.split("") : []
challeng_path = cha_path.reject(&:blank?)[0].try(:strip)
if game.status == 2 && game.final_score >= 0
exercise_cha_score = exercise_cha.question_score
answer_status = 1
end
if exercise_cha.exercise_shixun_answers.where(:user_id => user.id).empty?
if GameCode.where(:game_id => game.try(:id), :path => challeng_path).first.present?
game_code = GameCode.where(:game_id => game.try(:id), :path => challeng_path).first
code = game_code.try(:new_code)
else
begin
g = Gitlab.client
Rails.logger.info "commit_exercise_path---- #{challeng_path}"
if game.present?
code = g.files(game.myshixun.gpid, challeng_path, "master").try(:content)
else
code = g.files(question.shixun.gpid, challeng_path, "master").try(:content)
end
code = tran_base64_decode64(code)
rescue Exception => e
@error_messages = e.message
Rails.logger.info "commit_exercise---- #{@error_messages}"
end
end
ExerciseShixunAnswer.create(:exercise_question_id => question.id, :exercise_shixun_challenge_id => exercise_cha.id, :user_id => user.id,
:score => exercise_cha_score, :answer_text => code, :status => answer_status)
end
score4 += exercise_cha_score
end
end
end
end
score = score1 + score2 + score3 + score4
end
task :publish => :environment do
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.student.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 + 3600, Time.now - 3600)
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.members.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
task :end => :environment do
exercises = Exercise.where("end_time <=? and exercise_status = 2",Time.now)
exercises.each do |exercise|
course = exercise.course
exercise.update_column('exercise_status', 3)
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)
s_score = calculate_student_score(exercise, exercise_user.user)
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_answer.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty?
exercise_user.update_attributes(:subjective_score => 0)
end
end
end
end
all_exercises = Exercise.where("end_time > ? and exercise_status = 2",Time.now)
exercise_ids = all_exercises.blank? ? "(-1)" : "(" + all_exercises.map(&:id).join(",") + ")"
ExerciseGroupSetting.where("end_time <= '#{Time.now}' and exercise_id in #{exercise_ids}").each do |exercise_setting|
exercise = exercise_setting.exercise
users = exercise.course.members.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?
exercise_user.update_attributes(:commit_status => 1, :end_at => Time.now, :status => true)
s_score = calculate_student_score(exercise, exercise_user.user)
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_answer.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty?
exercise_user.update_attributes(:subjective_score => 0)
end
end
end
end
end
end