socialforge/app/controllers/poll_controller.rb

348 lines
11 KiB
Ruby
Raw Normal View History

2015-01-12 16:56:53 +08:00
class PollController < ApplicationController
before_filter :find_poll_and_course, :only => [:edit,:update,:destroy,:show,:statistics_result,:create_poll_question,:commit_poll,:commit_answer]
2015-01-12 16:56:53 +08:00
before_filter :find_container, :only => [:new,:create, :index]
2015-01-13 10:23:26 +08:00
before_filter :is_member_of_course, :only => [:index,:show]
before_filter :is_course_teacher, :only => [:new,:create,:edit,:update,:destroy]
2015-01-15 09:41:19 +08:00
include PollHelper
2015-01-12 16:56:53 +08:00
def index
if @course
@is_teacher = User.current.allowed_to?(:as_teacher,@course)
polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id}")
@polls = paginateHelper polls,10 #分页
2015-01-12 16:56:53 +08:00
respond_to do |format|
format.html{render :layout => 'base_courses'}
end
elsif @project
#项目的问卷调查相关代码
end
end
def show
@poll = Poll.find params[:id]
#已提交问卷的用户不能再访问该界面
if has_commit_poll?(@poll.id,User.current.id) && (!User.current.admin?)
render_403
else
@can_edit_poll = (!has_commit_poll?(@poll.id,User.current.id)) || User.current.admin?
2015-01-16 15:32:09 +08:00
@percent = get_percent(@poll,User.current)
poll_questions = @poll.poll_questions
@poll_questions = paginateHelper poll_questions,3 #分页
respond_to do |format|
format.html {render :layout => 'base_courses'}
end
end
2015-01-12 16:56:53 +08:00
end
def new
if @course
option = {
:polls_name => l(:label_poll_new),
:polls_type => @course.class.to_s,
:polls_group_id => @course.id,
:polls_status => 1,
:user_id => User.current.id,
:published_at => Time.now,
:closed_at => Time.now,
:polls_description => ""
}
@poll = Poll.create option
if @poll
redirect_to edit_poll_url @poll.id
2015-01-12 16:56:53 +08:00
end
elsif @project
#项目的问卷调查相关代码
end
end
def create
end
def edit
respond_to do |format|
format.html{render :layout => 'base_courses'}
end
2015-01-12 16:56:53 +08:00
end
def update
@poll.polls_name = params[:polls_name].empty? ? l(:label_poll_title) : params[:polls_name]
@poll.polls_description = params[:polls_description].empty? ? l(:label_poll_description) : params[:polls_description]
2015-01-12 16:56:53 +08:00
if @poll.save
respond_to do |format|
format.js
2015-01-12 16:56:53 +08:00
end
else
render_404
2015-01-12 16:56:53 +08:00
end
end
def destroy
if @poll && @poll.destroy
2015-01-12 16:56:53 +08:00
respond_to do |format|
format.js
end
end
end
def statistics_result
respond_to do |format|
format.html{render :layout => 'base_courses'}
end
end
#添加单选题
def create_poll_question
2015-01-14 15:35:04 +08:00
question_title = params[:poll_questions_title].nil? || params[:poll_questions_title].empty? ? l(:label_enter_single_title) : params[:poll_questions_title]
option = {
2015-01-15 16:50:06 +08:00
:is_necessary => (params[:is_necessary]=="true" ? 1 : 0),
2015-01-14 15:35:04 +08:00
:question_title => question_title,
:question_type => params[:question_type] || 1,
:question_number => @poll.poll_questions.count + 1
2015-01-14 15:35:04 +08:00
}
@poll_questions = @poll.poll_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]
question_option = {
:answer_position => i,
:answer_text => answer
2015-01-14 15:35:04 +08:00
}
@poll_questions.poll_answers.new question_option
end
end
if @poll_questions.save
respond_to do |format|
format.js
end
end
end
#修改单选题
def update_poll_question
@poll_question = PollQuestion.find params[:poll_question]
#@poll = @poll_question.poll
@poll_question.is_necessary = params[:is_necessary]=="true" ? 1 : 0
@poll_question.question_title = params[:poll_questions_title].nil? || params[:poll_questions_title].empty? ? l(:label_enter_single_title) : params[:poll_questions_title]
################处理选项
if params[:question_answer]
@poll_question.poll_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 = @poll_question.poll_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.answer_position = i
question.answer_text = answer
question.save
else
question_option = {
:answer_position => i,
:answer_text => answer
}
@poll_question.poll_answers.new question_option
end
end
end
@poll_question.save
respond_to do |format|
format.js
end
end
#删除单选题
def delete_poll_question
@poll_question = PollQuestion.find params[:poll_question]
@poll = @poll_question.poll
poll_questions = @poll.poll_questions.where("question_number > #{@poll_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
2015-01-15 09:41:19 +08:00
#提交答案
def commit_answer
2015-01-15 11:54:30 +08:00
pq = PollQuestion.find(params[:poll_question_id])
if has_commit_poll?(@poll.id,User.current.id) && (!User.current.admin?)
2015-01-16 15:32:09 +08:00
render :json => {:text => "failure"}
return
end
2015-01-15 11:54:30 +08:00
if pq.question_type == 1
#单选题
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
2015-01-16 15:32:09 +08:00
@percent = get_percent(@poll,User.current)
render :json => {:text => "ok" ,:percent => format("%.2f" ,@percent)}
2015-01-15 11:54:30 +08:00
else
2015-01-16 15:32:09 +08:00
render :json => {:text => "failure"}
2015-01-15 11:54:30 +08:00
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]
pv.poll_answer_id = params[:poll_answer_id]
if pv.save
2015-01-16 15:32:09 +08:00
@percent = get_percent(@poll,User.current)
render :json => {:text => "true",:percent => format("%.2f" ,@percent)}
2015-01-15 11:54:30 +08:00
else
2015-01-16 15:32:09 +08:00
render :json => {:text => "failure"}
2015-01-15 11:54:30 +08:00
end
else
if pv.delete
2015-01-16 15:32:09 +08:00
@percent = get_percent(@poll,User.current)
render :json => {:text => "false" ,:percent => format("%.2f" ,@percent)}
2015-01-15 11:54:30 +08:00
else
2015-01-16 15:32:09 +08:00
render :json => {:text => "failure"}
2015-01-15 11:54:30 +08:00
end
end
2015-01-15 17:36:30 +08:00
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?
2015-01-16 15:32:09 +08:00
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
2015-01-15 17:36:30 +08:00
else
2015-01-16 15:32:09 +08:00
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
2015-01-15 17:36:30 +08:00
end
2015-01-16 15:32:09 +08:00
2015-01-15 17:36:30 +08:00
else
2015-01-16 15:32:09 +08:00
render :json => {:text => "failure"}
2015-01-15 09:41:19 +08:00
end
end
2015-01-15 17:36:30 +08:00
#提交问卷
def commit_poll
2015-01-16 15:32:09 +08:00
@uncomplete_question = get_uncomplete_question(@poll,User.current)
2015-01-15 17:36:30 +08:00
if @uncomplete_question.count < 1
pu = get_poll_user(@poll.id,User.current.id)
pu.user_id = User.current.id
pu.poll_id = @poll.id
if pu.save
#redirect_to poll_index_path(:polls_group_id => @course.id,:polls_type => 'Course')
2015-01-15 18:22:30 +08:00
@status = 0 #提交成功
else
@status = 2 #未知错误
2015-01-15 17:36:30 +08:00
end
else
2015-01-15 18:22:30 +08:00
@status = 1 #有未做得必答题
2015-01-15 17:36:30 +08:00
end
respond_to do |format|
format.js
end
end
2015-01-12 16:56:53 +08:00
private
def find_poll_and_course
@poll = Poll.find params[:id]
@course = Course.find @poll.polls_group_id
rescue Exception => e
render_404
end
def find_container
if params[:polls_type] && params[:polls_group_id]
case params[:polls_type]
when "Course"
@course = Course.find_by_id params[:polls_group_id]
when "Project"
@project = Project.find_by_id params[:polls_group_id]
end
else
render_404
end
end
2015-01-13 10:23:26 +08:00
def is_member_of_course
render_403 unless(@course && User.current.member_of_course?(@course))
end
def is_course_teacher
render_403 unless(@course && User.current.allowed_to?(:as_teacher,@course))
2015-01-13 10:23:26 +08:00
end
2015-01-15 17:36:30 +08:00
#获取未完成的题目
2015-01-16 15:32:09 +08:00
def get_uncomplete_question poll,user
2015-01-15 17:36:30 +08:00
necessary_questions = poll.poll_questions.where("#{PollQuestion.table_name}.is_necessary = 1")
uncomplete_question = []
necessary_questions.each do |question|
2015-01-16 15:32:09 +08:00
answers = get_user_answer(question,user)
if answers.nil? || answers.count < 1
2015-01-15 17:36:30 +08:00
uncomplete_question << question
end
end
uncomplete_question
end
2015-01-16 15:32:09 +08:00
#获取用户对某个问题的答案
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
2015-01-15 17:36:30 +08:00
#PollUser记录用户是否已提交问卷有对应的记录则已提交没有则新建一个
def get_poll_user poll_id,user_id
pu = PollUser.find_by_poll_id_and_user_id(poll_id,user_id)
if pu.nil?
pu = PollUser.new
end
pu
end
2015-01-12 16:56:53 +08:00
end