#!/usr/bin/perl
# Copyright 2001-2020 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 = ('Main' => 'Main',
'Error' => 'Error',
'Continue' => 'Continue',
'Select Format' => 'Select Format',
'Separate with Spaces' => 'Separate with Spaces',
'Name' => 'Name',
'Group' => 'Group',
'Grade' => 'Grade',
'Homeroom' => 'Homeroom',
'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',
'Average' => 'Average',
'of Terms' => 'of Terms',
'Term' => 'Term',
'Final' => 'Final',
'Staff' => 'Staff',
);
my $self = 'labels.pl';
# used to force a grid onto labels
my $showGrid = 0;
use DBI;
use CGI;
use Cwd;
use Number::Format qw(round);
# Read config variables
eval require "../etc/admin.conf";
if ( $@ ) {
print $lex{Error}. " $@ \n";
die $lex{Error}. " $@\n";
}
eval require "../etc/repcard.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 $q = new CGI;
print $q->header( -charset, $charset );
my %arr = $q->Vars;
# setup for teacher site, if called from there.
if ( getcwd() =~ /tcgi/ ){ # we are in tcgi
$css = $tchcss;
$homepage = $tchpage;
$downloaddir = $tchdownloaddir;
$webdownloaddir = $tchwebdownloaddir;
}
# Display top of page
my $title = qq{OA $lex{Labels}};
print qq{$doctype\n
\n};
# A field to give a family name rather than an appended list of lastnames a/b/c.
my $altNameField = $g_FamilyGroupNameField;
# Currently only using 5163 2" x 4" format
my $shortname = "Fam1Labels$$";
my $filename = "$shortname.tex";
open(TEX,">", $filename) || die "Can't open tex file";
# my $fontsize = $arr{fontsize}. 'pt'; # set below
# delete $arr{fontsize};
my $papersize;
if ( $defaultpapersize ) {
$papersize = $defaultpapersize;
} else {
$papersize = 'letterpaper';
}
$fontsize = '12pt';
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 "$a_latex_header\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)
$format = '5163';
printLabelFormat(*TEX, $format );
print TEX "\\LabelInfotrue\n";
# if ( $showGrid ) {
print TEX "\\LabelGridtrue\n";
# }
print TEX "\\begin{document}\\begin{labels}\n";
# ------Start of family selections------------------------
# Find all family groups.
my $sth = $dbh->prepare("select distinct $groupFld from student
where $groupFld != '' and $groupFld is not null");
$sth->execute;
if ($DBI::errstr) {print $DBI::errstr; die $DBI::errstr; }
my @groupFields;
while ( my $fld = $sth->fetchrow ) {
push @groupFields, $fld; #
}
# Find all blank/null grouped students.
my $sth = $dbh->prepare("select studnum from student
where $groupFld = '' or $groupFld is null
order by grade,homeroom,lastname, firstname");
$sth->execute;
if ($DBI::errstr) {print $DBI::errstr; die $DBI::errstr; }
my @nogroup;
while ( my $sn = $sth->fetchrow ) {
push @nogroup, $sn;
}
# Now get family groupings by field chosen
# family groupings are in %family hash with {groupFld} -> @studnum array.
my (@family, %familysize,%familyname,%altfamilyname, %busroute);
my $sth = $dbh->prepare("select lastname, firstname, studnum, $altNameField, busroute from student
where $groupFld = ? order by lastname, firstname");
foreach my $gfield ( @groupFields ) {
$sth->execute( $gfield );
if ($DBI::errstr) {print $DBI::errstr; die $DBI::errstr; }
my ( %familynames, %bus);
# All of these people have the same group name
while ( my $ref = $sth->fetchrow_hashref ) {
my %r = %$ref;
foreach my $key ( keys %r ) {
# print qq{K:$key Val:$r{$key} \n};
( $r{$key} ) = latex_filter(( $r{$key} ));
}
# add to altfamilyname hash if they have one.
if ( $r{$altNameField} ) {
$altfamilyname{$gfield} = $r{$altNameField};
}
$familynames{ $r{lastname} } = 1; # all lastnames into a hash.
$bus{$r{busroute}} = 1;
# family{$gfield}[ studnums ];
if ( @{$family{$gfield}} ) { # that family already has studnums.
push @{$family{$gfield}}, $r{studnum};
} else {
$family{$gfield} = [ $r{studnum} ];
}
}
my $famname = join('/', keys %familynames);
$familyname{$gfield} = $famname;
my $buses = join('/', keys %bus);
$busroute{$gfield} = $buses;
my $childcount = scalar @{ $family{$gfield} };
$familysize{$childcount}{$famname}{$gfield} = $gfield;
}
print qq{
Family List Report
\n};
my $sth = $dbh->prepare("select * from student where studnum = ?");
my $first = 1;
foreach my $childcount ( sort {$b <=> $a} keys %familysize ) {
if ( $childcount < 3 ) { last; } # skip families with 1 or 2 students
foreach my $famname ( sort keys %{ $familysize{$childcount} } ) {
foreach my $gfield ( sort keys %{ $familysize{$childcount}{$famname} } ) {
if ( $first ) {
$first = 0;
} else {
print TEX "\n\n"; # blank line to start new label.
}
my $famname;
if ( $altfamilyname{$gfield} ) {
$famname = $altfamilyname{$gfield};
} else {
$famname = $familyname{$gfield};
}
# this is the label printing section.
print TEX "{\\bf\\Large $famname}\n";
print TEX "{\\bf Bus - $busroute{$gfield}}\n";
my $studcount;
foreach my $studnum ( @{ $family{$gfield} } ) {
if ( $studcount > 5 ) { # new label with same family name.
print TEX "\n\n"; # blank line to start new label.
print TEX "{\\bf\\Large $famname}\n";
print TEX "{\\bf Bus - $busroute{$gfield}}\n";
$studcount = 0;
}
$sth->execute($studnum);
if ($DBI::errstr) {print $DBI::errstr; die $DBI::errstr; }
# my ($lastname, $firstname, $homeroom, $grade, $house ) = $sth->fetchrow;
my $ref = $sth->fetchrow_hashref;
my %r = %$ref;
print TEX "$r{firstname} $r{lastname} HR-$r{homeroom}/GR-$r{grade}\n";
$studcount++;
} # student loop end
} # gfield loop end
} # famname loop end
} # childcount end
print TEX "\\end{labels}\n\\end{document}\n";
close TEX;
#----------------------------------------------
# Now do the smaller families, 1 or 2 students
# ---------------------------------------------
# Currently only using 5163 2" x 4" format
my $shortname2 = "Fam2Labels$$";
my $filename2 = "$shortname2.tex";
open(TEX,">", $filename2) || die "Can't open tex file";
my $papersize;
if ( $defaultpapersize ) {
$papersize = $defaultpapersize;
} else {
$papersize = 'letterpaper';
}
$fontsize = '12pt';
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 "$a_latex_header\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)
$format = '5162';
printLabelFormat(*TEX, $format );
print TEX "\\LabelInfotrue\n";
# Can Turn this OFF
print TEX "\\LabelGridtrue\n";
print TEX "\\begin{document}\\begin{labels}\n";
my $sth = $dbh->prepare("select lastname, firstname, homeroom, grade, house from student
where studnum = ?");
my $first = 1;
foreach my $childcount ( sort {$b <=> $a} keys %familysize ) {
if ( $childcount > 2 ) { next; } # skip families with 3 or more students
# foreach my $phone ( sort keys %{ $familysize{$childcount} } ) {
foreach my $famname ( sort keys %{ $familysize{$childcount} } ) {
foreach my $gfield ( sort keys %{ $familysize{$childcount}{$famname} } ) {
if ( $first ) {
$first = 0;
} else {
print TEX "\n\n"; # blank line to start new label.
}
my $famname;
if ( $altfamilyname{$gfield} ) {
$famname = $altfamilyname{$gfield};
} else {
$famname = $familyname{$gfield};
}
# this is the label printing section.
print TEX "{\\bf\\Large $famname}\n";
print TEX "{\\bf Bus $busroute{$gfield}}\n";
my $studcount;
foreach my $studnum ( @{ $family{$gfield} } ) {
$sth->execute($studnum);
if ($DBI::errstr) {print $DBI::errstr; die $DBI::errstr; }
my ($lastname, $firstname, $homeroom, $grade, $house ) = $sth->fetchrow;
print TEX "$firstname $lastname HR-$homeroom/GR-$grade\n";
$studcount++;
} # student loop end
} # gfield loop end
} # famname loop end
} # childcount end
print TEX "\\end{labels}\n\\end{document}\n";
close TEX;
# End of the small Families generation.
#----------------------------------------------
# Big Families
system("$pdflatex $filename >pdflog$$.txt");
system("mv $shortname.pdf $downloaddir");
system("mv pdflog$$.txt $downloaddir");
system("rm -f $shortname.*");
print qq{