Creating a Multiple Choice Quiz System with CGI

 in
Designing quizzes the easy way using CGI.

Over the last few months, we have looked at a number of techniques that CGI programmers can use to work on their programs. This month, we will look at a multiple-choice quiz system that uses a combination of techniques to create a simple, but effective, system for creating quizzes for our users. By the end of this short project, you will have not only a good idea of how to implement this type of interaction, but a working four-question quiz, as well.

Before we can begin, we will need to decide on a file format which will contain the questions and answers for our quiz. We could put all of the questions and answers inside of the program itself, but moving them to one or more external files will let us reuse the software with other quizzes on our system. Given that this is a simple quiz, let's say that the questions and answers for each quiz are stored in a file whose name is the same as the quiz name. Thus the quiz named “presidents” will be stored in a file named “presidents”, while the quiz named “unix” is stored in a file named “unix”.

Now that we have decided on filenames, we need to decide on a format for the contents of the file. Let's take the easy route, and put one question and its associated possible answers on each line in the file, each separated by tabs, and ending with the letter “a”, “b”, “c” or “d”, that corresponds to the correct answer.

So that the file can contain comments and whitespace, we'll say that any line beginning with a hash mark (#) is considered a comment, to be ignored. The same goes for any line consisting solely of whitespace. Allowing for comments and whitespace makes it possible for us to comment out questions that we no longer want to use, without having to delete them altogether.

Here is a sample quiz on the subject of cranberries, which we will put in a file named, oddly enough, “cranberries”:

# This is the quiz file about cranberries.
# Comment lines contain a hash mark (#) in the
# first column, and are ignored, as are lines
# containing only whitespace.
What color are cranberries?     Red     White\
        Blue    Dark green      A
What can you make with cranberries?     Muffins\
        Sauce   Steak   A and B D

Note that the questions and answers in this file can contain space characters, but not tab characters. This will typically not affect things very much, but it is a consideration to keep in mind. Also, while each line can be as long as needed, the question and its associated answers must remain on a single line of text (that is, must end in a carriage return).

Creating an Object

Our quiz program actually consists of two different programs working in concert with each other. The first, askquestion.pl, produces an HTML form that presents the user with a question and a list of possible answers. That form will be submitted to another CGI program, checkanswer.pl, which determines whether the user has selected the correct answer.

Because both of these CGI programs will have to access the same quiz file, it is probably a good idea to centralize such functions in a single Perl 5 object. Such an object would have to read the file and return a question of our choosing from the list of available questions. To make things a bit more interesting, this object should include a method that retrieves a random question from the file, which makes the quiz less predictable for the user.

The object that we will use in our quiz program is shown in Listing 1. All this code means is that you can place a:

use QuizQuestions;

statement near the top of both CGI programs to create a Perl object that reads the questions to the “cranberries” quiz. To do this, you can use this statement:

my $quiz = new QuizQuestions("cranberries");
For example, you could retrieve the fifth question with:
my @question = $quiz->getQuestion(5);
or a random question with:
my @question = $quiz->getRandomQuestion;
As you can see, the QuizQuestion object in Listing 1 has nothing to do with CGI programming per se. Even if we were creating a quiz system that wouldn't be used on the Web, this object would be a good starting point. By using an object to represent our data, we have also made it possible to change the file format we are using without modifying the CGI programs that access the data. If we were so inclined, we would be able to move the quiz data into an SQL table, and access it via a database client from within Perl. As long as the interface to the outside world remains the same, our CGI programs wouldn't care.

______________________

Webcast
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers

Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.

Learn More

Sponsored by AMD

White Paper
Red Hat White Paper: Using an Open Source Framework to Catch the Bad Guy

Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6

Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.

Learn more about catching the bad guy in this free white paper.

Learn More

Sponsored by DLT Solutions