return to first page linux journal archive
keywordscontents

Listing 2: The final version of edit-quizfile.pl.

#!/usr/bin/perl -w

use strict; use diagnostics; use CGI; # Available from http://www.perl.com/CPAN use QuizQuestions;

# Maximum number of questions in a quiz my $MAX_QUESTIONS = 10;

# Default quizname my $quizname = "";

# Create an instance of CGI my $query = new CGI;

# Print the MIME header that we will always use print $query->header("text/html");

# Determine how we were invoked my $request_method = $query->request_method;

# ------------------------------------------------------------ # If we were invoked via POST, then save the changes

if ($request_method eq "POST") { $quizname = $query->param("quizname");

# Create an instance of QuizQuestions my $questions = new QuizQuestions($quizname);

# Add questions to $questions my $counter = 0;

foreach $counter (0 .. $MAX_QUESTIONS) { # Only handle as many questions as were filled in, by # checking to see if the question was entered last unless ($query->param("question-$counter") ne "$counter");

# Set the question my @question = ($query->param("question-$counter"), $query->param("answer-a-$counter"), $query->param("answer-b-$counter"), $query->param("answer-c-$counter"), $query->param("answer-d-$counter"), $query->param("correct-$counter"));

# Add the question to the quiz $questions->addQuestion(@question); }

# Save the questions to disk my $result = $questions->saveFile; # If something went wrong, tell us what it was if ($result ne "0") { print $query->start_html(-title => "Error"); print "$result

\n"; print $query->end_html; exit; } }

# ------------------------------------------------------------ # If we were invoked using GET, then get the name of # the quizfile

if ($request_method eq "GET") { # If the query string is empty, then produce a page of HTML # containing ISINDEX if ($query->param("keywords") eq "") { print $query->start_html(-title => "Enter a quiz name"); print "Please enter the name of the quiz you want "; print "to create or edit in the below .

\n"; print "\n"; print $query->end_html;

exit; } else { # Get the name of the quiz $quizname = $query->param("keywords"); } }

# Whether we were invoked with GET or POST, display # the current state of the quiz

# Create a new instance of QuizQuestions my $questions = new QuizQuestions($quizname);

# Read the questions from disk, ignoring errors # (since we will happily create a new quiz) my $result = $questions->loadFile;

# Create the header for the HTML page print $query->start_html(-title => "Create/Edit a quiz");

# Define a form my $script_name = $query->script_name; print "

\n";

# Create the text element for the quiz name print "Editing quiz:

\n"; print "
\n\n";

# Now create one form element for each existing question

my $counter = 0; foreach $counter (0 .. $MAX_QUESTIONS) { # Get a particular question my ($qtext, $ansA, $ansB, $ansC, $ansD, $correct) = $questions->getQuestion($counter);

# Question text print "

Question $counter

\n"; print "Question text:

\n";

# Answer A print "Answer A:

\n";

# Answer B print "Answer B:

\n";

# Answer C print "Answer C:

\n";

# Answer D print "Answer D:

\n";

# Correct answer print "Correct answer: \n"; print "


\n\n"; }

# Now that the loop is complete, we can finish things up print '', "\n"; print "\n"; print $query->end_html;