#!/usr/bin/perl
# Copyright 2001-2008 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 = ('Student' => 'Student',
'Subject' => 'Subject',
'Terms' => 'Terms',
'Main' => 'Main',
'Add Subject Enrollments' => 'Add Subject Enrollments',
'Report Card' => 'Report Card',
'Record exists for'=> 'Record exists for',
'Your enrollments are now stored' =>
'Your enrollments are now stored',
'Please record the following error' =>
'Please record the following error',
'There was an error storing your data' =>
'There was an error storing your data',
'Please contact' => 'Please contact',
'Error' => 'Error',
'Skip' => 'Skip',
);
use DBI;
use CGI;
eval require "../../etc/admin.conf";
if ( $@ ) {
print $lex{Error}. " $@ \n";
die $lex{Error}. " $@\n";
}
my $q = new CGI;
print $q->header;
my %arr = $q->Vars;
# print Page head
print "$doctype\n
\n";
my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);
my $sth1 = $dbh->prepare("select description, teacher,
startrptperiod, endrptperiod from subject where subjsec = ?");
my $sth2 = $dbh->prepare("select lastname, firstname from student
where studnum = ?");
my $sth3 = $dbh->prepare("select term from eval
where subjcode = ? and studnum = ? order by term");
foreach my $key (sort keys %arr) { # $key contains studnum:subjsec
my ($studnum,$subjsec) = split(/:/,$key);
my $term;
%terms = ();
# Read the subject
$sth1->execute($subjsec);
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }
my ($description, $teacher, $startterm, $endterm) = $sth1->fetchrow;
# Get Student Name
$sth2->execute($studnum);
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }
my ($lastname, $firstname) = $sth2->fetchrow;
# Check for the existence of the same record(s),
# one for each term for each student in each subject.
$sth3->execute($subjsec, $studnum);
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr;}
# Put existing term records in the 'terms' hash; check for dups.
while (my $term = $sth3->fetchrow ) {
if ( $terms{$term} ) {
print $lex{'Record exists for'}. q{ }. $lex{Term};
print ": $term ". $lex{for}. " \n";
print "$description ($subjsec) ". $lex{for};
print " $lastname, $firstname ($studnum). \n";
next;
} else { # add to hash.
$terms{$term} = 1;
}
}
# print line for this record.
print "
$lastname, $firstname ($studnum)
";
print "
$description ($subjsec)
\n
";
# Loop through and check where term is missing...
for (my $i = $startterm;$i <=$endterm; $i++){
if (not $terms{$i}){ # fill in holes!
$sth = $dbh->prepare("insert into eval ( subjcode, studnum, term ) values ( ?, ?, ? )");
$sth->execute( $subjsec, $studnum, $i );
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }
print " $i ";
} else {
print "(". $lex{Skip}. " $i) ";
}
} # Loop for Terms
print "
\n"; # end of printed row
# Write a record into evaljrl table.
# wrEvalJrl($subjsec,$studnum); # not used anymore.
} # Next Student-Subject combo.
if (not $DBI::errstr ) {
print "
\n";
print "
". $lex{'Your enrollments are now stored'}. ".
\n";
} else {
print "
". $lex{'There was an error storing your data'}. ".\n";
print $lex{'Please contact'}. " $adminname ";
print "$adminemail\n";
print $lex{'Please record the following error'}. " $DBI::errstr
\n";
# Remove in August 2010...
#------------
sub wrEvalJrl { # write subject enrollment recs into evaljrl table
#------------
# Passed subject-section number (unique for a subject), and student number.
my $subjsec = shift;
my $studnum = shift;
# Get Student Information
my $sth = $dbh->prepare("select lastname, firstname, initial,
birthdate, provnum
from studentall where studnum = '$studnum'");
$sth->execute;
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }
my ($lastname, $firstname, $initial, $birthdate, $provnum) =
$sth->fetchrow;
$lastname = $dbh->quote($lastname);
$firstname = $dbh->quote($firstname);
$initial = $dbh->quote($initial);
# Add a single record for this student in this subject to evaljrl table.
$sth = $dbh->prepare("insert into evaljrl values (
$sql{default},'$subjsec','$studnum', now(),'enrol',
'Y',$lastname, $firstname , $initial ,'$birthdate','$provnum')");
$sth->execute;
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }
}