xpto

Ruby code posted by xpto
created at 11 Mar 16:34

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

# 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
1.45 KB in 4 ms with coderay