#!/usr/bin/perl BEGIN { open (STDERR, ">error.txt"); } #************************************************************ #** Robert W. Arts, M.S., Ph.D.** #** Pikeville College** #** 147 Sycamore Street** #** Pikeville, KY 41501** #** (606) 218-5476** #** Email: rarts@pc.edu** #************************************************************ # This Perl script is designed to accept a single input (text or numeric) # from a form, look for them in a database type flat file (comma delimited) , and # return a HTML page with the match (hopefully only one) in a table. # The script's design is to allow a student to input a designated code number # in order to retrieve their updated (defined by the instructor) course # grades. This script generates a HTML FORM, with itself as the "ACTION" # When this script is called it checks the STDIN for data it may # be passing to itself. If there is none, it generates the FORM # plus some help text on how to use it. If it does see search # keys in STDIN ($keys) it will again print the FORM, skip the # help text, open the data file, search for matches, and print # them as a HTML table. #****************************************************************** #******************** Subroutines ******************************** #****************************************************************** #****************************************************************** #**************** Get Passed data from form *********************** #****************************************************************** sub GET_STATE_INFO { read(STDIN, $save_string, $ENV{CONTENT_LENGTH}); # Yes- Use it @prompts = split(/&/,$save_string); foreach (@prompts) { ($tmp1, $tmp2) = split(/=/,$_); $tmp2 =~ s/\x2b/\x20/g; $tmp2 =~ s/%2C/\x2c/g; $tmp2 =~ s/%28/\x28/g; $tmp2 =~ s/%29/\x29/g; $tmp2 =~ s/%30/\x30/g; $tmp2 =~ s/%31/\x31/g; $tmp2 =~ s/%32/\x32/g; $tmp2 =~ s/%33/\x33/g; $tmp2 =~ s/%34/\x34/g; $tmp2 =~ s/%35/\x35/g; $tmp2 =~ s/%36/\x36/g; $tmp2 =~ s/%37/\x39/g; $tmp2 =~ s/%38/\x39/g; $tmp2 =~ s/%39/\x39/g; $fields{$tmp1}=$tmp2; } $keys = $fields{'keys'}; $search_type = $fields{'search_type'}; } #****************************************************************** #********** Search file for lines with all search keys ************ #****************************************************************** sub SEARCH_FOR_MATCH { @search_key = split(/\x20/,$keys); open(MYFILE,"102.csv") || print "

Error: Can't open grade book file<\H3>"; while() # Our Read loop { $in_line = $_; if ($search_type eq "and") { $found_flag = "Y"; } else { $found_flag = "N"; } foreach (@search_key) #check for each search key in the file line { if ( $in_line =~ m/$_/i) # Is key in the data line ? { if ($search_type eq "or") # Yes- { $found_flag = "Y"; # If its a OR search all we last; # need id one match } } else #No- { if ($search_type eq "and") # If its a AND search all we { # need is one non-match $found_flag = "N"; last; } } } #end of ForEach Loop if ( $found_flag eq "Y" ) { $kounter++; $in_line = $_; # split line in grade book data file into separate fields for printing in our table ($code, $datetime, $hw, $ap, $checks, $sum, $note, $test, $ec, $total, $avg, $grade, $hwc, $apc, $checksc, $sumc, $notec, $testc, $totalc) = split(/,/,$in_line); print "", $code, "\n"; print "", $datetime, "\n"; print "", $hw, "\n"; print "", $ap, "\n"; print "", $checks, "\n"; print "", $sum, "\n"; print "", $note, "\n"; print "", $test, "\n"; print "", $ec, "\n"; print "", $total, "\n"; print "", $avg, "\n"; print "", $grade, "\n"; print "\n"; print "Points Possible = \n"; print "", $hwc, "\n"; print "", $apc, "\n"; print "", $checksc, "\n"; print "", $sumc, "\n"; print "", $notec, "\n"; print "", $testc, "\n"; print "\n"; print "", $totalc, "\n"; print "\n"; $kounter++; if ( $kounter > $max_hits) { last; } # break out of read loop } } # end of Read loop } #****************************************************************** #**************** Main Routine *********************** #****************************************************************** $max_hits = 1; #Set the maximum number of matches to be returned $kounter = 0; #Intialize counter that counts hits returned print("Content-Type: text/html\n\n"); print("Physics 102 - Course Grade Retrieval Form\n"); print("\n"); &GET_STATE_INFO; print "

Physics 102 - Current Course Grade Lookup

\n"; print "

Enter your personal code number in the box below. \n"; print "

\n"; print "\n"; #print "
Select Search type: \n"; $search_type = "and"; #print "\n AND"; #print "\n OR"; print "\n\n"; print "\n
\n"; if ( $keys eq "") { print ""; } else { print "
"; print "Your Current Course Grades Are:"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; &SEARCH_FOR_MATCH($keys); if ($kounter > $max_hits+1 ) { print "\n"; print "\n"; } print "
Code #Date & Time UpdatedTotal Homework PointsTotal Attendance PointsTotal Check PointsTotal Summary PointsTotal Notebook PointsTotal Exam PointsTotal Extra Credit PointsTotal PointsCurrent Average PercentageCurrent Letter Grade
Limit of one match exceeded. Please contact instructor.
\n"; } print("\n"); print("\n");