2015-11-13 16:40:39 +08:00
|
|
|
|
class ExerciseController < ApplicationController
|
|
|
|
|
layout "base_courses"
|
|
|
|
|
|
2015-11-19 20:35:08 +08:00
|
|
|
|
before_filter :find_exercise_and_course, :only => [:create_exercise_question, :edit, :update, :show, :destroy, :commit_exercise, :commit_answer,:publish_exercise,:republish_exercise]
|
2015-11-17 19:47:48 +08:00
|
|
|
|
before_filter :find_course, :only => [:index,:new,:create,:student_exercise_list]
|
2015-11-18 13:55:42 +08:00
|
|
|
|
include ExerciseHelper
|
2015-11-17 19:29:25 +08:00
|
|
|
|
|
2015-11-18 10:49:57 +08:00
|
|
|
|
include ExerciseHelper
|
2015-11-13 16:40:39 +08:00
|
|
|
|
def index
|
2015-11-17 20:23:39 +08:00
|
|
|
|
remove_invalid_exercise(@course)
|
2015-11-13 17:23:52 +08:00
|
|
|
|
@is_teacher = User.current.allowed_to?(:as_teacher,@course)
|
|
|
|
|
if @is_teacher
|
|
|
|
|
exercises = @course.exercises
|
|
|
|
|
else
|
2015-11-17 19:47:48 +08:00
|
|
|
|
exercises = @course.exercises.where(:exercise_status => 2)
|
2015-11-13 17:23:52 +08:00
|
|
|
|
end
|
|
|
|
|
@exercises = paginateHelper exercises,20 #分页
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html
|
|
|
|
|
end
|
2015-11-13 16:40:39 +08:00
|
|
|
|
end
|
2015-11-13 17:23:52 +08:00
|
|
|
|
|
2015-11-13 16:40:39 +08:00
|
|
|
|
def show
|
2015-11-17 11:25:17 +08:00
|
|
|
|
@exercise = Exercise.find params[:id]
|
2015-11-19 17:18:58 +08:00
|
|
|
|
@is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin?
|
2015-11-17 11:25:17 +08:00
|
|
|
|
if @exercise.exercise_status != 2 && (!User.current.allowed_to?(:as_teacher,@course) || User.current.admin?)
|
|
|
|
|
render_403
|
|
|
|
|
return
|
|
|
|
|
end
|
2015-11-17 18:20:47 +08:00
|
|
|
|
# 已提交问卷的用户不能再访问该界面
|
2015-11-17 15:20:59 +08:00
|
|
|
|
if has_commit_exercise?(@exercise.id, User.current.id) && (!User.current.admin?)
|
2015-11-17 20:23:14 +08:00
|
|
|
|
redirect_to exercise_index_url(:course_id=> @course.id)
|
2015-11-17 11:25:17 +08:00
|
|
|
|
else
|
2015-11-18 13:55:42 +08:00
|
|
|
|
@can_edit_excercise = (!has_commit_exercise?(@exercise.id,User.current.id)) || User.current.admin?
|
2015-11-18 14:01:55 +08:00
|
|
|
|
# @percent = get_percent(@exercise,User.current)
|
2015-11-17 20:23:14 +08:00
|
|
|
|
exercise_questions = @exercise.exercise_questions
|
|
|
|
|
@exercise_questions = paginateHelper exercise_questions,5 #分页
|
2015-11-17 11:25:17 +08:00
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html {render :layout => 'base_courses'}
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-11-13 16:40:39 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def new
|
2015-11-17 19:29:25 +08:00
|
|
|
|
option = {
|
|
|
|
|
:exercise_name => "",
|
|
|
|
|
:course_id => @course.id,
|
|
|
|
|
:exercise_status => 1,
|
|
|
|
|
:user_id => User.current.id,
|
2015-11-17 20:23:14 +08:00
|
|
|
|
:time => "",
|
|
|
|
|
:end_time => "",
|
|
|
|
|
:publish_time => "",
|
2015-11-18 10:46:56 +08:00
|
|
|
|
:exercise_description => "",
|
|
|
|
|
:show_result => ""
|
2015-11-17 19:29:25 +08:00
|
|
|
|
}
|
|
|
|
|
@exercise = Exercise.create option
|
|
|
|
|
if @exercise
|
|
|
|
|
redirect_to edit_exercise_url @exercise.id
|
2015-11-17 11:25:17 +08:00
|
|
|
|
end
|
2015-11-13 16:40:39 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
2015-11-17 18:22:31 +08:00
|
|
|
|
if params[:exercise]
|
|
|
|
|
exercise = Exercise.find(params[:exercise_id]) if params[:exercise_id]
|
|
|
|
|
exercise ||= Exercise.new
|
|
|
|
|
exercise.exercise_name = params[:exercise][:exercise_name]
|
|
|
|
|
exercise.exercise_description = params[:exercise][:exercise_description]
|
|
|
|
|
exercise.end_time = params[:exercise][:end_time]
|
|
|
|
|
exercise.publish_time = params[:exercise][:publish_time]
|
|
|
|
|
exercise.user_id = User.current.id
|
|
|
|
|
exercise.time = params[:exercise][:time]
|
|
|
|
|
exercise.course_id = params[:course_id]
|
|
|
|
|
exercise.exercise_status = 1
|
|
|
|
|
if exercise.save
|
|
|
|
|
@exercise = exercise
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-11-13 16:40:39 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def edit
|
2015-11-17 15:18:07 +08:00
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html{render :layout => 'base_courses'}
|
2015-11-17 15:22:31 +08:00
|
|
|
|
end
|
2015-11-13 16:40:39 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
2015-11-17 19:29:25 +08:00
|
|
|
|
@exercise.exercise_name = params[:exercise][:exercise_name]
|
|
|
|
|
@exercise.exercise_description = params[:exercise][:exercise_description]
|
|
|
|
|
@exercise.time = params[:exercise][:time]
|
|
|
|
|
@exercise.end_time = params[:exercise][:end_time]
|
2015-11-17 20:23:14 +08:00
|
|
|
|
@exercise.publish_time = params[:exercise][:publish_time]
|
2015-11-18 14:05:29 +08:00
|
|
|
|
@exercise.show_result = params[:exercise][:show_result]
|
2015-11-17 11:25:17 +08:00
|
|
|
|
if @exercise.save
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
render_404
|
|
|
|
|
end
|
2015-11-13 16:40:39 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def destroy
|
2015-11-18 17:23:57 +08:00
|
|
|
|
if @exercise && @exercise.destroy
|
|
|
|
|
if @is_teacher
|
|
|
|
|
exercises = Exercise.where("course_id =?", @course.id)
|
|
|
|
|
else
|
|
|
|
|
exercises = Exercise.where("course_id =? and exercise_status =?", @course.id, 2)
|
|
|
|
|
end
|
|
|
|
|
@exercises = paginateHelper exercises,20 #分页
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-11-13 16:40:39 +08:00
|
|
|
|
end
|
|
|
|
|
|
2015-11-17 18:20:47 +08:00
|
|
|
|
# 统计结果
|
2015-11-13 17:23:52 +08:00
|
|
|
|
def statistics_result
|
2015-11-17 15:20:59 +08:00
|
|
|
|
@exercise = Exercise.find(params[:id])
|
2015-11-17 20:23:14 +08:00
|
|
|
|
exercise_questions = @exercise.exercise_questions
|
2015-11-17 15:20:59 +08:00
|
|
|
|
@exercise_questions = paginateHelper exercise_questions, 5
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html{render :layout => 'base_courses'}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-11-17 18:20:47 +08:00
|
|
|
|
# 添加题目
|
|
|
|
|
# question_type 1:单选 2:多选 3:填空题
|
2015-11-17 15:20:59 +08:00
|
|
|
|
def create_exercise_question
|
2015-11-17 19:49:33 +08:00
|
|
|
|
question_title = params[:question_title].nil? || params[:question_title].empty? ? l(:label_enter_single_title) : params[:question_title]
|
2015-11-17 15:20:59 +08:00
|
|
|
|
option = {
|
|
|
|
|
:question_title => question_title,
|
|
|
|
|
:question_type => params[:question_type] || 1,
|
2015-11-18 10:46:56 +08:00
|
|
|
|
:question_number => @exercise.exercise_questions.count + 1,
|
|
|
|
|
:question_score => params[:question_score]
|
2015-11-17 15:20:59 +08:00
|
|
|
|
}
|
2015-11-17 18:20:47 +08:00
|
|
|
|
@exercise_questions = @exercise.exercise_questions.new option
|
2015-11-18 18:28:17 +08:00
|
|
|
|
# params[:question_answer] 题目选项
|
2015-11-17 15:20:59 +08:00
|
|
|
|
if params[:question_answer]
|
|
|
|
|
for i in 1..params[:question_answer].count
|
|
|
|
|
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]
|
|
|
|
|
question_option = {
|
2015-11-18 10:46:56 +08:00
|
|
|
|
:choice_position => i,
|
|
|
|
|
:choice_text => answer
|
2015-11-17 15:20:59 +08:00
|
|
|
|
}
|
2015-11-18 10:46:56 +08:00
|
|
|
|
@exercise_questions.exercise_choices.new question_option
|
2015-11-17 15:20:59 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
# 如果是插入的话,那么从插入的这个id以后的question_num都将要+1
|
|
|
|
|
if params[:quest_id]
|
|
|
|
|
@is_insert = true
|
2015-11-17 18:20:47 +08:00
|
|
|
|
@exercise.exercise_questions.where("question_number > #{params[:quest_num].to_i}").update_all(" question_number = question_number + 1")
|
|
|
|
|
# @exercise_question_num = params[:quest_num].to_i
|
|
|
|
|
@exercise_questions.question_number = params[:quest_num].to_i + 1
|
2015-11-17 15:20:59 +08:00
|
|
|
|
end
|
2015-11-17 20:23:14 +08:00
|
|
|
|
if @exercise_questions.save
|
2015-11-18 18:28:17 +08:00
|
|
|
|
# params[:exercise_choice] 标准答案参数
|
2015-11-18 19:58:00 +08:00
|
|
|
|
# 问答题标准答案有三个,单独处理
|
|
|
|
|
if @exercise_questions.question_type == 3
|
|
|
|
|
for i in 1..params[:exercise_choice].count
|
|
|
|
|
standart_answer = ExerciseStandardAnswer.new
|
|
|
|
|
standart_answer.exercise_question_id = @exercise_questions.id
|
|
|
|
|
standart_answer.answer_text = params[:exercise_choice].values[i-1]
|
|
|
|
|
standart_answer.save
|
|
|
|
|
end
|
2015-11-18 16:06:34 +08:00
|
|
|
|
else
|
2015-11-18 19:58:00 +08:00
|
|
|
|
standart_answer = ExerciseStandardAnswer.new
|
|
|
|
|
standart_answer.exercise_question_id = @exercise_questions.id
|
|
|
|
|
if @exercise_questions.question_type == 1
|
|
|
|
|
standart_answer.exercise_choice_id = sigle_selection_standard_answer(params[:exercise_choice])
|
|
|
|
|
else
|
|
|
|
|
standart_answer.exercise_choice_id = multiselect_standard_answer(params[:exercise_choice])
|
|
|
|
|
end
|
|
|
|
|
standart_answer.save
|
2015-11-18 16:06:34 +08:00
|
|
|
|
end
|
2015-11-17 15:20:59 +08:00
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
2015-11-13 17:23:52 +08:00
|
|
|
|
|
2015-11-17 18:20:47 +08:00
|
|
|
|
# 修改题目
|
|
|
|
|
# params[:exercise_question] The id of exercise_question
|
|
|
|
|
# params[:question_answer] eg:A、B、C选项
|
|
|
|
|
def update_exercise_question
|
|
|
|
|
@exercise_question = ExerciseQuestion.find params[:exercise_question]
|
2015-11-18 19:58:00 +08:00
|
|
|
|
@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]
|
2015-11-19 16:21:31 +08:00
|
|
|
|
# 处理选项:如果选了某个选项,那么则要删除之前的选项
|
2015-11-17 18:20:47 +08:00
|
|
|
|
if params[:question_answer]
|
2015-11-19 11:38:44 +08:00
|
|
|
|
@exercise_question.exercise_choices.each do |answer|
|
|
|
|
|
answer.destroy unless params[:question_answer].keys.include? answer.id.to_s
|
|
|
|
|
end
|
2015-11-19 14:26:51 +08:00
|
|
|
|
for i in 1..params[:question_answer].count
|
2015-11-18 19:58:00 +08:00
|
|
|
|
question = @exercise_question.exercise_choices.find_by_id params[:question_answer].keys[i-1]
|
2015-11-19 14:26:51 +08:00
|
|
|
|
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]
|
2015-11-17 18:20:47 +08:00
|
|
|
|
if question
|
2015-11-19 16:21:31 +08:00
|
|
|
|
question.choice_position = i
|
|
|
|
|
question.choice_text = answer
|
2015-11-17 18:20:47 +08:00
|
|
|
|
question.save
|
|
|
|
|
else
|
|
|
|
|
question_option = {
|
2015-11-19 16:21:31 +08:00
|
|
|
|
:choice_position => i,
|
|
|
|
|
:choice_text => answer
|
2015-11-17 18:20:47 +08:00
|
|
|
|
}
|
2015-11-19 16:21:31 +08:00
|
|
|
|
@exercise_question.exercise_choices.new question_option
|
2015-11-17 18:20:47 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-11-19 16:21:31 +08:00
|
|
|
|
# 更新标准答案
|
|
|
|
|
if params[:exercise_choice]
|
|
|
|
|
if @exercise_question.question_type == 3
|
2015-11-19 17:36:58 +08:00
|
|
|
|
# 删除不合理的选项
|
|
|
|
|
@exercise_question.exercise_standard_answers.each do |answer|
|
|
|
|
|
answer.destroy unless params[:exercise_choice].keys.include? answer.id.to_s
|
|
|
|
|
end
|
2015-11-19 16:21:31 +08:00
|
|
|
|
for i in 1..params[:exercise_choice].count
|
2015-11-19 16:38:46 +08:00
|
|
|
|
# 找到对应的标准答案
|
|
|
|
|
question_standart = @exercise_question.exercise_standard_answers.find_by_id params[:exercise_choice].keys[i-1]
|
|
|
|
|
# 标准答案值
|
|
|
|
|
answer_standart = (params[:exercise_choice].values[i-1].nil? || params[:exercise_choice].values[i-1].empty?) ? l(:label_new_answer) : params[:exercise_choice].values[i-1]
|
|
|
|
|
if question_standart
|
|
|
|
|
question_standart.answer_text = answer_standart
|
|
|
|
|
question_standart.save
|
|
|
|
|
else
|
|
|
|
|
standart_answer_option = {
|
2015-11-19 17:31:26 +08:00
|
|
|
|
:answer_text => answer_standart
|
2015-11-19 16:38:46 +08:00
|
|
|
|
}
|
|
|
|
|
@exercise_question.exercise_standard_answers.new standart_answer_option
|
|
|
|
|
end
|
2015-11-19 16:21:31 +08:00
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
answer_standart = @exercise_question.exercise_standard_answers.first
|
2015-11-19 17:09:59 +08:00
|
|
|
|
answer_standart.exercise_choice_id = @exercise_question.question_type == 1 ? sigle_selection_standard_answer(params[:exercise_choice]) : multiselect_standard_answer(params[:exercise_choice])
|
2015-11-19 16:21:31 +08:00
|
|
|
|
answer_standart.save
|
|
|
|
|
end
|
|
|
|
|
@exercise_question.save
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
|
|
|
|
end
|
2015-11-17 18:20:47 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 删除题目
|
|
|
|
|
def delete_exercise_question
|
|
|
|
|
@exercise_question = ExerciseQuestion.find params[:exercise_question]
|
|
|
|
|
@exercise = @exercise_question.exercise
|
|
|
|
|
exercise_questions = @exercise.exercise_questions.where("question_number > #{@exercise_question.question_number}")
|
2015-11-17 20:23:14 +08:00
|
|
|
|
exercise_questions.each do |question|
|
2015-11-17 18:20:47 +08:00
|
|
|
|
question.question_number -= 1
|
|
|
|
|
question.save
|
|
|
|
|
end
|
2015-11-17 18:34:01 +08:00
|
|
|
|
if @exercise_question && @exercise_question.destroy
|
2015-11-17 18:20:47 +08:00
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-11-19 16:21:31 +08:00
|
|
|
|
# 发布试卷
|
2015-11-19 20:35:08 +08:00
|
|
|
|
def publish_exercise
|
2015-11-17 15:20:59 +08:00
|
|
|
|
@exercise.exercise_status = 2
|
|
|
|
|
@exercise.publish_time = Time.now
|
|
|
|
|
if @exercise.save
|
|
|
|
|
if params[:is_remote]
|
2015-11-17 20:23:14 +08:00
|
|
|
|
redirect_to exercise_index_url(:course_id => @course.id)
|
2015-11-17 15:20:59 +08:00
|
|
|
|
else
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-11-13 17:23:52 +08:00
|
|
|
|
end
|
|
|
|
|
|
2015-11-19 16:21:31 +08:00
|
|
|
|
# 重新发布试卷
|
2015-11-19 20:35:08 +08:00
|
|
|
|
# 重新发布的时候会删除所有的答题
|
|
|
|
|
def republish_exercise
|
2015-11-19 16:21:31 +08:00
|
|
|
|
@exercise.exercise_questions.each do |exercise_question|
|
|
|
|
|
exercise_question.exercise_ansers.destroy_all
|
|
|
|
|
end
|
2015-11-19 20:35:08 +08:00
|
|
|
|
@exercise.exercise_users.destroy_all
|
|
|
|
|
@exercise.exercise_status = 1
|
|
|
|
|
@exercise.save
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
|
|
|
|
end
|
2015-11-19 16:21:31 +08:00
|
|
|
|
end
|
|
|
|
|
|
2015-11-17 09:33:40 +08:00
|
|
|
|
def student_exercise_list
|
|
|
|
|
@is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin?
|
|
|
|
|
@exercise = Exercise.find params[:id]
|
|
|
|
|
@all_exercises = @course.exercises.order("created_at desc")
|
|
|
|
|
@exercise_count = @exercise.exercise_users.where('score is not NULL').count
|
|
|
|
|
if @is_teacher || (!@exercise.exercise_users.where(:user_id => User.current.id).empty? && Time.parse(@exercise.end_time.to_s).strftime("%Y-%m-%d-%H-%M-%S") <= Time.now.strftime("%Y-%m-%d-%H-%M-%S"))
|
|
|
|
|
@exercise_users_list = @exercise.exercise_users.where('score is not NULL')
|
2015-11-17 15:18:07 +08:00
|
|
|
|
@show_all = true;
|
2015-11-17 09:33:40 +08:00
|
|
|
|
elsif !@exercise.exercise_users.where(:user_id => User.current.id).empty? && Time.parse(@exercise.end_time.to_s).strftime("%Y-%m-%d-%H-%M-%S") > Time.now.strftime("%Y-%m-%d-%H-%M-%S")
|
|
|
|
|
@exercise_users_list = @exercise.exercise_users.where("user_id = ? and score is not NULL",User.current.id)
|
|
|
|
|
else
|
|
|
|
|
@exercise_users_list = []
|
|
|
|
|
end
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-11-19 16:21:31 +08:00
|
|
|
|
# 学生提交答卷,选中答案的过程中提交
|
2015-11-18 21:16:24 +08:00
|
|
|
|
def commit_answer
|
2015-11-19 11:38:44 +08:00
|
|
|
|
eq = ExerciseQuestion.find(params[:exercise_question_id])
|
|
|
|
|
# 已提交过的则不允许答题
|
2015-11-18 21:16:24 +08:00
|
|
|
|
if has_commit_exercise?(@exercise.id,User.current.id) && (!User.current.admin?)
|
|
|
|
|
render :json => {:text => "failure"}
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
if eq.question_type == 1
|
|
|
|
|
# 单选题
|
|
|
|
|
ea = ExerciseAnswer.find_by_exercise_question_id_and_user_id(params[:exercise_question_id],User.current.id)
|
|
|
|
|
if ea.nil?
|
|
|
|
|
# 尚未答该题,添加答案
|
|
|
|
|
ea = ExerciseAnswer.new
|
|
|
|
|
ea.user_id = User.current.id
|
|
|
|
|
ea.exercise_question_id = params[:exercise_question_id]
|
|
|
|
|
end
|
|
|
|
|
#修改该题对应答案
|
|
|
|
|
ea.exercise_choice_id = params[:exercise_choice_id]
|
|
|
|
|
if ea.save
|
|
|
|
|
# 保存成功返回成功信息及当前以答题百分比
|
|
|
|
|
@percent = get_percent(@exercise,User.current)
|
|
|
|
|
render :json => {:text => "ok" ,:percent => format("%.2f" ,@percent)}
|
|
|
|
|
else
|
|
|
|
|
#返回失败信息
|
|
|
|
|
render :json => {:text => "failure"}
|
|
|
|
|
end
|
|
|
|
|
elsif eq.question_type == 2
|
|
|
|
|
#多选题
|
|
|
|
|
ea = ExerciseAnswer.find_by_exercise_question_id_and_user_id(params[:exercise_question_id],User.current.id)
|
|
|
|
|
if ea.nil?
|
|
|
|
|
#尚未答该题,添加答案
|
|
|
|
|
ea = ExerciseAnswer.new
|
|
|
|
|
ea.user_id = User.current.id
|
|
|
|
|
ea.exercise_question_id = params[:exercise_question_id]
|
|
|
|
|
ea.exercise_choice_id = params[:exercise_choice_id]
|
|
|
|
|
if ea.save
|
|
|
|
|
@percent = get_percent(@exercise,User.current)
|
|
|
|
|
render :json => {:text => "true",:percent => format("%.2f" ,@percent)}
|
|
|
|
|
else
|
|
|
|
|
render :json => {:text => "failure"}
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
#pv不为空,则当前选项之前已被选择,再次点击则是不再选择该项,故删除该答案
|
|
|
|
|
if pv.delete
|
|
|
|
|
@percent = get_percent(@exercise, User.current)
|
|
|
|
|
render :json => {:text => "false" ,:percent => format("%.2f" , @percent)}
|
|
|
|
|
else
|
|
|
|
|
render :json => {:text => "failure"}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
elsif eq.question_type == 3
|
|
|
|
|
#单行文本,多行文本题
|
|
|
|
|
ea = ExerciseAnswer.find_by_exercise_question_id_and_user_id(params[:exercise_question_id], User.current.id)
|
|
|
|
|
if ea.nil?
|
|
|
|
|
# ea为空之前尚未答题,添加答案
|
|
|
|
|
if params[:answer_text].nil? || params[:answer_text].blank?
|
|
|
|
|
#用户提交空答案,视作不作答
|
|
|
|
|
@percent = get_percent(@exercise,User.current)
|
|
|
|
|
render :json => {:text => ea.answer_text,:percent => format("%.2f", @percent)}
|
|
|
|
|
else
|
|
|
|
|
#添加答案
|
|
|
|
|
ea = ExerciseAnswer.new
|
|
|
|
|
ea.user_id = User.current.id
|
|
|
|
|
ea.exercise_question_id = params[:exercise_question_id]
|
|
|
|
|
ea.answer_text = params[:answer_text]
|
|
|
|
|
if ea.save
|
|
|
|
|
@percent = get_percent(@exercise,User.current)
|
2015-11-19 21:19:18 +08:00
|
|
|
|
render :json => {:text => ea.answer_text,:percent => format("%.2f",@percent)}
|
2015-11-18 21:16:24 +08:00
|
|
|
|
else
|
|
|
|
|
render :json => {:text => "failure"}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
# ea不为空说明用户之前已作答
|
|
|
|
|
if params[:answer_text].nil? || params[:answer_text].blank?
|
|
|
|
|
# 用户提交空答案,视为删除答案
|
|
|
|
|
if ea.delete
|
|
|
|
|
@percent = get_percent(@exercise,User.current)
|
|
|
|
|
render :json => {:text => ea.answer_text,:percent => format("%.2f", @percent)}
|
|
|
|
|
else
|
|
|
|
|
render :json => {:text => "failure"}
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
#用户修改答案
|
|
|
|
|
ea.answer_text = params[:answer_text]
|
|
|
|
|
if ea.save
|
|
|
|
|
@percent = get_percent(@exercise,User.current)
|
|
|
|
|
render :json => {:text => pv.vote_text,:percent => format("%.2f", @percent)}
|
|
|
|
|
else
|
|
|
|
|
render :json => {:text => "failure"}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
render :json => {:text => "failure"}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 提交问卷
|
|
|
|
|
def commit_exercise
|
|
|
|
|
# 老师不需要提交
|
|
|
|
|
if User.current.allowed_to?(:as_teacher,@course)
|
|
|
|
|
redirect_to exercise_url(@exercise)
|
2015-11-19 11:38:44 +08:00
|
|
|
|
# REDO: 提示提交成功
|
2015-11-18 21:16:24 +08:00
|
|
|
|
else
|
|
|
|
|
# 答题过程中需要统计完成量
|
|
|
|
|
@uncomplete_question = get_uncomplete_question(@exercise, User.current)
|
|
|
|
|
# 获取改学生的考试得分
|
2015-11-19 21:19:18 +08:00
|
|
|
|
@score = calculate_student_score(@exercise, User.current)
|
2015-11-18 21:16:24 +08:00
|
|
|
|
if @uncomplete_question.count < 1
|
|
|
|
|
# 查看是否有已提交记录
|
|
|
|
|
eu = get_exercise_user(@exercise.id, User.current.id)
|
|
|
|
|
eu.user_id = User.current.id
|
|
|
|
|
eu.exercise_id = @exercise.id
|
2015-11-19 21:19:18 +08:00
|
|
|
|
eu.score = @score
|
2015-11-18 21:16:24 +08:00
|
|
|
|
if eu.save
|
|
|
|
|
#redirect_to poll_index_path(:polls_group_id => @course.id,:polls_type => 'Course')
|
|
|
|
|
@status = 0 #提交成功
|
|
|
|
|
else
|
|
|
|
|
@status = 2 #未知错误
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
@status = 1 #有未做得必答题
|
|
|
|
|
end
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-11-19 11:38:44 +08:00
|
|
|
|
# 计算学生得分
|
|
|
|
|
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)
|
2015-11-19 21:21:02 +08:00
|
|
|
|
score += question.question_score unless question.question_score.empty?
|
2015-11-19 11:38:44 +08:00
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
if answer.exercise_choice_id == standard_answer.exercise_choice_id
|
2015-11-19 21:21:02 +08:00
|
|
|
|
score += question.question_score unless question.question_score.empty?
|
2015-11-19 11:38:44 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
score
|
|
|
|
|
end
|
2015-11-18 21:16:24 +08:00
|
|
|
|
|
2015-11-13 16:40:39 +08:00
|
|
|
|
private
|
2015-11-18 21:16:24 +08:00
|
|
|
|
# ExerciseUser记录用户是否已提交问卷有对应的记录则已提交,没有则新建一个
|
|
|
|
|
def get_exercise_user exercise_id,user_id
|
|
|
|
|
eu = ExerciseUser.find_by_exercise_id_and_user_id(exercise_id,user_id)
|
|
|
|
|
if eu.nil?
|
|
|
|
|
eu = ExerciseUser.new
|
|
|
|
|
end
|
|
|
|
|
eu
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#获取未完成的题目
|
|
|
|
|
def get_uncomplete_question exercise,user
|
|
|
|
|
all_questions = exercise.exercise_questions
|
|
|
|
|
uncomplete_question = []
|
|
|
|
|
all_questions.each do |question|
|
|
|
|
|
answers = get_user_answer(question, user)
|
|
|
|
|
if answers.nil? || answers.count < 1
|
|
|
|
|
uncomplete_question << question
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
uncomplete_question
|
|
|
|
|
end
|
|
|
|
|
|
2015-11-19 11:38:44 +08:00
|
|
|
|
# 获取当前学生回答问题的答案
|
2015-11-18 14:01:55 +08:00
|
|
|
|
def get_user_answer(question,user)
|
2015-11-18 21:16:24 +08:00
|
|
|
|
user_answer = question.exercise_answers.where("#{ExerciseAnswer.table_name}.user_id = #{user.id}")
|
2015-11-18 14:01:55 +08:00
|
|
|
|
user_answer
|
|
|
|
|
end
|
|
|
|
|
|
2015-11-19 11:38:44 +08:00
|
|
|
|
# 获取问题的标准答案
|
|
|
|
|
def get_user_standard_answer(question,user)
|
|
|
|
|
standard_answer = question.exercise_standard_answers
|
|
|
|
|
standard_answer
|
|
|
|
|
end
|
|
|
|
|
|
2015-11-18 14:01:55 +08:00
|
|
|
|
# 是否完成了答题
|
2015-11-18 13:55:42 +08:00
|
|
|
|
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
|
|
|
|
|
|
2015-11-18 21:16:24 +08:00
|
|
|
|
# 获取答题百分比
|
2015-11-18 13:55:42 +08:00
|
|
|
|
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
|
|
|
|
|
|
2015-11-17 20:23:39 +08:00
|
|
|
|
def remove_invalid_exercise(course)
|
|
|
|
|
exercises = course.exercises.where("exercise_name=?","")
|
|
|
|
|
unless exercises.empty?
|
|
|
|
|
exercises.each do |exercise|
|
|
|
|
|
exercise.destroy
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-11-17 19:47:48 +08:00
|
|
|
|
end
|
|
|
|
|
|
2015-11-17 19:29:25 +08:00
|
|
|
|
def find_exercise_and_course
|
|
|
|
|
@exercise = Exercise.find params[:id]
|
|
|
|
|
@course = Course.find @exercise.course_id
|
|
|
|
|
rescue Exception => e
|
|
|
|
|
render_404
|
|
|
|
|
end
|
|
|
|
|
|
2015-11-13 16:40:39 +08:00
|
|
|
|
def find_course
|
|
|
|
|
@course = Course.find params[:course_id]
|
|
|
|
|
rescue Exception => e
|
|
|
|
|
render_404
|
|
|
|
|
end
|
|
|
|
|
end
|