#!/opt/local/bin/perl -w

use strict;
use lib '/Library/WebServer/perl/lib';
use EduFileTwo;
use CGI;

my $form = new CGI;

my $login_id = $form->cookie('login_id');
my $course_id_unescaped = $form->param('course_id');
my $course_id = $form->escapeHTML($course_id_unescaped);


my $student_id = EduFileTwo::Util::getStudentIDFromCookie($login_id);

if(!$student_id)
{
	my $redirect_loc = EduFileTwo::Constants::APPLICATION_ROOT . "doerrormessage.cgi?error_no=1";
	print $form->redirect($redirect_loc);
	exit(0);
}

if(!EduFileTwo::Util::isLoggedIntoCourse( $student_id, $course_id ) )
{
	my $redirect_loc = EduFileTwo::Constants::APPLICATION_ROOT . "doerrormessage.cgi?error_no=4";
	print $form->redirect($redirect_loc);
	exit(0);
}

print $form->header;
EduFileTwo::Util::printHead("Welcome to EduFile");

my $courseObj = new EduFileTwo::Course($course_id, 1);
my $courseName = $courseObj->getCourseName();
my $courseDesc = $courseObj->getCourseDescription();
my $facultyID = $courseObj->getFacultyID();

my $instructor = new EduFileTwo::Faculty($facultyID);
my $instructorName = $instructor->getName();
my $instructorEmail = $instructor->getEmailAddress();

print <<END_HTML;
<div align="center">
<table align="center" cellpadding="3" cellspacing="1" width="85%" border="0" bgcolor="#000000">
<tr>
	<th colspan="2" align="center" bgcolor="#000000">
	<font color="#FAF5E2" size=4><b>Course Info</b></font>
	</th>
</tr>
<tr>
	<td bgcolor="#FAF5E2" align="right">
	<b>Instructor Name:</b>
	</td>
	<td bgcolor="#FAF5E2">
END_HTML
	my $out = "       $instructorName (<a href=\"mailto:$instructorEmail\">$instructorEmail</a>)\n";
	print $out;
print <<END_HTML;
	</td>
</tr>
<tr>
	<td bgcolor="#FAF5E2" align="right">
	<b>Course Name:</b>
	</td>
	<td bgcolor="#FAF5E2">
	$courseName
	</td>
</tr>
END_HTML

print <<END_HTML;
<tr>
	<td bgcolor="#FAF5E2" align="right" valign="top">
	<b>Course Description:</b>
	</td>
	<td bgcolor="#FAF5E2">
	$courseDesc
	</td>
</tr>
</table>
</div>
END_HTML

print <<END_HTML;
<div id="content2" align="center">
        <p>Click on the Filename to download the file you need.</p>

          
        <table width="600" border="0" cellspacing="1" cellpadding="3" bgcolor="#000000" align="center">
          <tr> 
            <td colspan="2"> 
              <table width="600" border="1" cellspacing="0" cellpadding="4" bgcolor="#FAF5E2" align="center">
                <tr bgcolor="#666666"> 
                  <td> 
                    <div align="center"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><font size="2"><font size="2"><font color="#FAF5E2">Update 
                      Since Last Download?</font></font></font></font></font></div>
                  </td>
                  <td> 
                    <div align="center"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><font size="2"><font size="2"><font color="#FAF5E2">Downloaded</font></font></font></font></font></div>
                  </td>
                  <td> 
                    <div align="center"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><font size="2" color="#FAF5E2">Filename</font></font></font></div>
                  </td>
                  <td> 
                    <div align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#FAF5E2">File 
                      Description</font></div>
                  </td>
                </tr>
END_HTML

my @files = EduFileTwo::FileData::getAllOrderBy($course_id, "categoryid", $student_id);
my $prev_category_id = -500;
foreach my $file (@files)
{ 
	my $fileID = $file->getID();
	my $description = $file->getDescription();
	my $wasdownloaded = $file->getWasDownloaded();
	my $category_id = $file->getCategoryID();
        my $fileSeqId = $file->getSequenceID();
	my $updatedSinceDownload = $file->getUpdatedSinceLastDownloaded();
	my $category = new EduFileTwo::Category($category_id);

	if($description eq "")
	{
		$description = "&nbsp;";
	}
	my $filename = $file->getID();
	
	if($category_id != $prev_category_id)
	{
		my $category_name = $category->getName();
print <<END_HTML;
    <tr> 
      <td colspan="5" bgcolor="#D4B77F"> 
        <div align="left"><b><font face="Arial, Helvetica, sans-serif" size="2">$category_name</font></b></div>
      </td>
	</tr>
END_HTML
	$prev_category_id = $category_id;
	}
	my $urlencodefilename = $filename;
	$urlencodefilename =~ s/ /\%20/g;
print <<END_HTML;
    <tr> 
      <td> 
        <div align="center"><font face="Arial, Helvetica, sans-serif" size="2">$updatedSinceDownload</font></div>
      </td>
      <td> 
        <div align="center"><font face="Arial, Helvetica, sans-serif" size="2">$wasdownloaded</font></div>
      </td>
      <td><font face="Arial, Helvetica, sans-serif" size="2"><a href="downloadfile.cgi?file_id=$urlencodefilename&file_seq_id=$fileSeqId&course_id=$course_id">$filename</a></font></td>
      <td><font face="Arial, Helvetica, sans-serif" size="2">$description</font></td>
    </tr>

END_HTML

}
				
print <<END_HTML;
              </table>
            </td>
          </tr>
        </table>

<form action="edituser.cgi" method="post">
<input type="hidden" name="user_id" value="$student_id">
<input type="Submit" value="Edit Personal Info">
</form>

      </div>

END_HTML


EduFileTwo::Util::printFoot();

#Subroutines
sub printMessage{
	print "<h1>$_[0]</H1>";
	print "<p>$_[1]</p>";
}
