#!/usr/bin/perl
# Don't alter the line just above...
#
# libfacinstreval.pl - perl script for submission of a faculty instruction
# evaluation form, evaluating library classes
#
# information sent to refinst@isu.edu
# from facultyinstructioneval.htm
#
# created 01/28/2010 by Kent Kearns
# last edit 04/17/2013 KK
#
# script checks that all required fields have some data entry in them before
# emailing the evaluation answers
#
# the user enters your name (required and assigned variable name of y_name),
# the name of the course they are reporting for (required and assigned variable
# name of c_name), instruction date (required and assigned the
# variable instr_date), instruction time (required and assigned the
# variable instr_time, Instructor's name (required, drop down list and assigned
# the variable name of instructor, Other Instructor's name, if applicable
# (not required, drop down list and is assigned the variable name of
# other_instructor.
#
# There are a total of 11 questions for evaluation ratings and 1 comment box.
# All questions are assigned the variable names associated with their number on
# the form, i.e. variables = question number (1-9, with possible 8a & 8b)
# required answers are 1-9, with none required for 8a and 8b or the comment box,
# Comment box is assigned the variable name associated with its number on
# the form, i.e. variables = 13_comm

############################################################
# Define recipient of email message.
# You must provide an email address for the recepient of postings
# (i.e. who is the resulting email message being sent to?)
# Insert their address between the single quotes below.
############################################################

$recipient ='refinst@isu.edu';
# $recipient ='gowemich@isu.edu';

############################################################
# Don't alter anything in this section unless you know how to use perl!
#
# HTML forms spit out their contents in a long ugly string like:
#  user_name=Joe+Doofus&email1=doofjoe@isu.edu&...
#
# What this section does is create a perl associative array of
# variable name/value pairs from that long, ugly string.
#
# Afterwards, the value of any item from your form can then be referenced by
#   $FORM{'variable_name'}
# where variable_name is the NAME= value assigned to a form's component.
############################################################

# read in raw input from the form.
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split out the name-value pairs by & seperator, and load into array
@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
    # split name-value pairs by = sign
    ($name, $value) = split(/=/, $pair);

    # Un-Webify plus signs and %-encoding
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $name =~ tr/+/ /;
    $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    # load name-value pairs into associative arrays, indexed by name
    $FORM{$name} = $value;
}

############################################################
# GENERAL INFO ABOUT PRINT COMMANDS
############################################################
# Sections between 
# 	print<<EOF;
# and 
# 	EOF
# get sent back to the browser. You are creating an HTML document 'on the fly',
# and you should send HTML code, not just plain text.
#
# Sections between
# 	print MAIL<<EOF;
# and
# 	EOF
# get printed to the email message generated by this script.
# The email message should be in plain text with no HTML code.
#
# You can print the values that were assigned to variables by including
#  $FORM{'variable_name'} in the print string, where variable_name is 
#  the name of any variable to which values were assigned in your form.
############################################################

# Print out a content-type for HTTP/1.0 compatibility: this tells user's browser
# that HTML code follows. Don't alter this line.

print "Content-type: text/html\n\n";

############################################################
# Check for unfilled form values and respond to user.
# The following code rejects the submission unless required fields are filled.
# The user is told to go back to the form and fill in the required fields.
#
# The conditions
# 	if (! $FORM{'variable_name'}) {
# will be true if the variable was not assigned by the form (i.e. if the
# form's field was left blank.
#
# Code is provided to check that the variables 'instructor', 'phone' 
# and other fields are filled in, otherwise you won't know who submitted
# the message. This provides no guarantee that the user will supply *accurate*
# information, though.
#
# You may repeat the section from 'elsif' to it's closing brace, substituting
# other variable names and messages, if you want to check more fields.
############################################################

