#!/usr/bin/perl
# Copyright 2001-2020 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',
'Grade' => 'Grade',
'Not Found' => 'Not Found',
'Error' => 'Error',
'Continue' => 'Continue',
'Month' => 'Month',
'Non-School Days' => 'Non-School Days',
'Set' => 'Set',
'Select' => 'Select',
'Part of Day' => 'Part of Day',
'Update' => 'Update',
'Updated' => 'Updated',
'Deleted' => 'Deleted',
'Record' => 'Record',
'Added' => 'Added',
'Home Room' => 'Home Room',
'Missing' => 'Missing',
'Value' => 'Value',
);
use DBI;
use CGI;
use CGI::Session;
use Time::JulianDay;
use Number::Format qw(:all);
use Cwd;
my $self = 'setNonSchoolDays.pl';
# Get current dir so know what path for config files.
my ($configpath, $teachermode );
if ( getcwd() =~ /tcgi/ ){ # we are in tcgi
$teachermode = 1;
$configpath = '..'; # go back one to get to etc.
} else {
$configpath = '../..'; # go back two to get to etc.
}
eval require "$configpath/etc/admin.conf.root";
if ( $@ ) {
print $lex{Error}. ": $@ \n";
die $lex{Error}. ": $@\n";
}
my $dbtype = 'mysql';
my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;
# Load Configuration Variables;
my $sth = $dbh->prepare("select id, datavalue from conf_system where filename = 'admin'");
$sth->execute;
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
while ( my ($id, $datavalue) = $sth->fetchrow ) {
eval $datavalue;
if ( $@ ) {
print "$lex{Error}: $@ \n";
die "$lex{Error}: $@\n";
}
}
# Teachermode
if ( $teachermode ) { # running on teacher site
$css = $tchcss;
$homepage = $tchpage;
# $downloaddir = $tchdownloaddir; # not needed in this script.
# $webdownloaddir = $tchwebdownloaddir;
}
my $q = new CGI;
print $q->header(-charset, $charset );
my %arr = $q->Vars;
my %periodmap = (1 => 'AM', 2 => 'PM');
my $title = "$lex{Set} PK/K Homerooms Closed";
print qq{$doctype\n
\n};
#foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key} \n"; }
# Values passed: page, date, and then any school databases;
if ( not $arr{page} ) {
showStartPage();
} elsif ( $arr{page} == 1 ) {
delete $arr{page};
selectDates();
} elsif ( $arr{page} == 2 ) {
delete $arr{page};
updateDates();
}
#--------------
sub updateDates {
#--------------
# foreach my $key ( keys %arr ) { print qq{K:$key V:$arr{$key} \n}; }
my $homeroom = $arr{homeroom};
delete $arr{homeroom};
my ($yr,$mo) = split('-', $arr{month});
if (length $mo == 1 ) { $mo = '0'. $mo; }
delete $arr{month};
print qq{
$lex{'Home Room'} $homeroom
\n};
# load any records for this month for this homeroom, to check for values turned off (and to be deleted)
my %dates;
my $sth = $dbh->prepare("select * from dates_homeroom
where month(date) = ? and year(date) = ? and homeroom = ?");
$sth->execute( $mo, $yr, $homeroom);
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
while ( my $ref = $sth->fetchrow_hashref ) { # these will only be for that grade.
my %r = %$ref;
$dates{$r{date}}{$r{period}} = $r{id};
}
# foreach my $date ( sort keys %dates) {
# foreach my $period ( sort keys %{$dates{$date}} ) {
# print qq{Date:$date Period:$period ID:$dates{$date}{$period} \n};
# }
# }
# check for any deselected date, period values that should be deleted.
my $sth = $dbh->prepare("delete from dates_homeroom where id = ?");
my %params;
foreach my $key ( keys %arr ) {
my ($date, $period) = split(':', $key);
$params{$date}{$period} = 1;
}
# go through current records for this month, this homeroom, and
# remove any extras not passed as parameters.
foreach my $date ( sort keys %dates ) {
foreach my $period ( sort keys %{ $dates{$date} } ) {
if ( not $params{$date}{$period} ) {
my $id = $dates{$date}{$period};
# print "ID:$id Date:$date Period:$period \n";
$sth->execute( $id );
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
print qq{