#!/usr/bin/perl # Copyright 2001-2009 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. # Function: Display discipline info by date range and by type. # Accept: startdate, enddate, type, infrac my %lex = ('Student Infractions' => 'Student Infractions', 'by Date' => 'by Date', 'Main' => 'Main', 'Discipline' => 'Discipline', 'Start Date' => 'Start Date', 'End Date' => 'End Date', 'School Days' => 'School Days', 'Category' => 'Category', 'Class' => 'Class', 'Grade' => 'Grade', 'Entry Error' => 'Entry Error', 'Totals' => 'Totals', 'Infraction' => 'Infraction', 'Student' => 'Student', 'Count' => 'Count', 'Percent' => 'Percent', 'PerDay' => 'PerDay', 'View Report' => 'View Report', 'Type' => 'Type', 'No Records Found' => 'No Records Found', 'Homeroom' => 'Homeroom', 'Error' => 'Error', ); my $self = 'rptdiscstat.pl'; use DBI; use CGI; use Date::Business; use Number::Format qw{:all}; eval require "../../etc/admin.conf"; if ( $@ ) { print $lex{Error}. ": $@
\n"; die $lex{Error}. ": $@\n"; } my $q = new CGI; print $q->header( -charset, $charset ); my %arr = $q->Vars; my $currdate; { my @time = localtime(time); my $year = $time[5] + 1900; my $month = $time[4] + 1; if (length($month) == 1 ) { $month = '0'. $month; } if (length($time[3]) == 1 ) { $time[3] = '0'. $time[3]; } $currdate = "$year-$month-$time[3]"; } my $startdate; if ( $arr{startdate} ) { $startdate = $arr{startdate}; } else { $startdate = $schoolstart; } my $enddate; if ( $arr{enddate} ) { $enddate = $arr{enddate}; } else { $enddate = $currdate; } my $type = $arr{type}; my $infraction = $arr{infrac}; # generated by a loop. my $dsn = "DBI:$dbtype:dbname=$dbase"; my $dbh = DBI->connect($dsn,$user,$password); my $schooldays = calcTermDays($startdate, $enddate); print "$doctype\n". $lex{'Student Infractions'}. q{ }. $lex{'by Date'}; print "\n"; print "$chartype\n\n"; print "[ ". $lex{Main}. " | ". $lex{Discipline}. " ]\n"; print "

". $lex{'Student Infractions'}. q{ }. $lex{'by Date'}. "

\n"; print "

". $lex{'Start Date'}. ": $startdate ". $lex{'End Date'}. ": $enddate\n"; print "". $lex{'School Days'}. ": $schooldays

\n"; if ( $type eq $lex{Category} ){ doCategory(); } elsif ( $type eq $lex{Class} ){ doClass(); } elsif ( $type eq $lex{Grade} ){ doGrade(); } elsif ($infraction){ doInfraction(); } else { # Boo Boo print $lex{'Entry Error'}. "!\n"; } # Bottom of Page print "
[ ". $lex{Main}. " | \n"; print "". $lex{Discipline}. " ]\n"; mkSearchForm(); print "\n"; #----------------- sub mkSearchForm { #----------------- print "
\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "
Statistical Reports
". $lex{'Start Date'}. ":". $lex{'End Date'}. ":
". $lex{Type}. ":
\n"; } #------------- sub doCategory { # Discipline by Category of Infraction. #------------- # Get the total records first, the ugly way... my $sth = $dbh->prepare("select count(*) from discipline where to_days(discipline.date) >= to_days('$startdate') and to_days(discipline.date) <= to_days('$enddate')"); $sth->execute; if ($DBI::errstr) {print $DBI::errstr; die $DBI::errstr; } my $total = $sth->fetchrow; if ( not $total ) { print $lex{'No Records Found'}. ".\n"; return; } # Now get records again, and loop through results, printing. $sth = $dbh->prepare("select distinct infraction, count(infraction) from discipline where to_days(discipline.date) >= to_days('$startdate') and to_days(discipline.date) <= to_days('$enddate') group by infraction order by infraction"); $sth->execute; if ($DBI::errstr) {print $DBI::errstr; die $DBI::errstr; } print "\n"; print "\n"; # Count the types and numbers. while ( my @infrac = $sth->fetchrow ) { my $percent = format_number( ($infrac[1] * 100)/$total , 2, 2) ; my $perday = format_number( ($infrac[1]/$schooldays), 2, 2); print "\n"; print ""; print "\n"; } print "\n"; print "\n"; print "
". $lex{Infraction}. q{ }. $lex{Category}. "". $lex{Count}; print "". $lex{Percent}. "". $lex{PerDay}. "
\n"; print "\n"; print "\n"; print "$infrac[1]$percent\%$perday
". $lex{Totals}. "$total
\n"; } #-------------- sub doClass { # Discipline by Class #-------------- # Get the total records first, the ugly way... my $sth = $dbh->prepare("select count(*) from discipline where to_days(discipline.date) >= to_days('$startdate') and to_days(discipline.date) <= to_days('$enddate') "); $sth->execute; if ($DBI::errstr) {print $DBI::errstr; die $DBI::errstr; } my $total = $sth->fetchrow; if ( not $total ) { print $lex{'No Records Found'}; return; } # Now get records again, and loop through results, printing. $sth = $dbh->prepare("select distinct studentall.homeroom, count(discipline.date) from studentall left outer join discipline on discipline.userid = studentall.studnum where to_days(discipline.date) >= to_days('$startdate') and to_days(discipline.date) <= to_days('$enddate') group by homeroom"); $sth->execute; if ($DBI::errstr) {print $DBI::errstr; die $DBI::errstr; } print ""; print "\n"; # Count the types and numbers. while (my @infrac = $sth->fetchrow){ # Find teacher(s) $sth1 = $dbh->prepare("select lastname, firstname from staff where homeroom = '$infrac[0]'"); $sth1->execute; if ($DBI::errstr) {print "Error: $DBI::errstr"; } my ($lastname, $firstname) = $sth1->fetchrow; my $percent = round( ($infrac[1] * 100) /$total, 2) ; my $perday = round( ($infrac[1]/$schooldays), 2); print ""; print "\n"; } print "\n"; print "
". $lex{Homeroom}. "". $lex{Count}. ""; print $lex{Percent}. "". $lex{PerDay}. "
$lastname, $firstname ($infrac[0])$infrac[1]$percent\%$perday
". $lex{Totals}. " $total
\n"; } #-------------- sub doGrade { # Discipline by Grade #-------------- # Get the total records first, the ugly way... my $sth = $dbh->prepare("select count(*) from discipline where to_days(discipline.date) >= to_days('$startdate') and to_days(discipline.date) <= to_days('$enddate') "); $sth->execute; if ($DBI::errstr) {print $DBI::errstr; die $DBI::errstr; } my $total = $sth->fetchrow; if ( not $total ) { print $lex{'No Records Found'}. ".\n"; return; } # Now get records again, and loop through results, printing. $sth = $dbh->prepare("select distinct studentall.grade, count(discipline.date) from studentall left outer join discipline on discipline.userid = studentall.studnum where to_days(discipline.date) >= to_days('$startdate') and to_days(discipline.date) <= to_days('$enddate') group by grade"); $sth->execute; if ($DBI::errstr) {print $DBI::errstr; die $DBI::errstr; } print ""; print "\n"; # Count the types and numbers. while ( my @infrac = $sth->fetchrow ) { my $percent = round( (($infrac[1] * 100)/$total), 2); my $perday = round( ($infrac[1]/$schooldays), 2); print ""; print "\n"; } print "\n"; print "\n"; print "
". $lex{Grade}. "". $lex{Count}. ""; print $lex{Percent}. "". $lex{PerDay}. "
$infrac[0]$infrac[1]$percent\%$perday
". $lex{Totals}. "$total
\n"; } #-------------- sub doInfraction { # Discipline by Infraction #-------------- print "

