diff --git a/app/helpers/exercises_helper.rb b/app/helpers/exercises_helper.rb index ea58ddb55..13ab4706c 100644 --- a/app/helpers/exercises_helper.rb +++ b/app/helpers/exercises_helper.rb @@ -120,21 +120,7 @@ module ExercisesHelper if ex.question_type > Exercise::COMPLETION #当为主观题和实训题时, ex_answered_scores = effictive_users.score_reviewed.pluck(:score).sum #该问题的全部得分 percent = (ex_total_score == 0.0 ? 0.0 : (ex_answered_scores / ex_total_score.to_f).round(3) * 100) #正确率 - end - - # if ex.question_type != Exercise::MULTIPLE - # ex_answered_scores = effictive_users.score_reviewed.pluck(:score).sum #该问题的全部得分 - # percent = (ex_total_score == 0.0 ? 0.0 : (ex_answered_scores / ex_total_score.to_f).round(3) * 100) #正确率 - # else - # multiple_score = 0 - # user_ids.each do |user_id| - # ex_answer_score = ex_answers.select{|answer| answer.user_id == user_id}&.first&.score.to_f - # multiple_score += ex_answer_score - # end - # percent = (ex_total_score == 0.0 ? 0.0 : (multiple_score / ex_total_score.to_f).round(3) * 100) #正确率 - # end - question_answer_infos = [] if ex.question_type <= Exercise::JUDGMENT #选择题和判断题 ex_choices = ex.exercise_choices @@ -178,42 +164,57 @@ module ExercisesHelper question_answer_infos.push(answer_option) end elsif ex.question_type == Exercise::COMPLETION #填空题 + effictive_users_count = effictive_users.pluck(:user_id).uniq.size ex_ordered = ex.is_ordered null_standard_answer = ex.exercise_standard_answers - null_stand_choice = null_standard_answer.pluck(:exercise_choice_id).uniq #一个exercise_choice_id可能对应多个answer_text + null_stand_choice = null_standard_answer.pluck(:exercise_choice_id) #一个exercise_choice_id可能对应多个answer_text null_stand_text = null_standard_answer.pluck(:answer_text) standard_answer_count = 0 each_null_score = null_stand_choice.size > 0 ? (ex&.question_score.to_f / null_stand_choice.uniq.size).round(3) : 0.0 all_user_count = 0 - null_stand_choice.each_with_index do |s,index| - user_count = 0 - if ex_ordered #有序排列 - s_choice_text = null_stand_text[index] - user_count = user_count + effictive_users.select{|answer| answer.exercise_choice_id == s && answer.answer_text == s_choice_text}.size - else - null_stand_text = null_stand_text.uniq - s_choice_text = null_stand_text[index] - user_count = user_count + effictive_users.select{|answer| answer.answer_text == s_choice_text }.size #回答了标准答案的用户 - end - answer_percent = ((effictive_users_count == 0) ? 0.0 : (user_count / effictive_users_count.to_f ).round(3)) - answer_option = { + if ex_ordered + null_stand_choice.each_with_index do |s,index| + s_choice_text = null_stand_text[index] + user_count = 0 + user_count = user_count + effictive_users.where("exercise_choice_id = ? and answer_text = ?",s,s_choice_text).pluck(:user_id).uniq.size + # user_count = user_count + effictive_users.select{|answer| answer.exercise_choice_id == s && answer.answer_text == s_choice_text}.size + answer_percent = ((effictive_users_count == 0) ? 0.0 : (user_count / effictive_users_count.to_f ).round(3)) + answer_option = { :choice_position => index+1, :choice_text => s_choice_text, :choice_users_count => user_count, :choice_percent => answer_percent.round(2).to_s, :right_answer => true - } - question_answer_infos.push(answer_option) - all_user_count += user_count - standard_answer_count += 1 + } + question_answer_infos.push(answer_option) + all_user_count += user_count + standard_answer_count += 1 + end + else + # cycled_stand = {} + null_stand_text.uniq.each_with_index do |stand, index| + user_count = 0 + user_count = user_count + effictive_users.where("answer_text = ?",stand).pluck(:user_id).uniq.size + answer_percent = ((effictive_users_count == 0) ? 0.0 : (user_count / effictive_users_count.to_f ).round(3)) + answer_option = { + :choice_position => index+1, + :choice_text => stand, + :choice_users_count => user_count, + :choice_percent => answer_percent.round(2).to_s, + :right_answer => true + } + question_answer_infos.push(answer_option) + all_user_count += user_count + standard_answer_count += 1 + end end answer_user_score = all_user_count * each_null_score percent = (ex_total_score == 0.0 ? 0.0 : (answer_user_score / ex_total_score.to_f).round(3) * 100) #正确率 # percent = commit_user_ids > 0 ? (all_user_count / commit_user_ids.to_f).round(3)*100 : 0.0 - user_wrong_count = (effictive_users_count - all_user_count) + user_wrong_count = effictive_users.where("score < 0").pluck(:user_id).uniq.size if effictive_users_count > 0 && user_wrong_count >= 0 wrong_percent = (user_wrong_count / effictive_users_count.to_f ).round(3) @@ -481,17 +482,28 @@ module ExercisesHelper end end else - st_answer_text = standard_answer_array.pluck(:answer_text).reject(&:blank?).map{|a| a.strip.downcase} + st_answer_text = standard_answer_array.pluck(:answer_text).reject(&:blank?).map{|a| a.strip.downcase}.uniq answers_content.each do |u| u_answer_text = u.answer_text.strip.downcase - if st_answer_text.include?(u_answer_text) #只要标准答案包含用户的答案,就有分数。同时,下一次循环时,就会删除该标准答案。防止用户的相同答案获分 - u.update_column("score",q_score_2) - score2 = score2 + q_score_2 - st_answer_text.delete(u_answer_text) + if st_answer_text.size == 1 + if st_answer_text.first == u + u.update_column("score",q_score_2) + score2 = score2 + q_score_2 + else + u.update_column('score',-1.0) + score2 += 0.0 + end else - u.update_column('score',-1.0) - score2 += 0.0 + if st_answer_text.include?(u_answer_text) #只要标准答案包含用户的答案,就有分数。同时,下一次循环时,就会删除该标准答案。防止用户的相同答案获分 + u.update_column("score",q_score_2) + score2 = score2 + q_score_2 + st_answer_text.delete(u_answer_text) + else + u.update_column('score',-1.0) + score2 += 0.0 + end end + end end else diff --git a/db/migrate/20191113013258_gcc_course_competition_rank_data.rb b/db/migrate/20191113013258_gcc_course_competition_rank_data.rb new file mode 100644 index 000000000..344d8061c --- /dev/null +++ b/db/migrate/20191113013258_gcc_course_competition_rank_data.rb @@ -0,0 +1,9 @@ +class GccCourseCompetitionRankData < ActiveRecord::Migration[5.2] + def change + competition = Competition.find_by(identifier: 'gcc-course-2019') + stage = competition.competition_stages.first + return if competition.blank? || stage.blank? + + competition.competition_scores.update_all(competition_stage_id: stage.id) + end +end