#!/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 = ('Main' => 'Main', 'Error' => 'Error', 'Schedule Entry' => 'Schedule Entry', 'Schedule' => 'Schedule', 'Grade' => 'Grade', 'Term' => 'Term', 'Terms' => 'Terms', 'Day' => 'Day', 'Save' => 'Save', 'Period' => 'Period', 'Subject-Section' => 'Subject-Section', 'Add Backings' => 'Add Backings', 'Records added for' => 'Records added for', 'Contact' => 'Contact', 'Separate with Spaces' => 'Separate with Spaces', 'Additional Subjects' => 'Additional Subjects', 'Add Schedule Entries for these terms' => 'Add Schedule Entries for these terms', 'Continue' => 'Continue', 'Teacher' => 'Teacher', 'OR' => 'OR', 'Per' => 'Per', 'No Enrolled Subjects Found' => 'No Enrolled Subjects Found', ); use DBI; use CGI; my $self = "schedadd.pl"; my $maxbackings = 6; # maximum number of backed classes per cycle. #($sec, $min, $hour, $mday, $mon, $year, # $wday, $yday, $iddst) = localtime(time); my @tim = localtime(time); my $year = $tim[5] + 1900; my $month = $tim[4] + 1; my $currdate = "$year-$month-$tim[3]"; 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); # print page header. print "$doctype\n". $lex{'Schedule Entry'}. "\n"; print "\n"; print "$chartype\n\n"; print "[ ". $lex{Main}. " |\n"; print "". $lex{Schedule}. " ]\n"; print "

". $lex{'Schedule Entry'}. "

\n"; # show Starting Page if ( $arr{writeflag} ){ updateRecords(); # Write Records } elsif ( not $arr{grade} and not $arr{teacher} ) { showStartPage(); } # parse passed values for termX elements. Extract X. foreach my $key ( keys %arr ) { if ( $key =~ m/term(\d*)/ ) { push @tmp,$1; } } @sortedterms = sort { $a <=> $b } @tmp; my $termselect; { my $startterm = $sortedterms[0]; my $endterm = $sortedterms[-1]; $termselect = "and startrptperiod <= $startterm and endrptperiod >= $endterm"; # print "TS: $termselect START: $startterm END: $endterm
\n"; } my $select; if ( $arr{grade} ) { # split grades to select subjects from if ( $arr{subjectselect} ){ @selgrades = split /\s/,$arr{subjectselect}; } else { $selgrades[0] = $arr{grade}; } foreach my $gr ( @selgrades ) { $select .= "or grade = '$gr' "; } $select =~ s/^or/where/; # change leading 'or' to 'where'. } else { # teacher my ( $dud, $userid ) = split /\(/, $arr{teacher}; chop $userid; $select = "where teacher $sql{like} ". q{'%(}. $userid. q{)%'}; } #print "Select: $select
\n"; # Load Subject-Sections into array. $sth = $dbh->prepare("select description, smdesc, subjsec from subject $select $termselect order by description"); $sth->execute; if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my @subjects; while ( my ( $description,$smdesc, $subjsec ) = $sth->fetchrow ){ # use truncated longer description if no small description. if ( not $smdesc ) { $smdesc = substr($description,0,8);} push @subjects,"$smdesc ($subjsec)"; } # print "Subjects ", @subjects, "
\n"; # Print Teacher or Grade print "
"; if ( $arr{grade} ) { print "". $lex{Grade}. " $arr{grade}\n"; } else { # print grade print "". $lex{Teacher}. " $arr{teacher}\n"; } # Print Terms print " ". $lex{Terms}. "\n "; foreach my $trm ( @sortedterms ){ print $lex{Term}. " $trm  "; $trm = $trm.' '; } print "
\n"; # Print Main Layout Table print "
"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; # Set Days and Periods my $days = $daysPerCycle; # One for each Day. my $maxppd = 0; if ( $arr{grade} ) { $maxppd = $g_ppd{ $arr{grade} }; } else { foreach my $key ( keys %g_ppd ) { if ( $g_ppd{ $key } > $maxppd ) { $maxppd = $g_ppd{ $key }; } } } # Print Header print ""; for (1..$daysPerCycle){ print "\n"; } print "\n"; for my $ppd ( 1..$maxppd ) { print ""; for ( 0..$days ) { if ($_ == 0) { print "\n"; } else { print "\n"; } } print "\n"; } my $span = $days + 1; print "
". $lex{Day}. " $_
". $lex{Per}. " $ppd"; my $id = "$ppd:$_"; prSelections( $id ); print "
\n"; print "

". $lex{'Add Backings'}. "

