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.

______________________

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState