#! /usr/bin/perl
# Copyright 2001-2023 Leslie Richardson
my %lex = ('Report' => 'Report',
'Main' => 'Main',
'Attendance' => 'Attendance',
'Periods Per Day' => 'Periods Per Day',
'View/Download' => 'View/Download',
'View Log File' => 'View Log File',
'Last Name' => 'Last Name',
'First' => 'First',
'School Days' => 'School Days',
'Error' => 'Error',
'Date' => 'Date',
'Paper Size' => 'Paper Size',
'Letter' => 'Letter',
'Legal' => 'Legal',
'A4' => 'A4',
'Continue' => 'Continue',
'Records per Page' => 'Records per Page',
'Not Defined' => 'Not Defined',
'Grade' => 'Grade',
'Student' => 'Student',
'Not Found' => 'Not Found',
'Homeroom' => 'Homeroom',
'Year End' => 'Year End',
'Total' => 'Total',
'Percent' => 'Percent',
'Days' => 'Days',
'Enrolled' => 'Enrolled',
'Show Withdrawn Students' => 'Show Withdrawn Students',
'Missing' => 'Missing',
);
use DBI;
use CGI;
use Number::Format qw(:all);
use Time::JulianDay;
use Cwd;
# Constants
my $self = 'rptattyear2.pl';
my $defmaxrecords = 28; # Maximum records per PDF page.
# store yrmo totals for page bottom.
my %globAtt, %globEnrol;
# Default to creating PDF report
my $pdf = '1';
my $html;
# Get current dir so know what path for config files.
my $configpath;
my $teachermode;
if ( getcwd() =~ /tcgi/ ){ # we are in tcgi
$teachermode = 1;
$configpath = '..'; # go back one to get to etc.
} else {
$configpath = '../..'; # go back two to get to etc.
}
# main config file
eval require "$configpath/etc/admin.conf";
if ( $@ ) {
print $lex{Error}. ": $@
\n";
die $lex{Error}. ": $@\n";
}
eval require "$configpath/lib/libattend.pl";
if ( $@ ) {
print $lex{Error}. ": $@
\n";
die $lex{Error}. ": $@\n";
}
eval require "$configpath/lib/libDate.pl";
if ( $@ ) {
print $lex{Error}. ": $@
\n";
die $lex{Error}. ": $@\n";
}
eval require "$configpath/lib/liblatex.pl";
if ( $@ ) {
print $lex{Error}. ": $@
\n";
die $lex{Error}. ": $@\n";
}
my $dsn = "DBI:mysql:$dbase";
$dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;
my @tim = localtime(time);
my $year = $tim[5] + 1900;
my $month = $tim[4] + 1;
my $day = $tim[3];
if (length($month) == 1){ $month = "0".$month;}
if (length($day) == 1){ $day = "0".$day;}
my $currsdate = "$year-$month-$day";
my $currdate = "$month[$month] $day, $year";
my $q = new CGI;
my %arr = $q->Vars;
print $q->header( -charset, $charset );
# Get current dir so know what CSS to display and shift to teacher settings.
if ( getcwd() =~ /tcgi/ ) { # we are in tcgi
$css = $tchcss;
$homepage = $tchpage;
$downloaddir = $tchdownloaddir;
$webdownloaddir = $tchwebdownloaddir;
}
# HTML Header
my $title = "$lex{'Year End'} $lex{Attendance} $lex{Report} 2";
print qq{$doctype\n
$title\n};
print qq{\n};
print qq{\n};
print qq{
\n};
print qq{$chartype\n\n};
print qq{[ $lex{Main} };
if (not $teachermode ) {
print qq{| $lex{Attendance} ]\n}; }
else {
print qq{]\n};
}
print qq{$title
\n};
if ( not $arr{page} ) {
showStartPage();
} else {
delete $arr{page};
}
my $enddate;
if ($arr{date}){
$enddate = $arr{date};
} else {
$enddate = $currsdate;
}
my $startdate = $schoolstart;
if ( not $arr{homeroom} ) {
print qq{$lex{Missing} $lex{Homeroom}
\n};
print qq{\n};
exit;
}
# Get grade of this homeroom
my $sth = $dbh->prepare("select distinct grade from student
where homeroom = ? and grade is not null and grade != ''
order by grade asc");
$sth->execute( $arr{homeroom} );
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
my $grade = $sth->fetchrow; # largest grade value
my (%schooldays, $nsdmode );
if ( $grade eq 'K' or $grade eq 'PK' or $grade eq 'P3' ) {
# Function from LibAttend; pass a homeroom on the end if P3,K,PK
%schooldays = mkSchoolDays( $startdate, $enddate, $dbh, $arr{homeroom} );
$nsdmode = 1; # random days closed in low elementary grades (P3,PK,K)
# returns hash of schooldays in month. key is yyyy-mm and value is schooldays in month
} else { # higher grade levels
%schooldays = mkSchoolDays( $startdate, $enddate, $dbh,"GR:$grade" );
}
my $table = 'student';
if ( $arr{showwithdrawn} ) {
$table = 'studentall';
}
my $select = qq{where homeroom = ?};
my $sth = $dbh->prepare("select * from $table $select order by lastname, firstname");
$sth->execute( $arr{homeroom} );
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
my $studrows = $sth->rows;
# Get Homeroom Teacher Name
my $sth1 = $dbh->prepare("select userid from staff_multi
where field_name = 'homeroom' and field_value = ?");
$sth1->execute( $arr{homeroom} );
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
my $userid = $sth1->fetchrow;
$sth1 = $dbh->prepare("select lastname, firstname from staff where userid = ?");
$sth1->execute( $userid );
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
my ($lastname, $firstname) = $sth1->fetchrow;
# Check that Teacher attendance was done up to this date.
# checkAttEntry( $enddate, $arr{homeroom} );
# Open TEX file, and print Header
if ( $pdf ) {
$logfile = "pdflog$$.txt";
$shortname = "rptattyear2-$$";
$filename = "$shortname.tex";
open(TEX,">$filename") || die "Can't open tex file";
print_tex_doc_start(); # Setup the start of the file.
}
# Setup statements
my $sth5 = $dbh->prepare("select count(*) from student where studnum = ?");
my $sth4 = $dbh->prepare("select count(*) from attend where studentid = ?");
print_tex_header();
while ( $sref = $sth->fetchrow_hashref ){
# Check that they have some attendance records, to verify withdrawn students enrolled this year.
if ( $arr{showwithdrawn} ) {
$sth4->execute( $sref->{studnum} ); # count attendance records
if ($DBI::errstr) { print $DBI::errstr; die;}
my $attcount = $sth4->fetchrow;
if ( $attcount < 1 ) { next; }
}
# LaTeX filter the student record
foreach my $key ( keys %$sref ) {
( $sref->{$key} ) = latex_filter( $sref->{$key} );
}
my %sr = %$sref;
$studentname = "$sr{lastname}, $sr{firstname} $sr{initial}";
$studnum = $sr{studnum};
# Now let's find which table student from: student or studentwd
=head if ( $html ) {
$sth5->execute( $studnum );
if ($DBI::errstr) { print $DBI::errstr; die;}
my $active = $sth5->rows; # returns 1 if in student, 0 if not.
if ( not $active ){
$studentname = qq{$studentname};
}
}
=cut
# Print this student's record
print_tex_record( $sref );
$linecount++;
if ( $linecount >= $arr{maxrecords} ){ # New page.
print TEX "\\end{tabular}\\newpage\n\n";
print_tex_header();
$linecount = 0;
}
} # End of student loop
# Attendance Row
print TEX "\\rowcolor[gray]{0.90}";
print TEX "\\multicolumn{2}{|r|}{\\bf $lex{Student}-$lex{Days} $lex{Attendance} }";
my $asum;
foreach my $yrmo ( sort keys %schooldays ){
print TEX "& \\bf $globAtt{$yrmo} ";
$asum += $globAtt{$yrmo};
}
print TEX "& $asum & \\\\ \\hline\n\n";
# Enrollment Row
print TEX "\\rowcolor[gray]{0.90}";
print TEX "\\multicolumn{2}{|r|}{\\bf $lex{Student}-$lex{Days} $lex{Enrolled} }";
my $esum;
foreach my $yrmo ( sort keys %schooldays ){
print TEX "& \\bf $globEnrol{$yrmo} ";
$esum += $globEnrol{$yrmo};
}
print TEX "& $esum & \\\\ \\hline\n\n";
# Percent Attendance Row
print TEX "\\rowcolor[gray]{0.90}";
print TEX "\\multicolumn{2}{|r|}{\\bf $lex{Percent} $lex{Attendance}}";
foreach my $yrmo ( sort keys %schooldays ){
my $percent;
if ( $globEnrol{$yrmo} ) {
$percent = format_number($globAtt{$yrmo} / $globEnrol{$yrmo} * 100, 1);
} else { $percent = 0; }
print TEX "& \\bf $percent\\% ";
}
my $globPercent;
if ( $esum ) {
$globPercent = format_number($asum / $esum * 100, 1);
} else {
$globPercent = 0;
}
print TEX "& \\bf\\large $globPercent\\% & \\\\ \\hline\n\n";
print TEX "% print_tex_end here\n";
print TEX "\\end{tabular}\n\n\\end{document}";
close TEX;
system("$pdflatex $filename > $logfile");
system("mv $shortname.pdf $downloaddir");
system("mv $logfile $downloaddir");
system("rm -f $shortname.*");
print qq{\n};
print qq{[ $lex{Main} | };
if ( not $teachermode ) {
print qq{$lex{Attendance} | };
}
print qq{$lex{'View Log File'} ]
\n};
print qq{