#!/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.
# OUTLINE: Find the current term. Find the student's classes for this
# term and then match them to the scheduled time.
my %lex = ('Student Search' => 'Student Search',
'Main' => 'Main',
'Schedule' => 'Schedule',
'Term' => 'Term',
'No Term Found' => 'No Term Found',
'The current date is' => 'The current date is',
'Student Not Found!' => 'Student Not Found!',
'Subject' => 'Subject',
'Teacher' => 'Teacher',
'Day' => 'Day',
'Period' => 'Period',
'Location' => 'Location',
'No Subject Found!' => 'No Subject Found!',
'Where is Waldo' => 'Where is Waldo',
'Student Number' => 'Student Number',
'or' => 'or',
'last,first/last/initials' => 'last,first/last/initials',
'Name' => 'Name',
);
use DBI;
use CGI;
use Date::Business;
# Set Dates
my @month = ('','January','February','March','April','May','June',
'July','August','September','October','November','December');
my @tim = localtime(time);
my $year = $tim[5] + 1900;
my $month = $tim[4] + 1;
my $day = $tim[3];
if (length($month) == 1){ $month = '0'.$month;}
if (length($day) == 1){ $day = '0'.$day;}
my $currdate = "$year-$month-$day";
my $currsdate = "$year$month$day";
my $currdate1 = "@month[$month] $day, $year";
my $date = $currsdate; # Short Date, no hyphens
my $dateobj = new Date::Business(DATE=>$date);
my $q = new CGI;
print $q->header;
my %arr = $q->Vars;
my $student = $arr{student};
# Read config variables
require "../../etc/admin.conf" || die "Cannot read admin.conf!";
if (not $maxterms) {
$maxterms = 12; # maximum # of terms to search for.
}
# Figure out the current term, if not passed term
$term = 0;
for (1..$maxterms){
my $startdate = $g_termstart{$_}; $startdate =~ s/-//g;
my $startobj = new Date::Business(DATE => $startdate);
my $enddate = $g_termend{$_}; $enddate =~ s/-//g;
my $endobj = new Date::Business(DATE => $enddate);
$startoffset = $dateobj->diffb($startobj,'prev','next');
$endoffset = $dateobj->diffb($endobj,'prev','next');
# print "StartOffset: $startoffset Endoffset: $endoffset \n";
if ($startoffset >= 0 and $endoffset <= 0){
$term = $_; last;
}
}
#Print Page Header
print "$doctype\n
\n";
print $lex{'The current date is'}. ": $date\n";
print "\n";
die;
}
my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);
my @studnum;
if ($student =~ /\d+/) { # we have a student number
$studnum = $student;
push @studnum, $studnum
} else { # we have words hopefully with a comma
($lastname,$firstname) = split /\,/, $student;
$firstname =~ s/^\s*//;
$lastname =~ s/^\s*//;
if ($lastname and $firstname){ # both entered.
$sth = $dbh->prepare("select studnum from student
where lastname = ? and firstname = ?");
$sth->execute( $lastname, $firstname );
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }
while ( my $studnum = $sth->fetchrow ){
push @studnum,$studnum;
}
} elsif ($lastname and not $firstname){ # only lastname (no comma)
if (length($lastname) == 2){ # search by initials: fi, li.
my $fi = substr($lastname,0,1); my $li = substr($lastname,1,1);
$sth = $dbh->prepare("select studnum from student
where lastname $sql{like} '$li%' and firstname $sql{like} '$fi%'");
} else {
$lastname = $dbh->quote( $lastname );
$sth = $dbh->prepare("select studnum from student
where lastname = $lastname order by firstname");
}
$sth->execute;
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }
while ( my $studnum = $sth->fetchrow){
push @studnum,$studnum;
}
} else { # print an error....
print_error( $lex{'Student Not Found!'} );
}
}
# If no student number found above, then print again, and die;
if (not @studnum) {
print_error( $lex{'Student Not Found!'} );
}
foreach $studnum (@studnum){
# Find the student's name
my $sth = $dbh->prepare("select lastname,firstname from student
where studnum = ? ");
$sth->execute( $studnum );
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr;}
my ($lastname,$firstname) = $sth->fetchrow;
# Now search for this student's classes in this term and matching period
# in schedule table.
$sth = $dbh->prepare("select schedat.subjsec, schedat.day,
schedat.period from eval
left outer join schedat on eval.subjcode= schedat.subjsec
where eval.studnum = ? and eval.term = ? and
schedat.term = ? order by schedat.day, schedat.period");
$sth->execute( $studnum, $term, $term );
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr;}
# Prep for getting subject info inside the loop.
my $sth2 = $dbh->prepare("select description, teacher, location from subject
where subjsec = ?");
print "
$firstname $lastname
\n";
print "
\n";
print "
". $lex{Subject}. "
". $lex{Teacher}. "
". $lex{Day};
print "
". $lex{Period}. "
". $lex{Location}. "
\n";
while ( my ( $subjsec,$day,$period ) = $sth->fetchrow){
$sth2->execute($subjsec);
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr;}
my ($desc, $tch, $location) = $sth2->fetchrow;
if (not $desc){ $desc = "". $lex{'No Subject Found!'}. "";}
print "
$desc ($subjsec)
$tch
\n";
print "
$day
$period
$location
\n";;
}
print "
\n";
}
print_error();
#--------------
sub print_error {
#--------------
my $errstring = shift;
print "