| |||
Top level code for quiz system demo
Good CGI scripting. example from a Well House Consultants training course
More on Good CGI scripting. [link] Source code: quiz.pl Module: P774
#!/usr/bin/env perl
# GLOBALS - change the following when moving between systems $sessdir = "./sessions"; $archivedir = "./archives"; $sesslife = 6; # Maximum time between pages, hours $qndir = "./questions"; $template = "main.htp"; $title = "Open Source Quiz System"; $email = "graham@wellho.net"; $homesite = "http://www.wellho.net"; $copyright = "Copyright, Well House Consultants, 2005"; @topics = ( "Perl (Elementary level)" => ["Perl.1",15], "Perl (Intermediate level)" => ["Perl.2",15], "Perl (Advanced level)" => ["Perl.3",15] ); # END OF GLOBALS - the rest of the code here shouldn't need changing! ##################################################################### use whc_web; use questionset; send_header(); @now = (gmtime())[3,4,5]; $fill{"now"} = sprintf("%d/%d/%d",$now[0],$now[1]+1,$now[2]+1900); $fill{"title"} = $title; $fill{"copyright"} = $copyright; # item 1. read any incoming form #------------------------------- collect_form(); # item 2. Use the value read to pick up the session #-------------------------------------------------- $sessname = read_session($form{plsess}); $fill{plsess} = $sessname ; $stepnumber = $session{step}; # Inputs in %session and %form translate to %session and %fill # item 3. Do any work to analyse inputs and validate them #-------------------------------------------------------- $aok = 1; $errstr = ""; if ($stepnumber == 0) { # Initialisation # Move on to next step - prepare for page 1 $stepnumber = 1; } elsif ($stepnumber == 1) { # Name, email address and quiz type have been entered $aok *= checkinput("yourname","name",'\w', "Name must include a word character"); $aok *= checkinput("youremail","email",'^\s*\S+@\S+\s*$', "email address of form xxx\@xxx.xxx required"); $aok *= checkradio("subject","subject", "You must select a subject"); if ($aok) { # Choose quiz questions, and note in the session $thistest = new questionset($session{subject},"",@topics); $qorder = $thistest->getqdata(); $session{topicname} = $thistest->gettopic(); $session{qorder} = $qorder; $session{setat} = time(); $stepnumber++; } } elsif ($stepnumber == 2) { # Quiz has been completed. Recover the question set and mark. $thistest = new questionset($session{subject},$session{qorder},@topics); ($testlen, $score, $skip,$mark) = $thistest -> mark_quiz(); $session{testlen} = $testlen; $session{score} = $score; $session{skip} = $skip; $session{answerat} = time(); $stepnumber++; } else { } # item 4. Do work to prepare for next page #----------------------------------------- $fill{status} = "You are at page $stepnumber of 3"; $fill{stuff} = ""; $fill{errstr} = $errstr; $fill{form} = "<form method=POST><table border=1><input name=plsess type=hidden ". "value=".$fill{plsess}.">"; if ($stepnumber == 1) { $fill{"top"} = "Please identify yourself and select a test"; $fill{"form"} .= formbox("Please enter your name","yourname"); $fill{"form"} .= formbox("Please enter your email address","youremail"); $fill{"form"} .= radioset("Please choose a test","subject",@topics); } elsif ($stepnumber == 2) { $fill{"top"} = "Please complete the following questions on $session{topicname}"; $testlen = $thistest->getqcount(); for ($k=0; $k<$testlen; $k++) { ($qntext,$qninfo) = $thistest->getquestion($k,0); $fill{"form"} .= radioset($qntext,$k+1,@$qninfo); } } elsif ($stepnumber == 3) { $fill{"top"} = "<b>".webify($session{name}). ", you scored $session{score} out of $session{testlen} on $session{topicname}. ". "You skipped $session{skip} questions.</b>"; $elapsed = $session{answerat} - $session{setat}; $mins = $elapsed / 60; $secs = $elapsed % 60; $fill{"top"} .= sprintf("<br>You took %d minutes and %d seconds to complete the quiz", $mins,$secs); $fill{"stuff"} = $mark; $completed = 1; } else { } $fill{form} .= "<tr><td> </td><td><input type=submit></td></tr></table></form>"; $fill{form} = "<a href=\"\">Restart Quiz</a>" if ($completed); # No next form if application completed # item 5. Save the session #------------------------- # If the user's session has been completed, don't save the "in progress" file" if (! $completed) { $session{step} = $stepnumber; save_session($sessname); } else { archive_session($sessname); } # item 6. Send out the response page #----------------------------------- # Complete the output template and send it to the user $fill{step} = $stepnumber; send_response($template); Learn about this subject
This module and example are covered on our public Using Perl on the Web course. If you have a group of
three or more trainees who need to learn the subject, we can also arrange a
private or on site course for you.
Books covering this topic
Yes. We have over 700 books in our library. Books
covering Perl are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Good CGI scripting." training module. You'll find a description of the topic and some
other closely related examples on the "Good CGI scripting." module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. | |||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |