|
|
|
@ -24,6 +24,7 @@ class PollController < ApplicationController
|
|
|
|
|
render_403
|
|
|
|
|
else
|
|
|
|
|
@can_edit_poll = (!has_commit_poll?(@poll.id,User.current.id)) || User.current.admin?
|
|
|
|
|
@percent = get_percent(@poll,User.current)
|
|
|
|
|
poll_questions = @poll.poll_questions
|
|
|
|
|
@poll_questions = paginateHelper poll_questions,3 #分页
|
|
|
|
|
respond_to do |format|
|
|
|
|
@ -172,7 +173,7 @@ class PollController < ApplicationController
|
|
|
|
|
def commit_answer
|
|
|
|
|
pq = PollQuestion.find(params[:poll_question_id])
|
|
|
|
|
if has_commit_poll?(@poll.id,User.current.id) && (!User.current.admin?)
|
|
|
|
|
render :text => 'failure'
|
|
|
|
|
render :json => {:text => "failure"}
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
if pq.question_type == 1
|
|
|
|
@ -185,9 +186,10 @@ class PollController < ApplicationController
|
|
|
|
|
end
|
|
|
|
|
pv.poll_answer_id = params[:poll_answer_id]
|
|
|
|
|
if pv.save
|
|
|
|
|
render :text => "ok"
|
|
|
|
|
@percent = get_percent(@poll,User.current)
|
|
|
|
|
render :json => {:text => "ok" ,:percent => format("%.2f" ,@percent)}
|
|
|
|
|
else
|
|
|
|
|
render :text => "failure"
|
|
|
|
|
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)
|
|
|
|
@ -197,38 +199,64 @@ class PollController < ApplicationController
|
|
|
|
|
pv.poll_question_id = params[:poll_question_id]
|
|
|
|
|
pv.poll_answer_id = params[:poll_answer_id]
|
|
|
|
|
if pv.save
|
|
|
|
|
render :text => "true"
|
|
|
|
|
@percent = get_percent(@poll,User.current)
|
|
|
|
|
render :json => {:text => "true",:percent => format("%.2f" ,@percent)}
|
|
|
|
|
else
|
|
|
|
|
render :text => "failure"
|
|
|
|
|
render :json => {:text => "failure"}
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
if pv.delete
|
|
|
|
|
render :text => "false"
|
|
|
|
|
@percent = get_percent(@poll,User.current)
|
|
|
|
|
render :json => {:text => "false" ,:percent => format("%.2f" ,@percent)}
|
|
|
|
|
else
|
|
|
|
|
render :text => "failure"
|
|
|
|
|
render :json => {:text => "failure"}
|
|
|
|
|
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 = PollVote.new
|
|
|
|
|
pv.user_id = User.current.id
|
|
|
|
|
pv.poll_question_id = params[:poll_question_id]
|
|
|
|
|
end
|
|
|
|
|
pv.vote_text = params[:vote_text]
|
|
|
|
|
if pv.save
|
|
|
|
|
render :text => pv.vote_text
|
|
|
|
|
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]
|
|
|
|
|
pv.vote_text = params[:vote_text]
|
|
|
|
|
if pv.save
|
|
|
|
|
@percent = get_percent(@poll,User.current)
|
|
|
|
|
render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
|
|
|
|
|
else
|
|
|
|
|
render :json => {:text => "failure"}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
render :text => "failure"
|
|
|
|
|
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)}
|
|
|
|
|
else
|
|
|
|
|
render :json => {:text => "failure"}
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
pv.vote_text = params[:vote_text]
|
|
|
|
|
if pv.save
|
|
|
|
|
@percent = get_percent(@poll,User.current)
|
|
|
|
|
render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
|
|
|
|
|
else
|
|
|
|
|
render :json => {:text => "failure"}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
render :text => "failure"
|
|
|
|
|
render :json => {:text => "failure"}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#提交问卷
|
|
|
|
|
def commit_poll
|
|
|
|
|
@uncomplete_question = get_uncomplete_question(@poll)
|
|
|
|
|
@uncomplete_question = get_uncomplete_question(@poll,User.current)
|
|
|
|
|
if @uncomplete_question.count < 1
|
|
|
|
|
pu = get_poll_user(@poll.id,User.current.id)
|
|
|
|
|
pu.user_id = User.current.id
|
|
|
|
@ -277,17 +305,41 @@ class PollController < ApplicationController
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#获取未完成的题目
|
|
|
|
|
def get_uncomplete_question poll
|
|
|
|
|
def get_uncomplete_question poll,user
|
|
|
|
|
necessary_questions = poll.poll_questions.where("#{PollQuestion.table_name}.is_necessary = 1")
|
|
|
|
|
uncomplete_question = []
|
|
|
|
|
necessary_questions.each do |question|
|
|
|
|
|
if question.poll_votes.nil? || question.poll_votes.count < 1
|
|
|
|
|
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
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_complete_question(poll,user)
|
|
|
|
|
questions = poll.poll_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
|
|
|
|
|
|
|
|
|
|
def get_percent poll,user
|
|
|
|
|
complete_count = get_complete_question(poll,user).count
|
|
|
|
|
(complete_count.to_f / poll.poll_questions.count.to_f)*100
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#PollUser记录用户是否已提交问卷有对应的记录则已提交,没有则新建一个
|
|
|
|
|
def get_poll_user poll_id,user_id
|
|
|
|
|
pu = PollUser.find_by_poll_id_and_user_id(poll_id,user_id)
|
|
|
|
|