#! /usr/bin/perl # Copyright 2001-2008 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 $self = 'rptcustomclasslist.pl'; my %lex = ('Main' => 'Main', 'No Student(s) Found' => 'No Student(s) Found', 'View/Download' => 'View/Download', 'Custom Classlists' => 'Custom Classlists', 'Main' => 'Main', 'View Log File' => 'View Log File', 'Continue' => 'Continue', 'Homeroom' => 'Homeroom', 'Grade' => 'Grade', 'Leave blank for all students' => 'Leave blank for all students', 'Cell Width' => 'Cell Width', 'Max Students/Page' => 'Max Students/Page', 'Select' => 'Select', 'Repeat Entry Elements' => 'Repeat Entry Elements', 'Classlist' => 'Classlist', 'Rm' => 'Rm', 'Students' => 'Students', 'Error' => 'Error', 'Paper Size' => 'Paper Size', 'Letter' => 'Letter', 'Legal' => 'Legal', 'A4' => 'A4', 'Font Size' => 'Font Size', 'Gray Shade' => 'Gray Shade', 'Smaller=Darker' => 'Smaller=Darker', 'Include Student Number, Gender, Youngest' => 'Include Student Number, Gender, Youngest', 'Additional Field' => 'Additional Field', ); use DBI; use CGI; use Cwd; # Configurable settings my $maxstudents = 30; # maximum students per page. my $width = 8; # default width of columns, 8 mm; my $extrafieldsize = 18; # studnum and extra field in mm. my $defaultgrayshade = '0.95'; # shading for alternate rows my $entrylimit = 18; # Max entries allowed for rotated text. my $maxentrysize = 14; # Maximum number of characters for the entry my $namewidth = 50; # Width of the first 'Name' column in mm. my $q = new CGI; print $q->header; my %arr = $q->Vars; 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 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $iddst) = localtime(time); $year = $year + 1900; $wday++; $mon++; my $currdate = "$dow[$wday], $month[$mon] $mday, $year"; my $dsn = "DBI:$dbtype:dbname=$dbase"; my $dbh = DBI->connect($dsn,$user,$password); # Get current dir so know what CSS to display; if (getcwd() =~ /tcgi/){ # we are in tcgi $css = $tchcss; $homepage = $tchpage; } print "$doctype\n". $lex{'Custom Classlists'}. " $chartype\n
[ ". $lex{Main}. " ]
\n"; # Get values for Customization, then die. # Values passed are width, maxstudents, repeat, entry1 to entry8 if ( not $arr{flag} ) { showStartPage(); } # Setup Extra fields my ( $studnumfield, $extrafield, $extrafieldName ); if ( $arr{studnumfield} ) { $studnumfield = 1; delete $arr{studnumfield}; } if ( $arr{extrafield} ) { ( my $dud, $extrafield ) = split /\(/, $arr{extrafield}; chop $extrafield; # remove trailing parenthesis my $sth = $dbh->prepare("select fieldname from meta where tableid = 'student' and fieldid = ?"); $sth->execute( $extrafield ); if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } $extrafieldName = $sth->fetchrow; ( $extrafieldName ) = latex_filter( $extrafieldName ); delete $arr{extrafield}; } # Set Sizes # Letter paper size is 279mm x 216mm # 200mm wide - 50mm name, 20mm extra fld, leaves 130mm for columns. # a4 paper is 297mm x 210mm; legal is 356mm x 216mm # The tabcolsep of 1mm leaves 128 mm where each col is $width + 1mm wide. my $fontsize = $arr{fontsize}. 'pt'; if ( $arr{papersize} eq $lex{Letter} ) { $textwidth = '200mm'; $textheight = '270mm'; $availablesize = 146; $papersize = 'letterpaper'; } elsif ( $arr{papersize} eq $lex{A4} ) { $textwidth = '194mm'; $textheight = '286mm'; $availablesize = 136; $papersize = 'a4paper'; } else { # must be legal $textwidth = '200mm'; $textheight = '340mm'; $availablesize = 146; $papersize = 'legalpaper'; } delete $arr{papersize}; # no longer needed. # Restrict Gray Shading values if ( $arr{grayshade} > 1 ) { $arr{grayshade} = 1; } if ( $arr{grayshade} < 0.5 ) { $arr{grayshade} = 0.5; } my $wi = $arr{width} - 1; # width of each col desired (in mm) # take away 1 for 1mm tabcolsep. # Set column count after shrinking available size if ( $studnumfield ) { $availablesize -= $extrafieldsize; } if ( $extrafield ) { $availablesize -= $extrafieldsize; } my $colcount = $availablesize / ($arr{width} + 1); $colcount = int $colcount; # truncate #print "Column Count: $colcount \n"; # Filter for Passed values that might crash LaTeX foreach my $key ( keys %arr ) { ( $arr{$key} ) = latex_filter( $arr{$key} ); } if ( $arr{maxstudents} ) { $maxlines = $arr{maxstudents}; } else { $maxlines = $maxstudents; } # push entry text into array. for (1..$entrylimit){ my $entryname = 'entry'.$_; if ($arr{$entryname}){ # if it exists...push into array push @entry,$arr{$entryname}; } } my $entrysize = $#entry + 1; #print "Entry size: $entrysize\n"; if ($arr{repeat} and $entrysize){ $repeatcount = $colcount / $entrysize; $repeatcount = int $repeatcount; } elsif ($entrysize){ $repeatcount = 1; # only do it once if not repeat. } else { $repeatcount = 0;} #print "Repeat Count: $repeatcount\n"; my $shortname = "cclasslist$$"; my $filename = "$shortname.tex"; open(TEX,">$filename") || die "Can't open tex file"; print TEX "\\documentclass[$fontsize,$papersize ]{article} \\usepackage{array,newcent,rotating,colortbl,inputenc} \\inputencoding{latin1} \\pagestyle{empty} \\setlength{\\textwidth}{$textwidth} \\setlength{\\textheight}{$textheight} \\setlength{\\hoffset}{-36mm} \\setlength{\\voffset}{-36mm} \\addtolength{\\evensidemargin}{0mm} \\addtolength{\\oddsidemargin}{0mm} \\setlength{\\tabcolsep}{1mm} \\setlength{\\extrarowheight}{2mm} \\newcolumntype{G}{>{\\columncolor[gray]{1.00}}p{$namewidth mm}}\n"; # Note above... a columncolor of 1.00 means white, smaller number is darker. print TEX "\\begin{document}\n"; my $select; my $group = 'homeroom'; if ( $arr{group} eq $lex{Homeroom} and $arr{homeroom} ) { my $grpvalue = $dbh->quote( $arr{homeroom} ); $select = "where homeroom = $grpvalue"; $group = 'homeroom'; } elsif ( $arr{group} eq $lex{Grade} and $arr{homeroom} ){ my $grpvalue = $dbh->quote( $arr{homeroom} ); $select = "where grade = $grpvalue"; $group = 'grade'; } elsif ( $arr{group} eq $lex{Homeroom} or $arr{group} eq $lex{Grade} ) { $group = $arr{group}; } else { # should never happen unless attempted hack. print $lex{Error}. "
$arr{group}\n"; print "\n"; die; } my $sth = $dbh->prepare("select lastname, firstname, $group, studnum from student $select order by $group,lastname,firstname"); $sth->execute; if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } $rows = $sth->rows; if ($rows < 1){ # No students found... print '

