Visit Your Local PBS Station PBS Home PBS Home Programs A-Z TV Schedules Watch Video Donate Shop PBS Search PBS
FMC Home Link PBS Program LinkFMC Book LinkViewer's Voices LinkInteractivity LinkTeacher's Guide
  Timeline LinkTrends Challenge Link
FMC Logo 1

     Trends Topics

        Education
        Reading
        Work
        Religion
     
   Immigration
        Mobility
        Suicide
        Violence
        Poverty

 

 

 

 

 

 

 

 

FMC Logo 2


Trends in Reading

Real or Imaginary?  Find Out.

Do you agree or disagree with the following statement?

 

#!/usr/bin/perl BEGIN {use CGI::Carp qw(fatalsToBrowser); }; use CGI; $page = new CGI; ################ Poll # # Copyright 1999 by Greg Billock # Distributed under GPL # # Reads poll data file and constructs HTML from it # # Call with ?poll=filename to print the file for that particular # poll. # # Call with ?action=index to print the index of all polls. # # Call with ?action=display to print results (not vote form) # # Call with ?action=ballot to display the ballot form # # Call with ?action=vote to register the vote # # Call with ?action=addform to add a new poll # # Call with ?action=briefdisplay for SSI-type display # Call with ?action=briefballot for SSI-type ballot ########################### # configuration section # $polldatafile = "polls.txt"; # PAM CHANGE THIS LINE FOR EACH SERVER # for apexresearch # = "http://www.apexresearch.com/cgi-bin/poll/poll.cgi"; # for dipsy.pbs.org # = "http://dipsy.pbs.org/century/cgi-bin/poll/poll.cgir"; # for www.pbs.org $selfurl = "http://www.pbs.org/cgi-bin/fmc/poll/poll.cgi"; $password = "995544"; $title = "CGI DID-YA-KNOW Polls"; $bodystyle = ""; #################### #execution starts &readparam; $pollname = $cgiVals{'poll'}; $action = $cgiVals{'action'}; #read in universal data file &readdatafile; $pollfile = $allpollfiles{$pollname}; if ($pollfile) { $datafile = $pollfile; &readpollfile; } if ($action eq "") { $action = "index"; } if ($action eq 'index') { &printhtmlhead; &printindex; &printhtmlfoot; } if ($action eq 'display') { &printhtmlhead; &printbriefdisplay; &printhtmlfoot; } if ($action eq 'ballot') { &printhtmlhead; &printballot; &printhtmlfoot; } if ($action eq 'vote') { #&printhtmlhead; &printvoteresult; #&printhtmlfoot; } if ($action eq 'briefdisplay') { # remark this next line for PBS servers &printack; &printbriefdisplay; } if ($action eq 'briefballot') { # remark this next line for PBS servers &printack; &printballot; } if ($action eq 'addform') { &printhtmlhead; &printaddform; &printhtmlfoot; } if ($action eq 'addpoll') { &printhtmlhead; &processaddform; &printhtmlfoot; } if ($action eq 'admin') { &printhtmlhead; &printadminform; &printhtmlfoot; } ########## end execution ####### # printadminform # Prints the form used to change statuses sub printadminform { &procadmin; print <Polls Admin


Add a New Poll


Administer existing polls

EndOfHTML ; foreach $pname (@allpollnames) { #name print ""; print "\n"; #active/inactive/delete if ($allpollstatus{$pname} eq 'active') { print "\n"; } else { print "\n"; } #view results print "\n"; print "\n\n"; } print "
$pname - $allpolltitles{$pname}\n"; print "Activate
\n"; print "De-Activate
\n"; print "Delete\n"; print "
\n"; print "Activate
\n"; print "De-Activate
\n"; print "Delete\n"; print "
\n"; print "[View Results]

\n"; print "Password: \n"; print "

"; print "
"; print "

\n"; print "


\n"; print "Go to index"; } sub procadmin { $submitpass = $cgiVals{'submitpass'}; if ($submitpass ne $password) { return; } #figure out commands.... @newpollnames = (); foreach $pname (@allpollnames) { $command = $cgiVals{$pname}; if ($command eq 'activate') { $allpollstatus{$pname} = "active"; @newpollnames = (@newpollnames, $pname); } if ($command eq 'deactivate') { $allpollstatus{$pname} = "inactive"; @newpollnames = (@newpollnames, $pname); } if ($command eq 'delete') { delete $allpollstatus{$pname}; delete $allpolltitles{$pname}; delete $allpollfiles{$pname}; } } @allpollnames = @newpollnames; &writedatafile; } # printaddform # Prints the form used to add a poll sub printaddform { print <Add a new poll

Poll name (one word):

Poll filename:

Poll question:

Poll selection options (one per line):

Poll DID YOU KNOW:

Poll Display URL http://


Password:

EndOfHTML ; } # processaddform # Prints ack for processing the add-poll form sub processaddform { $pollname=$cgiVals{'newpoll'}; $polltitle = $cgiVals{'title'}; $datafile = $cgiVals{'filename'}; $pollquestion= $cgiVals{'question'}; $newpolloptions = $cgiVals{'options'}; $pollurl = $cgiVals{'linkurl'}; $submitpass = $cgiVals{'submitpass'}; if ($submitpass ne $password) { print "

