#! /usr/bin/perl # Copyright 2001-2010 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 %lex = ('Main' => 'Main', 'No Student(s) Found' => 'No Student(s) Found', 'View/Download' => 'View/Download', 'Student Pictures Report' => 'Student Pictures Report', 'Main' => 'Main', 'View Log File' => 'View Log File', 'Continue' => 'Continue', 'Homeroom' => 'Homeroom', 'Grade' => 'Grade', 'Leave blank for all students' => 'Leave blank for all students', 'Report Type' => 'Report Type', 'PDF' => 'PDF', 'HTML' => 'HTML', 'Pictures per Row' => 'Pictures per Row', 'Select' => 'Select', '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', 'No Picture' => 'No Picture', 'Separate with Spaces' => 'Separate with Spaces', 'Blank=All' => 'Blank=All', 'PDF' => 'PDF', 'Sort by' => 'Sort by', 'Full Size' => 'Full Size', 'Thumbnail' => 'Thumbnail', 'Picture' => 'Picture', ); my $self = 'rptpicstudent1.pl'; use DBI; use CGI; use Cwd; # Configurable settings my $defaultgrayshade = '0.95'; # shading for alternate rows my $defaultPicsPerRow = 6; # number of pictures per row. my $heightadjust = 13; # increase page height by this amount (mm). 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; print $q->header( -charset, $charset ); my %arr = $q->Vars; # Set location of student images. my $imagepath; if ( $arr{piclocation} eq $lex{'Full Size'} ) { $imagepath = $picdir; } else { # use thumbnails. $imagepath = $tndir; } 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); $dbh->{mysql_enable_utf8} = 1; # Get current dir so know what CSS to display; if (getcwd() =~ /tcgi/){ # we are in tcgi $css = $tchcss; $homepage = $tchpage; } print "$doctype\n". $lex{'Student Pictures Report'}. "\n"; print "\n"; print "$chartype\n\n"; print "
[ ". $lex{Main}. " ]
\n"; print '

'. $lex{'Student Pictures Report'}. "

\n"; # Get values if ( not $arr{page} ) { showStartPage(); } else { delete $arr{page}; selectStudents(); } #----------------- sub selectStudents { #----------------- my $select; my $tableheader; # = "$arr{groupname} $arr{groupvalue}"; my $group; if ( $arr{groupname} eq $lex{Homeroom} and $arr{groupvalue} ) { $select = "where homeroom = ?"; $group = 'homeroom'; } elsif ( $arr{groupname} eq $lex{Grade} and $arr{groupvalue} ){ $select = "where grade = ?"; $group = 'grade'; } elsif ( $arr{groupname} eq $lex{Homeroom} or $arr{groupname} eq $lex{Grade} ) { $group = $arr{groupname}; # no value passed, } else { # should never happen unless attempted hack. print $lex{Error}. "
$arr{groupname}\n"; print "\n"; exit; } my @students = (); if ( $select ) { # we have some group value entered. my @groups = split / /, $arr{groupvalue}; my $sth = $dbh->prepare("select studnum from student $select order by lastname, firstname"); foreach my $grp ( @groups ) { $sth->execute( $grp ); if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } while ( my $stud = $sth->fetchrow ) { push @students, $stud; } } } else { # just get all students my $sth = $dbh->prepare("select studnum from student order by lastname, firstname"); $sth->execute; if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } while ( my $stud = $sth->fetchrow ) { push @students, $stud; } } if ( not @students ){ # No students found... print '

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

