#!/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. %lex = ('OA Labels' => 'OA Labels', 'Main' => 'Main', 'Top' => 'Top', 'No Records Found' => 'No Records Found', 'All Records' => 'All Records', 'Error' => 'Error', 'Continue' => 'Continue', 'Select Format' => 'Select Format', 'Separate with Spaces' => 'Separate with Spaces', 'Lastname' => 'Lastname', 'Group' => 'Group', 'Grade' => 'Grade', 'Homeroom' => 'Homeroom', 'Select Students' => 'Select Students', 'Select' => 'Select', 'Sort by' => 'Sort by', 'Select by' => 'Select by', 'Student' => 'Student', 'Select' => 'Select', 'Checked?' => 'Checked?', 'View/Download' => 'View/Download', 'View Log File' => 'View Log File', 'Labels' => 'Labels', 'Font Size' => 'Font Size', 'No Page Found' => 'No Page Found', 'Unable to open label file' => 'Unable to open label file', 'Year End Marks' => 'Year End Marks', 'Mark based on' => 'Mark based on', 'Final Term' => 'Final Term', 'Average' => 'Average', 'of Terms' => 'of Terms', 'Term' => 'Term', 'Final' => 'Final', ); my $self = 'labels.pl'; my $markfield = 'a1'; # used to force a grid onto labels my $showGrid = 0; use DBI; use CGI; my $q = new CGI; print $q->header; my %arr = $q->Vars; # Read config variables eval require "../etc/admin.conf"; if ( $@ ) { print $lex{Error}. " $@
\n"; die $lex{Error}. " $@\n"; } eval require "../lib/libmeta.pl"; 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; #$mon++; #$wday++; #my $currdate = "$dow[$wday], $month[$mon] $mday, $year"; # Display top of page print "$doctype\n",$lex{'OA Labels'}," \n"; print "$chartype\n\n"; print "[ ",$lex{Main}," ]
\n"; my $dsn = "DBI:$dbtype:dbname=$dbase"; my $dbh = DBI->connect($dsn,$user,$password); # Show start page if only beginning. if ( not $arr{page} ) { showStartPage(); } elsif ( $arr{page} == 1 ) { delete $arr{page}; selectStudents(); } elsif ( $arr{page} == 2 ) { delete $arr{page}; printLabels(); } elsif ( $arr{page} eq 'Mark1' ) { # Mark printing delete $arr{page}; showGradeMarkMethod(); } elsif ( $arr{page} eq 'Mark2' ) { # print Mark Labels for Cum Folders delete $arr{page}; printMarkLabels(); } else { print $lex{Error}; print $lex{'No Page Found'}. "\n"; } #----------------- sub printLabels { #----------------- my ($dud, $labelfile) = split /\[/, $arr{format}; delete $arr{format}; chop $labelfile; $labelfile = "../template/label/". $labelfile; # read file and get description unless (open (FH,"<$labelfile")) { print $lex{'Unable to open label file'}. ": $!\n"; die $lex{'Unable to open label file'}. ": $!\n"; } my $desc = ; my $format = ; # Now slurp in rest of file my $layout; { local $/; $layout = ; close FH;} #print "D:$desc F:$format LO:", $layout, "
\n"; while ( $layout =~ m{\<\#(.*?)\#\>}g ) { $fields .= $1.','; } chop $fields; # remove trailing comma my $selection = "select $fields from student where studnum = ?"; #print "SEL: $selection
\n"; my $sth = $dbh->prepare( $selection ); my $sortorder = $arr{sortorder}; delete $arr{sortorder}; my $shortname = "MLabels$$"; my $filename = "$shortname.tex"; open(TEX,">$filename") || die "Can't open tex file"; my $fontsize = $arr{fontsize}. 'pt'; delete $arr{fontsize}; #my $papersize = lc( $arr{papersize} ). 'paper'; if ( $defaultpapersize ) { $papersize = $defaultpapersize; } else { $papersize = 'letterpaper'; } #foreach my $key (sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; } print TEX "\\documentclass[$fontsize]{article}\n"; print TEX "\\usepackage{geometry}\n\\geometry{$papersize}\n"; print TEX "\\usepackage{inputenc}\n"; print TEX "\\inputencoding{latin1}\n"; print TEX "\\renewcommand{\\familydefault}{\\sfdefault}\n"; print TEX "\\usepackage[newdimens]{labels}\n"; # This printing is now done by printLabelFormat() below (this is Avery label format) #print TEX "\\LabelRows=10\n\\LabelCols=3\n"; #print TEX "\\LeftPageMargin=4.2mm\n\\RightPageMargin=4.2mm\n"; #print TEX "\\TopPageMargin=12.7mm\n\\BottomPageMargin=12.7mm\n"; #print TEX "\\InterLabelColumn=2.1mm\n\\InterLabelRow=0mm\n"; #print TEX "\\LeftLabelBorder=4mm\n\\RightLabelBorder=4mm\n"; #print TEX "\\TopLabelBorder=2mm\n\\BottomLabelBorder=2mm\n"; printLabelFormat(*TEX, $format ); print TEX "\\LabelInfotrue\n"; if ( $showGrid ) { print TEX "\\LabelGridtrue\n"; } print TEX "\\begin{document}\\begin{labels}\n"; # Loop through all students, printing their label. foreach my $key (sort keys %arr ) { #print "K:$key V:$arr{$key}
\n"; my ($dud, $studnum) = split /:/, $key; # Get values for this student $sth->execute( $studnum ); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } my $hash_ref = $sth->fetchrow_hashref; # Filter Records foreach my $key ( keys %{ $hash_ref } ) { ( $hash_ref->{$key} ) = latex_filter( $hash_ref->{$key} ); } my $newlayout = $layout; # Now put in field values into layout block $newlayout =~ s{\<\#(.*?)\#\>} { exists($hash_ref->{$1}) ? $hash_ref->{$1} : $1 }gsex; #$result = 'print TEX <<"EOF";'."\n".$layout."\n".'EOF'; #while (my @arr = $sth->fetchrow ){ # eval $result; #} $newlayout =~ s/\n\s*\n/\n/g; # strip blank lines (or lines with whitespace only) print TEX $newlayout. "\n\n"; } print TEX "\\end{labels}\n\\end{document}\n"; 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{Labels}. "

