#!/usr/bin/perl
# Copyright 2001-2022 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 = ('Main' => 'Main',
'Timetable' => 'Timetable',
'Edit' => 'Edit',
'Terms' => 'Terms',
'Update' => 'Update',
'Term' => 'Term',
'Day' => 'Day',
'Per' => 'Per',
'Subject-Section' => 'Subject-Section',
'Period' => 'Period',
'Record' => 'Record',
'Backings' => 'Backings',
'Add' => 'Add',
'Additional' => 'Additional',
'Adding' => 'Adding',
'Missing' => 'Missing',
'Continue' => 'Continue',
'Delete' => 'Delete',
'Contact' => 'Contact',
'Separate With Spaces' => 'Separate With Spaces',
'Error' => 'Error',
'Record(s) Stored' => 'Record(s) Stored',
'Staff' => 'Staff',
'Not Found' => 'Not Found',
'Configuration' => 'Configuration',
'Record Exists' => 'Record Exists',
'Course' => 'Course',
'Skipping' => 'Skipping',
);
# max number of backed classes per cycle.
#(control display and entry)
my $maxbackings = 12;
use DBI;
use CGI;
my $self = "ttEdit.pl";
my @time = localtime(time);
my $year = $time[5] + 1900;
my $month = $time[4] + 1;
my $currdate = "$year-$month-$time[3]";
# Set prepath for config file:
my $prepath = '../..';
eval require "$prepath/etc/admin.conf";
if ( $@ ) {
print $lex{Error}. ": $@ \n";
die $lex{Error}. ": $@\n";
}
my $q = new CGI;
my %arr = $q->Vars;
my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;
print $q->header( -charset, $charset );
# print page header.
my $title = "$lex{Edit} $lex{Timetable}";
print qq{$doctype\n
\n};
if ( not $arr{page} ) {
showStartPage();
} elsif ( $arr{page} == 1 ) {
delete $arr{page};
setTimetable();
} elsif ( $arr{page} == 2 ) {
delete $arr{page};
updateRecords();
} elsif ( $arr{page} == 3 ) {
delete $arr{page};
printBackingsForm(); # $arr{userid}, $arr{terms} );
} elsif ( $arr{page} == 4 ) {
delete $arr{page};
addBackings();
} elsif ( $arr{page} == 5 ) {
delete $arr{page};
deleteErrors();
}
#----------------
sub deleteErrors {
#----------------
# foreach my $key ( sort keys %arr ) { print qq{K:$key V:$arr{$key} \n}; }
my ($userid, $currterm) = ( $arr{userid}, $arr{currterm});
delete $arr{userid};
delete $arr{currterm};
my $sth1 = $dbh->prepare("select * from schedat where id = ?");
my $sth = $dbh->prepare("delete from schedat where id = ?");
foreach my $id ( sort keys %arr ) {
# Load the record, then delete
$sth1->execute($id);
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr;}
my $ref = $sth1->fetchrow_hashref;
my %r = %$ref;
$sth->execute($id);
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr;}
print qq{
Error $r{subjsec} / Term $r{term} Deleted
\n};
}
print qq{\n};
print qq{\n};
exit;
}
#----------------
sub showStartPage {
#----------------
# Load terms from schedat;
my @terms;
my $sth = $dbh->prepare("select distinct term from schedat
where term != '' order by term");
$sth->execute;
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr;}
while ( my $trm = $sth->fetchrow ){ push @terms, $trm; }
# recover terms from subject if not any terms in schedat
if ( not @terms ) {
$sth = $dbh->prepare("select min(startrptperiod) from subject");
$sth->execute;
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr;}
my $startterm = $sth->fetchrow;
$sth = $dbh->prepare("select max(endrptperiod) from subject");
$sth->execute;
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr;}
my $endterm = $sth->fetchrow;
foreach my $trm ( $startterm .. $endterm ) {
push @terms, $trm;
}
}
# Load Courses
my %courses; # courses{subjsec} = teacher (ie. userid)
$sth = $dbh->prepare("select * from subject");
$sth->execute;
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
while ( my $ref = $sth->fetchrow_hashref ) {
my %r = %$ref;
$courses{$r{subjsec}} = $r{teacher};
}
# Load Timetable
my %teachers;
$sth = $dbh->prepare("select distinct subjsec from schedat");
$sth->execute;
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
while ( my $subjsec = $sth->fetchrow ) {
my $userid = $courses{$subjsec};
if ( not $userid ) { next; } # skip any blanks
$teachers{$userid} = 1;
}
if ( not %teachers ) { # no timetable entries
print qq{