"Quiz" site


#1

Hi,

I am thinking about how to best design a quiz site in RoR. I think it
should be fairly easy, but have run into a wall. In the simplest form,
the view would grab all of the questions for a particular quiz and
display them (e.g. multiple choice, so 4 option buttons per question).
However, I can’t figure out how to “mark” the quiz. The form is
submitted to the controller, which must somehow work out whether each
one is correct. Perhaps each option group could have some form of
identifier that the controller looks at, makes a new “question” object
and checks “question.correct_answer” or somesuch? Once this is done,
the users profile can be updated with their result (etc).

Now if I want to display questions in a random order (say a quiz has 40
questions, I only want 20 for a particular attempt), I can easily grab
only 20 and jumble them up. But how does the controller know how to
mark them? Is the idea above of having the identifier sufficient here?

As a final question, I was considering different types of question. For
example, a multiple choice question (so one row per option), a question
where the user must enter a number/string (one row with the correct
answer). The view could add a ‘type’, but checking whether a number
answer is actually a number would suddenly come down to the controller,
not the model. Unless of course I use different tables for different
types of answer (so different models), or there is some sort of OOo foo
with the model that can distinguish and have different
validates_something tests.

It seems that suddenly the application is not only a data-bound
input/output site, and I’m stuck :slight_smile:

David


#2

David wrote:

Hi,

I am thinking about how to best design a quiz site in RoR. I think it
should be fairly easy, but have run into a wall. In the simplest form,
the view would grab all of the questions for a particular quiz and
display them (e.g. multiple choice, so 4 option buttons per question).
However, I can’t figure out how to “mark” the quiz. The form is
submitted to the controller, which must somehow work out whether each
one is correct. Perhaps each option group could have some form of
identifier that the controller looks at, makes a new “question” object
and checks “question.correct_answer” or somesuch? Once this is done,
the users profile can be updated with their result (etc).

How about an Answer table? The Answer table would have a question_id
column, and a correct_answer column.

class Question < ActiveRecord
has_one :answer

returns true if the given answer is correct

def is_correct?(guess)
guess == correct_answer
end

end

class Answer < ActiveRecord
belongs_to :question
end

The correct_answer could by just a varchar type to hold ‘A’, ‘B’, ‘C’,
or ‘D’, or whatever you want to use.

You could then easily have a Quiz table that has_many :questions, and
off you go.

Make sense?

Jeff
www.softiesonrails.com


#3

create database “localhost:quiz_development”
PAGE_SIZE=8192 length = 1024 pages
DEFAULT CHARACTER SET ISO8859_1;
commit;

create table userids
(
ID char(36) not null primary key,
name varchar (255)
);

create table presentations
(
ID char(36) not null primary key,
textvalue varchar (1024),
audio varchar (1024),
visual varchar (1024)
);

create table quizzes
(
ID char(36) not null primary key,
name varchar (255) not null,
preamble_presentation_id char(36),
postamble_presentation_id char(36),
foreign key (preamble_presentation_id) references presentations(ID),
foreign key (postamble_presentation_id) references presentations(ID)
);

create table questions
(
ID char(36) not null primary key,
PARENT_ID char(36) not null,
presentation_id char(36) not null,
seq integer not null,

foreign key (PARENT_ID) references quizzes(ID),
foreign key (presentation_id) references presentations(ID)
);
create unique ascending index quest_seq on questions (PARENT_ID,SEQ);

create table answers
(
ID char(36) not null primary key,
PARENT_ID char(36) not null,
presentation_id char(36) not null,
seq integer not null,
iscorrect char (1) default ‘N’ not null,

foreign key (PARENT_ID) references questions(ID),
foreign key (presentation_id) references presentations(ID)
);
create unique ascending index seq on answers (PARENT_ID,SEQ);

create table quizruns
(
ID char(36) not null primary key,
quiz_id char(36) not null,
user_id char(36) not null,
started TIMESTAMP not null,
finished TIMESTAMP,
asked integer not null,
answered integer not null,
correct integer not null,

foreign key (quiz_id) references quizzes(ID),
foreign key (user_id) references userids(ID)
);

create table answerruns
(
ID char(36) not null primary key,
quizrun_id char(36) not null,
started TIMESTAMP not null,
finished TIMESTAMP,
answer_id char(36),

foreign key (quizrun_id) references quizruns(ID),
foreign key (answer_id) references answers(ID)
);

grant all on userids to quiz;
grant all on presentations to quiz;
grant all on quizzes to quiz;
grant all on questions to quiz;
grant all on answers to quiz;
grant all on quizruns to quiz;
grant all on answerruns to quiz;
commit;