#! /usr/local/bin/perl --

########################################################################
# qa-ann.pl  Version 4.1 January 22, 1997
# Copyright (c) 1996-1997 Matthew J. Walker
# Written for the UCLA Virtual Office Hours Project
# Code can be freely distributed for educational use, as long as header 
# remains intact.  See included Copyright and Licensing information.
# For info send email to voh@chem.ucla.edu
#
# Edit this file, replacing "/" with ":" for Mac in paths
########################################################################


require 'voh-lib.pl';
&SetupVOH;

$file_number = 0 ;  # initial value

$lecture_path ="$root_path/$in{lecture}";
$qa_file_name = "$lecture_path/qa.html";
$announcement_file ="$lecture_path/announcements.html";

if ($in{request}) {               # coming from qaarf.html
   ($in{request} eq "announcement") && (&CreateAnnForm); #announcement?
   ($in{verification} !~ /$verification_string/) && (&Failure(24));
   &GetQuestion ;
   &CreateAnswerForm("None");
} elsif ($in{question_file}) {   # coming from qa form
   if ($in{killit} eq "killit") {  # delete the question?
      if (-e "$questions_path/$in{question_file}") {
         unlink "$questions_path/$in{question_file}";
      }
      $file_number = $in{file_number} ;
      &GetQuestion;
      &CreateAnswerForm("Deleted");
   } elsif ($in{skip} eq "skip") {  # skip the question?
      $file_number = ++$in{file_number} ;
      &GetQuestion;
      &CreateAnswerForm("Skipped");
   } else {                        # answer the question.
      unless ($in{answer}) { &Failure(04) ;}
      &WriteFile(Question_Answer);
      if (-e "$questions_path/$in{question_file}") {
         unlink "$questions_path/$in{question_file}";
      }
      $file_number = $in{file_number} ;
      &GetQuestion;
      &CreateAnswerForm("Answered Successfully");
   }
} elsif ($in{announcement}) {   # coming from announcement form
   &WriteFile(Announcement);
   $success = "Announcement Posted Successfully";
   &Success;
} else {                        # shouldn't get here, but...
   &Failure(07) ;
}

# CreateAnswerForm
# Generates Question Answer Form and sends it to the client
# The HTML Generated by this subroutine will need to be edited to
# reflect the content of your site!

sub CreateAnswerForm {
   local($html);
   $html = <<"EOC";
$HTTP_header
<! follows W3 html 2.0 standard>
<html>
<head>
<title>$HTML_title // Question Answer Form</Title>
</head>
<body $HTML_bgcolor  $HTML_background >
<h1>Question Answer Form</h1>
<hr>
<form method="post" ACTION="http:$cgi_url/qa-ann.pl">
<input type="hidden" name="lecture" value="$in{lecture}">
<input type="hidden" name="question_file" 
value="$questions[$file_number]">
<input type="hidden" name="department" value="$in{department}">
<input type="hidden" name="file_number" value="$file_number">
<h2>Question:</h2>
<TEXTAREA NAME="question" ROWS=8 COLS=60 WRAP>$question</textarea><br>
<input type="checkbox" name="killit" value ="killit">
If you want to <b>delete</b> this question without answering it.<br>
<input type="checkbox" name="skip" value ="skip">
If you want to <b>skip</b> this question and answer it later.
<h2>Answer:</h2>
<TEXTAREA NAME="answer" ROWS=8 COLS=60 WRAP></textarea>
<h2>$warning</h2>
<INPUT TYPE="submit" VALUE="Submit Answer">
<INPUT TYPE="reset" VALUE="Reset Submission Form">
<hr>
$HTML_footer
EOC
   if ($system_type eq "Mac") {
      MacPerl::Reply($html);
   } else {
      print ($html);
   }
exit;
}

# GetQuestion
# Retrieves the first question from the proper directory

