diff --git a/app/controllers/homework_commons_controller.rb b/app/controllers/homework_commons_controller.rb index 74f1bb371..439da8add 100644 --- a/app/controllers/homework_commons_controller.rb +++ b/app/controllers/homework_commons_controller.rb @@ -139,7 +139,7 @@ class HomeworkCommonsController < ApplicationController @student_works = [] end - unless @student_works.blank? + if @student_works.size > 0 # 教师评阅搜索 0: 未评, 1 已评 unless params[:teacher_comment].blank? student_work_ids = StudentWorksScore.where(student_work_id: @student_works.map(&:id)).pluck(:student_work_id) @@ -169,6 +169,8 @@ class HomeworkCommonsController < ApplicationController or student_id like ?", "%#{params[:search]}%", "%#{params[:search]}%") end + @work_count = @student_works.size + # 排序 rorder = params[:order].blank? ? "update_time" : params[:order] b_order = params[:b_order].blank? ? "desc" : params[:b_order] @@ -178,23 +180,22 @@ class HomeworkCommonsController < ApplicationController @student_works = @student_works.joins(user: :user_extension).order("user_extensions.#{rorder} #{b_order}") end - @work_count = @student_works.size - @work_excel = @student_works.includes(:student_works_scores, user: :user_extension, myshixun: :games) - @students = @course.students + @work_excel = @student_works # 分页参数 page = params[:page] || 1 limit = params[:limit] || 20 @student_works = @student_works.page(page).per(limit) + @students = @course.students.where(user_id: @student_works.pluck(:user_id)).preload(:course_group) if @homework.homework_type == "practice" @student_works = @student_works.includes(:student_works_scores, user: :user_extension, myshixun: :games) else @student_works = @student_works.includes(:student_works_scores, :project, user: :user_extension) end - # @members = @course.students.where(user_id: @student_works.pluck(:user_id)).includes(:course_group) end if params[:format] == "xlsx" + @work_excel = @work_excel.includes(:student_works_scores, user: :user_extension, myshixun: :games) complete_works = @work_excel.present? ? @work_excel.where("work_status > 0").size : 0 if @user_course_identity >= Course::STUDENT tip_exception(403, "无权限操作") diff --git a/app/helpers/exercises_helper.rb b/app/helpers/exercises_helper.rb index 4a0c5509d..5ec9bcad4 100644 --- a/app/helpers/exercises_helper.rb +++ b/app/helpers/exercises_helper.rb @@ -88,8 +88,8 @@ module ExercisesHelper question_infos = [] percent = 0.0 questions.includes(:exercise_choices).each do |ex| - ex_total_score = user_ids.count * ex&.question_score #该试卷的已回答的总分 - ex_answers = ex.exercise_answers + ex_total_score = user_ids.count * ex&.question_score.to_f #该试卷的已回答的总分 + # ex_answers = ex.exercise_answers if ex.question_type != Exercise::PRACTICAL ques_title = ex.question_title ques_less_title = nil @@ -98,23 +98,23 @@ module ExercisesHelper else ques_title = ex.shixun.name ques_less_title = ex.question_title - effictive_users = ex.exercise_shixun_answers.search_shixun_answers("user_id",user_ids) + ex_answers = ex.exercise_shixun_answers + effictive_users = ex_answers.search_shixun_answers("user_id",user_ids) end effictive_users_count = effictive_users.size #有效回答数,可能有重复的用户id,这里仅统计是否回答这个问题的全部人数 ex_answered_scores = effictive_users.score_reviewed.pluck(:score).sum #该问题的全部得分 if ex.question_type > Exercise::COMPLETION #当为主观题和实训题时, - if ex_total_score != 0.0 - percent = (ex_answered_scores / ex_total_score.to_f).round(3) * 100 #正确率 - end + percent = (ex_total_score == 0.0 ? 0.0 : (ex_answered_scores / ex_total_score.to_f).round(3) * 100) #正确率 end question_answer_infos = [] if ex.question_type <= Exercise::JUDGMENT #选择题和判断题 ex_choices = ex.exercise_choices standard_answer = ex.exercise_standard_answers.pluck(:exercise_choice_id).sort #标准答案的位置 - right_users_count = 0 #该问题的回答正确的人数 + right_users_count = 0 + #该问题的正确率 if ex.question_type == Exercise::MULTIPLE #多选题 user_ids.each do |user_id| ex_choice_ids = effictive_users.map{|e| e.exercise_choice_id if e.user_id == user_id}.reject(&:blank?).uniq @@ -129,15 +129,14 @@ module ExercisesHelper end percent = effictive_users_count > 0 ? (right_users_count / effictive_users_count.to_f).round(3)*100 : 0.0 + + #每个选项的正确率 ex_choices.each do |c| right_answer = standard_answer.include?(c.choice_position) #选项的标准答案为选项的位置 - answer_this_choice = effictive_users.search_exercise_answer("exercise_choice_id",c.id) - answer_users_count = answer_this_choice.size - if effictive_users_count == 0 - answer_percent = 0.0 - else - answer_percent = (answer_users_count / effictive_users_count.to_f ).round(3) - end + answer_users_count = effictive_users.select{|answer| answer.exercise_choice_id == c.id}.size + + answer_percent = (effictive_users_count == 0 ? 0.0 : (answer_users_count / effictive_users_count.to_f ).round(3)) + answer_option = { :choice_position => c.choice_position, :choice_text => c.choice_text, @@ -150,7 +149,7 @@ module ExercisesHelper elsif ex.question_type == Exercise::COMPLETION #填空题 ex_ordered = ex.is_ordered null_standard_answer = ex.exercise_standard_answers - null_stand_choice = null_standard_answer.pluck(:exercise_choice_id) + 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 all_user_count = 0 @@ -159,20 +158,17 @@ module ExercisesHelper s_choice_text = null_stand_text[index] if ex_ordered #有序排列 user_ids.each do |u| - user_answers = ex_answers.search_answer_users("user_id",u).search_answer_users("exercise_choice_id",s) - user_answers_choice = user_answers.present? ? user_answers.first.answer_text : "" + user_answers = ex_answers.where(user_id:u,exercise_choice_id:s).select(:answer_text) + user_answers_choice = user_answers.exists? ? user_answers&.first&.answer_text.to_s : "" if s_choice_text == user_answers_choice user_count += 1 end end else - user_count = user_count + effictive_users.search_exercise_answer("answer_text",s_choice_text).count #回答了标准答案的用户 - end - if effictive_users_count == 0 - answer_percent = 0.0 - else - answer_percent = (user_count / effictive_users_count.to_f ).round(3) + 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 = { :choice_position => index+1, :choice_text => s_choice_text, @@ -237,22 +233,13 @@ module ExercisesHelper all_zero_scores = cha_shixun_answer.search_shixun_keys("score",0.0).size #零分人数 shixun_scores = user_ids.count * cha_score shixun_answered_scores = cha_shixun_answer.score_reviewed.pluck(:score).sum #该问题的全部得分 - - if shixun_answered_scores == 0.0 - game_percent = 0.0 - else - game_percent = (shixun_answered_scores / shixun_scores.to_f).round(3) * 100 #正确率 - end + game_percent = (shixun_answered_scores == 0.0 ? 0.0 : (shixun_answered_scores / shixun_scores.to_f).round(3) * 100) #正确率 shixun_score_array = [full_scores,no_full_scores,all_zero_scores] shixun_chas = [] shixun_score_array.each_with_index do |s,index| right_answer = (index == 0) - if effictive_users_count == 0 - score_percent = 0.0 - else - score_percent = (s.to_i / effictive_users_count.to_f ).round(3) - end + score_percent = (effictive_users_count == 0 ? 0.0 : (s.to_i / effictive_users_count.to_f ).round(3)) answer_option = { :choice_position => index+1, :choice_text => index+1,