添加和编辑题目

This commit is contained in:
huang 2015-11-17 18:20:47 +08:00
parent 651f6565be
commit 4a0865ab76
1 changed files with 62 additions and 22 deletions

View File

@ -21,7 +21,7 @@ class ExerciseController < ApplicationController
render_403 render_403
return return
end end
#已提交问卷的用户不能再访问该界面 # 已提交问卷的用户不能再访问该界面
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 poll_index_url(:course_id=> @course.id) redirect_to poll_index_url(:course_id=> @course.id)
else else
@ -67,20 +67,10 @@ class ExerciseController < ApplicationController
end end
def destroy def destroy
if @exercise && @exercise.destroy
if @is_teacher
polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id}")
else
polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id} and polls_status = 2")
end
@polls = paginateHelper polls,20 #分页
respond_to do |format|
format.js
end
end
end end
#统计结果 # 统计结果
def statistics_result def statistics_result
@exercise = Exercise.find(params[:id]) @exercise = Exercise.find(params[:id])
exercise_questions = @exercise.poll_questions exercise_questions = @exercise.poll_questions
@ -90,17 +80,16 @@ class ExerciseController < ApplicationController
end end
end end
#添加题目 # 添加题目
#question_type 1单选 2多选 3填空题 # question_type 1单选 2多选 3填空题
def create_exercise_question def create_exercise_question
question_title = params[:exercise_questions_title].nil? || params[:poll_questions_title].empty? ? l(:label_enter_single_title) : params[:poll_questions_title] question_title = params[:exercise_questions_title].nil? || params[:poll_questions_title].empty? ? l(:label_enter_single_title) : params[:poll_questions_title]
option = { option = {
:is_necessary => (params[:is_necessary]=="true" ? 1 : 0),
:question_title => question_title, :question_title => question_title,
:question_type => params[:question_type] || 1, :question_type => params[:question_type] || 1,
:question_number => @poll.poll_questions.count + 1 :question_number => @exercise.exercise_questions.count + 1
} }
@poll_questions = @poll.poll_questions.new option @exercise_questions = @exercise.exercise_questions.new option
if params[:question_answer] if params[:question_answer]
for i in 1..params[:question_answer].count 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] 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]
@ -108,15 +97,15 @@ class ExerciseController < ApplicationController
:answer_position => i, :answer_position => i,
:answer_text => answer :answer_text => answer
} }
@poll_questions.poll_answers.new question_option @exercise_questions.poll_answers.new question_option
end end
end end
# 如果是插入的话那么从插入的这个id以后的question_num都将要+1 # 如果是插入的话那么从插入的这个id以后的question_num都将要+1
if params[:quest_id] if params[:quest_id]
@is_insert = true @is_insert = true
@poll.poll_questions.where("question_number > #{params[:quest_num].to_i}").update_all(" question_number = question_number + 1") @exercise.exercise_questions.where("question_number > #{params[:quest_num].to_i}").update_all(" question_number = question_number + 1")
@poll_question_num = params[:quest_num].to_i # @exercise_question_num = params[:quest_num].to_i
@poll_questions.question_number = params[:quest_num].to_i + 1 @exercise_questions.question_number = params[:quest_num].to_i + 1
end end
if @poll_questions.save if @poll_questions.save
respond_to do |format| respond_to do |format|
@ -126,6 +115,57 @@ class ExerciseController < ApplicationController
end end
# 修改题目
# 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]
@exercise_questions.question_title = params[:exercise_questions_title].nil? || params[:exercise_questions_title].empty? ? l(:label_enter_single_title) : params[:exercise_questions_title]
################处理选项
if params[:question_answer]
@exercise_question.exercise_answers.each do |answer|
answer.destroy unless params[:question_answer].keys.include? answer.id.to_s
end
# 界面需要判断选择题至少有一个选项
for i in 1..params[:question_answer].count
question = @exercise_question.exercise_answers.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]
if question
question.exercise_choices_id = i
question.answer_text = answer
question.save
else
question_option = {
:exercise_choices_id => i,
:answer_text => answer
}
@exercise_question.exercise_answers.new question_option
end
end
end
@exercise_question.save
respond_to do |format|
format.js
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}")
poll_questions.each do |question|
question.question_number -= 1
question.save
end
if @poll_question && @poll_question.destroy
respond_to do |format|
format.js
end
end
end
#发布问卷 #发布问卷
def publish_excercise def publish_excercise
@exercise.exercise_status = 2 @exercise.exercise_status = 2