#!/usr/local/bin/perl ######################################################## # Quirex 1.3 # # By Thomas Tsoi 11.8.99 # # webmaster@cgi.com.hk # ######################################################## # # # CGI.com.hk: # # http://www.cgi.com.hk/ # # # # #################################################### # # # # You can distribute this script and use it freely as # # long as this header is not edited in the script. # # # ######################################################## ############################## # mg33 # mg33 $data_questions = "/home/people/ibch/mariuszj/CBB/PXQ/quiz_ques.txt"; # The database of questions, one question per line # chmod 644 ############################## $data_choices = "/home/people/ibch/mariuszj/CBB/PXQ/quiz_choices.txt"; # The database of multiple choices # chmod 644 ############################## $data_answers = "/home/people/ibch/mariuszj/CBB/PXQ/quiz_ans.txt"; # The database of answers # chmod 644 ############################## $data_desc = "/home/people/ibch/mariuszj/CBB/PXQ/quiz_desc.txt"; # The database of additional information to the questions or answers # chmod 644 ############################## $data_last = "/home/people/ibch/mariuszj/CBB/PXQ/quiz_last.txt"; # The file to store the data of challengers # chmod 666 ############################## $html_head = "/home/people/ibch/mariuszj/CBB/PXQ/quiz_head.txt"; # HTML template of the header # chmod 644 ############################## $html_foot = "/home/people/ibch/mariuszj/CBB/PXQ/quiz_foot.txt"; # HTML template of the footer # chmod 644 ############################## $url = "http://users.man.poznan.pl/mariuszj/CBB/htbins/quiz.cgi"; # URL of this CGI script ############################### # $num_questions = 210; # Number of questions in the question database # Not necessary if you use admin.cgi to add questions ############################### $least_questions = 1; # The smallest number of questions a challenger has to take # i.e. If you set this to 10, no one can take only 5 or 8 questions ############################### $last_num = 15; # Number of recent challengers to display ################################# $remote_host1="$ENV{REMOTE_HOST}"; $remote_addr1="$ENV{REMOTE_ADDR}"; $http_referer1="$ENV{ HTTP_REFERER}"; ########################################################## # Do not edit the following unless you know what you are doing # require 'cookie.lib'; require ("cgi-lib.pl"); &ReadParse(*form); $name = $form{'name'}; $email = $form{'email'}; $action = $form{'action'}; if (! $num_questions > 0) { open(QUES, "$data_questions"); @questions = ; close(QUES); $num_questions = $#questions + 1; } $num_display = defined($form{'number'})? $form{'number'}: $least_questions; if ($form{'number'} =~ /^all$/i) {$num_display = $num_questions;} if ($num_display < $least_questions) {$num_display = $least_questions;} if ($num_display > $num_questions) {$num_display = $num_questions;} if ($ENV{'QUERY_STRING'} eq "last" || $action eq "last") { &Last; } elsif ($action eq "submit") { # if (exists $ENV{'HTTP_COOKIE'}) { ##########-MJ-18-12-2012-########## # modification to skip COOKIE checking # if (true) { ##########-MJ-18-12-2012-########## # modification to skip COOKIE checking # &DisplayAnswer; } else { &NoLast; } } else { &DisplayQuiz; } ########################################################### ########################################################### sub DisplayQuiz { print "Content-type: text/html\n"; # mg33 ### Create, update, or delete the cookie $exists = 0; # any cookies? $cookies = ''; # cookies $cookie = 0; # our cookie? $visit = 0; # default counter if (exists $ENV{'HTTP_COOKIE'}) { $exists = 1; $cookies = $ENV{'HTTP_COOKIE'}; @cookies = split(';',$ENV{'HTTP_COOKIE'}); foreach (@cookies) { ($k,$v) = split('=',$_); $k =~ s/ //g; if ($k eq 'VISIT') { $cookie = 1; $visit = $v; } } } $newvisit = 1; $set = "Set-Cookie: " . "VISIT=$newvisit; " . "path=/mariuszj/CBB/htbins; " . # "path=/CBB/PXQ; " . "expires=Tuesday, 28-Dec-2090 23:59:40 GMT"; # future print "$set" , # set cookie "\n\n"; # end of header # mg33 print &header; print "

\n"; print "