Sorry, password incorrect. No changes recorded.

\n"; return; } @options = split(/\n/,$newpolloptions); @polloptions = (" ", @options); $pollnumoptions = scalar(@polloptions) - 1; for ($i=1; $i<=$pollnumoptions; $i++) { $pollvotes[$i] = 0; } #write the new data to the new file &writepollfile; open(DATA, ">>$polldatafile"); print (DATA "name=$pollname\n"); print (DATA "title=$polltitle\n"); print (DATA "file=$datafile\n"); print (DATA "status=active\n"); close(DATA); #ack to user print "

Changes recorded!

\n"; print "To view new poll, click "; print "here

"; } # printvoteresult # Prints a display of a poll file after recording vote sub printvoteresult { $votedoption = $cgiVals{'option'}; $pollvotes[$votedoption]++; $tally++; &writepollfile; &printURL; #&printdisplay; } #printindex # Prints an index of all polls registered sub printindex { &readdatafile; print <$title



 

Current polls:

EndOfHTML ; #print first item specially $firstpoll = shift(@allpollnames); $curpollbgcolor = "#FFCCFF"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n\n"; #print the rest of the polls... foreach $pname (@allpollnames) { print "\n"; print "\n"; print "\n"; } else { print "[Inactive]\n"; } print "\n"; print "\n\n"; } print <
 
$allpolltitles{$firstpoll}\n"; print "[Vote!]\n\n"; print "[View Results]\n
$allpolltitles{$pname}\n"; if ($allpollstatus{$pname} eq 'active') { print "[Vote!]\n"; print "[View Results]
EndOfHTML ; } # printbriefdisplay # Prints a display of a poll file in brief (i.e. in a table) sub printbriefdisplay { print "

$pollquestion
\n"; for($i=1; $i<=$pollnumoptions; $i++) { if ($tally != 0) { $percent = $pollvotes[$i] / $tally; } else { $percent = 0; } $percent = $percent * 100; $percent = int($percent); print "\n"; print " \n"; } print "
$polloptions[$i]
$percent%

total votes: $tally
\n"; } # printdisplay # Prints a display of a poll file sub printdisplay { print <$title


Poll results for....

 

$pollquestion


EndOfHTML ; #print out results for($i=1; $i<=$pollnumoptions; $i++) { print "\n"; print " \n"; if ($tally != 0) { $percent = $pollvotes[$i] / $tally; } else { $percent = 0; } $percent = $percent * 100; $percent = int($percent); print " \n\n"; } print <
$polloptions[$i]
$percent%

$polltitle
More polls EndOfHTML ; } # printURL # Prints a JavaScript to direct the browser # to the result page for this Poll sub printURL { print $page->redirect("http://$pollurl"); # print < # top.document.location = "http://$pollurl"; # #

$title

# #
#

Poll results for.... #

  # # #
# #

$pollquestion #
#


# # # # EndOfHTML # ; # # print < #
# # #
#
# $polltitle # #
# EndOfHTML # ; } # printbriefballot # Prints a *brief* ballot for a poll file sub printbriefballot { print <$polltitle

$pollquestion

