diff --git a/app/controllers/exercise_controller.rb b/app/controllers/exercise_controller.rb index 0afc9dfda..a291a2229 100644 --- a/app/controllers/exercise_controller.rb +++ b/app/controllers/exercise_controller.rb @@ -1,7 +1,7 @@ class ExerciseController < ApplicationController layout "base_courses" - before_filter :find_exercise_and_course, :only => [:create_exercise_question, :edit, :update, :show, :destroy] + before_filter :find_exercise_and_course, :only => [:create_exercise_question, :edit, :update, :show, :destroy, :commit_exercise, :commit_answer] before_filter :find_course, :only => [:index,:new,:create,:student_exercise_list] include ExerciseHelper @@ -265,10 +265,163 @@ class ExerciseController < ApplicationController end end + # 学生提交答卷 + def commit_answer + eq = ExerciseQuestion.find(params[:poll_question_id]) + 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) + render :json => {:text => pv.vote_text,:percent => format("%.2f",@percent)} + 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) + else + # 答题过程中需要统计完成量 + @uncomplete_question = get_uncomplete_question(@exercise, User.current) + # 获取改学生的考试得分 + score = get_answer_score(@exercise) + 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 + eu.score = score + 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 + + private + # 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 + # 获取问题的答案 def get_user_answer(question,user) - user_answer = question.poll_votes.where("#{PollVote.table_name}.user_id = #{user.id}") + user_answer = question.exercise_answers.where("#{ExerciseAnswer.table_name}.user_id = #{user.id}") user_answer end @@ -285,7 +438,7 @@ class ExerciseController < ApplicationController complete_question end - + # 获取答题百分比 def get_percent exercise,user complete_count = get_complete_question(exercise,user).count if exercise.exercise_questions.count == 0 diff --git a/config/routes.rb b/config/routes.rb index 422f470d5..516b8977f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -153,6 +153,8 @@ RedmineApp::Application.routes.draw do get 'student_exercise_list' get 'export_exercise' post 'create_exercise_question' + post 'commit_answer' + post 'commit_exercise' end collection do #生成路径为 /exercise/方法名