#!/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". $lex{'Assign Lockers'}. "\n"; print "\n"; print "\n"; print "[ ". $lex{Main}. " \n"; if ( $runmode eq 'main' ) { print "| ". $lex{Fees}. " \n"; } print " ]\n"; print "

". $lex{'Assign Lockers'}. "

\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 "

". $lex{'No Lockers Found'}. "

\n"; print "\n"; die; } # Now load all the lockers, checking for assigned ones. my $sth1 = $dbh->prepare("select count(*) from lok_rlink where locker_num = ?"); while ( my $locker_num = $sth->fetchrow ) { $sth1->execute( $locker_num ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my $count = $sth1->fetchrow; if ( $count >= $arr{studentsperlocker} ) { next; } # skip this locker, if maxed out. push @lockers, $locker_num; } # Now find locks, if going to assign locks, as well. my @locks = (); if ( $arr{assignlocks} ) { # First find the locks for a pool (or not). if ( $arr{lockpool} ) { $sth = $dbh->prepare("select lock_num from lok_lock where pool = ? order by lock_num"); $sth->execute( $arr{lockpool} ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } } else { # load them all $sth = $dbh->prepare("select lock_num from lok_lock order by lock_num"); $sth->execute; if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } } my $sth1 = $dbh->prepare("select count(*) from lok_link where lock_num = ?"); while ( my $lock_num = $sth->fetchrow ) { $sth1->execute( $lock_num ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my $count = $sth1->fetchrow; if ( $count > 0 ) { next; } # skip this lock push @locks, $lock_num; } } # Now start the form. print "
\n"; print "\n"; print "\n"; print ""; print "\n"; if ( $arr{assignlocks} ) { print ""; } print "\n"; my $rowcolor = 'gray'; # row colors are blue or gray; my $sth1 = $dbh->prepare("select lastname, firstname from student where studnum = ?"); my $sth2 = $dbh->prepare("select count(*) from lok_rlink where locker_num = ?"); # Loop through all lockers, assigning students, and possibly locks. foreach my $locker_num ( @lockers ) { # Figure out how many students to add to this locker. $sth2->execute( $locker_num ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my $count = $sth2->fetchrow; my $studcount = $arr{studentsperlocker} - $count; if ( $studcount == 0 ) { next; } # skip this locker, if maxed out. if ( $studcount < 0 ) { print $lex{Error}. q{ }. $lex{'Too many students for locker'}. " $locker_num\n"; } $addlock = 1; # so only 1 lock per locker, if assigning. while ( $studcount ) { # Get Next student my $studnum = pop @students; if ( not $studnum ) { last; } # done all students; # Get Student Name $sth1->execute( $studnum ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my ( $lastname, $firstname ) = $sth1->fetchrow; print "\n\n"; if ( $arr{assignlocks} ) { if ( $addlock ) { # only 1 lock per locker my $lock_num = pop @locks; $addlock = 0; if ( $lock_num ) { # if we still have some locks left... print ""; } else { print ""; } } else { print ""; } } print "\n"; $studcount--; } # toggle row color; if ( $rowcolor eq 'blue'){ $rowcolor = 'gray'; } else { $rowcolor = 'blue'; } } # end of lock loop. print "
". $lex{Student}. "". $lex{'Locker Number'}. "". $lex{'Assign Locks'}. "
"; print "$lastname, $firstname"; print "$lock_num => ". $lex{'No Lock'}. "
\n"; print "\n"; print "
"; if ( $runmode eq 'main' ) { print "[ ". $lex{Fees}. " ]\n"; } print "[ ". $lex{Top}. " ]

\n"; print "
\n"; } # end of selectLockers #---------------- sub showStartPage { #---------------- # Show existing grades and homerooms (for selection of students). # Show existing locker locations and numbers (in that location). # Allow auto-assign of locks. # Misc Options: Gender Matching, Students per Locker. # Get Locker Locations my $sth = $dbh->prepare("select distinct location from lok_locker where location != '' and location is not NULL order by locker_num"); $sth->execute; if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my @locations; while ( my $loc = $sth->fetchrow ) { push @locations, $loc; } # Get Lock Pools my $sth = $dbh->prepare("select distinct pool from lok_lock where pool != '' and pool is not NULL order by pool"); $sth->execute; if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my @lockpools; while ( my $pool = $sth->fetchrow ) { push @lockpools, $pool; } print "
\n"; print "\n"; print "\n"; print "\n\n"; print "\n\n"; print "\n\n"; print "\n"; print "\n\n"; print "\n\n"; print "\n\n"; print "\n"; print "\n\n"; print "\n\n"; print "\n"; print "
". $lex{'Student Group'}. ""; print ""; print " "; print $lex{'Blank=All'}. "
". $lex{'Students per Locker'}. ""; print "\n"; print "
". $lex{'Match Gender'}. ""; print " "; print $lex{if}. q{ }. $lex{'Students per Locker'}. " > 1
\n"; print "
". $lex{'Locker Location'}. ""; if ( @locations ) { print "\n"; } else { print "\n"; } print "
". $lex{or}. " ". $lex{'Starting Locker'}; print ""; print "\n"; print "
". $lex{'Ending Locker'}. ""; print "\n"; print "
\n"; print "
". $lex{'Assign Locks'}. ""; print "
". $lex{'Lock Pool'}. ""; print "
\n"; print "\n"; print "
\n"; exit; } #---------------- sub addRecords { # assign lockers to students; #---------------- # foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; } my $sth = $dbh->prepare("insert into lok_rlink values( ?,? )"); my $sth1 = $dbh->prepare("insert into lok_link values( ?,? )"); my $sth2 = $dbh->prepare("select lastname, firstname from student where studnum = ?"); print "\n"; foreach my $key ( sort keys %arr ) { my ($sn, $studnum ) = split /-/, $key; if ( $sn eq 'SN' ) { # we have a student number; add locker if ( $arr{$key} ) { # we have a locker number in $arr{$key} $sth->execute( $arr{$key}, $studnum ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } $sth2->execute( $studnum ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my ( $lastname, $firstname ) = $sth2->fetchrow; print "\n"; } } else { # we're adding a lock to a locker. if ( $arr{$key} ) { # we have locker_num in $arr{$key}, and lock_num in $key $sth1->execute( $arr{$key}, $key ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } print $lex{Lock}. " $key ". $lex{Assigned}. " "; print $lex{Locker}. "$arr{$key}\n"; } } } if ( $runmode eq 'main' ) { print "[ ". $lex{Fees}. " ]\n"; } print "[ ". $lex{Top}. " ]

\n"; print "\n"; exit; }
$lastname, $firstname ". $lex{Assigned}. " "; print $lex{Locker}. "$arr{$key}