#!/usr/cs/bin/perl
# -*- perl -*-  -*- font-lock -*-
#
# User-defined Variables.  Set these to what you need them to
# be.

use CGI qw( :all ), u; # make this a CGI page

use Valid; # local copy of Email::Valid
use Lite; # local copy of MIME::Lite

use POSIX; # the strftime command...
use HTTP::Request::Common; # the POST command...
use LWP::UserAgent; # ...and something that can send it over TCP/IP

my $query  = new CGI;

my $APPLICATION_FOLDER
         = "/home/cohoon/tapestry/2012/private/submissions/";

my %emails = (
	'Virginia' => 'tapestry-workshop@cs.virginia.edu',
	'Pennsylvania' => 'rpowell@cis.upenn.edu',
	'North Carolina' => '2012ncsu.tapestry.workshop@gmail.com',
	'Nebraska' => 'lksoh@cse.unl.edu',
	'Michigan' => 'tapestry@secs.oakland.edu',
	'Minnesota' => 'jrosato@css.edu',
);

my %urls = (
	"North Carolina" => "http://www.csc2.ncsu.edu/workshops/tag/uva_app.php",
);


print $query->header;
print $query->start_html( -title => 'Workshop Application',
                          -BGCOLOR => "#ffffff" );


sub trim($) { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s; }

my $errorMessage = "";




