#!/usr/bin/perl
#  Copyright 2001-2025 Leslie Richardson

#  This file is part of Open Admin for Schools.

# View FP Records


my %lex = ('Main' => 'Main',
	   'Error' => 'Error',
	   'Continue' => 'Continue',
	   'Start Date' => 'Start Date',
	   'End Date' => 'End Date',

	   );

use DBI;
use CGI;
use Cwd;

my $self = 'fpView.pl';

# my $editScript = './fpEdit.pl';

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


# Get current dir so know what CSS to display and shift settings.
if ( getcwd() !~ /tcgi/ ) { # we are in cgi, not teacher tcgi
    $tchcss = $css;
    $tchpage = $homepage;
    $tchdownloaddir = $downloaddir;
    $tchwebdownloaddir = $webdownloaddir;
}


my $q = new CGI;
print $q->header( -charset, $charset);
my %arr = $q->Vars;

my $userid = $ENV{'REMOTE_USER'};

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


# Page Header
my $title = qq{View F&amp;P Reading Records};
print qq{$doctype\n<html><head><title>$title</title>\n}; 
print qq{<link rel="stylesheet" href="$tchcss" type="text/css">\n};

if ( not $arr{page} ) { # calendar popup.
    print qq{<link rel="stylesheet" type="text/css" media="all" };
    print qq{href="/js/calendar-blue.css" title="blue">\n};
    print qq{<script type="text/javascript" src="/js/calendar.js"></script>\n};
    print qq{<script type="text/javascript" src="/js/lang/calendar-en.js"></script>\n};
    print qq{<script type="text/javascript" src="/js/calendar-setup.js"></script>\n};
}


print qq{$chartype\n</head><body style="padding:1em 2em;">\n};

print qq{[ <a href="$tchpage">$lex{Main}</a> ]\n};
print qq{<h1>$title</h1>\n};


if ( not $arr{page} ) {
    showStartPage();

} elsif ( $arr{page} == 1 ) {
    delete $arr{page};
    showFPRecords();
}


#----------
sub fmtDate {
#----------

    my ( $year, $mon, $day ) = split /-/, shift;
    return "$year-$s_month[$mon]-$day";
}



#----------------
sub showStartPage {
#----------------

    # foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}<br>\n"; }


    print qq{<table cellspacing="0" border="1" cellpadding="3">\n};
    print qq{<tr><th>Test<br>Grade</th><th># Recs</th></tr>\n};

    my $sth = $dbh->prepare("select distinct tgrade from fp_book where tgrade is not NULL and tgrade != '' ");
    $sth->execute;
    
    my $sth1 = $dbh->prepare("select count(*) from fp_book where tgrade = ? ");

    
    while ( my $grade = $sth->fetchrow ) {
	
	# Count Records for each grade
	$sth1->execute($grade);
	if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }
	my $count = $sth1->fetchrow;
	
	print qq{<tr><td class="bla">}; #Grade $grade };
	print qq{<form action="$self" method="post">\n};
	print qq{<input type="hidden" name="page" value="1">\n};
	print qq{<input type="hidden" name="grade" value="$grade">\n};
	print qq{<input type="submit" value="Grade 3"></form>\n};
	print qq{</td><td class="bcn">$count</td></tr>\n};
    }

    print qq{</table>\n};
    print qq{</body></html>\n};

    exit;

}


#---------------
sub showFPRecords {
#---------------

    # foreach my $key ( sort keys %arr ) { print qq{K:$key V:$arr{$key}<br>\n}; }
    # just passed grade
    
    my $grade = $arr{grade};

    print qq{<h3>Grade $grade</h3>\n};
    
    
    my $sth = $dbh->prepare("select * from fp_book where tgrade = ? ");
    $sth->execute($grade);
    if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }

    my $sth1 = $dbh->prepare("select lastname, firstname from studentall where studnum = ?");
    
    while ( my $ref = $sth->fetchrow_hashref ) {
	my %r = %$ref;

	$sth1->execute($r{studnum});
	if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }
	my ($ln,$fn) = $sth1->fetchrow;
	
	
	# Start Table
	print qq{<table cellpadding="4" cellspacing="0" border="0" };
	print qq{style="border:1px solid gray;float:left;margin:0.5em;padding:0.6em;">\n};
	print qq{<caption style="font=weight:bold;font-size:120%;background-color:#EEE">};
	print qq{$fn $ln $r{tdate}</caption>\n};
	
	# title
	print qq{<tr><td class="bra"><span style="color:#090;">$count</span> Title</td>};
	print qq{<td>$r{title}</td></tr>\n};

	# System: NOTE field is actually tsystem
	print qq{<tr><td class="bra">System</td><td>$r{tsystem}</td></tr>\n};

	# Book type
	print qq{<tr><td class="bra">Type</td><td>$r{bktype}</td></tr>\n};

	# Reading Level
	print qq{<tr><td class="bra">Reading Level</td><td>$r{readlevel}</td></tr>\n};

	# Accuracy
	print qq{<tr><td class="bra">Accuracy</td><td>$r{accuracy}</td></tr>\n};

	# Comprehension
	print qq{<tr><td class="bra">Comprehension</td><td>$r{comprehension}</td></tr>\n};

    
	# Assistance
	print qq{<tr><td class="bra">Assistance</td><td>$r{assist}</td></tr>\n};
	
	# Self correction
	print qq{<tr><td class="bra">Self Correction</td><td>$r{selfcorrection}</td></tr>\n};

	# Fluency
	print qq{<tr><td class="bra">Fluency</td><td>$r{fluency}</td></tr>\n};

	# Rate (trate)
	print qq{<tr><td class="bra">Rate</td><td>$r{trate}</td></tr>\n};

	# Write about Reading
	print qq{<tr><td class="bra">Write About Reading</td><td>$r{writeabout}</td></tr>\n};

	print qq{</table>\n\n};
    
    }

    
    print qq{<p style="clear:left;">[ <a href="$self">View Other Grades</a> ]\n};
    print qq{</body></html>\n};

    exit;

} # end of showFPRecords
