From deb0f151c477fec0e023038e8791182f71acea33 Mon Sep 17 00:00:00 2001 From: huang Date: Wed, 18 Nov 2015 13:55:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E9=80=89=E6=95=B0=E5=AD=97=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/exercise_controller.rb | 27 +++++++++++++++++++++++--- app/helpers/exercise_helper.rb | 7 ++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/controllers/exercise_controller.rb b/app/controllers/exercise_controller.rb index c892b6583..d06f4ee8d 100644 --- a/app/controllers/exercise_controller.rb +++ b/app/controllers/exercise_controller.rb @@ -3,7 +3,7 @@ class ExerciseController < ApplicationController before_filter :find_exercise_and_course, :only => [:create_exercise_question, :edit, :update, :show] before_filter :find_course, :only => [:index,:new,:create,:student_exercise_list] - helper :Exercise + include ExerciseHelper def index remove_invalid_exercise(@course) @@ -29,7 +29,7 @@ class ExerciseController < ApplicationController if has_commit_exercise?(@exercise.id, User.current.id) && (!User.current.admin?) redirect_to exercise_index_url(:course_id=> @course.id) else - @can_edit_poll = (!has_commit_exercise?(@exercise.id,User.current.id)) || User.current.admin? + @can_edit_excercise = (!has_commit_exercise?(@exercise.id,User.current.id)) || User.current.admin? @percent = get_percent(@exercise,User.current) exercise_questions = @exercise.exercise_questions @exercise_questions = paginateHelper exercise_questions,5 #分页 @@ -145,7 +145,7 @@ class ExerciseController < ApplicationController if @exercise_questions.save standart_answer = ExerciseStandardAnswer.new standart_answer.exercise_question_id = @exercise_questions.id - @exercise_questions.question_type == 3 ? standart_answer.answer_text = params[:exercise_choice] : standart_answer.exercise_choice_id = params[:exercise_choice] + @exercise_questions.question_type == 3 ? standart_answer.answer_text = translate_standard_answer(params[:exercise_choice]) : standart_answer.exercise_choice_id = translate_standard_answer(params[:exercise_choice]) standart_answer.save respond_to do |format| format.js @@ -239,6 +239,27 @@ class ExerciseController < ApplicationController end private + def get_complete_question(exercise,user) + questions = exercise.exercise_questions + complete_question = [] + questions.each do |question| + answers = get_user_answer(question,user) + if !(answers.nil? || answers.count < 1) + complete_question << question + end + end + complete_question + end + + def get_percent exercise,user + complete_count = get_complete_question(exercise,user).count + if exercise.exercise_questions.count == 0 + return 0 + else + return (complete_count.to_f / exercise.exercise_questions.count.to_f)*100 + end + end + def remove_invalid_exercise(course) exercises = course.exercises.where("exercise_name=?","") unless exercises.empty? diff --git a/app/helpers/exercise_helper.rb b/app/helpers/exercise_helper.rb index 410a9936b..6b8d8644e 100644 --- a/app/helpers/exercise_helper.rb +++ b/app/helpers/exercise_helper.rb @@ -1,9 +1,14 @@ # encoding: utf-8 module ExerciseHelper + # 单选 + def translate_standard_answer(params) + answer = params.ord - 64 + end + #判断用户是否已经提交了问卷 def has_commit_exercise?(exercise_id, user_id) - pu = PollUser.find_by_poll_id_and_user_id(excercise_id, user_id) + pu = PollUser.find_by_poll_id_and_user_id(exercise_id, user_id) if pu.nil? false else