my $unchecked = qr/(?:)/;
my $nonblank = qr/\S/;
my $filters = [
	#  fieldName            display name (if different)       regular expression
	["firstChoice",        "First workshop of interest",      $nonblank],
	["secondChoice",       "Second workshop of interest",     $unchecked],
	["thirdChoice",        "Third workshop of interest",      $unchecked],
	["name",               "Name",                            $nonblank],
	["position",           "Position",                        $nonblank],
	["school",             "School",                          $nonblank],
	["street1",            "Street address",                  $nonblank],
	["street2",            "Street address 2",                $unchecked],
	["city",               "Town/City",                       $nonblank],
	["state",              "State",                           $nonblank],
	["zipcode",            "Zipcode",                         qr/^[0-9]{5}(-[0-9]{4})?$/],
	["phone",              "School telephone",                qr/[0-9]{3}[ \-\.]?[0-9]{3}[ \-\.]?[0-9]{4}/],
	["email1",             "School e-mail address",           qr/^[A-Za-z0-9._%+-]+@(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,6}$/],
	["email2",             "School e-mail address",           $unchecked],
	["semail1",            "Primary e-mail address",          qr/^[A-Za-z0-9._%+-]+@(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,6}$/],
	["semail2",            "Primary e-mail address",          $unchecked],
	["sphone",             "Primary telephone",               qr/[0-9]{3}[ \-\.]?[0-9]{3}[ \-\.]?[0-9]{4}/],
	["cell",               "Cell phone",                      $unchecked],
	["curriculum",         "Computing courses at school",     $nonblank],
	["courses",            "Computing courses you teach",     $nonblank],
	["responsibilities",   "Other responsibilities",          $unchecked],
	["schoolLevel",        "School level",                    $nonblank],
	["schoolSetting",      "School setting",                  $nonblank],
	["schoolSex",          "School population",               $nonblank],
	["schoolType",         "School type",                     $nonblank],
	["schoolMission",      "School mission",                  qr/^[^\(]|^\(other\)..+/],
	["schoolSize",         "School size",                     $nonblank],
	["schoolMixture",      "School racial minorities",        $nonblank],
	["gradesTaught",       "Grades taught",                   $nonblank],
	["progStudents",       "Programming enrollment",          qr/^[0-9]+$/],
	["progFemale",         "Females in programming",          qr/^[0-9]+$/],
	["progUnder",          "Minorities in programming",       qr/^[0-9]+$/],
	["apStudents",         "AP CS enrollment",                qr/^[0-9]+$/],
	["apFemale",           "Females in AP CS",                qr/^[0-9]+$/],
	["apUnder",            "Minorities in AP CS",             qr/^[0-9]+$/],
	["priorAttendance",    "Last Tapestry",                   $nonblank],
	["expectation",        "Expectations for workshop",       $nonblank],
	["heardAboutWorkshop", "How you found this workshop",     $nonblank],
	["priorHelp",          "How last Tapestry helped you",    $unchecked],
	["questionComment",    "Questions or comments",           $unchecked],
];

my %posting = ();
foreach my $row (@$filters) {
	my ($field, $text, $regex) = @$row;
	my @values = $query->param($field);
	my $value = join(",", @values);
	if ($field eq "email2") {
		if ( $posting{"email1"} ne $value ) {
			$errorMessage .= "

E-mail fields don't match

\n"; } } elsif ($field eq "semail2") { if ( $posting{"semail1"} ne $value ) { $errorMessage .= "

Summer E-mail fields don't match

\n"; } } else { if ( $value =~ $regex ) { $posting{$field} = trim($value); } else { $errorMessage .= "

$text field missing or invalidly formatted.

\n"; } } } if ( ! $posting{"priorHelp"} && $posting{"priorAttendance"} =~ /20[0-9][0-9]/ ) { $errorMessage .= "

Repeat applicants should describe the benefit they gained last time.

\n"; } if ( $errorMessage ne '' ) { print $errorMessage; print "


Please go back

"; exit; } # everything is supplied and checks out # figure out the year of application... august->december = next year my @now = gmtime; my @local = localtime; my $timeStamp = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime); my $year = 1900+$now[5] + ($now[4] > 7); # create the various deliverables my $textual = ""; my $html = ""; my $csv = ""; my $csvhead = ""; foreach my $row (@$filters) { my ($field, $text, $regex) = @$row; next unless defined $posting{$field}; my $value = $posting{$field}; $value =~ s/\f/ /g; if ( $value ) { $textual .= "$text: \t$value\t\n"; } my $bit = $value; $bit =~ s/&/&/g; $bit =~ s//>/g; $bit =~ s/[\n\r]+//g; $bit =~ s/'/'/g; $bit =~ s/"/"/g; $html .= "\n"; $bit = $value; $bit =~ s/[\n\r]+/ /g; $bit =~ s/"/''/g; $csvhead .= "\"$field\","; $csv .= "\"$bit\","; } $textual .= "Time stamp: \t@timestamp\t\n"; $html .= "\n"; $html .= "
$text$bit
Time stamp:$timeStamp
"; $csvhead .= "\"timeStamp\""; $csv .= "\"$timeStamp\""; $posting{"timeStamp"} = "$timeStamp"; # usage: saveFile("raw-request-$year.csv", $csv, "\n", true) sub saveFile { my $filename = shift; my $data = shift; my $seperator = shift; my $showError = shift; $filename =~ s/\s+//g; my $file = $APPLICATION_FOLDER . "$filename"; unless ( open( STREAM, ">>" . $file ) ) { if ($showError) { print "
Problem with server, please try resubmitting.
"; exit; } } print STREAM $data . $seperator; close( STREAM ); } saveFile( "raw-request-$year.txt", $textual, "\n\f\n", true ); saveFile( "raw-request-$year.csv", $csv, "\n", true ); # print web page results print "

Thank you for applying to the workshop.

You should receive an email shortly acknowledging your application.

" . "Application information" . "
\n" . $html . "
"; $firstChoice = $posting{'firstChoice'}; $secondChoice = $posting{'secondChoice'}; $thirdChoice = $posting{'thirdChoice'}; sub notify { my $whom = shift; saveFile( "$whom-$year.txt", $textual, "\n\f\n", true ); saveFile( "$whom-$year.csv", $csv, "\n", false ); if ($emails{$whom}) { $msg = MIME::Lite->new( From => 'cohoon@virginia.edu', To => $emails{$whom}, Cc => 'tapestry.applications@gmail.com', Subject => "$year Tapestry workshop application", Type => 'multipart/mixed', ); $msg->attach(Type => 'text/plain' ,Data => $textual ); $msg->attach(Type => 'text/csv' ,Data => "$csvhead\n$csv" ,Filename => "applicant.csv" ); $msg->send; } if ($urls{$whom}) { my $ua = LWP::UserAgent->new; my $response = $ua->request( POST $urls{$whom}, \%posting ); } } notify( $posting{'firstChoice'} ); if ( ( $posting{'secondChoice'} ne 'None' ) && ( $posting{'firstChoice'} ne $posting{'secondChoice'} ) ) { notify( $posting{'secondChoice'} ); } if ( ( $posting{'thirdChoice'} ne 'None' ) && ( $posting{'secondChoice'} ne $posting{'thirdChoice'} ) && ( $posting{'firstChoice'} ne $posting{'thirdChoice'} ) ) { notify( $posting{'thirdChoice'} ); } # my $sendmail = "/usr/sbin/sendmail -t -oi -oem"; my $msg = MIME::Lite->new( From => 'cohoon@virginia.edu', To => $posting{'email1'}, Cc => 'tapestry-workshop@cs.virginia.edu', Bcc => 'tapestry.applications@gmail.com', Subject => 'Confirmation of your ' . $year . ' Tapestry workshop application', Type => 'multipart/mixed', ); $msg->attach(Type => 'text/html' ,Data => "

Dear $posting{name},

Thank you for applying to Tapestry $year. The application materials we have received are included below.

$html" ); $msg->send; print $query->end_html;