#!/usr/bin/perl -wT # (c) 2005 Christoph Berg # All rights reserved. # This program is free software covered by the GNU GPL v2. use strict; my $quizfile = "quiz.txt"; my $template = "quiz.html"; my $statsfile = "stats"; my $qcount = 0; my @QUESTION; my @ANSWER; my @SOLUTION; my @TEXT; my $try = 0; # read questions open Q, $quizfile or die "$quizfile: $!"; while () { chomp; if(/^\* (.*)/) { push @QUESTION, $1; my $a = 1; while() { last unless /^([+-]) (.*)/; chomp; push @{$ANSWER[$qcount]}, $2; push @SOLUTION, $a if $1 eq '+'; $a++; } push @TEXT, $_; $qcount++; } } close Q; # read POST input my %POST; if($ENV{REQUEST_METHOD} eq "POST") { my $input = ""; while() { $input .= $_; } %POST = split /[=&]/, $input; } # start writing output print "Content-Type: text/html\n\n"; open TMPL, $template or die "$template: $!"; while() { last if /SPLIT/; print; } if(%POST) { if ($POST{try} and $POST{try} =~ /^\d+$/) { $try = $POST{try}; } my $score = 0; for my $q (0 .. $qcount - 1) { if($POST{"q$q"} and $POST{"q$q"} == $SOLUTION[$q]) { $score++; } } my $per = sprintf "%.1f", $score/$qcount*100; print "

You got $score of $qcount ($per%) right.

\n"; if ($per <= 25) { print "You could get *that* score just by guessing.\n" } elsif ($per <= 35) { print "You've never heard of Debian before, have you?\n" } elsif ($per <= 50) { print "Your Debian knowledge is not really good, but that's not the end of life.\n" } elsif ($per <= 75) { print "Your Debian knowledge is good, but you lack some deeper insights.\n" } elsif ($per <= 90) { print "Pretty good. Some answers were still wrong, though.\n" } elsif ($per < 100) { print "Almost perfect. Are you a Debian Developer?\n" } else { print "Perfect. Go apply for DPL :-)\n"; } print "

\n"; open S, ">>$statsfile" or warn "$statsfile: $!"; print S scalar(localtime). " host:$ENV{REMOTE_ADDR} score:$score/$qcount try:$try agent:$ENV{HTTP_USER_AGENT}\n"; close S; } if (open S, "$statsfile") { my @hist = qw/0 0 0 0 0 0 0 0 0 0 0/; my $count = 0; print "\n"; while () { next unless /try:1 /; if (/score:(\d+)\/(\d+)/) { $hist[int($1 / $2 * 10)]++; $count++; } } foreach my $a (0 .. 9) { printf "
%d-%d%% %d\n", $a * 10, $a * 10 + 9, $hist[$a], $hist[$a]; } printf "
100%% %d\n", $hist[10], $hist[10]; printf "
%d\n", $count; print "
\n"; close S; } if (%POST) { print < You may try again or click to unfold the right answers below. (Sorry, this uses Javascript.)


EOT } print "
\n"; for my $q (0 .. $qcount - 1) { print "

Question ".($q+1).": $QUESTION[$q]

\n"; print "
\n"; my $nr; my $right; foreach $a (@{$ANSWER[$q]}) { $nr++; my $checked = ""; if($POST{"q$q"} and $POST{"q$q"} == $nr) { my $color = "red"; if($POST{"q$q"} == $SOLUTION[$q]) { $color = "green"; $right = 1; } print ""; $checked = " checked=\"checked\""; } print " $nr: $a\n"; if($checked) { print ""; } print "
\n"; } if($right) { print "Correct: $TEXT[$q]\n"; } elsif($POST{"q$q"}) { print < Wrong: click for right answer $TEXT[$q] EOT } print "
\n"; } my $submit = "Tell me I'm right!"; $submit = "I'd like to try again!" if %POST; print "
\n"; $POST{try}++; print "\n"; print "
\n"; #foreach (sort keys %POST) { # print "$_=$POST{$_}
\n"; #} # #foreach (sort keys %ENV) { # print "$_=$ENV{$_}
\n"; #} while() { print; }