#!/usr/bin/perl # Copyright Les Richardson 2001-2008 my %lex = ('Homeroom Assignment' => 'Homeroom Assignment', 'Update Homerooms' => 'Update Homerooms', 'Student' => 'Student', 'Hroom:Teacher' => 'Hroom:Teacher', 'Not Defined' => 'Not Defined', 'Not Found' => 'Not Found', 'Grade' => 'Grade', 'Main' => 'Main', 'Eoy' => 'Eoy', 'There was an error storing your data.' => 'There was an error storing your data.', 'Contact' => 'Contact', 'Record the following error' => 'Record the following error', 'Error' => 'Error', 'Your homeroom update is now stored.' => 'Your homeroom update is now stored.', ); my $self = 'resethroom.pl'; use DBI; use CGI; my $q = new CGI; print $q->header; my %arr = $q->Vars; eval require "../../etc/admin.conf"; if ( $@ ) { print $lex{Error}. " $@
\n"; die $lex{Error}. " $@\n"; } my $dsn = "DBI:$dbtype:dbname=$dbase"; my $dbh = DBI->connect($dsn,$user,$password); # Start Page Head print "$doctype\n". $lex{'Homeroom Assignment'}. " $chartype\n\n"; print "[ ". $lex{Main}. " | \n"; print "". $lex{Eoy}. " ]\n"; print "

". $lex{'Homeroom Assignment'}. "

\n"; # update records if necessary. if ( $arr{flag} ) { delete $arr{flag}; resetRecords(); } # Read teachers into a data structure my $sth = $dbh->prepare("select lastname, firstname, grade, homeroom from staff where homeroom != '' order by grade, lastname, firstname"); $sth->execute; if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } # Put Teachers into Data Struct while ( my ( $lastname, $firstname, $grade, $homeroom ) = $sth->fetchrow ){ if ( not $grade ) { next; } # skip staff without grade, since also needed. if ( not $homeroom{ $grade } ) { $homeroom{ $grade } = { }; # hash constructor } if ( not $homeroom{$grade}->{$homeroom} ) { $homeroom{$grade}->{$homeroom} = [ ]; # array constructor } push @{ $homeroom{$grade}->{$homeroom} }, "$firstname $lastname"; } # Now test construction #foreach my $grade ( sort keys %homeroom ) { # foreach my $hrm (sort keys %{ $homeroom{$grade} } ) { # foreach my $name ( @{ $homeroom{$grade}->{$hrm} } ){ # print "Name: $name Hr:$hrm Gr:$grade
\n"; # } # } #} $sth = $dbh->prepare("select studid, lastname, firstname, grade, homeroom from student order by grade, lastname, firstname"); $sth->execute; print "
\n"; print "\n"; print "\n"; my $currgrade = "-1"; my $oldgrade; while ( my ( $studid, $lastname, $firstname, $grade, $homeroom ) = $sth->fetchrow ) { $oldgrade = $currgrade; $currgrade = $grade; if (not $currgrade){ $currgrade = $lex{'Not Defined'}; } if ( $oldgrade ne $currgrade ) { # we have a new grade unless ( $oldgrade eq '-1' ){ print "\n\n";} print "

\n"; print "\n"; print "\n"; # setup homeroom/teacher choices for this grade %hrtable = (); # empty table foreach my $hrm ( keys %{ $homeroom{$currgrade} } ) { my $teachernames; foreach my $name ( @{ $homeroom{$currgrade}->{$hrm} } ) { $teachernames .= $name. '/'; } chop $teachernames; # remove trailing slash; if ( not $teachernames ){ $teachernames = $lex{'Not Found'}; } $hrtable{$hrm} = $teachernames; } } print "\n\n"; } # End of Student Loop print "
". $lex{Grade}. ": $currgrade
". $lex{Student}. ''. $lex{'Hroom:Teacher'}. "
$lastname, $firstname"; print "
\n"; #--------------- sub resetRecords { #--------------- #foreach my $key ( keys %arr ) { print "K:$key V:$arr{$key}
\n"; } foreach my $key ( keys %arr ) { my ($rmnum, $teacher) = split /:/,$arr{$key}; $rmnum =~ s/://; #print "Room: $rmnum Snum: $key \n
" ; $sth = $dbh->prepare("update student set homeroom = ? where studid = ?"); $sth->execute( $rmnum, $key ); if ($DBI::errstr ) { print $DBI::errstr; } } if ( not $DBI::errstr ) { print '

'. $lex{'Your homeroom update is now stored.'}. "

"; } else { print "

". $lex{'There was an error storing your data.'}. "\n"; print $lex{Contact}. " $adminname at $adminemail\n"; print $lex{'Record the following error'}. ": $DBI::errstr \n"; } print "[ ". $lex{Main}. " | ". $lex{Eoy}; print " ]

\n"; exit; }