#! /usr/bin/perl # Copyright 2001-2012 Leslie Richardson # This file is part of Open Admin for Schools. # Open Admin for Schools is free software; you can redistribute it # and/or modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. my $templatepath = '../template/'; my %lex = ('Main' => 'Main', 'Student Roster' => 'Student Roster', 'View/Download' => 'View/Download', 'View Log File' => 'View Log File', 'Cannot open' => 'Cannot open', 'Error' => 'Error', 'Homeroom' => 'Homeroom', 'Sort by' => 'Sort by', 'Continue' => 'Continue', 'Grade' => 'Grade', 'Select by' => 'Select by', 'Font Size' => 'Font Size', 'Records per Page' => 'Records per Page', 'Name' => 'Name', 'Paper Size' => 'Paper Size', 'Letter' => 'Letter', 'Legal' => 'Legal', 'A4' => 'A4', 'Blank=All' => 'Blank=All', 'Separate with Spaces' => 'Separate with Spaces', 'Select' => 'Select', 'Mode' => 'Mode', 'Missing' => 'Missing', 'Template' => 'Template', 'Creator' => 'Creator', 'Band' => 'Band', ); my $self = 'rptstudrost.pl'; use DBI; use CGI; use Cwd; # Read config variables eval require "../etc/admin.conf"; if ( $@ ) { print $lex{Error}. " $@
\n"; die $lex{Error}. " $@\n"; } eval require "../lib/liblatex.pl"; if ( $@ ) { print $lex{Error}. " $@
\n"; die $lex{Error}. " $@\n"; } my $q = new CGI; my %arr = $q->Vars; print $q->header( -charset, $charset ); # setup for teacher site, if called from there. if ( getcwd() =~ /tcgi/ ){ # we are in tcgi $css = $tchcss; $homepage = $tchpage; $downloaddir = $tchdownloaddir; $webdownloaddir = $tchwebdownloaddir; } my $recordsPerPage = 2; if ( $arr{recordsperpage} ) { $recordsPerPage = $arr{recordsperpage}; } my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $iddst) = localtime(time); $year = $year + 1900; $mon++; $wday++; my $currdate = "$dow[$wday], $month[$mon] $mday, $year"; # Page Header my $title = "$lex{'Student Roster'}"; print "$doctype\n$title $chartype\n\n"; print "
[ $lex{Main} | \n"; print "$lex{Template} $lex{Creator} ]
\n"; print "

$title