sub GetQuestion {
   $question = "" ;
   opendir(QUESTIONS, "$questions_path") || (&Failure(11)) ;
   @questions = sort (grep(!/^\.\.?$/, readdir(QUESTIONS)));
   if (-T "$questions_path/$questions[$file_number]") {
   open(QUESTION, "$questions_path/$questions[$file_number]") ||
(&Failure(12)) ;
   while(<QUESTION>) {
      $question .= $_ ;
   }
   close(QUESTION);
   closedir(QUESTIONS) ;
   } else {
   # If the file isn't text then the dir was empty.
   # This is is monster hack, but....
   $success = "All questions have been answered." ;
   &Success;
   }
}

# CreateAnnForm
# Creates Announcement Submission form and sends it to client
# Again, edit the HTML!

sub CreateAnnForm {
   local($html);
   $html = <<"EOA" ;
$HTTP_header
<! follows W3 html 2.0 standard>
<html>
<head>
<title>$HTML_title // Announcement Submission Form</Title>
</head>
<body $HTML_bgcolor  $HTML_background >
<h1>Announcement Submission Form</h1>
<hr>
<form method="post" ACTION="http:$cgi_url/qa-ann.pl">
<input type="hidden" name="lecture" value="$in{lecture}">
<input type="hidden" name="department" value="$in{department}">
<h2>Announcement:</h2>
<TEXTAREA NAME="announcement" ROWS=8 COLS=60 WRAP></textarea>
<h2>$warning</h2>
<INPUT TYPE="submit" VALUE="Submit Announcement">
<INPUT TYPE="reset" VALUE="Reset Submission Form">
<hr>
$HTML_footer
EOA
   if ($system_type eq "Mac") {
      MacPerl::Reply($html);
   } else {
      print ($html);
   }
exit;
}


# WriteFile
# Writes formated QA pair or announcement to proper file.

sub WriteFile {
   local($test,$new_text,$html) = @_ ;
   if ($test eq "Question_Answer"){
      $open_file_name = $qa_file_name ;
      $html = &FormatQA;
   } elsif ($test eq "Announcement") {
      $open_file_name = $announcement_file;
      $html = &FormatAnnouncement;
   }
   open(FILE, "$open_file_name") || (&Failure(13)) ;
   while (<FILE>) {
      $new_text .= $_ ;
   }
   close FILE ;
   $new_text =~  s/(<!-- begin -->)/$1\n$html/ ;
   open(FILE, ">$open_file_name") || &Failure(13) ;
   print FILE ($new_text) ;
   close FILE ;
}

# FormatQA
# Formats form input for public posting to file.

sub FormatQA {
  local($output);
  $output .=  "<h2>Question:</h2>\n" ;
  $output .=  $in{question} ;
  $output .=  "<h2>Answer:</h2>\n" ;
  $output .=  "<p> $in{answer} <hr><hr>\n ";
  return $output;
}

# FormatAnnouncement
# Formats form input for posting to announcements file.

sub FormatAnnouncement {
  local($output);
  $output .=  "<h2> $date </h2>\n" ;
  $output .=  "<p> $in{announcement} <hr>\n ";
  return $output;
}


# Success
# Creates HTML text to send to client after post.
# Edit HTML!!

sub Success {
   local($html, $temp);
   ($temp = $in{lecture}) =~ s#:# #;
   $html = <<"EOS" ;
$HTTP_header
<! follows W3 html 2.0 standard>
<html>
<head>
<title>$HTML_title // Success!</Title>
</head>
<body $HTML_bgcolor  $HTML_background >
<h1> $success </h1>
Go back to the:
<ul>
<li><a href="$link_path/">$temp Class Page</a>
<li><a href="$link_path/announcements.html">Announcements</a>
<li><a href="$link_path/qa.html">Answered Questions</a>
</ul>
$HTML_footer
EOS
   if ($system_type eq "Mac") {
      MacPerl::Reply($html);
   } else {
      print ($html);
   }
exit;
}


