#!/usr/local/bin/perl #********************************************* #* PerlTest V.1.1 * #* By: Matt Hughes * #* June 6, 1999 * #* flashlightbrown@hotmail.com * #********************************************* use CGI qw(:standard); print header; #*******Variables that need to be personalized*********** $answerFile = "/usr/www/users/mhughes/perltest/answers.txt"; $logFile = "/usr/www/users/mhughes/perltest/testlog.htm"; $logFileUrl = "http://www.flashlightbrown.com/perltest/testlog.htm"; $testUrl = "http://www.flashlightbrown.com/perltest/test.htm"; $graphFile = "/usr/www/users/mhughes/perltest/graphlog.htm"; $graphFileUrl = "http://www.flashlightbrown.com/perltest/graphlog.htm"; $identity_factor = 1; #*******Initialize variables******************* $date = localtime(time); $total = 0; $right=0; $NumOfResults=0; $percentscore=0; $maxWidth=200; #*******Read the answer file and create array of correct answers***** open (FILE, "$answerFile") || die "Can't Open $answerFile: $!\n"; @LINES=; close(FILE); $NumOfQuestions=@LINES; for ($i=0;$i<$NumOfQuestions;$i++) { $_=$LINES[$i]; chop; $correct[$i] = $_; } #*******Recieve responses and create array of user's answers***** @paramanswers = param(); if ($identity_factor eq 0) { for ($i=0;$i<$NumOfQuestions;$i++) { $answers[$i] = param($paramanswers[$i]); } } if ($identity_factor eq 1) { $identity = param($paramanswers[0]); for ($i=1;$i<=$NumOfQuestions;$i++) { $answers[$i-1] = param($paramanswers[$i]); } } #*******Compare responses with correct answers and count how many are right***** for ($i=0;$i<$NumOfQuestions;$i++) { # if ((index($correct[$i], lc($answers[$i]))) != -1){ if ($correct[$i] eq lc($answers[$i])){ $right=++$right; } } #*******Call all the subroutines************ &get_old_scores; &calculate_results; &append_log; &get_correct_incorrect; &display_results; &append_graph; #*******SUBROUTINES************************* #*******Read the previous scores from the test log and count them up******** sub calculate_results { $percentscore = int (($right/$NumOfQuestions)*100); $total = $total + $percentscore; $totalaveragescore = int ($total/$NumOfResults); } #*******Read from the testlog file to get old scores************ sub get_old_scores { open (FILE,"$logFile") || die "Can't Open $logFile: $!\n"; @OLDLOG=; $SIZE=@OLDLOG; for ($i=0;$i<=$SIZE;$i++) { $_=$OLDLOG[$i]; if (//) { $NumOfResults=++$NumOfResults; $ScoreLine = $OLDLOG[$i+1]; $pos1 = index($ScoreLine, "=")+1; $pos2 = index($ScoreLine, "%"); $ThisScore = int(substr($ScoreLine, $pos1, ($pos2-$pos1))); $total = $total + $ThisScore; } } close (FILE); } #*******Read from the testlog file to count correct and incorrect answers******* sub get_correct_incorrect { open (FILE,"$logFile") || die "Can't Open $logFile: $!\n"; @OLDLOG=; $SIZE=@OLDLOG; for ($a=0;$a<=$NumOfQuestions-1;$a++) { $incorrectArray[$a] = 0; $correctArray[$a] = 0; } for ($i=0;$i<=$SIZE;$i++) { $_=$OLDLOG[$i]; if (//) { for ($l=1;$l<=$NumOfQuestions;$l++) { $answerLine = $OLDLOG[$i+$l]; if (index($answerLine, "Incorrect") != -1) { $incorrectArray[$l-1] = ++$incorrectArray[$l-1]; } } } } close (FILE); } #*******Display feedback for user listing scores********************** sub display_results { print ""; print "You Have Completed the Test
"; print "Score = ", $percentscore, " %

"; print "Number of people tested = ", $NumOfResults, "
"; print "Average of everyone = ", $totalaveragescore, " %

"; print "

"; print "To view the testing log, click here
"; print "To view the graph log, click here
"; print "To return to the test, click here
"; # print "@incorrectArray
"; } #*******Append log file************************** sub append_log { #*******Read the log file into the OLDLOG array and count the number of lines************* open (FILE,"$logFile") || die "Can't Open $logFile: $!\n"; @OLDLOG=; close(FILE); $SIZE=@OLDLOG; #*******open the log file to overwrite it with the new addition********* open (FILE,">$logFile") || die "Can't Open $logFile: $!\n"; for ($i=0;$i<=$SIZE;$i++) { $_=$OLDLOG[$i]; if (//) { print FILE ""; $_=$OLDLOG[$i+1]; print FILE "Mean = ",$totalaveragescore, "%"; $_=$OLDLOG[$i-1]; } if (//) { print FILE "\n"; print FILE "$NumOfResults) "; print FILE "$date
"; if ($identity_factor eq 1) { print FILE "$identity

"; } else { print FILE "
"; } print FILE "Answers:
\n"; print FILE "\n"; for ($j=0;$j<$NumOfQuestions;$j++) { print FILE "#", $j+1, ": "; print FILE $answers[$j]; # if ((index($correct[$j], lc($answers[$j]))) != -1){ if ($correct[$j] eq lc($answers[$j])){ print FILE " Correct
\n"; } else{ print FILE " Incorrect
\n"; } } print FILE "\n"; print FILE "
Score = ", $percentscore, "%
"; print FILE "

\n"; } else { print FILE $_; } } close (FILE); } #*******Update graph file************************** sub append_graph { #*******Read the graph file into the OLDLOG array and count the number of lines************* open (FILE,"$graphFile") || die "Can't Open $graphFile: $!\n"; @OLDLOG=; close(FILE); $SIZE=@OLDLOG; #*******open the graph file to overwrite it with the new addition********* open (FILE,">$graphFile") || die "Can't Open $logFile: $!\n"; $endofgraph = 0; for ($i=0;$i<=$SIZE;$i++) { $_=$OLDLOG[$i]; if (//) { $tester = 1; print FILE "\n"; for ($k=1;$k<$NumOfQuestions;$k++) { $i=$i+1; $_=$OLDLOG[$i]; $blueWidth=((($NumOfResults-$incorrectArray[$k])/$NumOfResults)*$maxWidth); $NumOfCorrect=$NumOfResults-$incorrectArray[$k]; print FILE ""; print FILE "$k) Correct"; print FILE ""; print FILE "$NumOfCorrect"; print FILE ""; $redWidth=(($incorrectArray[$k]/$NumOfResults)*$maxWidth); print FILE ""; print FILE "Incorrect"; print FILE ""; print FILE "$incorrectArray[$k]"; print FILE "\n"; if ($k eq ($NumOfQuestions-1)) { print FILE "\n"; } } } else { print FILE $_; } } close (FILE); }