#! /usr/local/bin/perl -w use strict; use CGI; ############################## # # Written by Joshua Cheung, last updated: 2/7/05 # ############################## ############################## #### DECLARE GLOBALS HERE #### #---------------------------- my %G = ( ####################### #!! HTML PROPERTIES !!# ####################### ## Main Body Attributes "BodyBGColor" => '#FFFFFF', ## Horizontal Rule (the stripe) Attributes "HtmlHrColor" => '#C96161', "HtmlHrSize" => '2', "HtmlHrWidth" => '95%', ### This can be a percent (with % at the end) or pixel ## Instruction Attributes "InstructionFontColor" => '#000000', "InstructionFontType" => 'arial, helvetica, sans-serif', "InstructionFontSize" => '12px', "InstructionBackgroundColor" => '#D9C2BC', "InstructionCellHeight" => '100', # Leave blank to stretch with window "InstructionCellWidth" => '150', # Leave blank to stretch with window ## Regular Text Attributes "RegularFontColor" => '#000000', "RegularFontType" => 'arial, helvetica, sans-serif', "RegularFontSize" => '16px', ## Link & Steps Link Attributes "LinkFontColor" => 'blue', "LinkFontType" => 'arial, helvetica, sans-serif', "LinkFontSize" => '14px', "LinkDecoration" => 'none', ### eg. underline, overline, blink, none "LinkMouseOverColor" => 'blue', "LinkMouseOverDecoration" => 'underline', ### eg. underline, overline, blink, none "StepsLinkFontSize" => '12px', "StepsLinkBackgroundColor"=> '#D9C2BC', "StepsCellHeight" => '', # Leave blank to stretch with window "StepsCellWidth" => '', # Leave blank to stretch with window ## Main Title "TitleFontColor" => 'black', ## Sub Title "SubTitleFontColor" => 'black', ########################### #!! END HTML PROPERTIES !!# #!! START OTHER GLOBALS !!# ########################### ### DEBUG, 1 debug mode on, 0 debug mode off "debug" => 0, ### HTTP ADDRESS OF THIS SCRIPT "myHomepage" => 'http://math.ucsd.edu/~ashenk/', ### HTTP ADDRESS OF THIS SCRIPT "myAddress" => 'http://math.ucsd.edu/~ashenk/cgi-bin/tutorials.cgi', ### E-Calculus Page "eCalculusAddress" => 'http://www.math.ucsd.edu/~ashenk/e-calculus/', ### LOCATION OF TUTORIALS DIRECTORY RELEVANT TO THIS SCRIPT "TutorialsDir" => '../Tutorials', ### LOCATION OF CHAPTER & SECTION TITLES FILE RELEVANT TO THIS SCRIPT "TitlesFile" => '../Tutorials/Ch_&_Sec_Titles', ### HTTP ADDRESS OF TUTORIALS DIRECTORY "HttpTutorialsAddress" => 'http://math.ucsd.edu/~ashenk/Tutorials', ### Chapters directory Regular Expression ### ($1) is the chapter number "ChapterRegEx" => '^Ch([0-9]+)$', ### Sections directory Regular Expression ### ($1) is the chapter number, ### ($2) is the section number "SectionRegEx" => '^Sec([0-9]+)_([0-9]+)$', ### Problems directory Regular Expression ### ($1) is the chapter number, ### ($2) is the section number, ### ($3)-($5) are each characters of the problem number "ProblemRegEx" => '^[PB]([0-9]+)_([0-9]+)_([a-zA-Z]?)([0-9]{1,3})([a-zA-Z]?)$', ### Step (eg. R1, S2, A, etc.) Regular Expression ### ($1) is the type of step, either step, result, or answer, ### ($2) is the step/result number "StepRegEx" => '^([rRsSaA])([aA0-9]*)$', ### Problem Image filename Regular Expression ### ($1) is the chapter number, ### ($2) is the section number, ### ($3) is the problem number "PImageRegEx" => '^[PB]([0-9]+)_([0-9]+)_([a-zA-Z0-9]+)_PP.(gif|png)$', ### Step Image filename Regular Expression ### ($1) is the chapter number, ### ($2) is the section number, ### ($3) is the problem number, ### ($4) is the step, result or answer, "SImageRegEx" => '^[PB]([0-9]+)_([0-9]+)_([a-zA-Z0-9]+)_([rRsSaA][aA0-9]*).(gif|png)$' ); #---------------------------- ############################## ############################## ###### OTHER VARIABLES ####### #---------------------------- my ($myAction, $gChapter, $gSection, $gProblem, $gStep); my $q = new CGI; my %IntToWord = ( "0" => "Zero", "1" => "One", "2" => "Two", "3" => "Three", "4" => "Four", "5" => "Five", "6" => "Six", "7" => "Seven", "8" => "Eight", "9" => "Nine", "10" => "Ten", "11" => "Eleven", "12" => "Twelve", "13" => "Thirteen", "14" => "Fourteen", "15" => "Fifteen", "16" => "Sixteen", "17" => "Seventeen", "18" => "Eighteen", "19" => "Nineteen", "20" => "Twenty", "21" => "Twentyone", "22" => "Twentytwo", "23" => "Twentythree", "24" => "Twentyfour", "25" => "Twentyfive", "26" => "Twentysix", "27" => "Twentyseven", "28" => "Twentyeight", "29" => "Twentynine", "30" => "Thirty" ); $myAction = $q->param('action'); if ($q->param('chapter')) { $gChapter = $q->param('chapter'); } if ($q->param('section')) { $gSection = $q->param('section'); } if ($q->param('problem')) { $gProblem = $q->param('problem'); } if ($q->param('step')) { $gStep = $q->param('step'); } #---------------------------- ############################## ############################## ####### ERROR CHECKING ####### # Checking fields that were passed in #---------------------------- if (!($myAction)) { $myAction = ""; } if (($gChapter) && !($gChapter =~ /$G{"ChapterRegEx"}/)) { $myAction = ""; } if (($gSection) && !($gSection =~ /$G{"SectionRegEx"}/)) { $myAction = ""; } if (($gProblem) && !($gProblem =~ /$G{"ProblemRegEx"}/)) { $myAction = ""; } if (($gStep) && !($gStep =~ /$G{"StepRegEx"}/)) { $myAction = ""; } #---------------------------- ############################### ############################## ########### HEADER ########### #---------------------------- print "Content-type: text/html\n\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n\n"; print "\n"; #---------------------------- ############################## ############################## ###### ACTION BRANCHES ####### #---------------------------- if ($myAction =~ /^chapter$/) { if ($G{"debug"}) { print "!chapter area!
\n"; } printSections($gChapter); } elsif ($myAction =~ /^section$/) { if ($G{"debug"}) { print "!section area!
\n"; } printProblems($gChapter, $gSection); } elsif ($myAction =~ /^problem$/) { if ($G{"debug"}) { print "!problem area!
\n"; } printProblemAndSteps($gChapter, $gSection, $gProblem, $gStep); } else { if ($G{"debug"}) { print "!default area!
\n"; } printChapters(); } #---------------------------- ############################## ############################## ########### FOOTER ########### #---------------------------- print "
\n"; print "\n"; if ($myAction =~ /^author$/) {print "  Script Written by Joshua Cheung, © 2003";} print "\n"; print "
\n"; print "\n\n"; #---------------------------- ############################## ################################### #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!# #!!!!!!!!!! SUBROUTINES !!!!!!!!!!# #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!# ################################### ############################## ####### printChapters ######## ############################## sub printChapters { my (@chapters, $chapter, $chapter_num, $dh1, $dh2, $entry, $chapter_dir, @sections, $section, $section_num, $count, $dh3, $section_dir, $title); if (!(opendir($dh1, $G{"TutorialsDir"}))) { if ($G{"debug"}) { print "opendir failed for tutorials directory! \"".$G{"TutorialsDir"}."\"
\n"; } } else { while ($entry = readdir($dh1)) { if ($entry =~ /$G{"ChapterRegEx"}/) { $chapters[$1] = $entry; } } #-----------------# #--- HTML HERE ---# #-----------------# print "
\n"; print "
\n"; print "

