Adding support for likert scales
by tdaxp ~ August 19th, 2007
I’m working on another draft of my dissertation proposal presentation (I haven’t given it yet, but I keep finding ways to make it better), and I found some scales I would like to use.

However, I realized that as the questions are pretty similar, I don’t want to have to manually put it nearly identical material each time. I also want to format the displayed questions prettily, to present a proper likert scale…
So I want to create a new feature on manage_questions, called “clone,” which will create a nearly identical copy of an question that varies only in its id. This should be pretty easy, as ruby already has an ActiveRecord::clone function that does this.
First, create a new option entry after the destroy line in app/views/manage_questions/list.rhtml:
<td><%= link_to ‘Clone’, { :action => ‘clone_question’, :id => question }, :method => :post %></td>
(clone_question is used instead of clone because clone is a reserved word)
And then in manage_questions_controller.rb:
def clone_question
time = Time.new
old_question = Question.find(params[:id])
new_question = old_question.clone
new_question["name"] = old_question["name"] + " (cloned copy at " + time.to_s + ")"
result_of_save = new_question.save
if result_of_save
QuestionOption.clone_question_options(old_question.id,new_question.id)
flash[:notice] = ‘Question was successfully cloned with ‘ + old_question.id.to_s + ‘ to ‘ + new_question.id.to_s
else
flash[:notice] = ‘Question not cloned’
end
redirect_to :action => ‘list’
end
In question_option.rb:
def self.clone_question_options(old_question_id,new_question_id)
question_options = self.find(:all,:conditions => ['question_id = ?' , old_question_id ] )
if question_options
for old_question_option in question_options
new_question_option = old_question_option.clone
new_question_option.question_id = new_question_id
new_question_option.save
end
end
end
One final thing: when the question options are displayed, they should be presented in the order of their option id. That’s really easy in Question.rb, edit such that:
def self.find_question_options_array(question_id)
@options = QuestionOption.find(:all,:conditions=>['question_id = ?', question_id],
rder => ‘option_id ASC’)
end
Finally, after some housekeeping (we don’t need the
Question list <%= @student.current_question_list %> and ordering is <%= @student.current_ordering %> line anymore), we’re done!