#!/usr/bin/perl -T
###########################
#
# MarlboroClassSchedule.cgi
# is a web .cgi script to display an editable
# Marlboro College class schedule.
# The documentation is at the end of this file in POD format.
#
# $Id: MarlboroClassSchedule.cgi,v 1.3 2002/04/12 09:19:20 msie Exp $
###########################
package MarlboroClassSchedule;
# -== initialization ============================================-
# I'd rather define globals with the "our" keyword,
# but on akbar we're still stuck at perl 5.005 ...
use vars qw( $VERSION $cgi
$title1 $title2 $contact
$datafilename $lastmodified $blockdata
$schedulehtml
);
$VERSION = "0.1";
use CGI;
use CGI::Carp qw(fatalsToBrowser);
# -== main ======================================================-
defineGlobals();
readData(); # Define $blockdata from the disk file $datafilename .
# If the user has clicked "Save Changes",
if ($cgi->param('saveChanges')) {
my $whichBlock = $cgi->param('whichBlock');
my $text = $cgi->param('text');
$blockdata->{ $whichBlock } = $text;
writeData();
}
# When accessed through the internet, via apache .cgi, caller() is false.
# Under mod_perl, caller() is 'Apache::Registry'.
# If we're invoked either of these ways, output a web page.
# (Otherwise, another file such as test.pl is executing this file.)
if ( not caller() or caller() eq 'Apache::Registry' ) {
print $cgi->header();
if (my $whichBlock = $cgi->param('edit')) {
print htmlEdit($whichBlock);
}
else {
print htmlSchedule();
}
}
# -== define various global parameters ==========================-
sub defineGlobals {
$cgi = new CGI;
$title1 = 'Marlboro College - Fall 2002 Class Schedule';
$title2 = 'Marlboro College
Fall 2002 Class Schedule';
$contact =
'Jim Mahoney (mahoney@marlboro.edu)';
# I don't think this is really necessary - I put it in to have short
# keys for the hash which stores the text for each time block, but it
# would probably be cleaner to just uses the longer, full names everywhere.
%timeblocks = (
mon830 => "Monday 8:30am",
wed830 => "Wednesday 8:30am",
fri830 => "Friday 8:30am",
mon930 => "Monday 9:30am",
wed930 => "Wednesday 9:30am",
fri930 => "Friday 9:30am",
mon1030 => "Monday 10:30am",
wed1030 => "Wednesday 10:30am",
fri1030 => "Friday 10:30am",
tues830 => "Tuesday 8:30am",
thurs830 => "Thursday 8:30am",
tues1000 => "Tuesday 10:00am",
thurs1000 => "Thursday 10:00am",
mon1130 => "Monday 11:30am",
tues1130 => "Tuesday 11:30am",
wed1130 => "Wednesday 11:30am",
thurs1130 => "Thursday 11:30am",
fri1130 => "Friday 11:30am",
mon130 => "Monday 1:30pm",
tues130 => "Tuesday 1:30pm",
thurs130 => "Thursday 1:30pm",
fri130 => "Friday 1:30pm",
mon330 => "Monday 3:30pm",
tues330 => "Tuesday 3:30pm",
thurs330 => "Thursday 3:30pm",
fri330 => "Friday 3:30pm",
tues630 => "Tuesday 6:30pm",
wed630 => "Wednesday 6:30pm",
thurs630 => "Thursday 6:30pm",
fri630 => "Friday 6:30pm",
);
# The web server must have read/write access to this file.
$datafilename = "datafile.txt";
}
# -== data persistence routines ==========================================-
# Storable reports errors by throwing exceptions which are caught with eval{}.
# See http://theoryx5.uwinnipeg.ca/CPAN/data/Storable/Storable.html
use Storable;
# Since this routine creates the file $datafilename
# and sets its privileges, whoever runs it must have
# write access to the directory where $datafilename lives.
sub initData {
$blockdata = {};
unlink $datafilename;
writeData();
chmod 0666, $datafilename;
}
sub readData {
initData() unless -e $datafilename;
$lastmodified = -M $datafilename;
$lastmodified = scalar localtime( time() - 24*3600*$lastmodified );
$lastmodified =~ s/\d+:\d+:\d+ //;
eval { $blockdata = Storable::lock_retrieve( $datafilename ) };
if ($@) { die "couldn't read data : $@"; }
}
sub writeData {
# nstore is "network store" - uses file format compatible across platforms
eval { Storable::lock_nstore( $blockdata, $datafilename ) };
if ($@) { die "couldn't write data : $@"; }
}
# -== html string for edit page ==================================-
sub htmlEdit {
my ($whichBlock) = @_;
return <<"END_EDIT_HTML";
|
Editing $timeblocks{$whichBlock}
|
|