'. $lex{'No Student(s) Found'}. "!

\n"; print "[ ". $lex{Main}. " ]\n"; print "\n"; system("rm -f $shortname.*"); #cleanup die; } my $curroom = -1; my $teachername; for ( my $i=1; $i<=$rows; $i++ ) { my ($lastname, $firstname, $groupval, $studnum ) = $sth->fetchrow; $oldroom = $curroom; $curroom = $groupval; if ( $oldroom eq $curroom ) { # We have another record for same page $linecount++; $studcount++; if ($linecount > $maxlines) { # We'll print a new page header here # New Page Header $linecount = 0; print TEX "\\end{tabular}\\\\ \\newpage\n"; prHeader(); } # Print Normal Record. prRecord($lastname, $firstname, $studnum); } else { # We are starting a new page # Get Teacher(s) Name for the room. if ($curroom and $curroom != -1) { $sth1 = $dbh->prepare("select sal, lastname from staff where homeroom = ?"); $sth1->execute( $curroom ); if ($DBI::errstr) { print "Teacher Error: $DBI::errstr"; die $DBI::errstr; } $teachrows = $sth1->rows; $teachername = ''; for ( 1..$teachrows ){ my ($sal, $lastname) = $sth1->fetchrow; $teachername .= " $sal~$lastname"; } } # End of teacher name if ($i != 1) { $studcount++; prStudCount(); print TEX "\n\\end{tabular} \n\n \\newpage \n"; } $linecount = 0; $studcount = 0; prHeader(); prRecord($lastname, $firstname, $studnum); } # End of Starting a New Page. } # End of Loop for all students $studcount++; prStudCount(); print TEX "\\end{tabular}\\\\ \n\\end{document}"; close TEX; # Solve download location issues with cgi vs tcgi.. # Get current dir so know what CSS to display; if (getcwd() =~ /tcgi/){ # we are in tcgi $downloaddir = $tchdownloaddir; $webdownloaddir = $tchwebdownloaddir; } system("$pdflatex $filename >pdflog$$.txt"); system("mv $shortname.pdf $downloaddir"); system("mv pdflog$$.txt $downloaddir"); system("rm -f $shortname.*"); print "

"; print $lex{'View/Download'}. ' ', $lex{'Custom Classlists'}. "

\n"; print "

[ Main |\n "; print $lex{'View Log File'}. "\n ]

