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/app/helpers/exercise_helper.rb

428 lines
14 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.

# encoding: utf-8
module ExerciseHelper
def shixun_question_level level
case level
when 1
"初级题"
when 2
"中级题"
when 3
"高级题"
when 4
"顶级题"
end
end
def exercise_shixun_json_data shixuns
shixuns.map do |shixun|
shixun_owner = shixun.owner.try(:show_name)
shixun_path = shixun_path(shixun)
shixun_level = shixun.shixun_level
owner_path = user_path(shixun.owner)
shixun.attributes.dup.except("gpid", "identifier", "major_id", "webssh", "homepage_show", "fork_from", "git_url",
"mirror_script_id", "repo_name", "pod_life").merge({
shixun_owner: shixun_owner,
shixun_level: shixun_level,
shixun_path: shixun_path,
owner_path: owner_path
})
end
end
def un_commit_num exercise
course = exercise.course
member = course.members.where(:user_id => User.current.id).first
exercise_users = exercise.exercise_users
student_count = course.student.count
if member.present? && member.teacher_course_groups.count > 0
group_students = course.members.where(:course_group_id => member.teacher_course_groups.pluck(:course_group_id)).map(&:user_id)
student_count = group_students.size
exercise_users = exercise_users.where(:user_id => group_students)
end
student_count - exercise_users.where(:commit_status => 1, :user_id => course.student.map(&:student_id)).count
end
def has_commit_num exercise
course = exercise.course
member = course.members.where(:user_id => User.current.id).first
exercise_users = exercise.exercise_users
if member.present? && member.teacher_course_groups.count > 0
group_students = course.members.where(:course_group_id => member.teacher_course_groups.pluck(:course_group_id)).map(&:user_id)
exercise_users = exercise_users.where(:user_id => group_students)
end
exercise_users.where(:commit_status => 1, :user_id => course.student.map(&:student_id)).count
end
def un_comment_num exercise
course = exercise.course
member = course.members.where(:user_id => User.current.id).first
exercise_users = exercise.exercise_users
if member.present? && member.teacher_course_groups.count > 0
group_students = course.members.where(:course_group_id => member.teacher_course_groups.pluck(:course_group_id)).map(&:user_id)
exercise_users = exercise_users.where(:user_id => group_students)
end
exercise_users.where(:commit_status => 1, :subjective_score => -1).count
end
def has_comment_num exercise
course = exercise.course
member = course.members.where(:user_id => User.current.id).first
exercise_users = exercise.exercise_users
if member.present? && member.teacher_course_groups.count > 0
group_students = course.members.where(:course_group_id => member.teacher_course_groups.pluck(:course_group_id)).map(&:user_id)
exercise_users = exercise_users.where(:user_id => group_students)
end
exercise_users.where("commit_status = 1 and subjective_score != -1").count
end
def answer_is_correct eq, user
is_correct = false
case eq.question_type
when 1
answer = get_user_answer(eq, user)
standard_answer = get_user_standard_answer(eq, user)
is_correct = !answer.empty? && !standard_answer.empty? && answer.first.exercise_choice.choice_position == standard_answer.first.exercise_choice_id
when 2
standard_answer = get_user_standard_answer(eq, user)
is_correct = !standard_answer.empty? && get_mulscore(eq, user).to_i == standard_answer.first.exercise_choice_id
when 3
answer = get_user_answer(eq, user)
standard_answer = get_user_standard_answer(eq, user)
is_correct = !answer.empty? && !standard_answer.empty? && standard_answer.collect{|answer| answer.strip}.include?(answer.first.answer_text.strip.downcase)
when 5
is_correct = eq.exercise_shixun_answers.where(:status => 1, :user_id => user.id).count == eq.shixun.challenges.count
end
is_correct
end
def question_correct_rate eq, user_ids
count = 0
rate = 0
user_ids_str = user_ids.length == 0 ? "(-1)" : "("+user_ids.join(",")+")"
user_ids.each do |user_id|
user = User.where(:id => user_id).first
case eq.question_type
when 1, 2, 3
if user && answer_is_correct(eq, user)
count += 1
end
rate = user_ids.length == 0 ? 0 : count.to_f / user_ids.length
when 4
rate = user_ids.length * eq.question_score > 0 ? eq.exercise_answers.where("user_id in #{user_ids_str} and score <> -1").collect(&:score).sum.to_f / (user_ids.length * eq.question_score) : 0
when 5
rate = user_ids.length * eq.question_score > 0 ? eq.exercise_shixun_answers.where("user_id in #{user_ids_str}").collect(&:score).sum.to_f / (user_ids.length * eq.question_score) : 0
end
end
rate = format("%.1f", rate*100)
end
#多选的时候查询去重
def total_answer id, user_ids
user_ids = user_ids.blank? ? "(-1)" : "(" + user_ids.join(",") + ")"
total = ExerciseAnswer.find_by_sql("SELECT distinct(user_id) FROM `exercise_answers` where exercise_question_id = #{id} and user_id in #{user_ids}").count
end
def shixun_question_score eq, user
score = 0
if eq.question_type == 5
eq.exercise_shixun_answers.where(:user_id => user.id).each do |answer|
score += answer.score
end
end
score
end
# 单选
def sigle_selection_standard_answer(params)
size = params.ord - 96
if size > 0 # 小写字母答案
answer = params.ord - 96
else
answer = params.ord - 64
end
end
# 多选
def multiselect_standard_answer(params)
size = params.ord - 96
answer = []
if size > 0 # 小写字母答案
for i in 0..(params.length-1)
answer << (params[i].ord - 96).to_s
end
else
for i in 0..(params.length-1)
answer << (params[i].ord - 64)
end
end
answer = answer.sort
answer.join("")
end
#
def fill_standart_answer(params, standart_answer)
params.each do |param|
standart_answer.answer_text = param.value
standart_answer.save
end
end
# 获取多选的得分
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
# 判断用户是否已经提交了问卷
# status 为0的时候是用户点击试卷。为1表示用户已经提交
def has_commit_exercise?(exercise_id, user_id)
pu = ExerciseUser.where("exercise_id=? and user_id=? and status=?",exercise_id, user_id, true)
if pu.empty?
false
else
true
end
end
def can_edit_exercise exercise, exercise_user
exercise_end = exercise.end_time.nil? ? false : exercise.end_time > Time.now
if exercise.time == -1
can_edit_excercise = exercise_end && exercise_user.commit_status == 0
else
left_time = (Time.now.to_i - exercise_user.start_at.to_i) < exercise.time.to_i * 60
can_edit_excercise = !exercise_user.status && left_time && exercise_end
end
can_edit_excercise
end
# 判断学生是否点击过问卷点击则为他保存一个记录记录start_at
def has_click_exercise?(exercise_id, user_id)
pu = ExerciseUser.where("exercise_id=? and user_id=? and status=?",exercise_id, user_id, false)
if pu.empty?
false
else
true
end
end
def convert_to_char(str)
result = ""
length = str.length
unless str.nil?
if length === 1
result += (str.to_i + 64).chr
return result
elsif length > 1
for i in 0...length
result += (str[i].to_i + 64).chr
end
return result
end
end
return result
end
def convert_to_chi_num num
result = ""
case num.to_i
when 1
result = '一'
when 2
result = '二'
when 3
result = '三'
when 4
result = '四'
when 5
result = '五'
when 6
result = '六'
when 7
result = '七'
when 8
result = '八'
when 9
result = '九'
end
return result
end
def get_current_score exercise
total_score = 0
mc_score = 0
mcq_score = 0
single_score = 0
multi_score = 0
shixun_score = 0
unless exercise.nil?
exercise.exercise_questions.each do |exercise_question|
unless exercise_question.question_score.nil?
total_score += exercise_question.question_score
case exercise_question.question_type
when 1
mc_score += exercise_question.question_score
when 2
mcq_score += exercise_question.question_score
when 3
single_score += exercise_question.question_score
when 4
multi_score += exercise_question.question_score
when 5
shixun_score += exercise_question.question_score
end
end
end
return total_score, mc_score, mcq_score, single_score, multi_score, shixun_score
end
return total_score, mc_score, mcq_score, single_score, multi_score, shixun_score
end
def answer_be_selected?(answer,user)
# pv = answer.exercise_answers.where("#{ExerciseAnswer.table_name}.user_id = #{user.id} ")
pv = answer.exercise_answers.select{|e| e.user_id == user.id}
if !pv.nil? && pv.size > 0
true
else
false
end
end
#获取未完成的题目
def get_uncomplete_question exercise, exercise_user
# all_questions = exercise.exercise_questions.includes(:exercise_answers).where("question_type != 5")
uncomplete_question = []
questions = exercise.user_question_list(exercise_user.id)
questions.includes(:exercise_answers).where("question_type != 5").each do |question|
# answers = get_user_answer(question, user)
answers = question.exercise_answers.select{|e| e.user_id == exercise_user.user_id}
if answers.empty?
uncomplete_question << question
end
end
uncomplete_question
end
# 获取未通关的实训
def get_uncomplete_shixun exercise, exercise_user
questions = exercise.user_question_list(exercise_user.try(:id))
all_questions = questions.where("question_type = 5")
uncomplete_shixun = []
all_questions.each do |question|
myshixun = Myshixun.where(:shixun_id => question.shixun_id, :user_id => exercise_user.user_id).first
unless myshixun && myshixun.is_complete?
uncomplete_shixun << question
end
end
uncomplete_shixun
end
# 题目已答或未答
def question_answer_or_not question, user
if question.question_type == 5
# is_answer = question.exercise_shixun_answers.where(:user_id => user.id, :status => 1).count > 0
is_answer = question.exercise_shixun_answers.select{|e| e.user_id == user.id && e.status == 1}.count > 0
else
# is_answer = user.exercise_answer.where(:exercise_question_id => question.id).count > 0
is_answer = question.exercise_answers.select{|e| e.user_id == user.id}.count > 0
end
is_answer
end
def question_commit_status question, user
status = ""
case question.question_type
when 3, 4
get_anwser_vote_text(question.id,user.id) == "" ? status = "未答" : status = ""
when 1, 2
get_user_answer(question,user).empty? ? status = "未答" : status = ""
when 5
question.exercise_shixun_answers.where(:user_id => user.id).empty? ? status = "未答" : status = ""
end
status
end
# 获取文本题答案
def get_anwser_vote_text(question_id,user_id)
pv = ExerciseAnswer.find_by_exercise_question_id_and_user_id(question_id,user_id)
if pv.nil?
''
else
pv.answer_text
end
end
def new_get_anwser_vote_text question, user
pv = question.exercise_answers.select{|exercise_answer| exercise_answer.user_id == user.id}
if pv.blank?
''
else
pv.first.try(:answer_text)
end
end
# 获取当前学生回答问题的答案
def get_user_answer(question,user)
user_answer = question.exercise_answers.where("#{ExerciseAnswer.table_name}.user_id = #{user.id}")
# user_answer = question.exercise_answers.select{|e| e.user_id == user.id}
user_answer
end
# 获取问题的标准答案
def get_user_standard_answer(question,user)
if question.question_type == 3
standard_answer =[]
question.exercise_standard_answers.each do |answer|
standard_answer << answer.answer_text.downcase
end
else
standard_answer = question.exercise_standard_answers
end
standard_answer
end
# 问题随机的下拉列表
def question_random_select
type = []
option1 = []
option1 << "题目不随机打乱"
option1 << 0
type << option1
option2 = []
option2 << "题目随机打乱"
option2 << 1
type << option2
end
# 选项随机的下拉列表
def choice_random_select
type = []
option1 = []
option1 << "选项不随机打乱"
option1 << 0
type << option1
option2 = []
option2 << "选项随机打乱"
option2 << 1
type << option2
end
#允许学生查看结果的下拉列表
def show_result_select
type = []
option1 = []
option1 << "允许学生查看测验结果"
option1 << 1
type << option1
option2 = []
option2 << "不允许学生查看测验结果"
option2 << 0
type << option2
end
end