After my last note on rails I took a long break, in order to critique two texts (of which Cognitive Development is the first), attend the Boyd Conference, and generally relax. But a month is long enough to go without work on what will now be the technological basis for the next installment of the Wary Guerrilla / Wary Student research program.

An Experiment with a Question List
Today’s work focused laid the foundations for a completely revamped experiment feature. Before, the experiment feature only dropped the user straight into a note-taking interface. Now, question lists are being integrated so that an entire battery can be given through the interface. The task is not completed today — indeed, all that was accomplished was presenting one question list instead of the notetaknig matrix — but it’s a start
After thanking the websites of continuous thinking, hokudai, rubyinside, and rubyonrails more practical help, read below the fold for implementation details…
A number of things being done today. The main thing is allowing the student to actually take the full experiment, and that requires keeping track of what questionlist the student is currently on. To do that, we will alter the study model to have a current_question_list field
ruby script/generat migration alter_student_add_current_question_list
And then for 025_alter_student_add_current_question_list.rb
class AlterStudentAddCurrentQuestionList < ActiveRecord::Migration
def self.up
add_column :students, :current_question_list, :integer, :default => 0
end
def self.down
remove_column :students, :current_question_list
end
end
Now in the student model, add code to retrieve the next question list
In student.rb
def self.find_next_question_list(condition_id,current_question_list)
to_find = QuestionListCondition.find(
:first,
:conditions=> [ ‘condition_id = ? AND ordering > ?’, condition_id, current_question_list ],
:order => ‘ordering ASC’
)
@to_return = to_find.question_list_id
end
In question.rb
def self.find_by_question_list_id(question_list_id)
@questions = find(:all,:conditions => [‘list_id = ?’,question_list_id], :order => "ordering ASC"
end
def self.find_question_options_array(question_id)
@options = QuestionOption.find(:all,:conditions=>[‘question_id = ?’, question_id], :order => ‘display_text ASC’)
end
def self.find_question_options_map(question_id)
@options = find_question_options_array(question_id).map {|u| [u.display_text, u.id] }
end
From student_controller.rb, update the following functions:
def select_condition_from_experiment
selected_experiment_id = params[:selected_experiment][:experiment_id]
@conditions = Condition.find_conditions(selected_experiment_id)
render :partial => "select_condition"
@student = Student.new(params[:student])
@student.save
session[:student] = @student.id;
end
def select_notes_from_condition
@student = Student.find(session[:student])
@student.condition_id = params[:student][:condition_id]
@student.save
@next_question_list = Student.find_next_question_list(@student.condition_id,@student.current_question_list)
@student.current_question_list = @next_question_list
@questions = Question.find_by_question_list_id(@student.current_question_list)
render :partial => "question_list_view"
#render_text @next_question_list
#@notes_fields = NotesField.find_by_condition(@student.condition_id)
#@notes_records = NotesRecord.find_by_condition(@student.condition_id)
#render :partial => "notes_view"
end
create the new file app/views/students/_question_list_view.rhtml
<h1>Question List Page</h1>
<%= form_remote_tag(
:update => "notetaking_display",
:url => { :action => :save_student_notes },
:position => "top" )
%>
<% for question in @questions %>
<p><h3><%= question.display_text %></h3>
<% if question.type_id == QuestionType::STRING %>
<%= text_field("question",question.id) %>
<% end %>
<% if question.type_id == QuestionType::TEXT %>
<%= text_area("question",question.id) %>
<% end %>
<% if question.type_id == QuestionType::SELECTION %>
<% @options = Question.find_question_options_map(question.id) %>
<%= select :question, question.id, @options %>
<% end %>
<% if question.type_id == QuestionType::CHECKBOX %>
<% @options = Question.find_question_options_array(question.id) %>
<% for option in @options %>
<%= check_box("question",question.id) %><%= option.display_text %>
<% end %>
<% end %>
<% if question.type_id == QuestionType::RADIO %>
<% @options = Question.find_question_options_array(question.id) %>
<% for option in @options %>
<%= radio_button("question",question.id,option.id) %><%= option.display_text %>
<% end %>
<% end %>
<% if question.type_id == QuestionType::INSTRUCTION %>
<% end %>
</p>
<% end %>
<%= end_form_tag %>
Update _select_condition.rhtml
<%= form_remote_tag(:update => "notetaking_display",
:url => { :action => :select_notes_from_condition },
:position => "top" ) %>
Your Condition: <%= select :student, :condition_id, @conditions %>
<%= submit_tag "Select Condition" %>
<%= end_form_tag %>
The index.rhtml in the same folder was also updated:
<html>
<head>
<title>Notes on Rails Mock Student Interface</title>
<%= javascript_include_tag "prototype" %>
</head>
<body>
<h3>Add to list using Ajax</h3>
<%= form_remote_tag(:update => "login_with_condition",
:url => { :action => :select_condition_from_experiment },
:position => "top" ) %>
<p><label for="student_name">Your Name</label><br/>
<%= text_field ‘student’, ‘name’ %>
<br />Your Experiment:
<%= select :selected_experiment, :experiment_id, @experiments %>
<%= submit_tag "Select Experiment" %></p>
<%= end_form_tag %>
<div id="login_with_condition"></div>
<div id="notetaking_display"></div>
</body>
</html>