\n"; # Setup Database access my $dsn = "DBI:$dbtype:dbname=$dbase"; my $dbh = DBI->connect($dsn,$user,$password); $dbh->{mysql_enable_utf8} = 1; if ( not $arr{page} ) { showStartPage(); } # Get the report template... my $templatefile = $arr{template}; unless ( open ( FH,"<", $templatepath. $templatefile )) { print $lex{Error}. q{ }. $lex{Template}.": $!\n"; die $lex{Error}. q{ }. $lex{Template}.": $!\n"; } my $formtext; my $desc = ; my $modeline = ; chomp $modeline; # remove trailing CR/LF. $modeline =~ s/^\s*//; # strip any leading spaces. my ($mode, $fmtstart,$fmtend) = split /::/, $modeline; my $header = ; # header information for both pdf or html. $header =~ s/^\s*//; # strip any leading spaces my $line4 = ; # for future expansion, if required. my $line5 = ; # Now load rest of template for layout. { local $/; $formtext = ; close FH;} # Create hash for fieldnames from meta. my $sth = $dbh->prepare("select fieldid, fieldname, required from meta where tableid = ?"); $sth->execute( 'student' ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my %fieldnames = (); while ( my ( $fieldid, $fieldname, $required ) = $sth->fetchrow ) { if ( $required ) { # has any value if ( $mode eq 'html' ) { $fieldname = "$fieldname"; } else { # pdf format $fieldname = "{\\bf $fieldname}"; } } $fieldnames{$fieldid} = $fieldname; } # Now put replacement fieldnames back in. $formtext =~ s{\<\*(.*?)\*\>} { exists( $fieldnames{$1} ) ? $fieldnames{$1} : $1 }gsex; # Replace other non-meta values in formtext. my %nonmeta = (); while ( $formtext =~ m/\<\#(.*)\#\>/g){ if ( $lex{ $1 } ) { # if we have a translation.... $nonmeta{$1} = $lex{$1}; } else { # no translation. $nonmeta{$1} = 'NT:'. $1; } } # now put them back in... $formtext =~ s{\<\#(.*?)\#\>} { exists( $nonmeta{$1} ) ? $nonmeta{$1} : $1 }gsex; # Update translation in header. %lex = ( %lex, %fieldnames ); # join %lex and %fieldnames #foreach my $key ( keys %lex ) { # ( $lex{$key} ) = latex_filter( $lex{$key} ); #} if ( $header ) { $header =~ s{\<\*(.*?)\*\>} { exists( $lex{$1} ) ? $lex{$1} : $1 }gsex; } # Find all fields , so we only wrap forms around them # ( faster than doing all fields in the table ) my @fields = (); while ( $formtext =~ m/\<\@(.*)\@\>/g){ push @fields, $1; } # foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; } # Now select Students my @students = (); my $sort1 = $arr{sortorder1}; my $sort2 = $arr{sortorder2}; my $sorttext = "order by $sort1, $sort2"; my $selecttext; if ( $arr{groupvalue} ) { # then we have to do something... my $group; my @groups = (); @groups = split /\s+/, $arr{groupvalue}; if ( $arr{grouptype} eq 'grade' ) { $group = 'grade'; } else { $group = 'homeroom'; } $selecttext = 'where '; my $first = 1; foreach my $grp ( @groups ) { $grp = $dbh->quote( $grp ); if ( not $first ) { # put in OR $selecttext .= ' or '; } else { $first = 0; } $selecttext .= $group. ' = '. $grp. ' '; } } #print "Select Text: $selecttext
\n"; #print "Sort Text: $sorttext
\n"; my $sth = $dbh->prepare("select studnum from student $selecttext $sorttext"); $sth->execute; if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } while ( my $studnum = $sth->fetchrow ) { push @students, $studnum; } # Now print output based on mode (html or pdf) if ( $mode eq 'pdf' ) { printPDF(); } elsif ( $mode eq 'html' ) { printHTML(); } else { # error; print "$lex{Error}: $lex{Mode} $lex{Missing}\n"; print "\n"; exit; } #----------- sub printPDF { #----------- # Start TEX Section. my $shortname = "studrost$$"; my $filename = "$shortname.tex"; open(TEX,">$filename") || die $lex{'Cannot open'}. " tex file\n"; # Set Paper Size, text width and height, fontsize my ( $papersize, $textwidth, $textheight ); if ( $arr{papersize} eq $lex{Letter} ) { $papersize = 'letterpaper'; $textwidth = $g_letterpaper_textwidth; $textheight = $g_letterpaper_textheight; } elsif ( $arr{papersize} eq $lex{Legal} ) { $papersize = 'legalpaper'; $textwidth = $g_legalpaper_textwidth; $textheight = $g_legalpaper_textheight; } elsif ( $arr{papersize} eq $lex{A4} ) { $papersize = 'a4paper'; $textwidth = $g_a4paper_textwidth; $textheight = $g_a4paper_textheight; } # print "Papersize: $papersize TW: $textwidth TH: $textheight
\n"; # Not required here, perhaps # $textheight =~ s/mm//;# $textheight += 16;# $textheight .= 'mm'; my $fontsize = $arr{fontsize}. 'pt'; print TEX "\\documentclass[ $fontsize, $papersize ]{article} \\usepackage{newcent,graphicx,array,colortbl,inputenc,multicol} $a_latex_header \\renewcommand{\\familydefault}{\\sfdefault} \\usepackage[pdftex,bookmarks=false,pdfstartview=FitH]{hyperref} \\setlength{\\textwidth}{ $textwidth } \\setlength{\\textheight}{ $textheight } \\setlength{\\hoffset}{-20mm} \\setlength{\\voffset}{-25mm} \\setlength{\\topmargin}{0mm} \\setlength{\\evensidemargin}{0mm} \\setlength{\\oddsidemargin}{0mm} \\setlength{\\parindent}{0mm} \\setlength{\\extrarowheight}{4pt} \\begin{document} \\pagestyle{empty}\n"; print TEX $lex{'Student Roster'}. " - {\\LARGE $schoolname }"; print TEX " \\hfill $currdate\n\\bigskip\n\n"; if ( $fmtstart ) { print TEX $fmtstart, "\n"; } $header =~ s/\_//g; # filter underscores if ( $header ) { print TEX $header, "\n"; } my $sth = $dbh->prepare("select * from student where studnum = ?"); my $counter = 1; foreach my $studnum ( @students ) { $sth->execute( $studnum ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my $ref = $sth->fetchrow_hashref; my %rec = %$ref; foreach my $key ( keys %rec ) { ( $rec{$key} ) = latex_filter( $rec{$key}); } my $text = $formtext; # make a new blank record to plug in... # Now put replacement text back in. $text =~ s{\<\@(.*?)\@\>} { $rec{$1} }gsex; $text =~ s/\_/ /g; # remove underscores in field names. print TEX $text,"\n"; if ( not ( $counter % $recordsPerPage)) { if ( $fmtend ) { print TEX $fmtend, "\n"; } print TEX "\\newpage\n\n"; if ( $fmtstart ) { print TEX $fmtstart, "\n"; } if ( $header ) { print TEX $header, "\n";} } $counter++; } # End of For loop if ( $fmtend ) { print TEX $fmtend; } print TEX "\\end{document}"; close TEX; system("$pdflatex $filename > pdflog$$.txt"); system("mv $shortname.pdf $downloaddir"); system("mv pdflog$$.txt $downloaddir"); system("rm $shortname.*"); print "

