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

#  This file is part of Open Admin for Schools.
#   Origin: from ppvtEdit.pl

my %lex = ('Main' => 'Main',
	   'Error' => 'Error',
	   'Edit' => 'Edit',
	   'Update' => 'Update',
	   'No Blanks Allowed' => 'No Blanks Allowed',
	   'No User Id' => 'No User Id',
	   'No Password' => 'No Password',
	   'Please Log In' => 'Please Log In',
	   'Record(s) Updated' => 'Record(s) Updated',
	   'Edit not allowed' => 'Edit not allowed',
	   'Raw Score' => 'Raw Score',
	   'Standard Score' => 'Standard Score',
	   'Confidence' => 'Confidence',
	   'Interval' => 'Interval',
	   'Percentile' => 'Percentile',
	   'Normal Curve Equivalent' => 'Normal Curve Equivalent',
	   'Stanine' => 'Stanine',
	   'Growth Scale Value' => 'Growth Scale Value',
	   'Age' => 'Age',
	   'Equivalent' => 'Equivalent',
	   'Close' => 'Close',

	   );

use DBI;
use CGI;


my $self = 'fpEdit.pl';

# Configuration Settings -----------------------
my $allow_author = 1; # allow author to edit
my $allow_fp = 1; # allow user with fp access code to edit
#-----------------------------------------------

my $q = new CGI;
my %arr = $q->Vars;

my @time = localtime(time);
my $year = $time[5] + 1900;
my $month = $time[4] + 1;
my $currdate = "$year-$month-$time[3]";


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

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

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

print $q->header( -charset, $charset);


# Check for FP access code
my $sth = $dbh->prepare("select field_value from staff_multi 
  where field_name = 'access' and userid = ?");
$sth->execute( $userid );
if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }

my $access_fp;
while ( my $val = $sth->fetchrow ) {
    if ( uc $val eq 'FP' ) { $access_fp = 1; }
    # print qq{VAL:$val ACCESS:$access_fp<br>\n};
}



# Page Header
my $title = qq{$lex{Edit} 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};
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} ) {
    editRecord( $arr{id} );
    
} elsif ( $arr{page} == 1 ) {
    delete $arr{page};
    writeRecord();
}


#-------------
sub editRecord {
#-------------

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

    my $id = shift;


    # Access Controls
    my $failflag;
    if ( not $allow_author and not $allow_fp ) { $failflag = 1; } # nobody edits
    elsif ( $allow_author and $ref->{tauthor} ne $userid ) { $failflag = 1; } # not author

    if ( $allow_fp and $access_fp ) { $failflag = 0; } # allow override of author failure
    if ( $failflag ) {
	print qq{<h3>$lex{'Edit not allowed'}</h3>\n};
	print qq{</body></html>\n};
	exit;
    }

    # Get F&P Record
    my $sth = $dbh->prepare("select * from fp_book where id = ?");
    $sth->execute( $id );
    if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }
    my $ref = $sth->fetchrow_hashref;
    my %r = %$ref;

    
    # Get Student Name
    my $sth1 = $dbh->prepare("select lastname, firstname from student where studnum = ?");
    $sth1->execute( $r{studnum} );
    if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
    my ( $lastname, $firstname ) = $sth1->fetchrow;

    
    # Form Heading
    print qq{<form action="$self" method="post"> \n};
    print qq{<input type="hidden" name="page" value="1">\n};
    print qq{<input type="hidden" name="id" value="$id">\n};

    print qq{<div><input type="submit" value="$lex{Update}"></div>\n};
    
    # Start Table
    print qq{<table cellpadding="4" cellspacing="0" border="0" };
    print qq{style="border:1px solid gray;margin:0.5em;padding:0.6em;">\n};

    # Student Name
    print qq{<tr><th colspan="2">$firstname $lastname</th></tr>\n};

    # Date
    print qq{<tr><td class="bra">Date</td><td>$r{tdate}</td></tr>\n};
    
    # Author
    print qq{<tr><td class="bra">Author</td><td>$r{tauthor}</td></tr>\n};
    
    # title
    print qq{<tr><td class="bra">Title</td>};
    print qq{<td><input type="text" name="title" };
    print qq{style="40ch;" value="$r{title}"></td></tr>\n};

    # System: NOTE field is actually tsystem
    print qq{<tr><td class="bra">System</td><td><input type="text" name="tsystem" };
    print qq{style="width:2ch;text-align:center;" value="$r{tsystem}"> 1 or 2</td></tr>\n};

    # Book type
    print qq{<tr><td class="bra">Type</td><td><select name="bktype">};
    print qq{<option>$r{bktype}</option>\n};
    print qq{<option value="fiction">Fiction</option><option value="nonfiction">Non-Fiction</option>};
    print qq{</select></td></tr>\n};

    # Reading Level
    print qq{<tr><td class="bra">Reading Level</td><td><input type="text" name="readlevel" };
    print qq{style="width:3ch;text-align:center;" value="$r{readlevel}"></td></tr>\n};

    # Accuracy
    print qq{<tr><td class="bra">Accuracy</td><td><input type="text" name="accuracy" \n};
    print qq{style="width:4ch;text-align:left;" value="$r{accuracy}"></td></tr>\n};

    # Comprehension
    print qq{<tr><td class="bra">Comprehension</td><td><input type="text" };
    print qq{name="comprehension" \n};
    print qq{style="width:4ch;text-align:center;" value="$r{comprehension}"></td></tr>\n};
    
    # Assistance
    print qq{<tr><td class="bra">Assistance</td><td><select name="assist">\n};
    print qq{<option value="$r{assist}">$r{assist}</option>\n};
    print qq{<option>Independent</option><option>Instructional</option><option>Hard</option>\n};
    print qq{</select></td></tr>\n};
    
    # Self correction
    print qq{<tr><td class="bra">Self Correction</td><td><input type="text" };
    print qq{name="selfcorrection" \n};
    print qq{style="width:4ch;text-align:center;" value="$r{selfcorrection}"></td></tr>\n};

    # Fluency
    print qq{<tr><td class="bra">Fluency</td><td><input type="text" name="fluency" \n};
    print qq{style="width:4ch;text-align:center;" value="$r{fluency}"></td></tr>\n};

    # Rate (trate)
    print qq{<tr><td class="bra">Rate</td><td><input type="text" name="trate" \n};
    print qq{style="width:4ch;text-align:center;" value="$r{trate}"></td></tr>\n};

    # Write about Reading
    print qq{<tr><td class="bra">Write About Reading</td><td><input type="text" };
    print qq{name="writeabout" \n};
    print qq{style="width:12ch;text-align:left;" value="$r{writeabout}"></td></tr>\n};

    print qq{</table>\n\n};
    
    print qq{<div><input  type="submit" value="$lex{Update}"></div>\n};

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

    exit;

}


#--------------
sub writeRecord {
#--------------

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

    my $id = $arr{id};
    delete $arr{id};
    if ( not $id ) {
	print qq{<div>Error: Record ID not found!</div>\n};
	print qq{</body></html>\n};
	exit;
    }
    # %arr hash values left are fields in record

    foreach my $key ( keys %arr ) {

#	print "K:$key VAL:$arr{$key}<br>\n";
	my $sth = $dbh->prepare("update fp_book set $key = ? where id = ? ");
	$sth->execute( $arr{$key}, $id);
	if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }
    }

    print qq{<h1>Record Updated</h1>\n};

    print qq{<p><form><input type="hidden" name="none">\n};
    print qq{<input type="button" value="$lex{Close} this Tab" };
    print qq{onClick="parent.close()"></form></p>};


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

    exit;

}
