#!/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 = ('Daybook' => 'Daybook',
'Main' => 'Main',
'Today is a non-cycle Day' => 'Today is a non-cycle Day',
'Day' => 'Day',
'Term' => 'Term',
'Homework Due' => 'Homework Due',
'Desc' => 'Desc',
'Update' => 'Update',
'Please Log In' => 'Please Log In',
'User Id' => 'User Id',
'Password' => 'Password',
'Not Found' => 'Not Found',
'Please check configuration file' => 'Please check configuration file',
'Error' => 'Error',
'Topic' => 'Topic',
'Category' => 'Category',
'Period' => 'Period',
'Time' => 'Time',
'Track' => 'Track',
'Term' => 'Term',
'Previous Week' => 'Previous Week',
'Next Week' => 'Next Week',
'Select' => 'Select',
'Date' => 'Date',
'Go' => 'Go',
'Cycle' => 'Cycle',
'Continue' => 'Continue',
);
my $self = 'crsplanner.pl';
use DBI;
use CGI;
use CGI::Session;
use Time::JulianDay;
my $q = CGI->new;
my %arr = $q->Vars; # Get passed values
eval require "../../etc/admin.conf";
if ( $@ ) {
print $lex{Error}. ": $@ \n";
die $lex{Error}. ": $@\n";
}
eval require "../../lib/libschedule.pl";
if ( $@ ) {
print $lex{Error}. ": $@ \n";
die $lex{Error}. ": $@\n";
}
eval require "../../lib/libsession.pl";
if ( $@ ) {
print $lex{Error}. ": $@ \n";
die $lex{Error}. ": $@\n";
}
my $dsn = "DBI:mysql:dbname=$dbase";
$dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;
# Set limits for school year: startjd and endjd
my $schoolstart = $g_MTrackTerm{1}{1}{'start'}; #override global
my $startjd = julian_day( split('-',$schoolstart));
my $endjd = julian_day( split('-', $schoolend));
# Current Date
my @tim = localtime(time);
my $year = $tim[5] + 1900;
my $month = $tim[4] + 1;
my $day = $tim[3];
if (length($month) == 1){ $month = "0".$month;}
if (length($day) == 1){ $day = "0".$day;}
my $currdate = "$year-$month-$day";
=head
# watch for a xxxx-xx-xx key and use for date...
foreach my $key (keys %arr) {
if ($key =~ m/\d+-\d+-\d/){ # if it contains a hyphen...
$arr{date} = $key;
delete $arr{$key};
last;
}
}
=cut
# Get Session
my $session = new CGI::Session("driver:mysql;serializer:FreezeThaw",
undef,{Handle => $dbh}) or die CGI::Session->errstr;
# Get/Set Session Values (a defined userid means it was passed)
if ( $arr{userid} ){ # we want to login, passed userid/password pair.
# Check password/userid against database (-1 no user, -2 wrong password);
my $error = checkPassword($arr{userid}, $arr{password}, $dbh);
if ($error == -1){
print $q->header( -charset, $charset );
login("$lex{'User Id'} $lex{'Not Found'}", $arr{userid});
} elsif ( $error == -2 ) {
print $q->header( -charset, $charset );
login("$lex{Password} $lex{'Not Found'}");
}
$cookietime = checkCookieTime( $arr{duration} );
# Set values for userid and logged_in in session
$session->param('logged_in','1');
$session->expire('logged_in', $cookietime);
$session->param('userid', $arr{userid});
$session->param('duration', $cookietime );
$userid = $arr{userid};
} else { # check logged_in value in session
if ( not $session->param('logged_in') ){
$userid = $session->param('userid');
print $q->header( -charset, $charset );
login( $lex{'Please Log In'}, $userid );
}
# Ok, we have a login. Values below we have in environment.
$userid = $session->param('userid');
$duration = $session->param('duration');
if ( not ( $duration =~ m/\+/)) { # add + and m if not present
$duration = checkCookieTime( $duration );
}
$session->expire('logged_in',$duration);
} # End of check for logged_in value
print $session->header( -charset, $charset );
# Heading of Document
my $title = "Course / Activity Planner";
print qq{$doctype\n
$title\n};
print qq{\n};
# Calendar popup.
print qq{\n};
print qq{\n};
print qq{\n};
print qq{\n};
print qq{$chartype\n};
print qq{\n};
if ( not $arr{page} ) {
showStartPage(); # select course/activity, going forward.
} elsif ( $arr{page} == 1 ) {
delete $arr{page};
planCourse();
}
#-------------
sub planCourse {
#-------------
foreach my $key ( sort keys %arr ) { print qq{K:$key V:$arr{$key} \n}; }
# Passed: crsact (either subjsec or ACT:id), periods, startdate
my $periods = $arr{periods};
if ( not $periods ) { $periods = 10; }
my ( $subjsec, $actid );
my ( $act, $actid ) = split(':', $arr{crsact});
if ( not $actid ) { $subjsec = $arr{crsact}; }
# either $actid or $subjsec is defined.
my %crsact;
if ( $subjsec ) {
my $sth = $dbh->prepare("select description from subject where subjsec = ?");
$sth->execute( $subjsec );
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
$title = $sth->fetchrow; # use common 'title' value
$crsact{title} = $title;
} elsif ( $actid ) {
my $sth = $dbh->prepare("select description from subject where subjsec = ?");
$sth->execute( $subjsec );
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
my $ref = $sth->fetchrow_hashref; # use common 'title' value
%$crsact = %$ref;
}
my $sth3 = $dbh->prepare("select distinct topic from dbkdata
where subjsec = ? and topic != '' order by date desc");
my $sth4 = $dbh->prepare("select distinct category from dbkdata
where subjsec = ? and category != '' order by category");
my $sth5 = $dbh->prepare("select * from dbkdata
where userid = ? and date = ? and period = ?");
# Activity values
my $sth6 = $dbh->prepare("select * from dbkactivity where id = ?");
# We need to do this since there may be missing values in the keys (periods) not scheduled. (prep)
my $maxperiods;
foreach my $per ( keys %courseact ) {
if ( $per > $maxperiods ) { $maxperiods = $per; }
}
my ( %course, %activity );
my $bgcolor;
# Loop through each course/activity
for my $period ( 1 .. $periods ){
if ( $bgcolor eq '#CCC' ) {
$bgcolor = '#CCF';
} else {
$bgcolor = '#CCC';
}
print qq{
\n};
my $first = 1;
foreach my $crsact ( keys %{ $courseact{$period} } ) {
if ( $first ) { $first = 0; } # turn off inside course loop
# Course Type (activity,course)
my ($ctype,$title, $description );
my ($id, $subjsec);
my ($course,$section) = split('-', $crsact);
if ($course eq 'ACTIV') { # we have an activity
$id = $section;
}
$subjsec = $crsact;
if ( $id ) { # get activity record;
$sth6->execute($id);
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
my $ref = $sth6->fetchrow_hashref;
$activity{$id} = $ref;
$ctype = 'Activity';
$title = $ref->{title};
$description = $ref->{description};
} else { # Get Subject Description
$sth2->execute( $subjsec );
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
my $desc = $sth2->fetchrow;
$course{$subjsec} = $desc;
$ctype = 'Course';
$title = $desc;
# Description is blank, since no values.
}
# Get previous topics for this course/activity
my @topics;
$sth3->execute( $crsact );
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
while ( my $topic = $sth3->fetchrow ) {
push @topics,$topic;
}
# Get previous categories
my @categories;
$sth4->execute( $crsact );
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr;}
while ( my $cat = $sth4->fetchrow ) {
push @categories, $cat;
}
# Get Previous Period (not in current day...)
my $pn; # previous note
$sth1 = $dbh->prepare("select notes, date, period from dbkdata
where subjsec = ? and to_days(date) < to_days('$currdate')
order by date desc, period desc");
$sth1->execute( $subjsec );
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
my $blankCounter = 0;
my $previousPeriod;
NOTE:
while ( my ($prevnote, $prevdate, $prevperiod) = $sth1->fetchrow) {
if ( not $prevnote ) {
$blankCounter++;
if ($blankCounter > $maxBlankCounter) { last; }
next NOTE;
}
# Get dow for prevdate;
my ($year,$month,$day) = split '-',$prevdate;
my $jd = julian_day($year,$month,$day);
my $dow = day_of_week($jd) + 1;
my $prevdow = $s_dow[$dow]; # @sdow (short days of week) defined in admin.conf
my $prevsdate = "$month-$day";
$previousPeriod = "$prevdow, $prevsdate: $prevnote\n";
last;
}
# get current data, access by userid, date, period, crsact
my %currdata;
my $sth1 = $dbh->prepare("select * from dbkdata
where userid = ? and period = ? and date = ? and subjsec = ?");
$sth1->execute( $userid, $period, $currdate, $crsact );
if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
# removed the while loop
my $ref = $sth1->fetchrow_hashref;
my $topic = $ref->{topic};
my $notes = $ref->{notes};
my $homework = $ref->{homework};
my $hwdate = $ref->{hwdate};
my $category = $ref->{category};
# Display the current subject and period.
print qq{
};
# Period and Time.
print qq{$lex{Period} $period };
print qq{$lex{Time}: $ptime{$period}{starttime}-$ptime{$period}{endtime}\n};
if ( not $title ) { $title = $lex{'Not Assigned'}; }
print qq{ $title ($crsact)
\n};
}
# Note Section
print qq{\n};
} # end of course/activity loop for this period
if ( $first ) { # no course assigned.
# Display the current subject and period.
print qq{
};
# Period and Time.
print qq{$lex{Period} $period };
print qq{$lex{Time}: $ptime{$period}{starttime}-$ptime{$period}{endtime}\n};
if ( not $title ) { $title = $lex{'Not Assigned'}; }
print qq{ $title
\n};
}
print qq{
\n};
} # end of periods loop
print qq{\n};
exit;
}
#----------------
sub showStartPage {
#----------------
# Get the courses and activities of this teacher (all terms.... let them choose a term as well)
# Get courses and their terms.
my (%course, @course);
my $sth = $dbh->prepare("select * from subject where teacher = ? order by grade, description");
$sth->execute( $userid );
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
while ( $ref = $sth->fetchrow_hashref ) {
my %r = %$ref;
push @course, $r{subjsec};
$course{$r{subjsec}} = {'description' => $r{description}, startterm => $r{startrptperiod},
endterm => $r{endrptperiod}, grade => $r{grade} };
}
# Get Activity (part of courses).
my %activity;
my $sth = $dbh->prepare("select * from dbkactivity where userid = ?");
$sth->execute( $userid );
if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
while ( $ref = $sth->fetchrow_hashref ) {
my %r = %$ref;
my $coursedesc;
if ( $r{subjsec} ) {
$coursedesc = $course{$r{subjsec}}{description};
} else {
$coursedesc = $r{acttype};
}
$activity{$r{id}} = {'description' => $r{description}, startterm => $r{startterm},
endterm => $r{endterm}, coursedesc => $coursedesc };
}
print qq{
Please select a Course or Activity to Plan
\n\n};
# Form Start
print qq{\n};
print qq{\n};
print qq{