#!/pkg/bin/perl
push(@INC,"/www/httpd/cgi-bin");
require 5.003;
require "cgi-lib.pl";
require "diag-ans.pl";                # only contains &Reply

#-------------------------- What This Does ----------------------------
# This gets input form from diag.html, grades the exam, and gives a new 
# form with the correct answers and solutions.
# 
#----------------------------- Configure ----------------------------
# This file has the grades on this exam. Can use in Excel.
$diag_grades = "/www/httpd/cgi-bin/210/diag/grades.csv";

# This one line file has the grade totals. 
$totals = "/www/httpd/cgi-bin/210/diag/totals";

# Answers to the problems. Note: $ans[0]=answer to problem #1
@ans = ("d","c","d","d","b", "a","b","b","b","b", "c","c","b","b","b",
	 "d","d","c","e","b", "d","c","b","c","b");	# Answers
$NunbProbs = $#ans +1;			# Number of problems on exam

# Text Fields -- we'll prune these for inadvertent tabs.
@TextFields = ("last","first","class","major","stat","computer");
#---------------------------- Main Program ----------------------------
&ReadParse;
print &PrintHeader;
&Preliminaries;
&GradeExam;
&RecordGrades;
&Reply;        # from diag-ans.pl

#----------------------------------------------------------------------
sub Preliminaries {

# Open the Totals file to read the totals so far.
# If the file does not exits, then create it with all zeroes.
if (! -e $totals ) { 
	$NewTotals = '0,' x $NunbProbs;
	`echo "$NewTotals" >$totals`; 
}
open(TOTALS, "$totals") || die "Can't open $totals\n";
while(<TOTALS>) {
chomp;				# Removes the line ending (Line Feed).
@Totals = split(/,/);		# Split the input at each comma
close (TOTALS);
}

$score = 0;			# initialize (score for this person)
$summary = "";			# initialize
$newdata = "";			# initialize
$wrong = "";			# initialize

# Record the Text Fields in the list $newdata
foreach $key (@TextFields) {
	$newdata = "$newdata\"$in{$key}\",";
}
# Add the answers to the supplementary questions A1-A3:
$newdata = "$newdata$in{A1}\",\"$in{A2}\",\"$in{A3}\",";
} #end Preliminaries

#----------------------------------------------------------------------
sub GradeExam {
for ($k = 1; $k<=$NunbProbs; $k++) { 
	$prob = "N$k";
	if ( $in{$prob} eq $ans[$k-1] ) { 
#		print "in{prob} = $in{$prob} ans = $ans[$k-1]\t";
#		print "$prob is correct<p>";
		$score++;		# Increment score for this person
		$Totals[$k-1]++;	# Increment class totals
		}
	else {
#		print "in{prob} = $in{$prob}  ans = $ans[$k-1]\t";
#		print "$prob is wrong<p>";
#		$bad{$prob} = "$in{$prob}";
	    ($prob1 = $prob) =~ s/N//;
		$wrong = "$wrong\"$prob:$in{$prob}\",";	# Incorrect problems
		$wrong1 = "$wrong1 &nbsp; $prob1" ;# Incorrect problems
		}
}
} # end GradeExam

#----------------------------------------------------------------------
sub RecordGrades {

# First we record the new class totals:
for ($k = 1; $k<=$NunbProbs; $k++) {
	$summary = "$summary$Totals[$k-1],";
}
`echo "$summary" >$totals`;	# Record new course score totals

# Next we record the data for this person
$newdata = "$newdata\"$score\",\#$wrong";

open(DATA, ">>$diag_grades") || die "Can't open $diag_grades\n";
print DATA "$newdata\n";		# Record data for this person
} # end RecordGrades

#----------------------------------------------------------------------