e-CALCULUS
by Al Shenk

\n"; print "
\n"; print "

Interactive Examples

\n"; print "
\n"; print " \n"; print " \n"; print " \n"; print " \n"; print "
\n"; #-----------------# foreach $chapter (@chapters) { if (!($chapter =~ /^$/)) { @sections = (); eval($chapter =~ /$G{"ChapterRegEx"}/); $chapter_num = $1; $chapter_dir = $G{"TutorialsDir"}."/".$chapter; if (!(opendir($dh2, $chapter_dir))) { if ($G{"debug"}) { print "opendir failed for chapter directory!
".$chapter_dir."\n"; } } else { while ($entry = readdir($dh2)) { if ($entry =~ /$G{"SectionRegEx"}/) { $sections[$2] = $entry; } } if (@sections > 0) { $title = getTitle($chapter); #-----------------# #--- HTML HERE ---# #-----------------# print " Chapter ". $chapter_num." "; if ($chapter_num < 10) { print "   "; } else { print " "; } print " ".$title."
\n"; #-----------------# } } closedir($dh2); } } #-----------------# #--- HTML HERE ---# #-----------------# print "
\n"; print "
\n"; print "
\n"; #-----------------# } closedir($dh1); } ############################## ####### printSections ######## ############################## sub printSections { my ($chapter) = @_; my ($chapter_num, $dh1, $dh2, $entry, $chapter_dir, @sections, $section, $section_num, $count, $section_dir, $title); @sections = (); eval($chapter =~ /$G{"ChapterRegEx"}/); $chapter_num = $1; $chapter_dir = $G{"TutorialsDir"}."/".$chapter; if (!(opendir($dh1, $chapter_dir))) { if ($G{"debug"}) { print "opendir failed for chapter directory!
".$chapter_dir."\n"; } } else { while ($entry = readdir($dh1)) { if ($entry =~ /$G{"SectionRegEx"}/) { $sections[$2] = $entry; } } if (@sections > 0) { $title = getTitle($chapter); #-----------------# #--- HTML HERE ---# #-----------------# print "
\n"; print "
\n"; print "

