|
|
@ -1,9 +1,9 @@
|
|
|
|
class PollController < ApplicationController
|
|
|
|
class PollController < ApplicationController
|
|
|
|
before_filter :find_poll_and_course, :only => [:edit,:update,:destroy,:show,:statistics_result,:create_poll_question]
|
|
|
|
before_filter :find_poll_and_course, :only => [:edit,:update,:destroy,:show,:statistics_result,:create_poll_question,:commit_poll]
|
|
|
|
before_filter :find_container, :only => [:new,:create, :index]
|
|
|
|
before_filter :find_container, :only => [:new,:create, :index]
|
|
|
|
before_filter :is_member_of_course, :only => [:index,:show]
|
|
|
|
before_filter :is_member_of_course, :only => [:index,:show]
|
|
|
|
before_filter :is_course_teacher, :only => [:new,:create,:edit,:update,:destroy]
|
|
|
|
before_filter :is_course_teacher, :only => [:new,:create,:edit,:update,:destroy]
|
|
|
|
|
|
|
|
include PollHelper
|
|
|
|
def index
|
|
|
|
def index
|
|
|
|
if @course
|
|
|
|
if @course
|
|
|
|
@is_teacher = User.current.allowed_to?(:as_teacher,@course)
|
|
|
|
@is_teacher = User.current.allowed_to?(:as_teacher,@course)
|
|
|
@ -138,6 +138,85 @@ class PollController < ApplicationController
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#提交答案
|
|
|
|
|
|
|
|
def commit_answer
|
|
|
|
|
|
|
|
pq = PollQuestion.find(params[:poll_question_id])
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
render :text => "ok"
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
render :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]
|
|
|
|
|
|
|
|
pv.poll_answer_id = params[:poll_answer_id]
|
|
|
|
|
|
|
|
if pv.save
|
|
|
|
|
|
|
|
render :text => "true"
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
render :text => "failure"
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
if pv.delete
|
|
|
|
|
|
|
|
render :text => "false"
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
render :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
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
render :text => "failure"
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#respond_to do |format|
|
|
|
|
|
|
|
|
# format.js
|
|
|
|
|
|
|
|
# format.json
|
|
|
|
|
|
|
|
#end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#提交问卷
|
|
|
|
|
|
|
|
def commit_poll
|
|
|
|
|
|
|
|
@uncomplete_question = get_uncomplete_question(@poll)
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
|
|
|
format.js
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
private
|
|
|
|
def find_poll_and_course
|
|
|
|
def find_poll_and_course
|
|
|
|
@poll = Poll.find params[:id]
|
|
|
|
@poll = Poll.find params[:id]
|
|
|
@ -166,4 +245,25 @@ class PollController < ApplicationController
|
|
|
|
def is_course_teacher
|
|
|
|
def is_course_teacher
|
|
|
|
render_403 unless(@course && User.current.allowed_to?(:as_teacher,@course))
|
|
|
|
render_403 unless(@course && User.current.allowed_to?(:as_teacher,@course))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#获取未完成的题目
|
|
|
|
|
|
|
|
def get_uncomplete_question poll
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
uncomplete_question << question
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
uncomplete_question
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
end
|
|
|
|
end
|