From 4a0865ab76a55b9aa36da8109b9509a846ec14d8 Mon Sep 17 00:00:00 2001 From: huang Date: Tue, 17 Nov 2015 18:20:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=92=8C=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/exercise_controller.rb | 84 +++++++++++++++++++------- 1 file changed, 62 insertions(+), 22 deletions(-) diff --git a/app/controllers/exercise_controller.rb b/app/controllers/exercise_controller.rb index 3870fe9c5..2237a9cfe 100644 --- a/app/controllers/exercise_controller.rb +++ b/app/controllers/exercise_controller.rb @@ -21,7 +21,7 @@ class ExerciseController < ApplicationController render_403 return end - #已提交问卷的用户不能再访问该界面 + # 已提交问卷的用户不能再访问该界面 if has_commit_exercise?(@exercise.id, User.current.id) && (!User.current.admin?) redirect_to poll_index_url(:course_id=> @course.id) else @@ -67,20 +67,10 @@ class ExerciseController < ApplicationController end 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 - #统计结果 + # 统计结果 def statistics_result @exercise = Exercise.find(params[:id]) exercise_questions = @exercise.poll_questions @@ -90,17 +80,16 @@ class ExerciseController < ApplicationController end end - #添加题目 - #question_type 1:单选 2:多选 3:填空题 + # 添加题目 + # question_type 1:单选 2:多选 3:填空题 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] option = { - :is_necessary => (params[:is_necessary]=="true" ? 1 : 0), :question_title => question_title, :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] 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] @@ -108,15 +97,15 @@ class ExerciseController < ApplicationController :answer_position => i, :answer_text => answer } - @poll_questions.poll_answers.new question_option + @exercise_questions.poll_answers.new question_option end end # 如果是插入的话,那么从插入的这个id以后的question_num都将要+1 if params[:quest_id] @is_insert = true - @poll.poll_questions.where("question_number > #{params[:quest_num].to_i}").update_all(" question_number = question_number + 1") - @poll_question_num = params[:quest_num].to_i - @poll_questions.question_number = params[:quest_num].to_i + 1 + @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 end if @poll_questions.save respond_to do |format| @@ -126,6 +115,57 @@ class ExerciseController < ApplicationController 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 @exercise.exercise_status = 2