Interactive Examples

\n"; print "
\n"; print "

Chapter ".$chapter_num.". ".$title."


\n"; print " Choose another chapter\n"; print "| Al Shenk's Homepage\n"; print "
\n"; print " \n"; #-----------------# foreach $section (@sections) { if (defined($section) && (!($section =~ /^$/))) { $section_dir = $G{"TutorialsDir"}."/". $chapter."/".$section; if (!(opendir($dh2, $section_dir))) { if ($G{"debug"}) { print "opendir failed for section directory! ".$section_dir."
\n"; } } else { $count = 0; while ($entry = readdir($dh2)) { if ($entry =~ /$G{"ProblemRegEx"}/) { $count++; } } if ($count > 0) { eval ($section =~ /$G{"SectionRegEx"}/); $section_num = $2; $title = getTitle($section); #-----------------# #--- HTML HERE ---# #-----------------# print " \n"; print " \n"; print " \n"; #-----------------# } } closedir($dh2); } } #-----------------# #--- HTML HERE ---# #-----------------# print "
\n"; print " Section ".$chapter_num.".".$section_num. " "; if ($section_num < 10) { print "   "; } else { print " "; } print " ".$title."
\n"; print "
\n"; print "
\n"; print "
\n"; #-----------------# } } closedir($dh1); } ############################## ####### printProblems ######## ############################## sub printProblems { my ($chapter, $section) = @_; my (%problems, $problem, $section_num, $chapter_num, $problem_num, $dh1, $dh2, $entry, $ch_sec_dir, $problem_gif_dir, $chp_title, $sec_title, $problem_title, $sec_num, $problem_gif_file, $problem_png_file, $temp_prob, $temp_prob2, $temp_prob3); $ch_sec_dir = $G{"TutorialsDir"}."/".$chapter."/".$section; if (!(opendir($dh1, $ch_sec_dir))) { if ($G{"debug"}) { print "opendir failed for section directory!
\n"; } } else { while ($entry = readdir($dh1)) { if ($entry =~ /$G{"ProblemRegEx"}/) { my $first = "$3"; my $second = sprintf("%0*d", 3, $4); my $third = "$5"; if ($first =~ /^[a-zA-Z]$/) { $first = "0"; } else { $first = "1"; } if (!($third =~ /^[a-zA-Z]$/)) { $third = "0"; } my $key = $first.$second.$third; $problems{$key} =$entry; } } $chp_title = getTitle($chapter); $sec_title = getTitle($section); eval($chapter =~ /$G{"ChapterRegEx"}/); $chapter_num = $1; eval($section =~ /$G{"SectionRegEx"}/); $section_num = $2; #-----------------# #--- HTML HERE ---# #-----------------# print "
\n"; print "
\n"; print "

