#!/usr/bin/perl # Copyright 2001-2007 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', 'Schedule' => 'Schedule', 'Write Schedule Entries' => 'Write Schedule Entries', 'Edit Schedule Entries' => 'Edit Schedule Entries', 'Terms' => 'Terms', 'Update' => 'Update', 'Term' => 'Term', 'Day' => 'Day', 'Per' => 'Per', 'Teacher' => 'Teacher', 'Subject-Section' => 'Subject-Section', 'Period' => 'Period', 'Blank=same as above' => 'Blank=same as above', 'No Term' => 'No Term', 'Updating Record for' => 'Updating Record for', 'Adding Record for' => 'Adding Record for', 'Deleting Record for' => 'Deleting Record for', 'Add Backings' => 'Add Backings', 'Additional' => 'Additional', 'Adding' => 'Adding', 'Missing' => 'Missing', 'Edit Schedule Entries for term(s)' => 'Edit Schedule Entries for term(s)', 'Continue' => 'Continue', 'Writing Entries for' => 'Writing Entries for', 'for Term(s)' => 'for Term(s)', 'Please record the following error' => 'Please record the following error', 'Your schedule items now stored' => 'Your schedule items now stored', 'There was an error storing your data' => 'There was an error storing your data', 'Please contact network adminstrator' => 'Please contact network adminstrator', 'Teacher User Id' => 'Teacher User Id', 'Separate multiple terms with spaces' => 'Separate multiple terms with spaces', ); # max number of backed classes per cycle. #(control display and entry) my $maxbackings = 8; use DBI; use CGI; use CGI::Session; use Cwd; my $self = "schedit.pl"; my @time = localtime(time); my $year = $time[5] + 1900; my $month = $time[4] + 1; my $currdate = "$year-$month-$time[3]"; my $q = new CGI; my %arr = $q->Vars; #print $q->header; my $userid; if ( $arr{userid} ) { $userid = $arr{userid}; delete $arr{userid}; } # Set prepath for config file: (/tcgi or cgi/schedule) my $prepath = '../..'; if (getcwd() =~ /tcgi/){ # we are in tcgi $prepath = '..'; } require "$prepath/etc/admin.conf" or die "Cannot read admin.conf!"; # Get current dir so know what CSS to display; if (getcwd() =~ /tcgi/){ # we are in tcgi $css = $tchcss; } my $dsn = "DBI:$dbtype:dbname=$dbase"; my $dbh = DBI->connect($dsn,$user,$password); # Session Management my $session; if (not $userid ) { $session = new CGI::Session("driver:mysql;serializer:FreezeThaw", undef,{Handle => $dbh}) or die CGI::Session->errstr; $userid = $session->param('userid'); } print $q->header; # print page header. print "$doctype\n". $lex{'Edit Schedule Entries'}. "\n"; print " $chartype\n\n"; print "[ ". $lex{Main}. " | ". $lex{Schedule}. " ]\n"; # Write Records / Do Backings if ($arr{writeflag}){ #print "

". $lex{'Write Schedule Entries'}. "

\n"; updateRecords(); exit 0; } elsif ($arr{backingsflag}) { # Show Backings Form printBackingsForm( $arr{userid}, $arr{terms} ); exit 0; } elsif ($arr{addbackings}) { # Add backings records delete $arr{addbackings}; addBackings(); exit 0; } # Load terms from eval my @terms; $sth = $dbh->prepare("select distinct term from eval where term != '' order by term"); $sth->execute; if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr;} while (my $trm = $sth->fetchrow){ push @terms, $trm; } # Get Teacher my $teachername; if ( not $arr{term} ) { print "

". $lex{'Edit Schedule Entries'}. "

\n"; getTeacher(); print "\n"; die; } else { # We have teacher, get name $sth = $dbh->prepare("select lastname, firstname from staff where userid = ?"); $sth->execute($userid); if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } my ($lastname, $firstname) = $sth->fetchrow; $teachername = "$firstname $lastname"; } # If here, we have passed term and teacher values ($arr{term} and $arr{userid}) my $select; $select = "where teacher like '%$userid%'"; # Load Subject-Sections for a teacher. $sth = $dbh->prepare("select description, smdesc, subjsec, grade from subject $select order by description"); $sth->execute; if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } # Find subjects and put into hash for selection. # Grades needed for maxPPD calc. my (@subjects, @grades); while (my ($description,$smdesc, $subjsec, $grade) = $sth->fetchrow){ # use truncated longer description if no small description. if (not $smdesc){ $smdesc = substr($description,0,8);} $subjects{$subjsec} = "$smdesc ($subjsec)"; $grades{$grade} = 1; } @grades = sort keys %grades; # Find maximum number of periods per day for table. my $maxPPD; foreach my $gr (@grades) { if ($g_ppd{$gr} > $maxPPD) { $maxPPD = $g_ppd{$gr}; } } # print top section above table: Name and Term(s) print "$teachername — ". $lex{Term}. " $arr{term}  \n"; print $lex{Additional}. q{ }. $lex{Terms}. " \n"; # Print Main Layout Table print "
\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print $lex{'Separate multiple terms with spaces'}; print "
\n"; print "\n"; my $days = $daysPerCycle; # One for each Day. # Print Header print ""; for (1..$daysPerCycle){ print ""; } print "\n"; for my $ppd ( 1..$maxPPD ){ print ""; for (0..$days){ if ($_ == 0){ print ""; } else { print ""; } } print "\n"; } my $span = $days + 1; print "
". $lex{Day}. " $_
". $lex{Per}. " $ppd"; my $id = "$ppd:$_"; prSelections($id, $arr{term}); print "
\n"; print "
\n"; #------------- sub getTeacher { #------------- print "
\n"; print "
". $lex{'Teacher User Id'}. q{ }; print "\n"; print $lex{'Edit Schedule Entries'}. " \n"; print $lex{Term}. "
\n"; print "

