socialforge/app/controllers/poll_controller.rb

143 lines
4.0 KiB
Ruby

class PollController < ApplicationController
before_filter :find_poll_and_course, :only => [:edit,:update,:destroy,:show,:statistics_result,:create_poll_question]
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]
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 #分页
respond_to do |format|
format.html{render :layout => 'base_courses'}
end
elsif @project
#项目的问卷调查相关代码
end
end
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'}
end
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
respond_to do |format|
format.html{render :layout => 'base_courses'}
end
end
elsif @project
#项目的问卷调查相关代码
end
end
def create
end
def edit
respond_to do |format|
format.html{render :layout => 'base_courses'}
end
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]
if @poll.save
respond_to do |format|
format.js
end
else
render_404
end
end
def destroy
if @poll && @poll.destroy
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
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,
:question_title => question_title,
:question_type => params[:question_type] || 1,
:question_number => @poll.poll_questions.count + 1
}
@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
}
@poll_questions.poll_answers.new question_option
end
end
if @poll_questions.save
respond_to do |format|
format.js
end
end
end
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
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))
end
end