#!/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 @fields = ('shortvowels','digraphs','vce','longerwords','endspellpat','rctrlvowels','longvowelteams',
	      'othervowels','dipsilent','suffixprefix','addaffix');

my %fieldtext = ('shortvowels' => 'Short Vowels','digraphs' => 'Digraphs','vce' => 'VCe',
		 'longerwords' => 'Longer Words','endspellpat' => 'Ending Spelling Patterns',
		 'rctrlvowels' => 'R-Controlled Vowels','longvowelteams' => 'Long Vowel Teams',
		 'othervowels' => 'Other Vowels','dipsilent' => 'Diphthongs &amp; Silent Letters',
		 'suffixprefix' => 'Suffixes &amp; Prefixes','addaffix' => 'Additional Affixes');


my $self = 'ufliEdit.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;

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

    
    # 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 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="1" };
    print qq{style="border:1px solid gray;margin:0.5em;">\n};

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

    foreach my $field ( @fields ) {
	print qq{<tr><td class="bra">$fieldtext{$field}</td>};
	print qq{<td style="padding:8px;">};
	print qq{<input type="text" name="$field" value="$r{$field}" style="width:2ch;"></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_ufli_test 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;

}
