Title / Description
Code # Let's build our own survey my_survey = Survey::Survey.new(:name => "Star Wars Quiz", :attempts_number => 4, :active => true) # Let's add some questions and options question_1 = Survey::Question.new(:text => 'How many droids help Luke in the first movie ?') question_1.options = [ Survey::Option.new(:text => '1 droid', :correct => false), Survey::Option.new(:text => '2 droids', :correct => true), Survey::Option.new(:text => '3 droids', :correct => false) ] question_2 = Survey::Question.new(:text => 'Who is the evil guy ?') question_1.options = [ Survey::Option.new(:text => 'Darth Vader', :weight => 100), Survey::Option.new(:text => 'Obi Wan', :weight => 0), Survey::Option.new(:text => 'Jabba', :weight => 50) ] my_survey.questions = [question_1, question_2] my_survey.save! # Let's answer it attempt = Attempt.new(:survey => my_survey, :user => user_1 ) answer_1 = Survey::Answer.new( :option => question_1.options.first ) answer_2 = Survey::Answer.new( :option => question_2.options.first ) attempt.answers << [answer_1, answer_2] # Let's check answers # select the first active Survey survey = Survey::Survey.active.first # select all the attempts from this survey survey_answers = survey.attempts # check the highest score for current user user_highest_score = survey_answers.for_participant(@user).high_score #check the highest score made for this survey global_highest_score = survey_answers.high_score
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code