\n"; print "[ ". $lex{Main}. " ]\n"; print "\n"; exit; } if ( $arr{reporttype} eq $lex{PDF} ) { showPdfReport( $group, $arr{groupvalue}, \@students ); } else { showHtmlReport( \@students ); } } #---------------- sub showPdfReport { #---------------- # Get Student Number list my ($group, $value, $studref) = @_; my @students = @$studref; #print "GRP:$group
\n"; #print "VAL:$value
\n"; #print "Students:", @students, "
\n"; # Set Font/Papersize # The tabcolsep is 1mm my $fontsize = $arr{fontsize}. 'pt'; # Set Paper Size, text width and height 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"; # Adjust Text Height. $textheight =~ s/mm//; $textheight += $heightadjust; $textheight .= 'mm'; # Restrict Gray Shading values if ( $arr{grayshade} > 1 ) { $arr{grayshade} = 1; } if ( $arr{grayshade} < 0.5 ) { $arr{grayshade} = 0.5; } # Set the Picture Width (in mm) my $wi = $textwidth; $wi =~ s/mm//; # strip mm. my $cellwidth = ( $wi - ( $arr{picsperrow} - 1 ) ) / $arr{picsperrow}; $cellwidth = int $cellwidth; $picwidth = $cellwidth - 1; $picwidth = $picwidth. 'mm'; $cellwidth = $cellwidth. 'mm'; # print "Text:$textwidth WI:$wi PPR:$arr{picsperrow} PW:$picwidth
\n"; my $shortname = "picrpt$$"; my $filename = "$shortname.tex"; open(TEX,">$filename") || die "Can't open tex file"; print TEX "\\documentclass[$fontsize,$papersize ]{article} \\usepackage{array,colortbl,graphicx,inputenc,fancyhdr} $a_latex_header \\pagestyle{empty} \\setlength{\\textwidth}{$textwidth} \\setlength{\\textheight}{$textheight} \\setlength{\\hoffset}{-36mm} \\setlength{\\voffset}{-26mm} \\addtolength{\\evensidemargin}{0mm} \\addtolength{\\oddsidemargin}{0mm} \\setlength{\\tabcolsep}{1mm} \\setlength{\\parindent}{0mm} \\setlength{\\footskip}{2mm} \\setlength{\\headsep}{2mm} \\setlength{\\extrarowheight}{2mm} \\pagestyle{fancy} \\lhead{$schoolname -- $schoolyear} \\rhead{Page \\thepage}\n"; if ( $tableheader ) { print TEX "\\chead{\\bf\\Large $tableheader}\n"; } print TEX "\\rfoot{ $currdate }\n\\thispagestyle{fancy}\n\\cfoot{ }\n"; #}{>{\\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\n"; my $sth = $dbh->prepare("select lastname, firstname, $group from student where studnum = ?"); my $curroom; my $count; my @studentprint = (); my $first = 1; foreach my $studnum ( @students ) { $sth->execute( $studnum ); my ($lastname, $firstname, $groupval ) = $sth->fetchrow; $oldroom = $curroom; $curroom = $groupval; if ( $first ) { print TEX "\\chead{\\bf\\Large ". ucfirst( $group). " $curroom}\n"; } if ( $curroom ne $oldroom and not $first ) { # flush buffer, new page, etc. printPictureRow( $arr{picsperrow}, $group, $picwidth, $cellwidth, \@studentprint ); @studentprint = (); print TEX "\\newpage\n\n"; print TEX "\\chead{\\bf\\Large ". ucfirst( $group). " $curroom}\n"; } $first = 0; $count++; push @studentprint, $studnum; # Print Picture Row if @studentprint full if ( ( $#studentprint + 1 == $arr{picsperrow} ) or ( $count == $#students + 1 ) ) { printPictureRow( $arr{picsperrow}, $group, $picwidth, $cellwidth, \@studentprint ); @studentprint = (); } # end of if student print row } # End of Loop for all students 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; } 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{'Student Pictures Report'}. "

\n"; print "

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

\n"; } # end of showPdfReport #---------------- sub showStartPage { # Get Custom Values #---------------- print "
\n"; print "\n"; print "\n"; # Continue Submit Button print "\n"; # Get Student Group print "\n"; print "\n"; # Get Sort Order - Not implemented; Will mess up headings # print "\n"; # Get Student Pictures per Row print "\n"; # Picture Directory to use print "\n"; # Get Paper Size print "\n"; # Get Font size print "\n"; # Get Gray Shading print "\n"; # Get Paper Size print "\n"; # Get Page Breaks #print "\n\n"; print "\n"; print "
". $lex{Select}; print " \n"; print $lex{'Blank=All'}. q{ ,}. $lex{'Separate with Spaces'}. "
". $lex{'Sort by'}. ""; # print "
". $lex{'Pictures per Row'}. "\n"; print ""; print "
". $lex{Picture}. ""; print "
". $lex{'Paper Size'}. ""; print "
". $lex{'Font Size'}. "\n"; print "
". $lex{'Gray Shade'}. "\n"; print ""; print " (0-1) ". $lex{'Smaller=Darker'}. "
". $lex{'Report Type'}. ""; print "
". $lex{PDF}. q{ }. $lex{'Page Break'}. ""; #print ""; #print "
\n"; exit; } #------------------ sub printPictureRow { #------------------ my ( $picsPerRow, $group, $picwidth, $cellwidth, $ref ) = @_; my @studentprint = @$ref; # print table header (only doing 1 row) print TEX "\\begin{tabular}{|"; for ( 1 .. $picsPerRow ) { print TEX "p{$cellwidth}|"; } print TEX "}\\hline\n"; my $sth = $dbh->prepare("select lastname, firstname, $group from student where studnum = ?"); # print picture rows. my $first = 1; for ( 1 .. $picsPerRow ) { # do the row. my $studnum = shift @studentprint; if ( not $first ) { print TEX ' & '; } else { $first = 0; } if ( $studnum ) { # Get Student Info $sth->execute( $studnum ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my ($lastname, $firstname, $groupval ) = $sth->fetchrow; if ( -e "$imagepath/$studnum.jpg" ) { print TEX "\\hfil\\includegraphics[width=$picwidth]{$imagepath/$studnum.jpg}"; } else { print TEX "\\hfil\\raisebox{16mm}{". $lex{'No Picture'}. "}"; } print TEX "\n\n{\\small\\sf $firstname $lastname}\\hfil"; } } # next picture print TEX "\\\\\\hline\n"; print TEX "\\end{tabular}\n"; } # end of printBuffer