diff --git a/app/controllers/exercise_questions_controller.rb b/app/controllers/exercise_questions_controller.rb index 61a1386fc..5f59c3cab 100644 --- a/app/controllers/exercise_questions_controller.rb +++ b/app/controllers/exercise_questions_controller.rb @@ -605,6 +605,33 @@ class ExerciseQuestionsController < ApplicationController ExerciseShixunAnswer.create!(ex_shixun_option) new_obj_score = ex_obj_score + @c_score end + total_scores = new_obj_score + ex_subj_score + if total_scores < 0.0 + total_scores = 0.0 + elsif total_scores > ex_all_scores + total_scores = ex_all_scores + end + ex_scores = { + :objective_score => new_obj_score, + :score => total_scores + } + @exercise_current_user.update!(ex_scores) + elsif @exercise_question.question_type == Exercise::PROGRAM + if ex_answers.exists? + ex_pro_old_score = ex_answers.first.score > 0.0 ? ex_answers.first.score : 0.0 + new_obj_score = ex_obj_score - ex_pro_old_score + @c_score + ex_answers.first.update_attribute("score", @c_score) + else #如果学生未答,则创建新的答题记录 + answer_option = { + :user_id => @user_id, + :exercise_question_id => @exercise_question.id, + :score => @c_score, + :answer_text => "" + } + ExerciseAnswer.create!(answer_option) + new_obj_score = ex_obj_score + @c_score + end + total_scores = new_obj_score + ex_subj_score if total_scores < 0.0 total_scores = 0.0 diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index 4247a0619..b96ab0d4b 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -1623,7 +1623,7 @@ class ExercisesController < ApplicationController ques_number = q.question_number end ques_status = 0 - if q.question_type != Exercise::PRACTICAL + if q.question_type < Exercise::PRACTICAL ques_vote = q.exercise_answers.select {|answer| answer.user_id == user_id} if ques_vote.present? @@ -1642,11 +1642,16 @@ class ExercisesController < ApplicationController end end end - else + elsif q.question_type == Exercise::PRACTICAL if Myshixun.exists?(user_id: user_id, shixun_id: q.shixun_id) ques_status = 1 question_answered += 1 end + else + if HackUserLastestCode.exists?(user_id: user_id, hack_id: q.hack_id) + ques_status = 1 + question_answered += 1 + end end question_status = { :ques_id => q.id, diff --git a/app/helpers/exercise_questions_helper.rb b/app/helpers/exercise_questions_helper.rb index 1cc613bc6..8315e2895 100644 --- a/app/helpers/exercise_questions_helper.rb +++ b/app/helpers/exercise_questions_helper.rb @@ -14,10 +14,10 @@ module ExerciseQuestionsHelper } answered_content.push(u_answer) end - elsif question.question_type == Exercise::SUBJECTIVE + elsif question.question_type == Exercise::SUBJECTIVE || question.question_type == Exercise::PROGRAM answered_content = exercise_answers.pluck(:answer_text) end - if question.question_type == Exercise::PRACTICAL && ex_user.present? && ex_user.commit_status == 1 #存在实训题,且用户已提交了的,如果实训题只做了一半就关闭,则相当于不要了 + if (question.question_type == Exercise::PRACTICAL || question.question_type == Exercise::PROGRAM) && ex_user.present? && ex_user.commit_status == 1 #存在实训题,且用户已提交了的,如果实训题只做了一半就关闭,则相当于不要了 if exercise.exercise_status == 3 #如果试卷已截止,则可以看到分数,否则不能查看分数 shixun_type = 2 else diff --git a/app/helpers/exercises_helper.rb b/app/helpers/exercises_helper.rb index d36f0dd3d..fc380a28d 100644 --- a/app/helpers/exercises_helper.rb +++ b/app/helpers/exercises_helper.rb @@ -15,7 +15,7 @@ module ExercisesHelper answers_content = q.exercise_answers.select{|answer| answer.user_id == user_id} end - if q_type <= Exercise::JUDGMENT + if q_type <= Exercise::JUDGMENT || q_type == Exercise::PROGRAM if answers_content.present? #学生有回答时,分数已经全部存到exercise_answer 表,多选题直接取第一个值,单选题和判断题选最后一个值(考虑并发) ques_score = q_type == Exercise::MULTIPLE ? answers_content.first.score : answers_content.last.score ques_score = ques_score < 0 ? 0.0 : ques_score @@ -454,6 +454,7 @@ module ExercisesHelper score1 = 0.0 #选择题/判断题 score2 = 0.0 #填空题 score5 = 0.0 #实训题 + score6 = 0.0 #编程题 ques_stand = [] #问题是否正确 exercise_end_time = end_time || Time.now exercise_questions = exercise.exercise_questions.includes(:exercise_standard_answers,:exercise_shixun_challenges) @@ -608,6 +609,34 @@ module ExercisesHelper end end end + elsif q.question_type == 6 #编程题 + if answers_content.present? + score6 += answers_content.first.score + else + hack = q.hack + hack_user = hack&.hack_user_lastest_codes&.find_by(user_id: user.id) + if hack_user.present? + last_passed_code = hack_user.hack_user_codes.where("status = 0 and created_at < '#{exercise_end_time}'").last + if last_passed_code.present? + pro_score = q.question_score + code_content = last_passed_code.code + else + pro_score = 0 + code_content = hack_user.hack_user_codes.where("created_at < '#{exercise_end_time}'").last&.code + end + else + pro_score = 0 + code_content = nil + end + pro_option = { + :exercise_question_id => q.id, + :user_id => user.id, + :score => pro_score.round(1), + :answer_text => code_content + } + ExerciseAnswer.create(pro_option) + score6 += pro_score + end end user_scores = answers_content.blank? ? 0.0 : answers_content.score_reviewed.pluck(:score).sum if user_scores > 0.0 @@ -629,7 +658,7 @@ module ExercisesHelper end end - total_score = score1 + score2 + score5 + total_score = score1 + score2 + score5 + score6 { "total_score":total_score.round(1), "stand_status":ques_stand @@ -923,10 +952,10 @@ module ExercisesHelper elsif ques_type == 4 answered_content = exercise_answers&.pluck(:answer_text) end - if ques_type == 5 #存在实训题,及已经做了实训题的 + if ques_type == 5 || ques_type == 6 #存在实训题,及已经做了实训题的 if ex_status == 3 || is_teacher_or == 1 #如果试卷已截止,则可以看到分数,否则不能查看分数 shixun_type = 2 - elsif ex_status == 2 && q.exercise_shixun_answers.present? #试卷未截止,且用户已回答的,则能看到答题的状态 + elsif ex_status == 2 && (q.exercise_shixun_answers.present? || q.exercise_answers.present?) #试卷未截止,且用户已回答的,则能看到答题的状态 shixun_type =1 end end diff --git a/app/models/exercise_question.rb b/app/models/exercise_question.rb index 47dea1175..de4a55d3a 100644 --- a/app/models/exercise_question.rb +++ b/app/models/exercise_question.rb @@ -48,4 +48,8 @@ class ExerciseQuestion < ApplicationRecord exercise_standard_answers.pluck(:answer_text) end + def hack_user user_id + hack_user = hack&.hack_user_lastest_codes&.find_by(user_id: user_id) + end + end diff --git a/app/views/exercise_questions/_exercise_questions.json.jbuilder b/app/views/exercise_questions/_exercise_questions.json.jbuilder index 043dede07..32e6a6ee3 100644 --- a/app/views/exercise_questions/_exercise_questions.json.jbuilder +++ b/app/views/exercise_questions/_exercise_questions.json.jbuilder @@ -125,4 +125,18 @@ elsif question.question_type == 6 json.hack_identifier hack.identifier json.description hack.description json.hack_id hack.id + if exercise_type == 3 || exercise_type == 4 + json.user_answer user_answer + end + + if (exercise_type == 3 || exercise_type == 4) && (shixun_type.present? && shixun_type > 0 ) #教师评阅/试卷截止后,答案公开/试卷未截止,但学生已做了实训题 + hack_user = question.hack_user(ex_answerer.id) + if hack_user.present? + evaluate_codes = hack_user.hack_user_codes.where("created_at < '#{@exercise_user_current&.end_at}'") + json.evaluate_codes evaluate_codes do |code| + json.(code, :status, :error_msg, :error_line, :expected_output, :input, :output, :execute_time, :execute_memory) + json.created_at code.created_at.strftime('%Y-%m-%d %H:%M:%S') + end + end + end end \ No newline at end of file