#!/usr/local/bin/perl
# signin.pl
# Author: Anthony Chang
# Description: Authenticates user to data file and generate member list
# Date created: 1/19/97
# Modification history:
#   1/22/97: Added functionality to list group members
#   1/25/97: Added functionality to check for existing evaluation entry

# Define data file
$classfile = "class.txt";
$groupfile = "groups.txt";
$evalfile = "eval.txt";


# Get input and assign to variables
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split up
@pairs = split(/&/, $buffer);
foreach $pair (@pairs){
  ($name, $value) = split(/=/, $pair);
  # Strip the string
  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $FORM{$name} = $value;
}

# Open data file
open (GROUPFILE, "$groupfile");
open (EVALFILE, "$evalfile");
open (CLASSFILE, "$classfile");


# Read data into arrays
@class = split(/ /, <CLASSFILE>);
$count = @class;

$groupcount=0;
while (<GROUPFILE>) {
	@groups[$groupcount] =  $_;
	$groupcount++;
}

$evalcount=0;
while (<EVALFILE>) {
	@eval[$evalcount] = $_;
	$evalcount++;
}

$userid = $FORM{'userid'};
$ssn = $FORM{'ssn'};

# $userid = 'guest';
# $ssn = '999999999';

# Validate userid
$pos=0;
while ($pos < $count) {
	$pword = $pos + 1;
	$idmatch = @class[$pos] cmp $userid;
	$pwdmatch = @class[$pword] cmp $ssn;
	if (($idmatch == 0) && ($pwdmatch == 0)) {
		$current = 0;
		while ($current < $groupcount) {
			@memlist = split(/:::/, @groups[$current]);
			$match = @memlist[0] cmp $userid;
			if ($match == 0) {
				$memcount = @memlist;
				# Peer evaluation
				print "Content-type: text/html\n\n";
				print "<HTML><HEAD><TITLE>90-727: Peer Evaluation Sheet</TITLE></HEAD><BODY>\n";
				print "<BODY BGCOLOR=\"#FFFFFF\" LINK=\"#006666\" VLINK=\"#333333\" ALINK=\"#CCCCCC\">\n";
				print "<H1>" . @memlist[1] . " " . @memlist[2] . "\'s Peer Evaluation Sheet</H1><BR>\n";

				print "<FORM ACTION=\"http://www.heinz.cmu.edu/~tinaikar/cgi/dss/process.pl\"";
				print "METHOD=\"POST\"> \n";

				$evalpos = 0;
				$existing = 0;
				while ($evalpos < $evalcount) {
					@evallist = split(/:::/, @eval[$evalpos]);
					$exist = @evallist[0] cmp $userid;
					if ($exist == 0) {
						$existing = 1;
						print "<HR><P>Our records indicate that you have already filled out ";
						print "your evaluation form on @evallist[1] <P><BR>\n";
						print "<CENTER><TABLE WIDTH=\"80%\">\n";
						print "<TR>\n";
						print "<TD WIDTH=\"60%\"><CENTER><B>Group Member</B></CENTER></TD>\n";
						print "<TD WIDTH=\"40%\"><CENTER><B>Grade</B></CENTER></TD>\n";
						print "</TR>";
						$rowcount = 0;
						$first = 5;
						while ($rowcount < @evallist[4]) {
							$last = $first + 1;
							$grade = $first + 2;
							print "<TR>\n";
							print "<TD WIDTH=\"60%\"><CENTER>@evallist[$first] @evallist[$last] </CENTER></TD>\n";
							print "<TD WIDTH=\"40%\"><CENTER> @evallist[$grade] </CENTER></TD>\n";
							print "</TR>";
							$rowcount ++;
							$first += 3;
						}
						print "<TR>";
						print "<TD WIDTH=\"100%\"><B>Comments:</B><BR><BR>\n";
						@evallist[$first] =~ s/\n//;
						print "@evallist[$first] \n";
						print "</TD></TR></TABLE></CENTER><BR>\n";
						print "If you would like change your evaluation, please fill out the form below.\n";
					}
					$evalpos++;
				}
				
				print "<INPUT TYPE=hidden NAME=\"status\" ";
				print "VALUE=\"";
				
				if ($existing == 0) {
					
					print "new\">\n";
				}
				else {
					print "existing\">\n";
				}

				print "<HR><P>Please use the following form to evaluate the contribution of ";
				print "your group members to the term project.  You are to grade each of them ";
				print "on a scale of 1-5.  Make sure you don't evaluate the &quot;goodness&quot; ";
				print "of the project, but the contribution of the members to the project!</p> \n \n";
				
				print "<P><HR><P> \n";
				print "<INPUT TYPE=hidden NAME=\"userid\" ";
				print "VALUE=\"" . @memlist[0] . "\">\n";
				print "<INPUT TYPE=hidden NAME=\"Name_FirstName\" ";
				print "VALUE=\"" . @memlist[1] . "\">\n";
				print "<INPUT TYPE=hidden NAME=\"Name_LastName\" ";
				print "VALUE=\"" . @memlist[2] . "\">\n";
				print "<FONT SIZE=4><STRONG>Evaluate your Group Members</STRONG></FONT><P> \n\n";

				# Print member evaluation
				$cur = 4;
				@memlist[($memcount-1)] =~ s/\n//;
				while ( $cur < $memcount) {
					$mempos = (($cur/2) - 1);
					$lname = $cur + 1;
					print "<BR><STRONG> @memlist[$cur] @memlist[$lname] :</STRONG><P>";
					print "<INPUT TYPE=hidden NAME=\"Member" . $mempos . "_FirstName\" ";
					print "VALUE=\"" . @memlist[$cur] . "\">\n";
					print "<INPUT TYPE=hidden NAME=\"Member" . $mempos . "_LastName\" ";
					print "VALUE=\"" . @memlist[$lname] . "\">\n";
					print "Choose the appropriate grade points: \n";
					print "<SELECT NAME=\"Grade" . $mempos . "\" SIZE=1>\n";
					print "<option selected VALUE=0>0 - No Contribution</option>\n";
					print "<option VALUE=1>1- Poor Contribution</option>\n";
					print "<option VALUE=2>2 - Did contribute, but not very satisfactory</option>\n";
					print "<option VALUE=3>3 - Not too good, not too bad</option>\n";
					print "<option VALUE=4>4 - Good contribution</option>\n";
					print "<option VALUE=5>5 - Excellent contribution</option>\n";
					print "</select><P><br>\n";
					$cur += 2;
				}
				
				print "<INPUT TYPE=hidden NAME=\"num_member\" ";
				print "VALUE=\"" . $mempos . "\">\n";
				
				print "<STRONG>Any further comments regarding your group?</STRONG><P>\n";
				print "<p><textarea name=\"comments\" rows=3 cols=62></textarea></p>\n";
				print "<p><input type=submit value=\"Submit Form\"> <input type=reset value=\"Reset Form\"> </p></form>\n";

				# Print footer
				print "</BODY></HTML>\n";

				close (CLASSFILE);
				close (GROUPFILE);
				close (EVALFILE);
				exit;
			}
			
			$current++;
		}
	}
	
	$pos += 2;
}


# Wrong password
print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>Login Failed</TITLE></HEAD><BODY>\n";
print "<BODY BGCOLOR=\"#FFFFFF\" LINK=\"#006666\" VLINK=\"#333333\" ALINK=\"#CCCCCC\">\n";
print "<H1>Login Failed</H1><BR>\n";
print "<HR><P>This server could not verify that you are authorized to access the document you ";
print "requested. Either you supplied the wrong credentials (e.g., bad password), or your browser ";
print "doesn't understand how to supply the credentials required.  Please ";
print "click on the Back button and try again. \n";
print "</BODY></HTML>\n";


# Close file
close (CLASSFILE);
close (GROUPFILE);
close (EVALFILE);


# End main structure
# -----------------------------------------------------------


# Character replacements
sub replace_chars {
    # Remove CRLF, replace with <BR>
    $FORM{$formname} =~ s/\r\n/<BR>/g;
    # Remove return, replace with <BR>
    $FORM{$formname} =~ s/\r/<BR>/g;
    # Remove newline(s), replace with <BR>
    $FORM{$formname} =~ s/\n\n/<BR>/g;
    $FORM{$formname} =~ s/\n/<BR>/g;}
