#!/usr/local/bin/perl
package viewstudent;
# use strict;
use lib '\\\\140.247.216.106\\inetpub\wwwroot\is';
use CGI ("shortcuts");
use CGI::Carp qw(fatalsToBrowser);
$query=new CGI;
#Use the ODBC module
use Win32::ODBC;
#our dsn
my $DSN='DRIVER={Microsoft Access Driver (*.mdb)};DBQ=\\\\140.247.216.106\\inetpub\\wwwroot\\is\\database\\hls_is_981.Mdb';
$user=$query->remote_user();
use ISLib;
my $ISobj=new ISLib('View Data');
print $query->header();

#open a connection to it
my $O = new Win32::ODBC($DSN) || die "Unable to open DSN $DSN\n";
$StudentID=$query->param('StudentID');
my $errorstring="";
my $liststring="";
my $menustring="";
if ($StudentID eq "") {$errorstring.="Error: no student chosen"}
unless ($errorstring) {
(! $O->Sql("SELECT *
	FROM Students
	WHERE StudentID=$StudentID")) || print $O->DumpError();
 while ($O->FetchRow()) {
	%sdata=$O->DataHash();
	$profilestring.=qq!<a name="top"></a><font size="+2">Profile: \u$sdata{'FirstName'} \u$sdata{'LastName'}</font> $sdata{Affiliation}<br>\n!;
	if (($user eq "is98") || ($user eq "webedit")) { $liststring.=qq! Name: \u$sdata{FirstReal} \u$sdata{LastReal}<br>\n!; } 
	$profilestring.= qq! <b>Email:</b> <a href="mailto:$sdata{'EmailName'}">$sdata{'EmailName'}</a>, <b>Phone:</b> $sdata{'PhoneNumber'},!;
	$profilestring.= qq!
		<b>Hometown:</b> $sdata{Hometown}<br>\n
		<b>Favorite website:</b> $sdata{FavoriteWeb}<br>\n
		<b>Favorite book:</b> $sdata{FavoriteBook}<br>\n
		<b>Internet research interests:</b> $sdata{Research}<br>\n
		<b>Fun facts:</b> $sdata{FunFacts}<br>\n
       		<hr width="75%" size="1"> !;
	}
 $i=1; #Number questions

 %sdata=();
 (! $O->Sql("SELECT *
	FROM Questions, Answers
	WHERE Answers.StudentID=$StudentID
	AND Questions.QuestionID = Answers.QuestionID
	ORDER BY Week, Questions.QuestionID")) || print $O->DumpError();

 while ($O->FetchRow()) {
    %sdata=$O->DataHash();
  if ( ($sdata{Closed}==1) || ($user eq "is98" ) || ($user eq "webedit") ) {
	$liststring.=qq!<DL><DT><a name="question$sdata{'QuestionID'}"></a><font size="-1">Question $i \($sdata{'QuestionID'}\): $sdata{'QuestionText'}</font>\n!;
	$short=$ISobj->shorten($sdata{'QuestionText'}, 60);
	$menustring.=qq!<a href="#question$sdata{'QuestionID'}">Question $i \($sdata{'QuestionID'}\): $short</a><br>\n!;
	my %words=(1 => 100,
	3 => 250,
	5 => 500,
 	10 => "" );
	$limit= ($words{$sdata{WordLimit}} || -1);
	$ans=$ISobj->redline($sdata{'TextAnswer'},$limit);
	$liststring.=qq!<DD><b>Answer:</b> $ans &nbsp\n!;
	   $i++;
	#get the text of multchoice.
	unless ($sdata{'MultChoice'}==7) { 
	   @choices="";
	   @choices=split /;/, $sdata{'Choices'};
	   $choice=$choices["$sdata{'MultChoice'}"];				
  	   $liststring.=qq!<br>Poll response: "$choice" \n!;
		}
	   ##if this had a response, print that too.
	   #if ($sdata{'ResponseRequested'}==1){
		$aID=$sdata{'AnswerID'};
		##open the response db, with a new object
		$O2=new Win32::ODBC($DSN) || print "Can't open DSN $DSN $!";
		(! $O2->Sql("SELECT * 
			FROM Responses, Students
			WHERE AnswerID=$aID
			AND Responses.StudentID=Students.StudentID
			"))|| print $O2->DumpError();
		while ($O2->FetchRow()){
		 %rdata=$O2->DataHash();
	         if ( ($rdata{Closed}==1) || ($user eq "is98" ) || ($user eq "webedit") ) {
	   	   if ($rdata{'Response'} ne "") {
		      $liststring.=qq!<DD><DL><DT><p><font size="-1"><i>Response from</i> \u$rdata{'FirstName'} \u$rdata{'LastName'}:</font>\n
			<DD>$rdata{'Response'}\n
			</DL>!;
		      }
		   } #end admin verif.
       		 }	
	##now go through responses
##doesn't do anything if ResponseRequested flag isn't set
	(! $O2->Sql("SELECT * FROM Responses, Answers, Questions, Students
	WHERE Responses.StudentID=$StudentID
	AND Answers.AnswerID=Responses.AnswerID
	AND Questions.QuestionID=Answers.QuestionID
	AND Answers.QuestionID=$sdata{QuestionID} 
	AND Students.StudentID=Answers.StudentID
	ORDER BY Questions.QuestionID ASC"))  || print $O2->DumpError();
	while ($O2->FetchRow()){
	   %moredata=$O2->DataHash();
	   if ($moredata{Response} ne ""){	
	     $liststring.=qq!<DD><p><font size="-1"><i>Response to</i> $moredata{FirstName} $moredata{LastName}'s Answer:<DL><DD> $moredata{'TextAnswer'}</font></DL> &nbsp\n!;
	     $liststring.=qq!<DD><b>Response</b>: $moredata{Response} </DL>\n!;
	     }
	   }
	$O2->Close() || print "Can't close connection 2";
	#   }
	
	$liststring.=qq!</DL><center><a href="#top">[top]</a></center>\n!;
	} #end admin verification	
   } # end that row question
##end validate for query block
} else {$menustring.="$errorstring";
}
print $ISobj->pagetop;
print qq!$profilestring \n $menustring \n 
 <hr width="75%" size="1"> \n
 $liststring!;
print $ISobj->pagebottom;