socialforge/lib/tasks/exercise_publish.rake

69 lines
2.9 KiB
Ruby

#coding=utf-8
namespace :exercise_publish do
desc "publish exercise and end exercise"
def calculate_student_score(exercise, user)
score = 0
score1 = 0
score2 = 0
score3 = 0
exercise_qustions = exercise.exercise_questions
exercise_qustions.each do |question|
answer = get_user_answer(question, user)
standard_answer = get_user_standard_answer(question, user)
unless answer.empty?
# 问答题有多个答案
if question.question_type == 3 && !standard_answer.empty?
if standard_answer.include?(answer.first.answer_text)
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
end
score = score1 + score2 + score3
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
course.members.each do |m|
exercise.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => false, :status => 2)
end
end
Rails.logger.info("log--------------------------------exercise_publish end")
puts "--------------------------------exercise_publish end"
end
task :end => :environment do
include ExerciseHelper
exercises = Exercise.where("end_time <=? and exercise_status = 2",Time.now)
exercises.each do |exercise|
course = exercise.course
exercise.update_column('exercise_status', 3)
course.student.each do |student|
exercise_user = ExerciseUser.where("user_id =? and exercise_id=?", student.student_id, exercise.id).first
if exercise_user
exercise_user = ExerciseUser.create(:user_id => student.student_id, :exercise_id => exercise.id, :start_at => exercise.end_time, :status => true,:score=>0)
else
exercise_user.update_column('commit_status', 1)
end
s_score = calculate_student_score(exercise, student.student)
exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (exercise_user.subjective_score > 0 ? exercise_user.subjective_score : 0)))
end
end
end
end