\n"; print "

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

\n"; exit; } # end of printLabels(); #----------------- sub selectStudents { #----------------- #foreach my $key (sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; } # print heading and start form. print "

". $lex{'OA Labels'}. q{ }. $lex{'Select Students'}. "

\n"; print "
\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; my @groups = split / /, $arr{groupValue}; my $firstgroup = $dbh->quote ( shift @groups ); #print "FG: $firstgroup
\n"; my $group; if ( $arr{groupType} eq $lex{Grade} ) { $group = 'grade'; } else { $group = 'homeroom'; } my $select; $select = "where $group = $firstgroup"; for my $grp ( @groups ) { $grp = $dbh->quote( $grp ); $select .= " or $group = $grp"; } #print "Select: $select
\n"; my $sortorder; my $sortflag; if ( $arr{sortorder} eq $lex{Group} ) { $sortorder = "order by $group, lastname, firstname"; $sortflag = 1; } else { $sortorder = 'order by lastname, firstname'; } my $sth = $dbh->prepare("select lastname, firstname, studnum, grade, homeroom from student $select $sortorder"); $sth->execute; if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } while (my ($lastname, $firstname, $studnum, $grade, $homeroom) = $sth->fetchrow ) { if ( $sortflag ) { if ( $group eq 'grade' ) { $sortfield = $grade. $lastname. $firstname; } else { $sortfield = $homeroom. $lastname. $firstname; } } else { $sortfield = $lastname. $firstname; } $sortfield =~ s/://g; # remove any colons; used for sorting in next page (label sort order) print "\n"; print "\n"; print "\n"; print "\n"; } print "\n"; print "
\n"; print "
". $lex{Select}. "". $lex{Student}. ""; print $lex{Grade}. "". $lex{Homeroom}. "
$lastname, $firstname ($studnum)$grade$homeroom
\n"; print "
\n"; exit; } #---------------- sub showStartPage { #---------------- # print heading and start form. print "

". $lex{'OA Labels'}. "

