#! /usr/bin/perl
#  Copyright 2001-2026 Leslie Richardson

#  This file is part of Open Admin for Schools.


my %lex = ('Main' => 'Main',
	   'View/Download' => 'View/Download',
	   'View Log File' => 'View Log File',
	   'Continue' => 'Continue',
	   'Grade' => 'Grade',
	   'Cell Width' => 'Cell Width',
	   'Maximum' => 'Maximum',
	   'Page' => 'Page',
	   'Select by' => 'Select by',
	   'Repeat Column Headers' => 'Repeat Column Headers',
	   'Classlist' => 'Classlist',
	   'Rm' => 'Rm',
	   'Students' => 'Students',
	   'Paper Size' => 'Paper Size',
	   'Letter' => 'Letter',
	   'Legal' => 'Legal',
	   'A4' => 'A4',
	   'Font Size' => 'Font Size',
	   'Gray Shade' => 'Gray Shade',
	   'Smaller=Darker' => 'Smaller=Darker',
	   'Include' => 'Include',
	   'Student Number' => 'Student Number',
	   'Additional Field' => 'Additional Field',
	   'Error' => 'Error',
	   'Width' => 'Width',
	   'Not Found' => 'Not Found',

	   );

my $self = 'rptSelectClasslist.pl';

use DBI;
use CGI;
use Cwd;

# Configurable settings
my $maxstudents = 28; # maximum students per page.
my $cellwidth = 8;  # default width of columns, 8 mm;
my $extrafieldwidth = 18; # studnum and extra field in mm.
my $defaultgrayshade = '0.90';  # shading for alternate rows

my $entrylimit = 31; # Max entries allowed for rotated text.
my $maxentrysize = 16; # Maximum number of characters for the entry
my $namewidth = 54; # Width of the first 'Name' column in mm.

my $studnumwidth = 12; # Width of local student number in mm.


eval require "../etc/admin.conf";
if ( $@ ) {
    print $lex{Error}. " $@<br>\n";
    die $lex{Error}. " $@\n";
}

eval require "../lib/liblatex.pl";
if ( $@ ) {
    print $lex{Error}. " $@<br>\n";
    die $lex{Error}. " $@\n";
}

my $q = new CGI;
print $q->header( -charset, $charset );
my %arr = $q->Vars;


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;
} elsif ( getcwd() =~ /ecgi/ ){ # we are in ea site
    $css = $eacss;
    $homepage = $eapage;
}


my $title = qq{Classlist - Select Students};
print qq{$doctype\n<html><head><title>$title</title>\n};
print qq{<link rel="stylesheet" href="$css" type="text/css">\n};
print qq{$chartype\n</head><body style="padding:1em 4em;">\n};
print qq{<div>[ <a href="$homepage">$lex{Main}</a> ]</div>\n};

print qq{<h1>$title</h1>\n};


# Get values for Customization, then exit;
# Values passed are width, maxstudents, repeat, entry1 to entry8
if ( not $arr{page} ) {
    showStartPage();
    
} elsif ( $arr{page} == 1 )  {
    delete $arr{page};
    selectStudents();
    
} elsif ( $arr{page} == 2 )  { # Here from select student
    delete $arr{page};
    if ( $arr{extrafield} eq 'mydata' ) {
	enterMyData();
    }
} 



my %myextrafields; # user entered data from enterMyData() function
foreach my $key ( sort keys %arr ) {
    my ($studnum, $type) = split(':', $key);
    if ( $type eq 'mydata' ) {
	$myextrafields{$studnum} = $arr{$key};
	delete $arr{$key};
    }
}



my @students;
foreach my $key ( sort keys %arr ) {
    if ( $key =~ m/^\d+$/ ) { # digit only, student number
	push @students, $key;
	delete $arr{$key};
    }
}
# only print attributes left in %arr now

# sorted list of students based on lastname, firstname
my (@sort, %sort);
my $sth = $dbh->prepare("select lastname, firstname from student where studnum = ?");
foreach my $studnum (@students) {
    $sth->execute( $studnum );
    if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
    my ($ln,$fn) = $sth->fetchrow;
    $sort{"$ln$fn$studnum"} = $studnum;
}
@sort = sort keys %sort;
# we now have an ordered list of student numbers.


#foreach my $key ( sort keys %arr ) { print qq{K:$key V:$arr{$key}<br>\n}; }