\n"; open(CHOICES, $data_choices); @choices = ; close(CHOICES); open(QUES, $data_questions); @questions = ; close(QUES); for ($i=0; $i<$num_display; $i++) { $sequence[$i] = -1; } for ($i=0; $i<$num_display; $i++) { do { $sequence[$i] = int(rand($num_questions)); $repeated = 0; for ($j=0; $j<$num_display; $j++) { if ($sequence[$i] == $sequence[$j] && $i != $j) { $repeated = 1; } } } while ($repeated == 1); } for ($i=0; $i<$num_display; $i++) { print ""; print ($i+1); chomp($questions[$sequence[$i]]); print ". $questions[$sequence[$i]]
\n"; chomp($choices[$sequence[$i]]); print ""; if ($choices[$sequence[$i]] ne "") { @temps = split(/\|/, $choices[$sequence[$i]]); foreach $temp (@temps) { print " $temp
"; } } else { print "Answer: "; } print "
\n"; print "

\n"; } print ""; print "Name: "; print (defined($name)? "
\n": "
\n"); print "E-mail: "; print (defined($email)? "": ""); print "
\n

\n"; $" = "\|"; print <


Last $last_num Challengers END_OF_HTML print &footer; } ########################################################### ########################################################### sub DisplayAnswer { # &send_mail; print "Content-type: text/html\n"; # mg33 ### Create, update, or delete the cookie $exists = 0; # any cookies? $cookies = ''; # cookies $cookie = 0; # our cookie? $visit = 0; # default counter if (exists $ENV{'HTTP_COOKIE'}) { $exists = 1; $cookies = $ENV{'HTTP_COOKIE'}; @cookies = split(';',$ENV{'HTTP_COOKIE'}); foreach (@cookies) { ($k,$v) = split('=',$_); $k =~ s/ //g; if ($k eq 'VISIT') { $cookie = 1; $visit = $v; } } } $newvisit = 1; $set = "Set-Cookie: " . "VISIT=; " . "path=/mariuszj/CBB/htbins; " . # "path=/CBB/PXQ; " . "expires=Wednesday, 03-Jan-2096 23:12:40 GMT"; # past print "$set" , "\n\n"; # set cookie # end of header # mg33 print &header; print < Name: $name
Email: $email