Interactive Examples

\n"; print "
\n"; print "

Section ".$chapter_num.".".$section_num.". ".$sec_title."


\n"; # print " Return to Section ".$chapter_num.".".$section_num." of e-Calculus\n"; print " Return to e-Calculus\n"; print "| Choose another chapter\n"; print "| Choose another section\n"; print "| Al Shenk's Homepage\n"; print "
\n"; print " \n"; #-----------------# foreach $problem_num ( sort { $a cmp $b } keys %problems ) { $problem_gif_dir = $G{"TutorialsDir"}."/".$chapter."/".$section."/".$problems{$problem_num}; $problem_gif_file = $problem_gif_dir."/".$problems{$problem_num}."_PP.gif"; $problem_png_file = $problem_gif_dir."/".$problems{$problem_num}."_PP.png"; if (!(opendir($dh2, $problem_gif_dir))) { if ($G{"debug"}) { print "opendir failed for $problem_gif_dir\n";} } elsif ( !(-e $problem_gif_file) && !(-e $problem_png_file)) { if ($G{"debug"}) { if (!(-e $problem_gif_file)) { print "file $problem_gif_file does not exist!\n";} if (!(-e $problem_png_file)) { print "file $problem_png_file does not exist!\n";} } } else { eval ($problems{$problem_num} =~ /$G{"ProblemRegEx"}/); $temp_prob = $4; $temp_prob2 = $5; $temp_prob3 = $3; if ($temp_prob =~ /^0+$/) { $temp_prob =~ s/^0+/0/; } else { $temp_prob =~ s/^0+//; } $problem_title = "Tune-up Exercise T".$temp_prob.$temp_prob2; if (!($temp_prob3 =~ /[tT]/)) { $problem_title = "Example ".$temp_prob.$temp_prob2; } #-----------------# #--- HTML HERE ---# #-----------------# print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; #-----------------# } closedir($dh2); } #-----------------# #--- HTML HERE ---# #-----------------# print "
\n"; print "

".$problem_title."

