#!/usr/bin/perl
# Copyright 2001-2021 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 = ('Report Card' => 'Report Card',
'Main' => 'Main',
'Code-Section' => 'Code-Section',
'Grade' => 'Grade',
'PowerDelete' => 'PowerDelete',
'Error' => 'Error',
'Group' => 'Group',
'Student' => 'Student',
'Term' => 'Term',
'Edit' => 'Edit',
'Delete' => 'Delete',
'Course' => 'Course',
'Enrollments' => 'Enrollments',
'Withdrawn' => 'Withdrawn',
'Room' => 'Room',
'Evaluations' => 'Evaluations',
'Records' => 'Records',
'Update' => 'Update',
'Updated' => 'Updated',
'Comment' => 'Comment',
);
my $self = 'evaldeled.pl';
use DBI;
use CGI;
eval require "../../etc/admin.conf";
if ( $@ ) {
print $lex{Error}. " $@ \n";
die $lex{Error}. " $@\n";
}
my $q = CGI->new;
print $q->header( -charset, $charset );
my %arr = $q->Vars;
my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;
# Page Header
my $title = "$lex{Edit}/$lex{Delete} $lex{Course} $lex{Evaluations}/$lex{Enrollments}";
print qq{$doctype\n
\n};
#foreach my $key ( keys %arr ) { print qq{K:$key V:$arr{$key} \n}; }
if ( not $arr{page} ) {
showStartPage();
} elsif ( $arr{page} == 1 ) {
delete $arr{page};
showStudents();
} elsif ( $arr{page} == 2 ) {
delete $arr{page};
deleteEdit(); # confirm deletion of records, and display edit forms
} elsif ( $arr{page} == 3 ) {
delete $arr{page};
updateEdit(); # update edit of records.
} elsif ( $arr{page} == 4 ) { # out of order, but will do delete, if confirmed.
delete $arr{page};
updateDelete(); # delete eval records.
}
#---------------
sub updateDelete {
#---------------
# delete records, after confirm in deleteEdit function (page 2). Pass through any edit recs.
print qq{
updateDelete - Page 4
\n};
foreach my $key ( sort keys %arr ) { print qq{K:$key V:$arr{$key} \n}; }
print qq{ \n\n};
my $subjsec = $arr{subjsec};
delete $arr{subjsec};
# Get Course Info
my $sth = $dbh->prepare("select * from subject where subjsec = ?");
$sth->execute( $subjsec);
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
my $cref = $sth->fetchrow_hashref;
my %c = %$cref; # course info now in %c hash.
if ( not $c{a1} ) { $c{a1} = 'Mark'; }
my $sth = $dbh->prepare("delete from eval where subjcode = ? and studnum = ? and term = ?");
my $sth1 = $dbh->prepare("select lastname, firstname from studentall where studnum = ?");
my $sth2 = $dbh->prepare("select * from eval where subjcode = ? and studnum = ? order by term");
my %name; # student names, colon separated.
# split values into delete or edit groups.
my (%deletesort, @edit);
foreach my $key ( keys %arr ) {
my ($mode, $studnum, $term) = split(':', $key );
if ( $mode eq 'edit' ) {
push @edit, $key;
next;
}
# Get Name, if not in name hash.
if ( not $name{$studnum} ) {
$sth1->execute( $studnum);
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
my ($lastname, $firstname) = $sth1->fetchrow;
$name{$studnum} = qq{$lastname:$firstname};
}
$deletesort{"$name{$studnum}$studnum$term"} = $key;
}
if (%deletesort) { # we have records to delete
print qq{
};
print qq{Delete Records for $c{description}
\n};
foreach my $key ( sort keys %deletesort ) {
my ($mode, $studnum, $term) = split(':', $deletesort{$key} );
my ($ln,$fn) = split(':', $name{$studnum});
$sth->execute( $subjsec, $studnum, $term);
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
print qq{
Deleted $fn $ln - Term $term
\n};
}
}
# Now do the form to go back to page 2, now without any deletes.
if ( @edit ) { # we have edits to do yet; link back to page 2
print qq{\n};
} else { # we're done. Provide links to do more.
print qq{\n};
print qq{\n};
}
print qq{\n};
exit;
} # end of updateDelete; go back to page 2 if still edits to do.
#-------------
sub updateEdit { # update passed records.
#-------------
# print qq{
\n};
my $subjsec = $arr{subjsec};
delete $arr{subjsec}; # leave for now.
# Get Course Info
my $sth = $dbh->prepare("select * from subject where subjsec = ?");
$sth->execute( $subjsec);
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
my $cref = $sth->fetchrow_hashref;
my %c = %$cref; # course info now in %c hash.
foreach my $key ( sort keys %arr ) {
my ($id, $field) = split(':', $key);
if ( not defined $arr{$key} ) { $arr{$key} = undef; } # make a NULL record.
# print qq{Field:$field ID:$id Value:$arr{$key} \n};
my $sth = $dbh->prepare("update eval set $field = ? where id = ?");
$sth->execute( $arr{$key}, $id);
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
}
print qq{