#!/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.
# schedteacher - display teacher timetables. If passed teacher userid,
# display only that teacher's timetable.
my $self = 'tchschedview.pl';
my $blank = 'Unscheduled';
my $colorCode = '#049';
my %lex = ('No Val' => 'No Val',
'View Teacher Timetable' => 'View Teacher Timetable',
'Schedule' => 'Schedule',
'Term' => 'Term',
);
use DBI;
use CGI;
use Cwd;
$q = new CGI;
# Set mode
my $mode;
my $currdir = getcwd();
my @dirs = split /\//,$currdir;
if ($dirs[-1] eq 'dbook') { $mode = 'popup';}
#print "Mode: $mode\n";
# Http Header to return
print $q->header;
my %arr = $q->Vars;
my $select;
if ( $arr{userid} ){
$select = " where subject.teacher like '%($arr{userid}%' ";
}
if ($arr{term}){
if ($arr{userid}){
$select .= " and schedat.term = '$arr{term}' ";
} else {
$select = " where schedat.term = '$arr{term}' ";
}
}
unless ( require "../../etc/admin.conf" ){
print "Cannot read admin.conf!";
die "Cannot read admin.conf!";
}
$dsn = "DBI:$dbtype:dbname=$dbase";
$dbh = DBI->connect($dsn,$user,$password);
if ($mode eq 'popup') { $stylesheet = $tchcss; } else { $stylesheet = $css;}
# print page header.
print "$doctype\n
$title
$chartype\n";
if ($mode eq 'popup') {
print "\n";
}
print "\n";
print "\n";
# onload=\"setbackground('8213-1')\">\n";
if ($mode ne 'popup') {
print "[ $schedulepage ]\n";
}
#print "Select is: $select \n";
# Find Numbers of Rows (periods) and Columns(Days) in Cycle
$sth = $dbh->prepare("select max(period) from schedat");
$sth->execute;
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr;}
$tblrows = $sth->fetchrow;
$sth = $dbh->prepare("select max(day) from schedat");
$sth->execute;
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr;}
$tblcols = $sth->fetchrow;
$sth = $dbh->prepare("select distinct schedat.subjsec, subject.description,
subject.teacher, schedat.term from schedat
left outer join subject on schedat.subjsec = subject.subjsec
$select order by subject.teacher, schedat.term");
$sth->execute;
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr;}
my $oldterm = -1; my $oldtch = -1;
my $firstflag = 1;
while (my ($subjsec,$desc,$teacher,$term) = $sth->fetchrow){
#print "Subj: $subjsec Tch: $teacher $term \n";
$oldterm = $currterm; $currterm = $term;
$oldtch = $currtch; $currtch = $teacher;
if (($currterm ne $oldterm or $currtch ne $oldtch) and not $firstflag){
# change in teacher or term
# run printTimetable with $teacher, $term, and @subjects
printTimetable($oldtch, $oldterm, \@subjects);
@subjects = (); # empty array.
push @subjects,$subjsec; # start new array with existing value.
} else {
push @subjects, $subjsec;
$firstflag = 0;
}
}
# print out the last table.
printTimetable($currtch, $currterm, \@subjects,$mode);
print "\n";
#-----------------
sub printTimetable { # print timetable for one teacher for 1 term.
#-----------------
my ($teacher, $term, $subjref,$mode) = @_;
my ($name,$userid) = split /\(/,$teacher;
my ($lastname, $firstname) = split /,/,$name;
if ($mode ne 'popup') {
print "
\n";
} else {
print "$firstname $lastname - ". $lex{Term}. " $term \n";
}
my @subject;
foreach my $subjsec (@{$subjref}){
my $sth = $dbh->prepare("select day, period from schedat
where term = '$term' and subjsec = '$subjsec' order by period, day");
$sth->execute;
while (my ($day, $period) = $sth->fetchrow){
if ($subject[$period][$day] and $subject[$period][$day] ne $subjsec){
# if a value exists and is not identical to subjsec then add to it.
$subject[$period][$day] .= " $subjsec";
} else { # just slap it in...
$subject[$period][$day] = $subjsec;
}
}
}
# Done filling 2D array.
my $sth = $dbh->prepare("select description from subject where subjsec = ?");
print "
\n";
# heading
print "
";
for (1..$tblcols){ print "
Day $_
"; }
print "
\n";
# Main body of table.
for my $i (1..$tblrows){
print "
Period $i
\n";
for (1..$tblcols){
if ($subject[$i][$_]){ # if we have a subjsec value;
my $firstflag = 1;
my @subjval = split /\s/,$subject[$i][$_];
print "
";
foreach my $sv ( @subjval ) {
if (not $firstflag){
print " ";
} else {
$firstflag = 0;
}
$sth->execute($sv);
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr;}
my $desc = $sth->fetchrow;
my $cls = 'c'. $sv;
# We are using span elements, since there may be
# more than 1 subject inside each period cell; not as pretty, though.
print "$desc ($sv)";
}
print "