\n"; #-----------------# while ($entry = readdir($dh2)) { if ($entry =~ /$G{"PImageRegEx"}/) { #-----------------# #--- HTML HERE ---# #-----------------# print "
\n"; #-----------------# } } #-----------------# #--- HTML HERE ---# #-----------------# print "
 
 
\n"; print "
\n"; print "
\n"; #-----------------# } closedir($dh1); } ############################## #### printProblemAndSteps #### ############################## sub printProblemAndSteps { my ($chapter, $section, $problem, $step) = @_; my ($problem_gif_dir, $step_type, @steps, $dh, $entry, $littleStep, $instruction, $step_image, $last_step, $prev_step_img, $prev_step, $next_step, $next_link, $problem_title, $step_link_data, $temp_chap, $temp_sec, $temp_prob, $temp_prob2, $temp_tuneup, $file_ext, $chapter_num, $section_num); $problem_gif_dir = $G{"TutorialsDir"}."/".$chapter."/".$section."/".$problem; if (!(opendir($dh, $problem_gif_dir))) { if ($G{"debug"}) { print "opendir failed for problem's gif directory! ".$problem_gif_dir."
\n"; } } else { while ($entry = readdir($dh)) { if ($entry =~ /$G{"SImageRegEx"}/) { my $myStep = $4; $file_ext = $5; if ($G{"debug"}) { print "File extensision is $file_ext\n";} if ($myStep =~ /^A/) { $steps[0][0] = $myStep; $steps[0][1] = "Answer"; } elsif ($myStep =~ /^S([0-9]+)/) { $steps[1+(2*($1-1))][0] = $myStep; $steps[1+(2*($1-1))][1] = "Step ".$1; } elsif ($myStep =~ /^R([0-9]+)/) { $steps[2*$1][0] = $myStep; $steps[2*$1][1] = "Result of Step ".$1; } } } $last_step = (@steps/2) - 1; if ($step =~ /$G{"StepRegEx"}/) { my $front = $1; my $back = $2; $prev_step_img = ""; if ($front =~ /^A/) { $instruction = "If you did not get the right answer, click Step 1 for an analysis of the solution.
"; $next_step = "S1"; } elsif ($front =~ /^S/) { if ($back > 1) { $prev_step = $back - 1; $prev_step = "R".$prev_step; $prev_step_img = "
\n"; } $instruction = "Write out the details of this step on a piece of paper. Check your work by clicking Result of Step ".$back." below.
"; $next_step = "R".$back; } elsif ($front =~ /^R/) { $prev_step = $step; $prev_step =~ s/[rR]/S/; $prev_step_img = "
\n"; if ($back >= $last_step) { $instruction = "This step completes the solution.
"; $next_step = "NO MORE STEPS!"; } else { $back++; $instruction = "When you have finished this step, click Step ".$back." to continue.
"; $next_step = "S".$back; } } $step_image = "
\n"; } else { $instruction = "Work out all the details of the solution on a piece of paper. Then check your conclusion by clicking Answer below."; $next_step = "A"; } $next_link = ""; $step_link_data = ""; foreach $littleStep (@steps) { if ($step eq @$littleStep[0]) { $step_link_data .= "\n"; } #-----------------# #--- HTML HERE ---# #-----------------# $step_link_data .= " ".@$littleStep[1]."
\n"; #-----------------# if ($step eq @$littleStep[0]) { $step_link_data .= "
\n"; } if (@$littleStep[0] =~ /^$next_step/) { $next_link = "    ".@$littleStep[1]."\n"; } } if ($next_link =~ /^$/) { $next_link = "    Start Over\n"; } eval ($problem =~ /$G{"ProblemRegEx"}/); $temp_chap = $1; $temp_sec = $2; $temp_tuneup = $3; $temp_prob = $4; $temp_prob2 = $5; if ($temp_chap =~ /^0+$/) { $temp_chap =~ s/^0+/0/; } else { $temp_chap =~ s/^0+//; } if ($temp_sec =~ /^0+$/) { $temp_sec =~ s/^0+/0/; } else { $temp_sec =~ s/^0+//; } if ($temp_prob =~ /^0+$/) { $temp_prob =~ s/^0+/0/; } else { $temp_prob =~ s/^0+//; } $problem_title = "Section ".$temp_chap.".".$temp_sec.": Tune-up Exercise T".$temp_prob.$temp_prob2.""; if (!($temp_tuneup =~ /[tT]/)) { $problem_title = "Section ".$temp_chap.".".$temp_sec.": Example ".$temp_prob.$temp_prob2.""; } eval($chapter =~ /$G{"ChapterRegEx"}/); $chapter_num = $1; eval($section =~ /$G{"SectionRegEx"}/); $section_num = $2; #-----------------# #--- HTML HERE ---# #-----------------# print "
\n"; print "
\n"; # print " Return to Section ".$chapter_num.".".$section_num." of e-Calculus\n"; print " Return to e-Calculus\n"; print "| Choose another chapter\n"; print "| Choose another section\n"; print "| Choose another problem\n"; print "| Al Shenk's Homepage\n"; print "
\n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print "
\n"; print "
\n"; print " ".$instruction."\n"; print "
\n"; print "
\n"; print " "; print " \n"; print "
\n"; print "
".$problem_title.":

\n"; print "
\n"; print $prev_step_img; print $step_image."
\n"; print "
\n"; if ($step) { print "
\n"; } print $next_link; print "
\n"; print "
\n"; print "
\n"; print "
\n"; print $step_link_data; print " Start Over
\n"; print "
\n"; print "
\n"; print "
\n"; #-----------------# } closedir($dh); } ############################## ########## getTitle ########## ############################## sub getTitle { my ($ChOrSec) = @_; my (@lines, $line, $lineRegEx, $chapter_num, $section_num, $title); if ($ChOrSec =~ /$G{"ChapterRegEx"}/) { $chapter_num = $IntToWord{$1}; $lineRegEx = '^.*Chapter'.$chapter_num.'{(.*)}'; } else { eval ($ChOrSec =~ /$G{"SectionRegEx"}/); $chapter_num = $IntToWord{$1}; $section_num = $IntToWord{$2}; $lineRegEx = '^.*Section'.$chapter_num.$section_num.'{(.*)}'; } if ($G{"debug"}) { print "Opening file: ".$G{"TitlesFile"}."
\n"; } open (MYFILE, "<".$G{"TitlesFile"}); @lines = ; if (!($lines[1])) { @lines = split(/[\r\n]/, $lines[0]); } $title = ""; close (MYFILE); foreach $line (@lines) { if ($line =~ /$lineRegEx/) { $title = $1; } } return $title; }