\n

A term is a period of time with tests at the end.

The terms are the period 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 term 1 and 2 while the second semester would be term 3 and 4.

\n"; print "

"; print "
\n"; } #--------------- sub prSelections { # print selection list for subject array #--------------- my $pid = shift; # format is period:day my $term = shift; my ($period,$day) = split /:/, $pid; my $found = 0; # find existing values for this period... $sth = $dbh->prepare("select id, subjsec from schedat where day = ? and period = ? and term = ?"); $sth->execute($day,$period,$term); if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } while (my ($id, $subjsec) = $sth->fetchrow ) { if ( $subjects{$subjsec} ) { # this is a subject that teacher teaches.. $found = 1; print "\n"; $counter++; } } if (not $found) { # no subjects found... (no teacher subjects in this period) print "\n"; } } #---------------- sub updateRecords { # update the records in schedat table. #---------------- # passed: currterm, terms, userid, teachername delete $arr{writeflag}; print "

". $lex{'Writing Entries for'}; print " $arr{teachername} ". $lex{'for Term(s)'}. ": $arr{currterm} $arr{terms}

\n"; delete $arr{teachername}; my $userid = $arr{userid}; delete $arr{userid}; if (not $arr{currterm}) { print $lex{'No Term'}; return; } my $currterm = $arr{currterm}; delete $arr{currterm}; my @terms = split /\s/, $arr{terms}; # split additional terms into array; currterm still def'd. my $terms = $arr{terms}; # Now check that terms exist... foreach my $trm (@terms) { # check 'em here... } delete $arr{terms}; # Check passed values #foreach my $key (sort keys %arr) { print "K:$key V:$arr{$key}
\n"; } #die; # Setup for finding grade(s) of each subject (if any). my $sth = $dbh->prepare("select grade from subject where subjsec = ?"); foreach my $key (sort keys %arr) { my ($period,$day,$id) = split /:/, $key; my ($desc,$subjsec) = split /\(/, $arr{$key}; chop $subjsec; # remove trailing bracket. my $grade; if ($subjsec) { # set grade $grade = $sth->execute($subjsec); } else { $grade = $sql{default}; } my $origsubjsec; if ($id > 0) { my $sth1 = $dbh->prepare("select subjsec from schedat where id = ?"); $sth1->execute($id); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } $origsubjsec = $sth1->fetchrow; } print " Subj: $subjsec Orig: $origsubjsec Day: $day Period: $period Id: $id Currtrm: $currterm
\n"; if ($id == -1) { # no record found; if ( $arr{$key} ) { # no current record, but new subjsec exists... add print ''. $lex{'Adding Record for'}. " $subjsec "; print $lex{Term}. ": $currterm \n"; print $lex{Day}. ": $day \n"; print $lex{Period}. ": $period
\n"; $sth1 = $dbh->prepare("insert into schedat values ($sql{default}, ? , ?, ?, ?, ?)"); $sth1->execute($day, $period, $subjsec, $grade, $currterm); if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } } } else { # we have an existing record for current term. # if subjsec is blank, then delete the record (in all terms for this Day:Per) if ( not $subjsec ) { print ''. $lex{'Deleting Record for'}. " $origsubjsec "; print $lex{Term}. ": $currterm \n"; print $lex{Day}. ": $day \n"; print $lex{Period}. ": $period
\n"; my $sth1 = $dbh->prepare("delete from schedat where id = ?"); $sth1->execute($id); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } } else { # we must update (if changed) if ($origsubjsec ne $subjsec) { # changed record, update print ''. $lex{'Updating Record for'}. " $subjsec "; print $lex{Term}. ": $currterm \n"; print $lex{Day}. ": $day \n"; print $lex{Period}. ": $period
\n"; my $sth1 = $dbh->prepare("update schedat set subjsec = ?, grade = ? where id = ? "); $sth1->execute($subjsec, $grade, $id); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } } } # End of Update for changed record. } # End of Else (Existing record) # End of Current Term; Next: Other Terms. my $sth1 = $dbh->prepare("select id from schedat where subjsec = ? and day = ? and period = ? and term = ?"); # now do rest of terms foreach my $trm (@terms) { # Look for original subjsec and new subjsec in current term, day, period. $sth1->execute($origsubjsec, $day, $period, $trm); if ( $DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } my $oldid = $sth1->fetchrow; $sth1->execute($subjsec, $day, $period, $trm); if ( $DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } my $newid = $sth1->fetchrow; print "Subj: $subjsec Orig: $origsubjsec Day: $day Period: $period Trm: $trm NewId: $newid OldID: $oldid
\n"; if ( not $subjsec and $oldid ) { # if orig exists, but new is null, delete. print ''. $lex{'Deleting Record for'}. " $origsubjsec "; print $lex{Term}. ": $trm \n"; print $lex{Day}. ": $day \n"; print $lex{Period}. ": $period
\n"; my $sth2 = $dbh->prepare("delete from schedat where id = ?"); $sth2->execute($oldid); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } } elsif ( $subjsec and $oldid and $subjsec ne $origsubjsec ) { # we have both. update. print ''. $lex{'Updating Record for'}. " $subjsec "; print $lex{Term}. ": $trm \n"; print $lex{Day}. ": $day \n"; print $lex{Period}. ": $period
\n"; my $sth2 = $dbh->prepare("update schedat set subjsec = ?, grade = ? where id = ? "); $sth2->execute($subjsec, $grade, $oldid); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } } elsif ( not $newid and $subjsec ) { # exist check and then add. print ''. $lex{'Adding Record for'}. " $subjsec "; print $lex{Term}. ": $trm \n"; print $lex{Day}. ": $day \n"; print $lex{Period}. ": $period
\n"; $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; } } } # End of Term Loop } # End of keys loop. if (not $DBI::errstr) { print "