if (! ($FORM{'y_name'})) {
print<<EOF; # print everything until EOF is encountered
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Your Name is needed!</H2><HR>
You must fill in the field marked "Your Name:".
<HR>Click your browser's "BACK" button to go back to
the form and fill in all required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end if

elsif (! ($FORM{'c_name'})) {
print<<EOF; # print everything until EOF is encountered
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Course Name is needed!</H2><HR>
You must fill in the field marked "Course Name:".
<HR>Click your browser's "BACK" button to go back to
the form and fill in all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'instr_date'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Date of instruction is needed!</H2><HR>
You must fill out the field marked "Instruction Date:".
<HR>Click your browser's "BACK" button to go back to
the form and fill in all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'instr_time'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Time of instruction is needed!</H2><HR>
You must fill out the field marked "Time:".
<HR>Click your browser's "BACK" button to go back to
the form and fill in all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'instructor'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Instructor is needed!</H2><HR>
You must select an Instructor from the drop down list in the field marked "Instructor:".
<HR>Click your browser's "BACK" button to go back to
the form and fill in all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'1'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Answer to Question 1 is needed!</H2><HR>
You must select an option for Question 1.
<HR>Click your browser's "BACK" button to go back to
the form and fill in all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'2'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Answer to Question 2 is needed!</H2><HR>
You must select an option for Question 2.
<HR>Click your browser's "BACK" button to go back to
the form and fill in all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'3'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Answer to Question 3 is needed!</H2><HR>
You must select an option for Question 3.
<HR>Click your browser's "BACK" button to go back to
the form and fill in all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'4'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Answer to Question 4 is needed!</H2><HR>
You must select an option for Question 4.
<HR>Click your browser's "BACK" button to go back to
the form and fill in all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'5'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Answer to Question 5 is needed!</H2><HR>
You must select an option for Question 5.
<HR>Click your browser's "BACK" button to go back to
the form and fill in all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'6'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Answer to Question 6 is needed!</H2><HR>
You must select an option for Question 6.
<HR>Click your browser's "BACK" button to go back to
the form and fill in all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'7'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Answer to Question 7 is needed!</H2><HR>
You must select an option for Question 7.
<HR>Click your browser's "BACK" button to go back to
the form and fill in all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'8'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Answer to Question 8 is needed!</H2><HR>
You must select an option for Question 8.
<HR>Click your browser's "BACK" button to go back to
the form and fill in all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'9'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Answer to Question 9 is needed!</H2><HR>
You must select an option for Question 9.
<HR>Click your browser's "BACK" button to go back to
the form and fill in all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

# added to generate a random number for each line so that Google Mail
# doesn't put duplicate lines into show quoted text

$rand_num1 = rand();
$rand_num2 = rand();
$rand_num3 = rand();
$rand_num4 = rand();
$rand_num5 = rand();
$rand_num6 = rand();
$rand_num7 = rand();
$rand_num8 = rand();
$rand_num9 = rand();
$rand_num10 = rand();
$rand_num11 = rand();
$rand_num12 = rand();
$rand_num13 = rand();
$rand_num14 = rand();
$rand_num15 = rand();
$rand_num16 = rand();
$rand_num17 = rand();
$rand_num18 = rand();

# We wouldn't get this far unless all required fields had input.

# Print a title and initial heading to user's browser. Modify at will 
# between print<<EOF; and EOF

print<<EOF;
<Head>
<Title>Faculty Instruction Evaluation Form</Title>
</Head>
<Body>
<H2>Faculty Instruction Evaluation Form</H2>
<HR>
Your evaluation has been sent to the ISU Oboler Library Instruction Staff.
<p><b>Please submit a separate evaluation for each class/section by returning to</b>
<a href="http://www.isu.edu/library/research/facultyinstructioneval.shtml">Faculty Instruction Evaluation Form</a>.
<br>
Thank You!
<br>
<HR>Return to <A HREF="http://www.isu.edu/library/">Library</A> home page
<HR></BODY></HTML>
EOF

################################
# Create email message
################################

# Start creating email message by piping output directly to sendmail program. 
# Don't alter this line.
open (MAIL, "|/usr/lib/sendmail $recipient") || die "Can't open sendmail!\n";

# Write email headers. The From: line of the outgoing email message will
# be automatically set to the user that the web server is running as, 
# not the original sender. You can't control the FROM line.
#
# Sender must enter his/her email address in a form field named 'email', 
# and that gets assigned to the Reply-To: field in the email message.
#
# In the space between the dashed lines, you can specify any text output to 
# the mail message.
#
# If you want to include the values of any variables sent from your form, 
# refer to them by $FORM{'variable-name'}
#
# The blank line after the SUBJECT: line is necessary, so don't delete it.
#
# You may remove the stuff between the 2nd dashed line and EOF if you wish:
# those lines print out information about where the form was posted from.
# You may also remove the dashed lines if you don't want them in your email
# message.

print MAIL<<EOF;
From: facinstrevalform@isu.edu
Subject: $FORM{'c_name'} for $FORM{'instructor'}

$rand_num1 - Name: $FORM{'y_name'}

$rand_num2 - Course Name: $FORM{'c_name'}

$rand_num3 - Date of Instruction: $FORM{'instr_date'}

$rand_num4 - Time of Instruction: $FORM{'instr_time'}

$rand_num5 - Instructor: $FORM{'instructor'}

$rand_num6 - Other Instructor: $FORM{'other_instructor'}
  
$rand_num7 - Question 1: $FORM{'1'}
    
$rand_num8 - Question 2: $FORM{'2'}
  
$rand_num9 - Question 3: $FORM{'3'}
  
$rand_num10 - Question 4: $FORM{'4'}
  
$rand_num11 - Question 5: $FORM{'5'}
  
$rand_num12 - Question 6: $FORM{'6'}
  
$rand_num13 - Question 7: $FORM{'7'}
  
$rand_num14 - Question 8: $FORM{'8'}
  
$rand_num15 - Question 8a: $FORM{'8a'}

$rand_num16 - Question 8b: $FORM{'8b'}

$rand_num17 - Question 9: $FORM{'9'}

$rand_num18 - Question 10 Comment: $FORM{'10_comment'}

EOF

# Close off the email message (which sends it) and exit.
close (MAIL);
exit;