# Setup Extra fields
my ( $studnumfield, $extrafield );

if ( $arr{extrafield} ) {
    $extrafield = $arr{extrafield};
    delete $arr{extrafield};
    
} elsif ( %myextrafields ) {
    $extrafield = 'mydata';
}
    

if ( $arr{extrafieldwidth} ) {
    $extrafieldwidth = $arr{extrafieldwidth}; # default defined at top of script
    delete $arr{extrafieldwidth};
}

if ( $arr{studnumfield} ) {
    $studnumfield = 1;
    delete $arr{studnumfield};
}



my ($extrafieldName, %defaultvalues); # default values only for selecthash form type
if ( $extrafield and not %myextrafields ) {

    my $sth = $dbh->prepare("select fieldname, defaultvalue, formtype from meta 
			    where tableid = 'student' and fieldid = ?");
    $sth->execute( $extrafield );
    if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
    my ($defaultvalue, $formtype); # these 2 are local
    ($extrafieldName, $defaultvalue, $formtype) = $sth->fetchrow;

    if ( $formtype eq 'selecthash' ) { # we want to display their text equivalent values from meta
	my @temp = split(/\s+/, $defaultvalue);  #split based on spaces
	foreach my $val ( @temp ) {
	    $val =~ s/_/ /g; # strip underscores used as separators
	}
	%defaultvalues = @temp;

    }

    ( $extrafieldName ) = latex_filter( $extrafieldName );
}

if ( %myextrafields ) {
    $extrafieldName = 'My Data';
}


# 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';

my ( $papersize, $textwidth, $textheight );

if ( $arr{papersize} eq 'letter' ) {
    $papersize = 'letterpaper';
    $textwidth = $g_letterpaper_textwidth;
    $textheight = $g_letterpaper_textheight;

} elsif ( $arr{papersize} eq 'legal' ) {
    $papersize = 'legalpaper';
    $textwidth = $g_legalpaper_textwidth;
    $textheight = $g_legalpaper_textheight;

} elsif ( $arr{papersize} eq 'a4' ) {
    $papersize = 'a4paper';
    $textwidth = $g_a4paper_textwidth;
    $textheight = $g_a4paper_textheight;

} 


# Restrict Gray Shading values
if ( $arr{grayshade} > 1 ) { $arr{grayshade} = 1; }
if ( $arr{grayshade} < 0.5 ) { $arr{grayshade} = 0.5; }

my $wi = $arr{cellwidth} - 1; # width of each col desired (in mm)
# take away 1 for 1mm tabcolsep.

my $availablesize = $textwidth - $namewidth;

# Set column count after shrinking available size
if ( $studnumfield ) { $availablesize -= $studnumwidth; }
if ( $extrafield ) { $availablesize -= $extrafieldwidth; }


my $colcount = $availablesize / $arr{cellwidth};
$colcount = int $colcount + 1; # truncate - removed + 1;

# print "Paper: $textwidth Count:$colcount Available: $availablesize<br>\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 qq{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 qq{Repeat Count: $repeatcount\n};


# Start TeX Document
my $shortname = "cclasslist$$";
my $filename = "$shortname.tex";

open(TEX,">$filename") || die "Can't open tex file";

print TEX "\\documentclass[$fontsize,$papersize ]{extarticle}
\\usepackage{array,noto,rotating,colortbl}
\\pagestyle{empty}
\\setlength{\\textwidth}{$textwidth}
\\setlength{\\textheight}{$textheight}

\\setlength{\\hoffset}{-40mm}
\\setlength{\\voffset}{-36mm}
\\addtolength{\\evensidemargin}{0mm}
\\addtolength{\\oddsidemargin}{0mm}
\\setlength{\\tabcolsep}{0.5mm}
\\setlength{\\extrarowheight}{2mm}
\\renewcommand{\\familydefault}{\\sfdefault}
\\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";


# Get student info
my $sth = $dbh->prepare("select lastname, firstname, grade, homeroom, studnum from student 
    where studnum = ?");

my $first = 1; 
my $linecount = $studcount = 0;

# Student Loop
foreach my $key ( @sort ) {
    my $studnum = $sort{$key};
    
    $sth->execute( $studnum ); 
    if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }

    # Get Teacher name, if any
#    my $sth1 = $dbh->prepare("select sal, lastname from staff as s, staff_multi as sm
#			     where s.userid = sm.userid and field_name = 'homeroom' and 
#			     field_value = ?");
#   $sth1->execute( $graderoom );
#   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

    
    my ( $lastname, $firstname, $grade, $homeroom, $studnum ) = $sth->fetchrow;

    if ( $first ) { # print header to start
	prHeader($teachername, $graderoom );
	$first = 0;
    }

    $linecount++;
    $studcount++;
    if ( $linecount > $maxlines ) { # We'll print a new page header here
	# New Page Header
	$linecount = 0;
	if ( $i != 1 ) { print TEX "\\end{tabular}\\\\ \\newpage\n"; }
	prHeader($teachername, $graderoom );
    }

    my $mydata = $myextrafields{$studnum};
    
    # Print Normal Record.
    prRecord($lastname, $firstname, $studnum, $grade,$homeroom, $mydata);

}

# now do bottom of page
prStudCount();
print TEX "\n\\end{tabular} \n\n \\newpage \n";

print TEX "\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;

} elsif (getcwd() =~ /ecgi/){ # we are in easite
    $downloaddir = $eadownloaddir;
    $webdownloaddir = $tchwebdownloaddir; # same as teacher; just 'download';
}

