|
|
|
@ -1,9 +1,9 @@
|
|
|
|
|
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,:commit_answer]
|
|
|
|
|
before_filter :find_container, :only => [:new,:create, :index]
|
|
|
|
|
before_filter :is_member_of_course, :only => [:index,:show]
|
|
|
|
|
before_filter :is_course_teacher, :only => [:new,:create,:edit,:update,:destroy]
|
|
|
|
|
|
|
|
|
|
include PollHelper
|
|
|
|
|
def index
|
|
|
|
|
if @course
|
|
|
|
|
@is_teacher = User.current.allowed_to?(:as_teacher,@course)
|
|
|
|
@ -19,10 +19,16 @@ class PollController < ApplicationController
|
|
|
|
|
|
|
|
|
|
def show
|
|
|
|
|
@poll = Poll.find params[:id]
|
|
|
|
|
poll_questions = @poll.poll_questions
|
|
|
|
|
@poll_questions = paginateHelper poll_questions,3 #分页
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html {render :layout => 'base_courses'}
|
|
|
|
|
#已提交问卷的用户不能再访问该界面
|
|
|
|
|
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?
|
|
|
|
|
poll_questions = @poll.poll_questions
|
|
|
|
|
@poll_questions = paginateHelper poll_questions,3 #分页
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html {render :layout => 'base_courses'}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
@ -60,12 +66,14 @@ class PollController < ApplicationController
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
|
|
|
|
@poll.polls_name = params[:polls_name]
|
|
|
|
|
@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]
|
|
|
|
|
if @poll.save
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html { redirect_to poll_index_url(:polls_type => @course.class.to_s, :polls_group_id => @course.id) }
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
render_404
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
@ -79,10 +87,8 @@ class PollController < ApplicationController
|
|
|
|
|
|
|
|
|
|
def statistics_result
|
|
|
|
|
@poll = Poll.find(params[:id])
|
|
|
|
|
@offset, @limit = api_offset_and_limit({:limit => 10})
|
|
|
|
|
@poll_questions = @poll.poll_questions
|
|
|
|
|
@poll_questions_count = @poll_questions.count
|
|
|
|
|
@poll_questions_pages = Paginator.new @poll_questions_count, @limit, params['page']
|
|
|
|
|
poll_questions = @poll.poll_questions
|
|
|
|
|
@poll_questions = paginateHelper poll_questions,3 #分页
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html{render :layout => 'base_courses'}
|
|
|
|
|
end
|
|
|
|
@ -96,11 +102,12 @@ class PollController < ApplicationController
|
|
|
|
|
@every_answer_count = poll_answer.poll_votes.count
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#添加单选题
|
|
|
|
|
def create_poll_question
|
|
|
|
|
question_title = params[:poll_questions_title].nil? || params[:poll_questions_title].empty? ? l(:label_enter_single_title) : params[:poll_questions_title]
|
|
|
|
|
option = {
|
|
|
|
|
:is_necessary => params[:is_necessary] || true,
|
|
|
|
|
:is_necessary => (params[:is_necessary]=="true" ? 1 : 0),
|
|
|
|
|
:question_title => question_title,
|
|
|
|
|
:question_type => params[:question_type] || 1,
|
|
|
|
|
:question_number => @poll.poll_questions.count + 1
|
|
|
|
@ -111,7 +118,7 @@ class PollController < ApplicationController
|
|
|
|
|
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 => params[:question_answer].values[i-1]
|
|
|
|
|
:answer_text => answer
|
|
|
|
|
}
|
|
|
|
|
@poll_questions.poll_answers.new question_option
|
|
|
|
|
end
|
|
|
|
@ -123,6 +130,135 @@ class PollController < ApplicationController
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
#提交答案
|
|
|
|
|
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'
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
render :text => "failure"
|
|
|
|
|
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')
|
|
|
|
|
@status = 0 #提交成功
|
|
|
|
|
else
|
|
|
|
|
@status = 2 #未知错误
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
@status = 1 #有未做得必答题
|
|
|
|
|
end
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
def find_poll_and_course
|
|
|
|
|
@poll = Poll.find params[:id]
|
|
|
|
@ -152,4 +288,24 @@ class PollController < ApplicationController
|
|
|
|
|
render_403 unless(@course && User.current.allowed_to?(:as_teacher,@course))
|
|
|
|
|
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
|