\n"; print "\n"; print "\n"; print "\n"; # Open the Label Templates $path = "../template/label"; my @files = glob($path."/*.label"); my @desc; for my $labelfile ( @files ) { # read each label file and get description unless (open (FH,"<$labelfile")) { print "Unable to open label file: $!\n"; die "Unable to open label file: $!\n"; } my $desc = ; $desc =~ s/\[//g; #strip any opening square labels $desc =~ s/^#//; #my $format = ; #my @layout = ; $labelfile =~ s/^.*\///; push @desc, $desc. ' ['.$labelfile.']'; } print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "
". $lex{'Select Format'}. "\n"; print "
". $lex{'Sort by'}. "\n"; print "
". $lex{'Select by'}. "\n"; print "\n"; print " "; print $lex{'Separate with Spaces'}. "
". $lex{'Font Size'}. "\n"; print "
". $lex{'Checked?'}. "
\n"; print "
\n"; # Year End Marks Labels print "
\n"; print "
\n"; print "\n"; print "\n"; print "
\n"; print "\n"; exit; } #---------------------- sub showGradeMarkMethod { #---------------------- # print heading and start form. print "

". $lex{'Year End Marks'}. "

\n"; print "
\n"; print "\n"; print "\n"; print "\n"; # Get all grades in school my $sth = $dbh->prepare("select distinct grade from student where grade != '' order by grade"); $sth->execute; if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } while ( my $grade = $sth->fetchrow ) { print "\n"; print "\n"; } print "\n"; print "
". $lex{'Mark based on'}. "". $lex{Final}. '
'. $lex{Term}; print "
". $lex{Average}. '
'. $lex{'of Terms'}. "
". $lex{Grade}. " $grade"; # Input values are either F - Final Term (best) or A - Average of Terms(not so good). print ""; print "
\n"; print "
\n"; print "\n"; exit; } #----------------- sub printMarkLabels { #----------------- use Number::Format qw(round); # load this to get the $additionalcomments value, to filter out this subject. eval require "../etc/repcard.conf"; if ( $@ ) { print $lex{Error}. " $@
\n"; die $lex{Error}. " $@\n"; } # Currently only using 5161 1" x 4" format with monospaced fonts and 3 colums my $subjectSize = 13; my $markSize = 3; #foreach my $key (sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; } my $shortname = "MLabels$$"; my $filename = "$shortname.tex"; open(TEX,">$filename") || die "Can't open tex file"; my $fontsize = $arr{fontsize}. 'pt'; delete $arr{fontsize}; if ( $defaultpapersize ) { $papersize = $defaultpapersize; } else { $papersize = 'letterpaper'; } $fontsize = '10pt'; print TEX "\\documentclass[$fontsize]{article}\n"; print TEX "\\usepackage{geometry}\n\\geometry{$papersize}\n"; print TEX "\\usepackage{inputenc}\n"; print TEX "\\usepackage{courier}\n"; # was luximono print TEX "\\inputencoding{latin1}\n"; #print TEX "\\renewcommand{\\familydefault}{\\sfdefault}\n"; print TEX "\\usepackage[newdimens]{labels}\n"; # This printing is now done by printLabelFormat() below (this is Avery label format) #print TEX "\\LabelRows=10\n\\LabelCols=3\n"; #print TEX "\\LeftPageMargin=4.2mm\n\\RightPageMargin=4.2mm\n"; #print TEX "\\TopPageMargin=12.7mm\n\\BottomPageMargin=12.7mm\n"; #print TEX "\\InterLabelColumn=2.1mm\n\\InterLabelRow=0mm\n"; #print TEX "\\LeftLabelBorder=4mm\n\\RightLabelBorder=4mm\n"; #print TEX "\\TopLabelBorder=2mm\n\\BottomLabelBorder=2mm\n"; $format = '5161'; printLabelFormat(*TEX, $format ); print TEX "\\LabelInfotrue\n"; # if ( $showGrid ) { print TEX "\\LabelGridtrue\n"; # } print TEX "\\begin{document}\n"; #print TEX "\\begin{labels}\n"; # prepare to select students by grade my $sth = $dbh->prepare("select lastname, firstname, studnum from student where grade = ? order by lastname, firstname" ); my $sth1 = $dbh->prepare("select distinct subjcode from eval where studnum = ?"); my $sth2 = $dbh->prepare("select description, smdesc, startrptperiod, endrptperiod from subject where subjsec = ?"); my $sth3 = $dbh->prepare("select $markfield from eval where subjcode = ? and studnum = ? and term = ?"); # Loop through each grade, getting student and his/her grades to print on label. foreach my $grade ( sort keys %arr ) { my $method = $arr{$grade}; $sth->execute($grade); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } while ( my ( $lastname, $firstname, $studnum ) = $sth->fetchrow ) { my ( %subjectValue, %subjectSort, %subjectName ); # Get subjsec values for this student $sth1->execute( $studnum ); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } my $subjcount; # count number of subjects while ( my $subjsec = $sth1->fetchrow ) { # Get subject values $sth2->execute( $subjsec ); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } my ($description, $smdesc, $startterm, $endterm) = $sth2->fetchrow; if ( $description eq $additionalcomments ) { next; } # skip # $additionalcomments are defined in repcard.conf; $subjcount++; # Get eval value(s); one or all. my $mark; if ( $method eq 'F' ) { # Final term for average $sth3->execute( $subjsec, $studnum, $endterm ); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } $mark = $sth3->fetchrow; } else { # method eq 'A' - find average value; my ($termcount, $total); for my $trm ( $startterm..$endterm ) { $sth3->execute( $subjsec, $studnum, $trm ); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } my $val = $sth3->fetchrow; $total =+ $val; $termcount++; } if ( $termcount ) { # can't divide by 0. $mark = round(0, $total / $termcount); } else { $mark = 0; } } # Now put values in storage; my $desc; if ( $smdesc ) { $desc = $smdesc; } else { $desc = substr $description, 0, 10; # limit to 10 chars. } $subjectValue{$subjsec} = sprintf "%-${$markSize}s", $mark; $subjectSort{"$desc:$subjsec"} = $subjsec; $subjectName{$subjsec} = sprintf "%-${$subjectSize}s", $desc; } # Now done all subjects for this student; if ( not $subjcount ) { next; } # next student; skip this student. # Set the label. print TEX "\\addresslabel[\\ttfamily]{%\n"; print TEX "$firstname $lastname - $schoolyear \\\\ \n"; my $count = 0; for my $key ( sort keys %subjectSort ) { my $subjsec = $subjectSort{$key}; #my $markfmt = '%-'. $markSize. 's'; my $subjfmt = '%-'. $subjectSize. 's'; my $mark = $subjectValue{$subjsec}; my $desc = sprintf($subjfmt, $subjectName{$subjsec}); print TEX $mark . '-'. $desc; $count++; if ( $count % 4 == 0 ) { # new row print TEX "\\\\ \n"; } } while ( $count % 4 != 0 ) { my $index = $count % 4; #print $index. ' '; if ( $index == 3 ) { print TEX "$index \\\\ \n\n"; } $count++; } print TEX "\n } \n"; } # End of this Student } # End of Grades #print TEX "\\end{labels}\n"; print TEX "\\end{document}\n"; 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{Labels}. "

\n"; print "

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

\n"; exit; } # end of printLabels(); # End of Print Mark Labels #------------------- sub printLabelFormat { #------------------- # Get file handle and avery format number. my $fh = shift; my $format = shift; if ( $format == '5160' ) { # print "INside 5160"; $f{LabelRows} = '10'; $f{LabelCols} = '3'; $f{LeftPageMargin} = '4.2mm'; $f{RightPageMargin} = '4.2mm'; $f{TopPageMargin} = '12.7mm'; $f{BottomPageMargin} = '12.7mm'; $f{InterLabelColumn} = '2.1mm'; $f{InterLabelRow} = '0mm'; $f{LeftLabelBorder} = '4mm'; $f{RightLabelBorder} = '4mm'; $f{TopLabelBorder} = '2mm'; $f{BottomLabelBorder} = '2mm'; } elsif ( $format == '5161' ) { #print "INside 5161"; $f{LabelRows} = '10'; $f{LabelCols} = '2'; $f{LeftPageMargin} = '5mm'; $f{RightPageMargin} = '4mm'; $f{TopPageMargin} = '13mm'; $f{BottomPageMargin} = '13mm'; $f{InterLabelColumn} = '5mm'; $f{InterLabelRow} = '0mm'; $f{LeftLabelBorder} = '3mm'; $f{RightLabelBorder} = '3mm'; $f{TopLabelBorder} = '2mm'; $f{BottomLabelBorder} = '2mm'; } elsif ( $format == '5162' ) { #print "INside 5162"; $f{LabelRows} = '7'; $f{LabelCols} = '2'; $f{LeftPageMargin} = '5mm'; $f{RightPageMargin} = '4mm'; $f{TopPageMargin} = '21mm'; $f{BottomPageMargin} = '21mm'; $f{InterLabelColumn} = '0mm'; $f{InterLabelRow} = '0mm'; $f{LeftLabelBorder} = '3mm'; $f{RightLabelBorder} = '3mm'; $f{TopLabelBorder} = '2mm'; $f{BottomLabelBorder} = '2mm'; } elsif ( $format == '5163' ) { #print "INside 5163"; $f{LabelRows} = '5'; $f{LabelCols} = '2'; $f{LeftPageMargin} = '5mm'; $f{RightPageMargin} = '4mm'; $f{TopPageMargin} = '13mm'; $f{BottomPageMargin} = '13mm'; $f{InterLabelColumn} = '5mm'; $f{InterLabelRow} = '0mm'; $f{LeftLabelBorder} = '3mm'; $f{RightLabelBorder} = '3mm'; $f{TopLabelBorder} = '2mm'; $f{BottomLabelBorder} = '2mm'; } # print format using passed file handle foreach my $key ( keys %f ) { print $fh "\\$key = $f{$key}\n"; } return; }