\n"; #---------------- sub showStartPage { # Entry Values for Custom Script #---------------- # Read student fields, and defaults into a @fields and %fieldname hash. my $sth = $dbh->prepare("select fieldid, fieldname from meta where tableid = 'student' order by fieldname"); $sth->execute; my @fields = (); my %fieldnames = (); while ( ( my $fieldid, $fieldname ) = $sth->fetchrow ) { $fieldname =~ s/\(//g; $fieldname =~ s/\)//g; # strip parenthese. (sp?) push @fields, $fieldid; $fieldname{ $fieldid } = $fieldname; } print '

'. $lex{'Custom Class List Entry'}. "

\n"; print "
\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n\n"; # Font size print "\n"; print "\n"; print "\n"; print "\n\n"; print "\n\n"; print "\n\n"; print "\n\n"; for ( 1..$entrylimit ) { my $entrytxt = "entry$_"; print "\n"; print ""; print "\n"; } print "\n"; print "
". $lex{Select}; print " \n"; print $lex{'Leave blank for all students'}. "
". $lex{'Cell Width'}. ""; print "mm
". $lex{'Font Size'}. "\n"; print "
". $lex{'Gray Shade'}. "\n"; print ""; print " (0-1) ". $lex{'Smaller=Darker'}. "
". $lex{'Paper Size'}. ""; print "
". $lex{'Include Student Number, Gender, Youngest'}; print ""; print ""; print "
". $lex{'Additional Field'}; print ""; print "
". $lex{'Max Students/Page'}. ""; print ""; print "
". $lex{'Repeat Entry Elements'}; print "?
Entry $_
\n"; exit; } #----------- sub prHeader { #----------- # Done at the start of a new page. print TEX "\\begin{tabular}{G|"; if ( $studnumfield ) { print TEX "p{ $extrafieldsize mm}|"; } if ( $extrafield ) { print TEX "p{ $extrafieldsize mm}|"; } for (1..$colcount){ print TEX "p{$wi mm}|"; } print TEX "}\n"; print TEX "\\raggedright\\bf $schoolname ". $lex{Classlist}; if ( $studnumfield ) { print TEX "& "; } if ( $extrafield ) { print TEX "& "; } for ( 1..$colcount ){ print TEX "& ";} print TEX "\\\\ \n"; print TEX "\\small\\raggedright $currdate\n\\bigskip "; if ( $studnumfield ) { print TEX "& "; } if ( $extrafield ) { print TEX "& "; } for ( 1..$colcount ){ print TEX "& ";} print TEX "\\\\ \n"; if ( $arr{group} eq $lex{Grade} ){ print TEX "\\Large\\raggedright ". $lex{Grade}. " $curroom "; } else { # homeroom print TEX "\\bf\\raggedright $teachername "; print TEX $lex{Rm}. " $curroom "; } if ( $studnumfield ) { print TEX "& "; } if ( $extrafield ) { print TEX "&\\hfil\\rule{6pt}{0pt}\\begin{rotate}{90}$extrafieldName"; print TEX "\\end{rotate}\\hfil"; #print TEX "& "; } my $remainder = $colcount - ($repeatcount * $entrysize); if ( $repeatcount ){ for (1..$repeatcount){ # times to loop to print rotated text. foreach my $txt ( @entry ) { print TEX "&\\hfil\\rule{6pt}{0pt}\\begin{rotate}{90}$txt"; print TEX "\\end{rotate}\\hfil"; } } } for (1..$remainder){ print TEX "& ";} print TEX "\\\\ \\hline\n"; } # End of prHeader #----------- sub prRecord { # print normal record (ie. line) #----------- my ( $lastname, $firstname, $studnum ) = @_; my ($youngest, $sex, $efield); if ( $studnumfield or $extrafield ) { # we have to load some values my $fields = 'youngest, sex '; if ( $extrafield ) { $fields .= ", $extrafield"; } my $sth1 = $dbh->prepare("select $fields from student where studnum = ?"); $sth1->execute( $studnum ); if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } ( $youngest, $sex, $efield ) = $sth1->fetchrow; ( $efield ) = latex_filter( $efield ); } if ( $alternatingcolor == 0 ) { print TEX "\\rowcolor[gray]{". $arr{grayshade}. "}"; $alternatingcolor = 1; } else { $alternatingcolor = 0;} print TEX "\\raggedright $lastname, $firstname"; if ( $studnumfield ) { my $snfld = $studnum. $youngest. $sex; print TEX "&\\footnotesize{$snfld}"; } if ( $extrafield ) { print TEX "&\\footnotesize{$efield}"; } for (1..$colcount){ print TEX "& ";} print TEX "\\\\\\hline\n"; } #-------------- sub prStudCount { #-------------- #if ($alternatingcolor == 0){ # continue color from above... my $grayval = $arr{grayshade} - 0.08; # make it darker, slightly print TEX "\\rowcolor[gray]{$grayval}"; #} print TEX $lex{Students}. ": $studcount"; if ( $studnumfield ) { print TEX "& "; } for (1..$colcount){ print TEX "& ";} print TEX "\\\\\\hline\n"; }