"; print "$lex{'View/Download'} $lex{'Student Roster'}

\n"; print "[ $lex{Main} | \n"; print "$lex{'View Log File'}"; print " ]
\n"; } # end of printPDF. #------------ sub printHTML { #------------ #foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; } print "

$lex{'Student Roster'} - $schoolname

\n"; print "

$currdate

\n"; if ( $fmtstart ) { print $fmtstart, "\n"; } if ( $header ) { print $header, "\n"; } my $sth = $dbh->prepare("select * from student where studnum = ?"); my $counter = 1; foreach my $studnum ( @students ) { $sth->execute( $studnum ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my $ref = $sth->fetchrow_hashref; my %rec = %$ref; my $text = $formtext; # make a new blank record to plug in... # Now put replacement text back in. $text =~ s{\<\@(.*?)\@\>} { $rec{$1} }gsex; print $text,"\n"; } # End of record printing loop print ""; print "[ $lex{Main} ]\n"; print "
\n"; } #---------------- sub showStartPage { #---------------- # Start Form print "
\n"; print "\n"; print "\n"; # Open the Report Templates my @files = glob($templatepath."rptstudrost*.tpl"); my %desc = (); for my $tplfile ( @files ) { # read each label file and get description unless ( open (FH,"<$tplfile")) { print "$lex{Error} $lex{Template}: $!\n"; die "$lex{Error} $lex{Template}: $!\n"; } # read first 2 lines of the template only. my $desc = ; chomp $desc; $desc =~ s/\[//g; # strip any opening square labels, just in case. my $modeline = ; $modeline =~ s/^\s*//; # strip any leading spaces my ($mode, $fmtstart,$fmtend) = split /::/, $modeline; # ignore rest of the file, not required. $tplfile =~ s/^.*\///; # strip leading path $desc{$tplfile} = "$desc ($mode)"; } print "\n"; # Create hash for fieldnames from meta. my $sth = $dbh->prepare("select fieldid, fieldname from meta where tableid = ?"); $sth->execute( 'student' ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my %fieldnames = (); while ( my ( $fieldid, $fieldname ) = $sth->fetchrow ) { $fieldnames{$fieldid} = $fieldname; } print "\n"; print "\n"; print "\n"; print "\n\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "
$lex{Select} $lex{Template}\n"; my $passed = $arr{tpl}; print "
$lex{'Sort by'}
1. \n"; print "
\n"; print "

2.

$lex{'Select by'}\n"; print " "; print "$lex{'Separate with Spaces'}, $lex{'Blank=All'}
$lex{'Paper Size'}"; print "
$lex{'Font Size'}
$lex{'Records per Page'}\n"; print "
\n"; print "
\n"; print "\n"; exit; }