2014-08-06 21:14:00 +03:00
module Admin
class QuestionsController < ApplicationController
before_filter :verify_organizer
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
def index
@conference = Conference . find_by ( short_title : params [ :conference_id ] )
@questions = Question . where ( global : true ) . all | Question . where ( conference_id : @conference . id )
@questions_conference = @conference . questions
@new_question = @conference . questions . new
end
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
def new
@conference = Conference . find_by ( short_title : params [ :conference_id ] )
@new_question = @conference . questions . new
end
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
def create
@conference = Conference . find_by ( short_title : params [ :conference_id ] )
@question = @conference . questions . new ( params [ :question ] )
@question . conference_id = @conference . id
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
respond_to do | format |
if @conference . save
format . html { redirect_to admin_conference_questions_path , notice : 'Question was successfully created.' }
else
flash [ :error ] = " Oops, couldn't save. Question and answer(s) have titles? "
format . html { redirect_to admin_conference_questions_path }
end
2014-02-09 18:05:57 +02:00
end
2014-08-06 21:14:00 +03:00
end
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
# GET questions/1/edit
def edit
@conference = Conference . find_by ( short_title : params [ :conference_id ] )
@question = Question . find ( params [ :id ] )
if @question . global == true && ! has_role? ( current_user , " Admin " )
redirect_to ( admin_conference_questions_path ( conference_id : @conference . short_title ) , alert : " Sorry, you cannot edit global questions. Create a new one. " )
end
2014-02-09 18:05:57 +02:00
end
2014-08-06 21:14:00 +03:00
# PUT questions/1
def update
@conference = Conference . find_by ( short_title : params [ :conference_id ] )
@question = Question . find ( params [ :id ] )
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
if @question . update_attributes ( params [ :question ] )
redirect_to ( admin_conference_questions_path ( conference_id : @conference . short_title ) , notice : " Question ' #{ @question . title } ' for #{ @conference . short_title } successfully updated. " )
else
redirect_to ( admin_conference_questions_path ( conference_id : @conference . short_title ) , notice : " Update of questions for #{ @conference . short_title } failed. " )
end
2014-02-09 18:05:57 +02:00
end
2014-08-06 21:14:00 +03:00
# Update questions used for the conference
def update_conference
@conference = Conference . find_by ( short_title : params [ :conference_id ] )
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
if @conference . update_attributes ( params [ :conference ] )
redirect_to ( admin_conference_questions_path ( conference_id : @conference . short_title ) , notice : " Questions for #{ @conference . short_title } successfully updated. " )
else
redirect_to ( admin_conference_questions_path ( conference_id : @conference . short_title ) , notice : " Update of questions for #{ @conference . short_title } failed. " )
end
2014-02-09 18:05:57 +02:00
end
2014-08-06 21:14:00 +03:00
# DELETE questions/1
def destroy
if has_role? ( current_user , " Admin " )
@question = Question . find ( params [ :id ] )
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
# Do not delete global questions
if @question . global == false
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
# Delete question and its answers
begin
Question . transaction do
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
@question . delete
@question . answers . each do | a |
a . delete
end
flash [ :notice ] = " Deleted question: #{ @question . title } and its answers: #{ @question . answers . map { | a | a . title } . join ',' } "
2014-02-09 18:05:57 +02:00
end
2014-08-06 21:14:00 +03:00
rescue ActiveRecord :: RecordInvalid
flash [ :error ] = " Could not delete question. "
2014-07-21 14:27:32 +02:00
end
2014-08-06 21:14:00 +03:00
else
flash [ :error ] = " You cannot delete global questions. "
2014-02-09 18:05:57 +02:00
end
else
2014-08-06 21:14:00 +03:00
flash [ :error ] = " You must be an admin to delete a question. "
2014-02-09 18:05:57 +02:00
end
2014-08-06 21:14:00 +03:00
@questions = Question . where ( global : true ) . all | Question . where ( conference_id : @conference . id )
@questions_conference = @conference . questions
end
2014-02-09 18:05:57 +02:00
end
2014-06-30 17:07:53 +02:00
end