". $lex{'Your schedule items now stored'}. ".

"; } else { print "

". $lex{'There was an error storing your data'}. ".\n"; print $lex{'Please contact network adminstrator'}; print " $adminemail\n"; print $lex{'Please record the following error'}; print ": $DBI::errstr

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

\n"; } # end of updateRecords #-------------------- sub printBackingsForm { #-------------------- my $userid = shift; my $terms = shift; my $sth = $dbh->prepare("select lastname, firstname from staff where userid = ?"); $sth->execute($userid); if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } my ($lastname, $firstname) = $sth->fetchrow; # Load Subject-Sections for teacher. my $sth = $dbh->prepare("select description, smdesc, subjsec, grade from subject where teacher like '%$userid%'"); $sth->execute; if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } my @subjects; while (my ($description,$smdesc, $subjsec, $grade) = $sth->fetchrow){ # use truncated longer description if no small description. if (not $smdesc){ $smdesc = substr($description,0,8);} $subjects{$subjsec} = "$smdesc ($subjsec)"; } print "$firstname $lastname ($userid) ". $lex{Terms}. " $terms\n"; print "
\n"; print "\n"; print "\n"; #print "

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

\n"; print "\n"; print "\n"; print "\n"; for my $rec (1..$maxbackings){ print "\n\n"; print "\n"; print "\n"; } print "
". $lex{'Subject-Section'}. "". $lex{Day}; print "". $lex{Period}. "
"; print "
"; print "\n"; print "
\n"; } #-------------- sub addBackings { #-------------- my $terms = $arr{terms}; delete $arr{terms}; my @terms = split / /, $terms; print '

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

\n"; my $sth = $dbh->prepare("select description, grade from subject where subjsec = ?"); if (not $terms) { print $lex{'No Terms'}; die $lex{'No Terms'}; } KEY: foreach my $key (sort keys %arr ) { if ( $key =~ m/^(\d)/ and $arr{$key} ) { #print "K:$key V:$arr{$key}
\n"; my $di = "d$1"; my $pi = "p$1"; if ( not $arr{$di} or not $arr{$pi} ) { print $lex{Missing}. q{ }. $lex{Period}. "/". $lex{Day}. "
\n"; die; } my ( $subj, $subjsec ) = split /\(/,$arr{$key}; chop $subjsec; # remove trailing bracket. $sth->execute($subjsec); if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } my ($description, $grade) = $sth->fetchrow; print $lex{'Adding'}. " $description ($subjsec) – ". $lex{Day}; print " $arr{$di} – ". $lex{Period}. " $arr{$pi}
\n"; $subjsec = $dbh->quote($subjsec); foreach my $trm ( @terms ) { my $sth1 = $dbh->prepare("insert into schedat values ( $sql{default},'$arr{$di}','$arr{$pi}', $subjsec, '$grade','$trm')"); $sth1->execute; if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } } } # End of IF } # End of foreach key loop print "

[ ". $lex{Schedule}. " ]

\n"; print "\n"; } # End of addBackings