\n"; print "\n"; print "\n"; for ( 1..$maxbackings ){ print "\n"; print "\n"; print "\n"; } print "
". $lex{'Subject-Section'}. "". $lex{Day}; print "". $lex{Period}. "
"; my $id = "b$_"; prSelections($id); print "
\n"; print "
\n"; #------------------ sub showStartPage { #------------------ # Load terms from subject ( instead of eval previously ) my $sth = $dbh->prepare("select distinct startrptperiod, endrptperiod from subject where startrptperiod != '' and endrptperiod != ''"); $sth->execute; if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my %terms; while ( my ( $startterm, $endterm ) = $sth->fetchrow ) { foreach my $trm ( $startterm .. $endterm ) { $terms{$trm} = 1; } } if ( not %terms ) { # no enrolled subjects found print "

". $lex{'No Enrolled Subjects Found'}. "

\n"; print "\n"; exit; } # Get Teachers my @teachers; my $sth = $dbh->prepare("select distinct userid, lastname, firstname from staff order by lastname, firstname"); $sth->execute; if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } while ( my ( $userid, $lastname, $firstname ) = $sth->fetchrow ) { push @teachers, "$lastname, $firstname ($userid)"; } print "
\n"; print "
\n"; print "\n"; print "\n"; print "
". $lex{'Schedule Entry'}. q{ – }. $lex{Grade}. ""; print "
". $lex{'Additional Subjects'}. q{ – }. $lex{Grade}. ""; print " "; print $lex{'Separate with Spaces'}. "
\n"; print "
\n"; print "". $lex{OR}. " ". $lex{Teacher}; print "
\n"; print "
\n"; print $lex{'Add Schedule Entries for these terms'}. ":
\n"; foreach my $trm ( sort {$a <=> $b} keys %terms ){ print $lex{Term}. " $trm  "; } print "

\n"; #print "

"; #print $le {Term}. " ". $le {'A period of time with report cards at the end'}; #print ".

\n"; print "

\n"; #print "The terms are the periods of time these entries in a #timetable are valid. In a K-6 school with 3 terms for the year, #the answer is all 3 terms. In a high school with 2 semesters and 2 #terms in each semester, the first semester timetable entries would #be for terms 1 and 2 while the second semester would be terms 3 and #4.

\n"; print "

"; print "\n"; exit; } #--------------- sub prSelections { # print selection list for subject array #--------------- my $id = shift; print "\n"; } #---------------- sub updateRecords { # Write the records to schedat table. #---------------- delete $arr{writeflag}; #foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; } my @terms = split /\s/,$arr{terms}; # same for all my $sth = $dbh->prepare("select grade from subject where subjsec = ?"); foreach my $key ( keys %arr ) { my ($period,$day) = split /:/,$key; if ( $day and $arr{$key} and $arr{terms} ) { # filled entry #print "K: $key V: $arr{$key} Day: $day Period: $period
\n"; my ($subj,$subjsec) = split /\(/,$arr{$key}; chop $subjsec; # remove trailing bracket. # Set Grade my $grade; if ( not $arr{grade} ) { $grade = $sth->execute($subjsec); } else { $grade = $arr{grade}; } foreach my $trm ( @terms ) { $sth1 = $dbh->prepare("insert into schedat values ( $sql{default},?,?,?,?,?)"); $sth1->execute( $day, $period, $subjsec, $grade, $trm ); if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } } } elsif ( $arr{$key} and $key =~ m/b(\d)/){ # one of the backups... my $di = "bd$1"; my $pi = "bp$1"; #print "Backup K: $key V:$arr{$key} T:$1 Day: $arr{$di} Period: $arr{$pi}
\n"; my ($subj,$subjsec) = split /\(/,$arr{$key}; chop $subjsec; # remove trailing bracket. if ( $arr{$di} and $arr{$pi} ) { # must have day and period, too. foreach my $trm ( @terms ){ my $sth1 = $dbh->prepare("insert into schedat values ( $sql{default},?,?,?,?,?)"); $sth1->execute( $arr{$di}, $arr{$pi}, $subjsec, $arr{grade}, $trm ); if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } } } # if day and period } # if backup } # end of for loop if ( not $DBI::errstr ) { print '

'. $lex{'Records added for'}. q{ }. $lex{Grade}. " $arr{grade} "; print $lex{Terms}. ": $arr{terms}

\n"; } else { print "

". $lex{Error}. " $DBI::errstr
\n"; print $lex{Contact}. " $adminname $adminemail

\n"; } print "

[ ". $lex{Schedule}. " ] \n"; print "

\n"; exit; }