EndOfHTML ; for($i=1; $i<=$pollnumoptions; $i++) { print "\n"; print "$polloptions[$i]
\n"; } print "\n"; print "
\n"; } # printballot # Prints a ballot for a poll file sub printballot { # JavaScript function to prevent blank submissions print "\n\n"; print < $pollquestion

EndOfHTML ; #print options for($i=1; $i<=$pollnumoptions; $i++) { print "\n"; print "$polloptions[$i]\n
\n"; } print < EndOfHTML ; } # readpollfile # Reads the data file specified by the referrer in $datafile # # returns $polltitle, $pollquestion, $pollvotes, $polloptions sub readpollfile { open(DATA,$datafile); @datalines = ; close(DATA); $tally = 0; foreach $dataline (@datalines) { chomp($dataline); $lcdata = lc($dataline); @splitdata = split(/=/,$dataline); if ( substr($lcdata,0,5) eq 'title' ) { $polltitle = $splitdata[1]; } if ( substr($lcdata,0,8) eq 'question' ) { $pollquestion = $splitdata[1]; } if ( substr($lcdata,0,7) eq 'linkurl' ) { $pollurl = $splitdata[1]; } if ( substr($lcdata,0,5) eq 'votes' ) { $tindex = substr($splitdata[0],6); $pollvotes[$tindex] = $splitdata[1]; $tally = $tally + $pollvotes[$tindex]; } if ( substr($lcdata,0,6) eq 'option' ) { $tindex = substr($splitdata[0],7); $polloptions[$tindex] = $splitdata[1]; } } $pollnumoptions = scalar(@polloptions) - 1; } # writepollfile # Writes the data file specified by the referrer sub writepollfile { #overwrite... open(DATA,">$datafile"); print (DATA "title=$polltitle\n"); print (DATA "question=$pollquestion\n"); print (DATA "linkurl=$pollurl\n"); for($i=1; $i<=$pollnumoptions; $i++) { print (DATA "votes.$i=$pollvotes[$i]\n"); print (DATA "option.$i=$polloptions[$i]\n"); } close(DATA); } # readdatafile # Reads the poll description data file # # returns @allpollnames, %allpolltitles, %allpollfiles, %allpollstatus sub readdatafile { open(DATA,$polldatafile); @datalines = ; close(DATA); $curpoll = ""; @allpollnames = (); foreach $dataline (@datalines) { chomp($dataline); $lcdata = lc($dataline); @splitdata = split(/=/,$dataline); if ( substr($lcdata,0,4) eq 'name' ) { $curpoll = $splitdata[1]; push(@allpollnames, $curpoll); } if ( substr($lcdata,0,5) eq 'title' ) { $allpolltitles{"$curpoll"} = $splitdata[1]; } if ( substr($lcdata,0,4) eq 'file' ) { $allpollfiles{"$curpoll"} = $splitdata[1]; } if ( substr($lcdata,0,6) eq 'status' ) { $allpollstatus{"$curpoll"} = $splitdata[1]; } } @allpollnames = reverse(@allpollnames); } # writedatafile # Writes current contents out to the data file sub writedatafile { open(DATA, ">$polldatafile"); #reverse to right down in chronological order @allpollnames = reverse(@allpollnames); foreach $pname (@allpollnames) { print (DATA "name=$pname\n"); print (DATA "title=$allpolltitles{$pname}\n"); print (DATA "file=$allpollfiles{$pname}\n"); print (DATA "status=$allpollstatus{$pname}\n"); } close(DATA); #reverse back to 'proper' order @allpollnames = reverse(@allpollnames); } #prints HTML header sub printhtmlhead { print("Content-type: text/html\n\n"); print <$title $bodystyle EndOfHTML ; } #prints HTML header sub printack { print("Content-type: text/html\n\n"); } #print HTML footer sub printhtmlfoot { print("\n"); } # read parameters # name/value pairs are in %cgiVals (global) hash sub readparam { if ( ($ENV{'REQUEST_METHOD'} eq 'POST') || ($ENV{'REQUEST_METHOD'} eq 'post') ) { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); #read for POST method to $buffer @cgiPairs = split(/\&/,$buffer); } else { #get the pairs of parameters passed to the script for GET method @cgiPairs = split(/\&/,$ENV{'QUERY_STRING'}); } #split the pairs into a %cgiVals hash foreach $pair ( @cgiPairs ) { ($var,$val) = split("=",$pair); $val =~ s/\+/ /g; $val =~ s/%(..)/pack("c",hex($1))/ge; $cgiVals{"$var"} = "$val"; } } #note! for textarea fields, add this: # $FORM{'comments'} =~ s/\r/\n/; # #which replaces the linefeeds with the \n newline sub putenv { foreach $key (keys(%ENV)) { print "$key = $ENV{$key}
\n"; } print "

\n"; foreach $key (keys(%cgiVals)) { print "$key = $cgiVals{$key}
\n"; } }

spacer

PBS Program | Trends of the Century | Viewer's Voices | Interactivity | Teacher's Guide

  spacer