# encoding: utf-8 module ExerciseHelper # 单选 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 # 判断学生是否点击过问卷,点击则为他保存一个记录,记录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 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 end end end return total_score, mc_score, mcq_score, single_score, multi_score end return total_score, mc_score, mcq_score, single_score, multi_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.count > 0 true else false end end #获取未完成的题目 def get_uncomplete_question exercise,user # all_questions = exercise.exercise_questions uncomplete_question = [] exercise.exercise_questions.includes(:exercise_answers).each do |question| # answers = get_user_answer(question, user) answers = question.exercise_answers.select{|e| e.user_id == user.id} if answers.empty? uncomplete_question << question end end uncomplete_question 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 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