system("$pdflatex $filename >pdflog$$.txt");
system("mv $shortname.pdf $downloaddir");
system("mv pdflog$$.txt $downloaddir");
system("rm -f $shortname.*");

print qq{<h1><a href="$webdownloaddir/$shortname.pdf">};
print qq{$lex{'View/Download'} Selected Student Classlist</a></h1>\n};
print qq{<p>[ <a href="$homepage">$lex{Main}</a> |\n <a href="$webdownloaddir/pdflog$$.txt">};
print qq{$lex{'View Log File'}</a>\n ]</p>};

print qq{</body></html>\n};
# End of Main Body.



#--------------
sub enterMyData {
#--------------

    # foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}<br>\n"; }
    
    # Form Header - Pass all values through 
    print qq{<form action="$self" method="post"> \n};
    print qq{<input type="hidden" name="page" value="2">\n};

    # Remove this so next page doesn't loop back here.
    if ( $arr{extrafield} eq 'mydata' ) {
	delete $arr{extrafield};
    }
        
    foreach my $key ( keys %arr ) {
	if ( $arr{$key} ) { # we have a value
	    print qq{<input type="hidden" name="$key" value="$arr{$key}">\n};
	}
    }

    
    # Get all the passed student value
    my @students;
    foreach my $key ( sort keys %arr ) {
	if ( $key =~ m/^\d+$/ ) { # digit only, student number
	    push @students, $key;
	}
    }

    my $sth = $dbh->prepare("select lastname, firstname, grade, homeroom from studentall3
          where studnum = ?");

    # Table Header
    print qq{<table cellpadding="3" cellspacing="0" border="0" };
    print qq{style="border:1px solid gray;margin:0.5em;padding:0.5em;float:left;">\n};
    
    print qq{<tr><th>Student</th><th>Gr/Hr</th><th>Extra Field Value</th></tr>\n};

    foreach my $studnum ( @students ) {

	# Get Name
	$sth->execute($studnum);
	if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }
	my ($lastname, $firstname, $grade, $homeroom) = $sth->fetchrow;
	
	print qq{<td class="la"><b>$lastname</b>, $firstname ($studnum)</td><td>$grade/$homeroom</td>\n};
	print qq{<td class="la"><input type="text" name="$studnum:mydata" style="width:arr{extrafieldwidth}mm;">};
	print qq{</td></tr>\n};
    }

    print qq{</table>\n};

    print qq{<div style="clear:left;"><input type="submit" value="$lex{Continue}"></div>\n};

    print qq{</form></body></html>\n};

    exit;

} # end of enterMyData




