From bc47382115689c4cfdddc93717b9e7c53c564e31 Mon Sep 17 00:00:00 2001 From: huang Date: Thu, 19 Nov 2015 11:38:44 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E7=AD=94=E5=8D=B7=E5=90=8E=EF=BC=8C=E7=BB=99=E5=87=BA=E5=BE=97?= =?UTF-8?q?=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/exercise_controller.rb | 42 +++++++++++++++++++++----- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/app/controllers/exercise_controller.rb b/app/controllers/exercise_controller.rb index a291a2229..58c6f772b 100644 --- a/app/controllers/exercise_controller.rb +++ b/app/controllers/exercise_controller.rb @@ -190,9 +190,9 @@ class ExerciseController < ApplicationController @exercise_question.question_score = params[:question_score] ################处理选项 if params[:question_answer] - # @exercise_question.exercise_choices.each do |answer| - # answer.destroy unless params[:question_answer].keys.include? answer.id.to_s - # end + @exercise_question.exercise_choices.each do |answer| + answer.destroy unless params[:question_answer].keys.include? answer.id.to_s + end # 界面需要判断选择题至少有一个选项 for i in 1..@exercise_question.exercise_choices.count question = @exercise_question.exercise_choices.find_by_id params[:question_answer].keys[i-1] @@ -265,9 +265,10 @@ class ExerciseController < ApplicationController end end - # 学生提交答卷 + # 学生提交答卷,选着答案的课程中提交 def commit_answer - eq = ExerciseQuestion.find(params[:poll_question_id]) + eq = ExerciseQuestion.find(params[:exercise_question_id]) + # 已提交过的则不允许答题 if has_commit_exercise?(@exercise.id,User.current.id) && (!User.current.admin?) render :json => {:text => "failure"} return @@ -369,11 +370,12 @@ class ExerciseController < ApplicationController # 老师不需要提交 if User.current.allowed_to?(:as_teacher,@course) redirect_to exercise_url(@exercise) + # REDO: 提示提交成功 else # 答题过程中需要统计完成量 @uncomplete_question = get_uncomplete_question(@exercise, User.current) # 获取改学生的考试得分 - score = get_answer_score(@exercise) + score = calculate_student_score(@exercise, User.current) if @uncomplete_question.count < 1 # 查看是否有已提交记录 eu = get_exercise_user(@exercise.id, User.current.id) @@ -395,6 +397,26 @@ class ExerciseController < ApplicationController end end + # 计算学生得分 + def calculate_student_score(exercise, user) + score = 0 + exercise_qustions = exercise.exercise_questions + exercise_qustions.each do |question| + answer = get_user_answer(question, user) + standard_answer = get_user_standard_answer(question, user) + # 问答题有多个答案 + if question.question_type == 3 + if standard_answer.exercise_choice_id.include?(answer.exercise_choice_id) + score = score + question.question_score + end + else + if answer.exercise_choice_id == standard_answer.exercise_choice_id + score = score + question.question_score + end + end + end + score + end private # ExerciseUser记录用户是否已提交问卷有对应的记录则已提交,没有则新建一个 @@ -419,12 +441,18 @@ class ExerciseController < ApplicationController uncomplete_question end - # 获取问题的答案 + # 获取当前学生回答问题的答案 def get_user_answer(question,user) user_answer = question.exercise_answers.where("#{ExerciseAnswer.table_name}.user_id = #{user.id}") user_answer end + # 获取问题的标准答案 + def get_user_standard_answer(question,user) + standard_answer = question.exercise_standard_answers + standard_answer + end + # 是否完成了答题 def get_complete_question(exercise,user) questions = exercise.exercise_questions From a900c70e763026d54b20474cde5e9c1c97c94685 Mon Sep 17 00:00:00 2001 From: huang Date: Thu, 19 Nov 2015 14:26:51 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=AF=95=E9=A2=98=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/exercise_controller.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/controllers/exercise_controller.rb b/app/controllers/exercise_controller.rb index 58c6f772b..dd4e543ff 100644 --- a/app/controllers/exercise_controller.rb +++ b/app/controllers/exercise_controller.rb @@ -188,15 +188,14 @@ class ExerciseController < ApplicationController @exercise_question = ExerciseQuestion.find params[:exercise_question] @exercise_question.question_title = params[:question_title].nil? || params[:question_title].empty? ? l(:label_enter_single_title) : params[:question_title] @exercise_question.question_score = params[:question_score] - ################处理选项 + # 处理选项 if params[:question_answer] @exercise_question.exercise_choices.each do |answer| answer.destroy unless params[:question_answer].keys.include? answer.id.to_s end - # 界面需要判断选择题至少有一个选项 - for i in 1..@exercise_question.exercise_choices.count + for i in 1..params[:question_answer].count question = @exercise_question.exercise_choices.find_by_id params[:question_answer].keys[i-1] - # answer = (params[:question_answer].values[i-1].nil? || params[:question_answer].values[i-1].empty?) ? l(:label_new_answer) : params[:question_answer].values[i-1] + answer = (params[:question_answer].values[i-1].nil? || params[:question_answer].values[i-1].empty?) ? l(:label_new_answer) : params[:question_answer].values[i-1] if question question.exercise_choices_id = i question.answer_text = answer