#! /usr/bin/perl

my %lex = ('Error' => 'Error',
    );

use DBI;

eval require "../../etc/admin.conf";
if ( $@ ) {
    print $lex{Error}. ": $@<br>\n";
    die $lex{Error}. ": $@\n";
}

my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;

my $sth = $dbh->prepare("select id,studnum, tdate from read_test
 order by id desc");
$sth->execute;
if ( $DBI::errstr ) { print "$DBI::errstr\n"; die; }

my $sth1 = $dbh->prepare("select count(*) from read_test_score where testid = ?");
my $sth2 = $dbh->prepare("select lastname, firstname from studentall where studnum = ?");
my $sth3 = $dbh->prepare("delete from read_test where id = ?");

while ( my ( $id, $studnum, $date ) = $sth->fetchrow ) {

    $sth1->execute( $id );
    if ( $DBI::errstr ) { print "$DBI::errstr\n"; die; }
    my $count = $sth1->fetchrow;

    if ( $count == 0 ) {

	# Get Name
	$sth2->execute( $studnum );
	if ( $DBI::errstr ) { print "$DBI::errstr\n"; die; }
	my ($lastname, $firstname) = $sth2->fetchrow;
	
	print qq{<div>No Scores for $firstname $lastname ($studnum) - $date</div>>\n};

	# Delete Old Record
#	$sth3->execute( $id );
	if ( $DBI::errstr ) { print "$DBI::errstr\n"; die; }

	
    }
}
