单选数字转换

This commit is contained in:
huang 2015-11-18 13:55:42 +08:00
parent 282c7f5bd2
commit deb0f151c4
2 changed files with 30 additions and 4 deletions

View File

@ -3,7 +3,7 @@ class ExerciseController < ApplicationController
before_filter :find_exercise_and_course, :only => [:create_exercise_question, :edit, :update, :show] before_filter :find_exercise_and_course, :only => [:create_exercise_question, :edit, :update, :show]
before_filter :find_course, :only => [:index,:new,:create,:student_exercise_list] before_filter :find_course, :only => [:index,:new,:create,:student_exercise_list]
helper :Exercise include ExerciseHelper
def index def index
remove_invalid_exercise(@course) remove_invalid_exercise(@course)
@ -29,7 +29,7 @@ class ExerciseController < ApplicationController
if has_commit_exercise?(@exercise.id, User.current.id) && (!User.current.admin?) if has_commit_exercise?(@exercise.id, User.current.id) && (!User.current.admin?)
redirect_to exercise_index_url(:course_id=> @course.id) redirect_to exercise_index_url(:course_id=> @course.id)
else 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) @percent = get_percent(@exercise,User.current)
exercise_questions = @exercise.exercise_questions exercise_questions = @exercise.exercise_questions
@exercise_questions = paginateHelper exercise_questions,5 #分页 @exercise_questions = paginateHelper exercise_questions,5 #分页
@ -145,7 +145,7 @@ class ExerciseController < ApplicationController
if @exercise_questions.save if @exercise_questions.save
standart_answer = ExerciseStandardAnswer.new standart_answer = ExerciseStandardAnswer.new
standart_answer.exercise_question_id = @exercise_questions.id 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 standart_answer.save
respond_to do |format| respond_to do |format|
format.js format.js
@ -239,6 +239,27 @@ class ExerciseController < ApplicationController
end end
private 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) def remove_invalid_exercise(course)
exercises = course.exercises.where("exercise_name=?","") exercises = course.exercises.where("exercise_name=?","")
unless exercises.empty? unless exercises.empty?

View File

@ -1,9 +1,14 @@
# encoding: utf-8 # encoding: utf-8
module ExerciseHelper module ExerciseHelper
# 单选
def translate_standard_answer(params)
answer = params.ord - 64
end
#判断用户是否已经提交了问卷 #判断用户是否已经提交了问卷
def has_commit_exercise?(exercise_id, user_id) 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? if pu.nil?
false false
else else