#----------------
sub selectStudents {
#----------------

    #foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}<br>\n"; }
    # chk is for checking all students
    my $chk;
    if ( $arr{chk} ) {
	$chk = qq{checked = "checked"};
	delete $arr{chk};
    }

   
    my ($sth, $groupname, @grades, @homerooms, @groups);
    if ( $arr{grade} ) { # we're picking a group
	@grades = $q->multi_param(grade);
	$groupname = qq{Grade};
	$sth = $dbh->prepare("select lastname, firstname, studnum, grade, homeroom from student
          where grade = ? order by lastname, firstname");
	@groups = @grades;
    } elsif ( $arr{homeroom} ) {
	@homerooms = $q->multi_param(homeroom);
	$groupname = qq{Homeroom};
	$sth = $dbh->prepare("select lastname, firstname, studnum, grade, homeroom from student
          where homeroom = ? order by lastname, firstname");
	@groups = @homerooms;
    } else { # everyone
	$sth = $dbh->prepare("select lastname, firstname, studnum from student
          order by lastname, firstname");
	$groupname = qq{Entire School};
	@groups = ('All');
    }

    if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }

    # Form Header
    print qq{<form action="$self" method="post"> \n};
    print qq{<input type="hidden" name="page" value="2">\n};

    foreach my $key ( keys %arr ) {
	if ( $key ne 'grade' and $key ne 'homeroom' ) { # then pass to next page
	    if ( $arr{$key} ) { # we have a value
		print qq{<input type="hidden" name="$key" value="$arr{$key}">\n};
	    }
	}
    }
    
    print qq{<div><input type="submit" value="$lex{Continue}"></div>\n};

    foreach my $grp ( @groups ) {
	if ($grp eq 'All' ) {
	    $sth->execute;
	} else {
	    $sth->execute($grp);
	}
    
	print qq{<table cellpadding="3" cellspacing="0" border="0" };
	print qq{style="border:1px solid gray;margin:0.5em;padding:0.5em;float:left;">\n};
    
	print qq{<tr><th>Select</th><th>$groupname $grp</th></tr>\n};

	while ( my ( $lastname, $firstname, $studnum, $grade, $homeroom ) = $sth->fetchrow ) {
	    print qq{<tr><td class="cn"><input type="checkbox" name="$studnum" value="1" $chk></td>\n};
	    print qq{<td class="la"><b>$lastname</b>, $firstname ($studnum)</td></tr>\n};
	}

	print qq{</table>\n};
    }
    
    print qq{<div style="clear:left;"><input type="submit" value="$lex{Continue}"></div>\n};

    print qq{</form></body></html>\n};

    exit;

} # end of selectStudents




#----------------
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;
    if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
    
    my ( @fields, %fieldnames );

    push @fields, 'mydata'; # for a custom field value
    $fieldname{mydata} = 'My Data';

    
    while ( ( my $fieldid, $fieldname ) = $sth->fetchrow ) {
#	$fieldname =~ s/\(//g;
#	$fieldname =~ s/\)//g; # strip parenthese. (sp?)
	push @fields, $fieldid;
	$fieldname{ $fieldid } = $fieldname;
    }

    
    # Get Grades
    my @grades;
    my $sth = $dbh->prepare("select distinct grade from student 
			    where grade is not NULL and grade != '';");
    $sth->execute;
    if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
    while ( my $grade = $sth->fetchrow ) {
	push @grades, $grade;
    }
    @grades = sort {$a <=> $b} @grades;


    # Get Homerooms
    my @homerooms;
    my $sth = $dbh->prepare("select distinct homeroom from student 
			    where homeroom is not NULL and homeroom != '';");
    $sth->execute;
    if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
    while ( my $hr = $sth->fetchrow ) {
	push @homerooms, $hr;
    }
    @homerooms = sort {$a <=> $b} @homerooms;

    
    print qq{<form action="$self" method="post">\n};
    print qq{<input type="hidden" name="page" value="1">\n};

    
    print qq{<table cellpadding="3" cellspacing="0" border="0" };
    print qq{style="float:left;border:1px solid gray;padding:0.5em;margin:0.5em;">\n};

    print qq{<tr><td></td><td>Use CTRL Key to select more than one</td></tr>\n};

    # Student By Grade
    print qq{<tr><td class="bra">$lex{'Select by'} $lex{Grade}</td>};
    print qq{<td><select name="grade" multiple size="6">\n};

    foreach my $gr ( @grades ) {
	print qq{<option>$gr</option>};
    }
    
    print qq{<option value="all">All Grades</option>\n};    
    print qq{</select></td></tr>\n};

    # OR
    print qq{<tr><td class="bra">OR</td><td></td></tr>\n};

    # Student by Homeroom
    my $sth = $dbh->prepare("select lastname, firstname from staff s, staff_multi sm 
			    where s.userid = sm.userid and field_name = 'homeroom' 
			    and field_value = ?");

    print qq{<tr><td class="bra">Homeroom</td><td>};
    print qq{<select name="homeroom" multiple size="6">};

    foreach my $hr ( @homerooms ) {
	$sth->execute($hr);
	if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }
	my ($lastname, $firstname) = $sth->fetchrow;
	my $hrname = $hr;
	if ( $lastname ) { $hrname = "$hr - $firstname $lastname"; }
	
	print qq{<option value="$hr">$hrname</option>\n};
    }
    
    print qq{<option value="all">All Homerooms</option>\n};
    print qq{</select></td></tr>\n};

    print qq{<tr><td></td><td class="bla">No Selection = Entire School</td></tr>\n};

    # Check Next Page
    print qq{<tr><td class="bra">Check Next Page</td>};
    print qq{<td><input type="checkbox" name="chk"  value="1"></td></tr>\n};

    
    # Use Legal Name, if present
#    print qq{<tr><td class="bra">Use Legal Name</td>};
#    print qq{<td><input type="checkbox" name="uselegal"  value="1"> (if present)</td></tr>\n};

    # Divider
    print qq{<tr><td colspan="2"><hr></td></tr>\n};

    
    # Cell Width
    print qq{<tr><td class="bra">$lex{'Cell Width'}</td>\n<td>};
    print qq{<input type="text" style="width:3ch;" name="cellwidth" value="$cellwidth"> mm</td></tr>\n};
    print qq{<tr><td class="ra">Skinnier cells</td><td>= More Cells up to 31 max (4.5mm)</td></tr>\n};

    # Font size
    print qq{<tr><td class="bra">$lex{'Font Size'}</td><td>\n};
    print qq{<select  name="fontsize">};
    print qq{<option value="$g_DefaultFontSize">$g_DefaultFontSize pt</option>};
    foreach my $fs ( @g_FontSizes ) {
	if ($fs eq $g_DefaultFontSize ) { next; }
	print qq{<option value="$fs">$fs pt</option>};
    }
    print qq{</select></td></tr>\n};

    # Gray Shade
    print qq{<tr><td class="bra">$lex{'Gray Shade'}</td><td>\n};
    print qq{<input type="text"  name="grayshade" value="$defaultgrayshade" style="width:5ch;">};
    print qq{ (0-1) $lex{'Smaller=Darker'}</td></tr>\n};

    # Paper
    print qq{<tr><td class="bra">$lex{'Paper Size'}</td><td>};
    print qq{ <select name="papersize"><option value="letter">$lex{Letter}</option>\n};
    print qq{<option value="legal">$lex{Legal}</option>};
    print qq{<option value="a4">$lex{A4}</option></select></td></tr>\n};

    # Include Student Number
    print qq{<tr><td class="bra">$lex{Include} $lex{'Student Number'}</td>};
    print qq{<td><input type="checkbox" name="studnumfield"  value="1"></td></tr>\n};

    # Max Students / Page
    print qq{<tr><td class="bra">$lex{Maximum} $lex{Students}/$lex{Page}</td>\n<td>};
    print qq{<input type="text" name="maxstudents" style="width:3ch;" value="$maxstudents">};
    print qq{</td></tr>\n};

    # Hor Bar
    print qq{<tr><td colspan="2"><hr style="height:3px;background-color:black;"></td></tr>\n};

    print qq{<tr><td colspan="2" class="bcn">Use 'My Data' to enter your own values in the extra field</td></tr>\n};

    # Additional/Extra Field
    print qq{<tr><td class="bra">$lex{'Additional Field'}</td>\n};
    print qq{<td><select name="extrafield"><option value=""></option>\n};
    foreach my $fld ( @fields ) {
	print qq{<option value="$fld">$fieldname{$fld} ($fld)</option>\n};
    }
    print qq{</select></td></tr>\n};

    # Additional/Extra Field Width
    print qq{<tr><td class="bra">$lex{'Additional Field'} $lex{Width}</td>\n};
    print qq{<td><input type="text" name="extrafieldwidth" style="width:3ch;" };
    print qq{value="$extrafieldwidth"> mm</td></tr>\n};
    # $extrafieldwidth is set at top of script as a default value.


    
    # Continue
    print qq{<tr><td></td><td  class="la"><input type="submit" value="$lex{Continue}"></td></tr>\n};

#    print qq{<div style="clear:left;"><input type="submit" value="$lex{Continue}"></div>\n};

    print qq{</table>\n};
    # End Table
    
    # New table
    print qq{<table cellpadding="3" cellspacing="0" border="0" };
    print qq{style="float:left;border:1px solid gray;padding:0.5em;margin:0.5em;">\n};
    
    print qq{<tr><td class="bla" colspan="2">$lex{'Repeat Column Headers'}?\n};
    print qq{ <input type="checkbox" name="repeat"></td></tr>\n};

    for ( 1..$entrylimit ) {
	my $entrytxt = "entry$_";
	print qq{<tr><td class="ra">Col Header $_</td>\n};
	print qq{<td><input type="text" name="$entrytxt" }; 
	print qq{size="$maxentrysize" maxlength="$maxentrysize"></td></tr>};
	print qq{\n};
    }

    print qq{<tr><td></td><td><input type="submit" value="$lex{Continue}"></td></tr>\n};
    print qq{</table></form></body></html>\n};

    exit;

}


#-----------
sub prHeader {
#-----------

    my ($teachername,$group) = @_;

    # Done at the start of a new page.
    print TEX "\\begin{tabular}{G|";
    if ( $studnumfield ) { print TEX "p{ $studnumwidth mm}|"; }
    if ( $extrafield ) { print TEX "p{ $extrafieldwidth 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";

    # Print Grade or Homeroom, if not sorting by name
    # if ( not $group ) { $group = $lex{'Not Found'}; }

    print TEX "\\bf\\raggedright Name \\hfill Gr/Hr ";
    
#   if ( $grouptype eq 'grade' ){
#   print TEX "\\Large\\raggedright $lex{Grade} $group ";
#    } else { # homeroom
#	print TEX "\\bf\\raggedright $teachername ";
#	print TEX $lex{Rm}. " $group ";
#    }


    if ( $studnumfield ) { print TEX "& "; }
    if ( $extrafield ) { 
	print TEX "&\\hfil\\rule{6pt}{0pt}\\begin{rotate}{90}$extrafieldName";
	print TEX "\\end{rotate}\\hfil";
    }

    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, $grade, $homeroom, $mydata ) = @_;  # these are passed.
    # mydata is a teacher entered value, not in normal student record

    
    my ($sex, $efield );
    my $fields = 'sex, legal_lastname, legal_firstname ';
    if ( $extrafield ne 'mydata') {
	$fields .= ", $extrafield"; 
    }
    my $sth1 = $dbh->prepare("select $fields from student where studnum = ?");
    $sth1->execute( $studnum );
    if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
    ( $sex, $legal_lastname,$legal_firstname, $efield ) = $sth1->fetchrow;
    ( $efield ) = latex_filter( $efield );


    # these will override the passed name for the legal name
    if ( $arr{uselegal} ) {
	my $fi = substr($firstname, 0, 1);
	my $li = substr($lastname, 0, 1);
	
	if ( $legal_lastname ) {
	    $lastname = "$li$fi ". $legal_lastname. "{\\bf\\tiny{a}}";
	}
	if ( $legal_firstname ) {
	    $firstname = $legal_firstname. "{\\bf\\tiny{a}}";
	}
    }
    

    if ( $alternatingcolor == 0 ) {
	print TEX "\\rowcolor[gray]{". $arr{grayshade}. "}";
	$alternatingcolor = 1;
    } else { $alternatingcolor = 0;}


    print TEX "\\raggedright {\\bf $lastname}, $firstname \\hfill $grade/$homeroom";

    if ( $studnumfield ) { 
	my $snfld = $studnum; # removed $sex;
	print TEX "&\\footnotesize{$snfld}";

    }

    if ( $extrafield ) {
	if ( %defaultvalues and $efield ) {
	    my $tmp = $efield; # store original value
	    $efield = $defaultvalues{$efield}; # display text equivalent
	    $efield .= qq{ ($tmp)};
	}
	if ( $mydata ) {
	    $efield = $mydata;
	}
	
	print TEX "&\\hfil\\footnotesize{$efield}\\hfil"; 
    }

    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 "& "; }
    if ( $extrafield ) { print TEX "& "; }
    for (1..$colcount){ print TEX "& ";}
    print TEX "\\\\\\hline\n";
}

