From dd42c89ee6972d08237fe549dffe51503c4f279a Mon Sep 17 00:00:00 2001 From: z9hang Date: Fri, 16 Jan 2015 15:51:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BE=E5=88=86=E6=AF=94=E9=99=A4=E6=95=B0?= =?UTF-8?q?=E4=B8=BA0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/poll_controller.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/app/controllers/poll_controller.rb b/app/controllers/poll_controller.rb index d77f7c872..221c482c7 100644 --- a/app/controllers/poll_controller.rb +++ b/app/controllers/poll_controller.rb @@ -178,20 +178,26 @@ class PollController < ApplicationController #单选题 pv = PollVote.find_by_poll_question_id_and_user_id(params[:poll_question_id],User.current.id) if pv.nil? + #尚未答该题,添加答案 pv = PollVote.new pv.user_id = User.current.id pv.poll_question_id = params[:poll_question_id] end + #修改该题对应答案 pv.poll_answer_id = params[:poll_answer_id] if pv.save + #保存成功返回成功信息及当前以答题百分比 @percent = get_percent(@poll,User.current) render :json => {:text => "ok" ,:percent => format("%.2f" ,@percent)} else + #返回失败信息 render :json => {:text => "failure"} end elsif pq.question_type == 2 + #多选题 pv = PollVote.find_by_poll_answer_id_and_user_id(params[:poll_answer_id],User.current.id) if pv.nil? + #尚未答该题,添加答案 pv = PollVote.new pv.user_id = User.current.id pv.poll_question_id = params[:poll_question_id] @@ -203,6 +209,7 @@ class PollController < ApplicationController render :json => {:text => "failure"} end else + #pv不为空,则当前选项之前已被选择,再次点击则是不再选择该项,故删除该答案 if pv.delete @percent = get_percent(@poll,User.current) render :json => {:text => "false" ,:percent => format("%.2f" ,@percent)} @@ -211,12 +218,16 @@ class PollController < ApplicationController end end elsif pq.question_type == 3 || pq.question_type == 4 + #单行文本,多行文本题 pv = PollVote.find_by_poll_question_id_and_user_id(params[:poll_question_id],User.current.id) if pv.nil? + #pv为空之前尚未答题,添加答案 if params[:vote_text].nil? || params[:vote_text].blank? + #用户提交空答案,视作不作答 @percent = get_percent(@poll,User.current) render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)} else + #添加答案 pv = PollVote.new pv.user_id = User.current.id pv.poll_question_id = params[:poll_question_id] @@ -229,7 +240,9 @@ class PollController < ApplicationController end end else + #pv不为空说明用户之前已作答 if params[:vote_text].nil? || params[:vote_text].blank? + #用户提交空答案,视为删除答案 if pv.delete @percent = get_percent(@poll,User.current) render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)} @@ -237,6 +250,7 @@ class PollController < ApplicationController render :json => {:text => "failure"} end else + #用户修改答案 pv.vote_text = params[:vote_text] if pv.save @percent = get_percent(@poll,User.current) @@ -335,7 +349,11 @@ class PollController < ApplicationController def get_percent poll,user complete_count = get_complete_question(poll,user).count - (complete_count.to_f / poll.poll_questions.count.to_f)*100 + if poll.poll_questions.count == 0 + return 0 + else + return (complete_count.to_f / poll.poll_questions.count.to_f)*100 + end end #PollUser记录用户是否已提交问卷有对应的记录则已提交,没有则新建一个