#!/usr/bin/perl
#  Copyright 2001-2022 Leslie Richardson

#  This file is part of Open Admin for Schools.


my %lex = ('Update' => 'Update',
	   'Edit' => 'Edit',
	   'Attendance' => 'Attendance',
	   'Student' => 'Student',
	   'Date' => 'Date',
	   'Reason' => 'Reason',
	   'Record(s) Updated' => 'Record(s) Updated',
	   'Contact' => 'Contact',
	   'Another' => 'Another',
	   'Today' => 'Today',
	   'Yesterday' => 'Yesterday',
	   'All' => 'All',
	   'Period' => 'Period',
	   'Subject-Section' => 'Subject-Section',
	   'min' => 'min',
	   'Late' => 'Late',
	   'Main' => 'Main',
	   'Error' => 'Error',
	   'Delete' => 'Delete',
	   'Grade' => 'Grade',
	   
	   );

my $self = 'atted.pl';

use DBI;
use CGI;

eval require "../../etc/admin.conf";
if ( $@ ) {
    print $lex{Error}. " $@<br>\n";
    die $lex{Error}. " $@<br>\n";
}

my $q = new CGI;
print $q->header( -charset, $charset ); 
my %arr = $q->Vars;


my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;

my $sth = $dbh->prepare("select * from attend where attid = ?"); 
$sth->execute( $arr{id} );
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
my ($attid, $studnum, $absdate, $reason, $period, $subjsec, $late) = $sth->fetchrow;


# Print Page Head.
my $title = "$lex{Edit} $lex{Attendance}";
print qq{$doctype\n<html><head><title>$title</title>
<link rel="stylesheet" href="$css" type="text/css">
$chartype\n</head><body style="padding:1em;">\n};

print qq{[ <a href="$homepage">$lex{Main}</a> | \n};
print qq{<a href="$attpage">$lex{Attendance}</a> ]\n};

print qq{<h1>$title</h1>\n};


if ( $arr{page} ) {
    delete $arr{page};
    updateRecord();
}


# Print Form Header
print qq{<form action="$self" method="post">
<input type="hidden" name="attid" value="$attid">
<input type="hidden" name="page" value="1">\n};

# Get Student Name
$sth = $dbh->prepare("select lastname, firstname, grade from studentall where studnum = ?");
$sth->execute( $studnum  );
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
my ( $lastname, $firstname, $grade ) = $sth->fetchrow;

my $ppd = $g_ppd{$grade};
if ( not $ppd ) { print qq{<h3>$lex{Error}: Periods per Day not defined for $lex{Grade} $grade</h3>\n}; }

# Name and Student Number
print qq{<table cellpadding="3" cellspacing="0" border="0">\n};
print qq{<tr><td class="bra">$lex{Student}</td>\n};
print qq{<td>$firstname $lastname ($studnum)</td></tr>\n};

# Date
print qq{<tr><td class="bra">$lex{Date}</td>\n};
print qq{<td class="la"><input type="text" Name="absdate" size="12" value="$absdate"></td></tr>\n};

# Reason
print qq{<tr><td class="bra">$lex{Reason}</td><td class="la">};
print qq{<select name="reason"><option>$reason</option>\n};
foreach my $reason ( @attend ) {  print qq{<option>$reason</option>\n}; }
print qq{</select></td></tr>\n};

# Period
print qq{<tr><td class="bra">$lex{Period}</td>\n};
print qq{<td class="la"><select name="period"><option>$period</option>};
for my $p (1..$ppd) { print qq{<option>$p</option>}; }
print qq{</select></td></tr>\n};

# Subjsec
print qq{<tr><td class="bra">$lex{'Subject-Section'}</td>\n<td class="la">};
print qq{<input type="text" Name="subjsec" size="12" maxlength="32" value="$subjsec"></td></tr>\n};

# Late
print qq{<tr><td class="bra">$lex{Late} ($lex{min})</td>\n<td class="la">};
print qq{<input type="text" Name="late" size="4" value="$late"></td></tr>\n};

print qq{</table><p><input type="submit" value="$lex{Update}">\n};
print qq{</form></body></html>\n};



#---------------
sub updateRecord {
#---------------

    my $sth = $dbh->prepare("update attend set
      absdate = ?, reason = ?, period = ?, 
      subjsec = ?, late = ?  where attid = ?");


    if ( not $arr{late} ) { $arr{late} = undef; }
    if ( not $arr{subjsec} ) { $arr{subjsec} = undef; }


    $sth->execute( $arr{absdate}, $arr{reason}, $arr{period} ,
		   $arr{subjsec}, $arr{late}, $arr{attid} );


    if ( not $DBI::errstr ) {
	print qq{<h3>$lex{'Record(s) Updated'}</h3>};

    } else {
	print qq{<h3>$lex{Error}: $DBI::errstr<br>\n};
	print qq{$lex{Contact} $adminname };
	print qq{<a href="mailto:$adminemail">$adminemail</a></h3>\n };

    }

    print qq{<p>[ <a href="$attpage">$lex{Attendance}</a> ]</p>\n};

    # Another Form
    print qq{<p><form action="attdeled.pl" method="post">\n};
    print qq{<select name="date"><option>$lex{Today}</option>\n};
    print qq{<option>$lex{Yesterday}</option><option>$lex{All}</option></select>\n};
    print qq{<input type="submit" value="};
    print qq{$lex{Edit}/$lex{Delete} $lex{Another}"></form></p>\n};

    print qq{</center></body></html>\n};

    exit;

}
