#!/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.
my %lex = (
'Fees' => 'Fees',
'Main' => 'Main',
'Top' => 'Top',
'Locker Number' => 'Locker Number',
'Assign Lockers' => 'Assign Lockers',
'Student' => 'Student',
'Error' => 'Error',
'Student Group' => 'Student Group',
'Students per Locker' => 'Students per Locker',
'Match Gender' => 'Match Gender',
'Locker Location' => 'Locker Location',
'Starting Locker' => 'Starting Locker',
'Ending Locker' => 'Ending Locker',
'Grade' => 'Grade',
'Homeroom' => 'Homeroom',
'or' => 'or',
'Continue' => 'Continue',
'if' => 'if',
'No Lockers Found' => 'No Lockers Found',
'Lock Pool' => 'Lock Pool',
'Assign Locks' => 'Assign Locks',
'Blank=All' => 'Blank=All',
'Too many students for locker' => 'Too many students for locker',
'No Lock' => 'No Lock',
'All Students have Lockers' => 'All Students have Lockers',
'Assigned' => 'Assigned',
'Lock' => 'Lock',
'Locker' => 'Locker',
);
my $self = 'lockerassign.pl';
my $maxstudentsperlocker = 6;
use DBI;
use CGI;
use Cwd;
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 $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;
$dbh->{mysql_enable_utf8} = 1;
# Get current dir so know what CSS to display;
my $runmode = 'main';
if ( getcwd() =~ /tcgi/){ # we are in tcgi
$css = $tchcss;
$homepage = $tchpage;
$runmode = 'teacher';
}
# Print Page Header
print "$doctype\n
\n";
if ( not $arr{page} ) {
showStartPage();
} elsif ( $arr{page} == 1 ) {
delete $arr{page};
selectLockers();
} elsif ( $arr{page} == 2 ) {
delete $arr{page};
addRecords();
}
#----------------
sub selectLockers {
#----------------
#foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key} \n"; }
my $studentsperlocker;
if ( $arr{studentsperlocker} =~ m/^\d/ and $arr{studentsperlocker} <= $maxstudentsperlocker
and $arr{studentsperlocker} > 0 ) {
$studentsperlocker = $arr{studentsperlocker};
} else {
$studentsperlocker = 1;
}
# homeroom or grade?
my $group;
if ( $arr{group} eq $lex{Grade} ) {
$group = 'grade';
} else {
$group = 'homeroom';
}
my @groups = split /\s/, $arr{groupid}; # multiple grades or homerooms.
delete $arr{group};
delete $arr{groupid};
my $matchgender;
if ( $arr{matchgender} and $studentsperlocker > 1 ) { # both must be true.
$matchgender = 1;
}
# get a list of students who do not have a locker
my @students = ();
my %names = ();
my %gender = ();
my $sth1 = $dbh->prepare("select count(*) from lok_rlink where studnum = ?");
foreach my $grp ( @groups ) { # loop through all grades / homerooms.
my $sth = $dbh->prepare("select studnum, lastname, firstname, sex from student
where $group = ? order by lastname, firstname");
$sth->execute( $grp );
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
while ( my ($studnum, $lastname, $firstname, $sex ) = $sth->fetchrow ) {
# Check for Locker assigned, skip if so.
$sth1->execute( $studnum );
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
my $count = $sth1->fetchrow;
if ( $count > 0 ) { next; }
push @students, $studnum;
$names{$studnum} = "$lastname, $firstname";
$gender{$studnum} = $sex;
}
}
# We should now have the students in @students and names in %names;
if ( not @students ) {
print "
". $lex{'All Students have Lockers'}. "
\n";
print "\n";
exit;
}
my $lockerrows;
my @lockers = ();
my $sth;
# find our lockers
if ( $arr{lockerlocation} ) { # use this to select locker
$sth = $dbh->prepare("select locker_num from lok_locker
where location = ?");
$sth->execute( $arr{lockerlocation} );
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
} elsif ( $arr{startlocker} and $arr{endlocker} and
( $arr{endlocker} > $arr{startlocker} ) ) {
$sth = $dbh->prepare("select locker_num from lok_locker
where locker_num >= ? and locker_num <= ? ");
$sth->execute( $arr{startlocker}, $arr{endlocker} );
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
} else { # find them all
$sth = $dbh->prepare("select locker_num from lok_locker");
$sth->execute;
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
}
$lockerrows = $sth->rows;
if ( $lockerrows < 1 ) {
print "