#!/usr/bin/perl #use CGI::Carp qw(fatalsToBrowser); use Net::SMTP; #use warnings; # define parameters for your application # Mail params $mail_server ='mail.bolton.ac.uk'; $mail_recipient ='my.email@myserver.ac.uk'; $data_file ='MCQtest.dat'; $mail_sender ='MCQ_assignment_fmethods_sem1_04-05\@bolton.ac.uk'; $debrief_url ='http://www.servername.ac.uk/username/MCQdebrief.html'; # create array of question names, to specify the output formatting order @itemnames = ( "Q1", "Q2", "Q3", "Q4", "Q5", "Q6", "Q7", "Q8", "Q9", "Q10", "Q11", "Q12", "Q13", "Q14", "Q15", "Q16", "Q17", "Q18", "Q19", "Q20" ); # create hash of correct answers for your questions, for scoring %itemanswers = ( "Q1" => "d", "Q2" => "c", "Q3" => "d", "Q4" => "b", "Q5" => "c", "Q6" => "d", "Q7" => "d", "Q8" => "d", "Q9" => "c", "Q10" => "a", "Q11" => "d", "Q12" => "c", "Q13" => "a", "Q14" => "a", "Q15" => "d", "Q16" => "c", "Q17" => "b", "Q18" => "c", "Q19" => "d", "Q20" => "a" ); # this might work to avoid defining @itemnames above, but it reorders keys #@itemnames = keys %itemanswers; # recover time and hostname $date_time=localtime; $remote_address=$ENV{'REMOTE_HOST'}; if (!$remote_address) { $remote_address=$ENV{'REMOTE_ADDR'}; } $forwarded_for=$ENV{'HTTP_X_FORWARDED_FOR'}; if ($forwarded_for) { $remote_address=$forwarded_for; } # read the input data (method=POST) read(STDIN, $formdat, $ENV{'CONTENT_LENGTH'}); # split the input data into individual form items @namevals = split(/&/,$formdat); # create a hash containing the form data foreach $pair (@namevals) { # separate into name and value ($name,$value) = split(/=/,$pair); # convert + signs to spaces $value =~ tr/+/ /; # convert hex pairs (%HH) to ASCII $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # store vals in a hash called %FORM $FORM{$name} = $value; } # now, prepare the outputs (file and email) open(SURVEY_FILE,"+>>$data_file") or die "$^E"; print SURVEY_FILE "$date_time,$remote_address,"; # email code $smtp = Net::SMTP->new($mail_server) || die ^E; $smtp->mail('web_form@bolton.ac.uk'); $smtp->to($mail_recipient); $smtp->data(); $smtp->datasend("To: $mail_recipient\n"); $smtp->datasend("From: $mail_sender\n"); $smtp->datasend("\n"); $smtp->datasend("Date\: $date_time \n"); $smtp->datasend("Remote Address\: $remote_address \n"); # print out the ID and name values to file... print SURVEY_FILE "$FORM{ID},$FORM{name},"; # ...and to email $smtp->datasend("ID\: $FORM{ID} \n"); $smtp->datasend("Name\: $FORM{name} \n"); # initialise the score $score = 0; # write one value (possibly empty) to file for each question item # even if it doesn't appear in the form submission # also, do scoring during the same pass foreach $thename (@itemnames) { # score the answer (undef values don't get counted) # add 1 point for each correct answer, no penalty for incorrect if($itemanswers{$thename} eq $FORM{$thename}) { $score++; } # print comma-delimited value (possibly empty) to file... print SURVEY_FILE "$FORM{$thename},"; # ...and name: value pair to email $smtp->datasend("$thename\: $FORM{$thename} \n"); } # after the raw data, print out the score too print SURVEY_FILE "$score\n"; $smtp->datasend("Score\: $score \n"); # data processing is over, so close outputs close (SURVEY_FILE); $smtp->dataend(); $smtp->quit; # finally, print message page back to user print "Content-type: text/html\n\n"; print "\n"; print "\n"; print "\n"; print "User Survey\n"; print ""; print ""; print "
\n"; print "


\n"; print "Your answers are being sent


Please wait...

\n"; print "

"; print "\n"; print "\n";