END_OF_HTML @sequence = split(/\|/, $form{'sequence'}); open(QUES, "$data_questions"); @questions = ; close(QUES); open(CHOICE, "$data_choices"); @choices = ; close(CHOICE); open(ANS, "$data_answers"); @answers = ; close(ANS); open(DESC, "$data_desc"); @desc = ; close(DESC); $correct = 0; for ($i=0; $i<$num_display; $i++) { print ""; print ($i+1); chomp($questions[$sequence[$i]]); print ". $questions[$sequence[$i]]
\n"; chomp($answers[$sequence[$i]]); chomp($choices[$sequence[$i]]); $tick = 0; if ($choices[$sequence[$i]] ne "") { print "Your answer is: $form{\"$sequence[$i]\"}
\n"; print "Correct answer: $answers[$sequence[$i]]
\n"; } else { @valid_answers = split(/\|/, $answers[$sequence[$i]]); foreach $ans (@valid_answers) { if ($ans =~ /^$form{$sequence[$i]}$/i) { $ans = "$ans"; $tick = 1; } } $valid_ans = join(", ", @valid_answers); print "Your answer: $form{\"$sequence[$i]\"}
\n"; print "Accepted answer(s): $valid_ans
\n"; } chomp($desc[$sequence[$i]]); if ($desc[$sequence[$i]] ne "") { print "(Note: $desc[$sequence[$i]])
\n"; } print "

\n"; if ($form{"$sequence[$i]"} eq $answers[$sequence[$i]] || $tick == 1) { $correct++; } } print < You have got $correct correct out of the $num_display questions.

and you will get different questions


Last $last_num Challengers END_OF_HTML print &footer; open(READ, "$data_last"); @last = ; close(READ); open(LAST, ">$data_last"); print LAST "$num_display|$correct|$name|$email|$remote_host1|remote_addr1|$http_referer1\n"; print LAST @last; close(LAST); } ########################################################### ########################################################### sub Last { print "Content-type: text/html\n\n"; # print &header; print <Last $last_num Challengers

END_OF_HTML open(READ, "$data_last"); @last = ; close(READ); $linenum = $#last; if ($linenum <= $last_num) { foreach $line (@last) { chomp($line); ($last_ttl, $last_score, $last_name, $last_email,$remote_host,$remote_addr,$http_referer) = split(/\|/, $line); print "\n"; if ($last_email ne "" && $last_name ne "") { print "\n"; } elsif ($last_name ne "") { print "\n"; } else { print "\n"; } print "\n"; print "\n"; print "\n"; print "\n"; # print "\n"; # print "\n"; print "\n"; } } else { $count = 0; foreach $line (@last) { chomp($line); ($last_ttl, $last_score, $last_name, $last_email,$remote_host,$remote_addr,$http_referer) = split(/\|/, $line); if ($count < $last_num) { print "\n"; if ($last_email ne "" && $last_name ne "") { print "\n"; } elsif ($last_name ne "") { print "\n"; } else { print "\n"; } print "\n"; print "\n"; print "\n"; print "\n"; # print "\n"; # print "\n"; print "\n"; $count++; } } } print "
Name Questions Attemped Score Percentage From
$last_name$last_nameAnonymous$last_ttl$last_score".(round($last_score/$last_ttl*100))."%$remote_host$remote_addr$http_referer
$last_name$last_nameAnonymous$last_ttl$last_score".(round($last_score/$last_ttl*100))."%$remote_host$remote_addr$http_referer
\n"; print &footer; } ########################################################### ########################################################### sub header { open(HTML, "$html_head"); my @lines = ; close(HTML); return @lines; } sub footer { open(HTML, "$html_foot"); my @lines = ; close(HTML); return @lines; } sub round{ local $num = @_[0]; return int($num + 0.5); } ################################################################### ## Mg33 ## ################################################################# sub NoLast { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); if ($sec < 10) { $sec = "0$sec"; } if ($min < 10) { $min = "0$min"; } if ($hour < 10) { $hour = "0$hour"; } if ($mon < 10) { $mon = "0$mon"; } if ($mday < 10) { $mday = "0$mday"; } if ($year>99) { $year=$year+1900; } $month = $mon; @months = ("January","February","March","April","May","June","July","August","September","October","November","December"); $date = "Time: $hour\:$min\:$sec
Date: $months[$month] $mday, $year "; print "Content-type: text/html\n\n"; print <

PXQuiz Message
$date

You already submitted this set of questions before.
Please note that you have only one try per set.

In order to get a new quiz form, go back to the Quiz Top Page and access "PXQuiz" again.

END_OF_HTML print "
\n"; print "

"; print &footer; print "
"; } ################################################ sub send_mail { $dokogo="mariusz\@leucine.ibch.poznan.pl"; $temat="quiz test."; $odkogo="mirek\@krystal.amu.edu.pl"; $text= "test mail from quiz."; open (MAIL, "|/usr/lib/sendmail -f $odkogo -t $dokogo") || die "sendmail: $!"; # print MAIL "From: $odkogo\n"; # print MAIL "To: $dokogo\n"; # print MAIL "Subject: $temat\n\n"; # print MAIL "Content-type: text/html\n\n"; print MAIL &header; ##################### print MAIL < Name: $name
Email: $email

END_OF_HTML @sequence = split(/\|/, $form{'sequence'}); open(QUES, "$data_questions"); @questions = ; close(QUES); open(CHOICE, "$data_choices"); @choices = ; close(CHOICE); open(ANS, "$data_answers"); @answers = ; close(ANS); open(DESC, "$data_desc"); @desc = ; close(DESC); $correct = 0; for ($i=0; $i<$num_display; $i++) { print MAIL ""; print MAIL ($i+1); chomp($questions[$sequence[$i]]); print MAIL ". $questions[$sequence[$i]]
\n"; chomp($answers[$sequence[$i]]); chomp($choices[$sequence[$i]]); $tick = 0; if ($choices[$sequence[$i]] ne "") { print MAIL "Your answer is: $form{\"$sequence[$i]\"}
\n"; print MAIL "Correct answer: $answers[$sequence[$i]]
\n"; } else { @valid_answers = split(/\|/, $answers[$sequence[$i]]); foreach $ans (@valid_answers) { if ($ans =~ /^$form{$sequence[$i]}$/i) { $ans = "$ans"; $tick = 1; } } $valid_ans = join(", ", @valid_answers); print MAIL "Your answer: $form{\"$sequence[$i]\"}
\n"; print MAIL "Accepted answer(s): $valid_ans
\n"; } chomp($desc[$sequence[$i]]); if ($desc[$sequence[$i]] ne "") { print MAIL "(Note: $desc[$sequence[$i]])
\n"; } print MAIL "

\n"; if ($form{"$sequence[$i]"} eq $answers[$sequence[$i]] || $tick == 1) { $correct++; } } print MAIL < You have got $correct correct out of the $num_display questions.

END_OF_HTML ################## print MAIL &footer; close (MAIL); }