". $lex{Infraction}. ": $infraction

\n"; # Get the total records first, the ugly way... my $sth = $dbh->prepare("select count(*) from discipline where to_days(discipline.date) >= to_days('$startdate') and to_days(discipline.date) <= to_days('$enddate') and infraction = '$infraction'"); $sth->execute; if ($DBI::errstr) {print $DBI::errstr; die $DBI::errstr; } my $total = $sth->fetchrow; if (not $total){ print $lex{'No Records Found'}. ".\n"; return; } # Now get records again, and loop through results, printing. $sth = $dbh->prepare("select distinct studentall.studnum, count(discipline.date) as counter from studentall left outer join discipline on discipline.userid = studentall.studnum where to_days(discipline.date) >= to_days('$startdate') and to_days(discipline.date) <= to_days('$enddate') and infraction = '$infraction' group by studnum order by counter desc, studentall.lastname, studentall.firstname"); $sth->execute; if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } print ""; print "\n"; # Count the types and numbers. while ( my @infrac = $sth->fetchrow ) { # Find student name $sth1 = $dbh->prepare("select lastname, firstname from studentall where studnum = ?"); $sth1->execute( $infrac[0] ); if ($DBI::errstr) {print $DBI::errstr; die $DBI::errstr; } my ($lastname, $firstname) = $sth1->fetchrow; my $percent = round( (($infrac[1] * 100)/$total), 2); my $perday = round( ($infrac[1]/$schooldays), 2); print "\n"; print ""; print "\n"; } print "\n"; print "
". $lex{Student}. "". $lex{Count}. "". $lex{Percent}; print "". $lex{PerDay}. "
\n"; print "$lastname, $firstname ($infrac[0])$infrac[1]$percent\%$perday
Totals $total
\n"; } #--------------- sub calcTermDays { #--------------- # this calculates the number of schooldays in a date range # taking school holidays into account. my ($startdate, $enddate) = @_; # It likes it in yyyymmdd format... $startdate =~ s/-//g; # strip out hyphens. $enddate =~ s/-//g; if (length($startdate) == 6){ # add 20 to date $startdate = '20'.$startdate; } if (length($enddate) == 6){ # add 20 to date $enddate = '20'.$enddate; } $end = new Date::Business(DATE=>$enddate); $start = new Date::Business(DATE=>$startdate); $schooldays = $end->diffb($start,'prev','next'); $schooldays++; # We now have the number of school days in current term # excluding holidays/pd days. # Now find holidays $sth = $dbh->prepare("select * from dates where to_days(date) >= to_days('$startdate') and to_days(date) <= to_days('$enddate')"); $sth->execute; if ($DBI::errstr) {print $DBI::errstr; die;} $holidays = $sth->rows; # Number of holidays in current term. $termdays = $schooldays - $holidays; return $termdays; }