#!/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.
use DBI;
use CGI;
use Time::JulianDay;
my %lex = ('View' => 'View',
'Eoy' => 'Eoy',
'Main' => 'Main',
'Dates' => 'Dates',
'School Year' => 'School Year',
'School Start' => 'School Start',
'School End' => 'School End',
'Term' => 'Term',
'Date' => 'Date',
'Type' => 'Type',
'Description' => 'Description',
'Track' => 'Track',
'Error' => 'Error',
'Day' => 'Day',
'Grade' => 'Grade',
'Default' => 'Default',
'Fraction' => 'Fraction',
'Cycle' => 'Cycle',
'Day' => 'Day',
'No' => 'No',
'Yes' => 'Yes',
'Closed' => 'Closed',
'Open' => 'Open',
'Start' => 'Start',
'End' => 'End',
'Total' => 'Total',
);
my %dowclosed = ('0' => 1, '6' => 1); # Day 0 is Sunday, Day 6 is Saturday
my $self = 'dateView.pl';
# Read config variables
eval require "../../etc/admin.conf";
if ( $@ ) {
print $lex{Error}. " $@ \n";
die $lex{Error}. " $@\n";
}
my $q = new CGI;
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;
# populate jdclosed with dates from dates table; used by calcTermDays;
my %jdclosed;
my $sth = $dbh->prepare("select date, dayfraction from dates
where date is not NULL and date != ''");
$sth->execute;
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
while ( my ( $date, $dayfraction ) = $sth->fetchrow ) {
my $jd = julian_day( split( '-', $date ) );
$jdclosed{$jd} = $dayfraction;
}
my $title = "$lex{View} $lex{Dates}";
print qq{$doctype\n
\n};
my $val = calcTermDays($g_MTrackTerm{$trk}{$trm}{start},
$g_MTrackTerm{$trk}{$trm}{end}, \%jdclosed );
my ($open,$closed) = split(':', $val);
$totaldays += $open;
print qq{
$open
$closed
\n};
}
print qq{
$lex{Total}
};
print qq{
$totaldays
\n};
print qq{
\n};
}
# Print the Stored Dates (in Dates Table)
print qq{
\n};
print qq{
$lex{Dates} $lex{Closed}
\n};
print qq{
$lex{Date}
$lex{Day}
$lex{Type}
\n};
print qq{
$lex{Description}
$lex{Cycle} $lex{Day}
};
print qq{
$lex{Fraction} Closed
Periods Closed
\n};
my $sth = $dbh->prepare("select * from dates order by date");
$sth->execute;
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
my $sth1 = $dbh->prepare("select grades, period from dates_periods
where date = ? order by grades, period");
while ( my ( $id, $date, $type, $desc1, $desc2, $dayincycle, $dayfraction ) = $sth->fetchrow ) {
my ($year, $month, $day ) = split('-', $date);
my $newdate = $year .'-'. $s_month[$month]. q{-}. $day;
my $jd = julian_day( split( '-', $date ) );
my $dow = day_of_week($jd);
# add periods closed for this date
my %periods;
$sth1->execute($date);
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
while ( my ($grades,$period) = $sth1->fetchrow ) {
push @{ $periods{$grades} }, $period;
}
if ( not $dayincycle ) {
$dayincycle = $lex{No};
} else {
$dayincycle = $lex{Yes};
}
if ($dayfraction eq '1.000' ) {
$dayfraction = 'All Day';
} else {
$dayfraction = 'Partial';
}
print qq{
\n};
# end of left div here, start right div
#print qq{
\n};
# now print each of the calendar months in a table.
my @tmp = split('-', $schoolstart);
my $startyear = $tmp[0];
my $count;
foreach my $mo ( $tmp[1]..12 ) {
showMonthCalendar( $startyear, $mo);
$count++;
if ( $count % 2 == 0 ) { print qq{\n \n}; }
}
print qq{ \n};
my $nextyear = $startyear + 1;
foreach my $mo ( 1..6 ) {
showMonthCalendar( $nextyear, $mo);
if ( $mo % 2 == 0 ) { print qq{\n \n}; }
}
print qq{\n};
#---------------
sub calcTermDays {
#---------------
# passed a start date and an end date for a term, and a ref to a
# hash storing the closure dates in julian day format, it will
# return a count of the days open in the month.
# outline: convert start/end dates to jd. Loop over all days. Use modulus function to ident weekend days.
my ($startdate, $enddate, $closeref ) = @_;
my %jdclosed = %$closeref;
=head
print qq{JDClosed \n";
foreach my $key ( sort keys %jdclosed ) {
print qq{K:$key V:$jdclosed{$key} \n";
}
print qq{End JDClosed \n";
=cut
my $startjd = julian_day(split('-',$startdate));
my $endjd = julian_day(split('-',$enddate));
my %modclosed; # modulus for days closed
foreach my $inc ( 0..6 ) {
my $testjd = $startjd + $inc;
my $dow = day_of_week($testjd);
if ( $dowclosed{$dow} ) { # $testjd is a closed dow
my $mod = $testjd % 7;
$modclosed{$mod} = 1;
}
}
# foreach my $d ( sort keys %modclosed ) {
# print qq{Closed Mod: $d \n";
# }
my ($daysopen, $daysclosed);
foreach my $jd ( $startjd..$endjd ) {
my $dow = day_of_week( $jd );
my $mod = $jd % 7;
if ( $modclosed{$mod} ) { # closed for weekend
next;
}
# check if a day closed
my $cl = $jdclosed{ $jd };
if ( $cl ) { # this gives us the fraction closed
$daysclosed += $cl;
# only add on the fraction of day open
$daysopen += (1 - $cl );
} else {
$daysopen += 1;
}
}
# print qq{ST:$startdate END:$enddate Count:$daycount \n";
return "$daysopen:$daysclosed";
}
#--------------------
sub showMonthCalendar {
#--------------------
# passed year-month value
my ($yr,$mo) = @_;
if ( length $mo == 1 ) { $mo = '0'. $mo; }
# print "Y:$yr M:$mo \n";
# Find dow for first of the month
my $startdate = "$yr-$mo-01";
my $startjd = julian_day( split('-', $startdate) );
my $startdow = day_of_week($startjd);
# Find last day of the month (eomday)
my $nextmonth = $mo + 1;
my $tempyear = $yr;
if ( $nextmonth == 1 ) { $tempyear++; }
my $eomjd = julian_day($tempyear,$nextmonth,'1');
my @temp = inverse_julian_day( $eomjd - 1 );
my $eomday = $temp[2];
# Set number of rows in the calendar
my $rowcount = 5; # 5 rows in the calendar.
my $totalcells = $eomday + $startdow; # total cells required in calendar.
if ( $totalcells > 36 ) { $rowcount = 6; } # to accomodate all the dates in the month.
# Add Style settings for calendar
print qq{\n};
# Get the days closed (>0.999) in the month.
my %dayclosed;
my $sth = $dbh->prepare("select * from dates where month(date) = ? and year(date) = ?");
$sth->execute( $mo, $yr );
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
while ( my $ref = $sth->fetchrow_hashref ) {
my $date = $ref->{date};
if ( exists $dayclosed{$date} ) { # Error;
print qq{