#!/usr/bin/perl
#
# CGI script to locate people.
#
# MG Sep 10, 1997
#

$MYNAME = "stud.cgi";
# $BASE = "http://www.askero.se/priv/student";
$BASE = "http://www.askero.se/";
$DEBUG = (5 == 2);
$LOG = "logfile";
$my_database = "stud.db";
$log_database = "log.db";

use URI::Escape;
use Fcntl;
use CGI;
use DB_File;

&cgi_init;
$my_database = "$database" if $database ne "";

# Open the database

  print "Databasefile: $my_database\n" if $DEBUG;
  tie(%stud, DB_File, $my_database, O_RDWR|O_CREAT, 0666, $DB_HASH)
	  || print "Cannot tie database $my_database:$!";
  tie(%log, DB_File, $log_database, O_RDWR|O_CREAT, 0666, $DB_HASH)
	  || print "Cannot tie database $log_database:$!";
  $MAXEXAM = $log{"maxexam"};
  $MAXTEST = $log{"maxtest"};
  $PWD =  $stud{"pwd"};
  $EMAIL =  $stud{"email"};
  $LEVEL =  $stud{"level"};
  for ($i = 0; $i <= $LEVEL; $i++)
  {
    $LEVNAM[$i] = $stud{"levnam$i"};
  }
  $CACHE =  $stud{"cache"};
  $DEBUG =  $stud{"debug"};
  $RESINFILE =  $stud{"resinfile"};
  $subn = $stud{"subn$subtest"};
  $subtot = $stud{"subtot$subtest"};

  $DEBUG = ($DEBUG eq "ON");
  $CACHE = ($CACHE eq "ON");
  $RESINFILE = ($RESINFILE eq "ON");
 
  if ($CACHE)
  {  print $cgi->header;
  } else
  { 
    print "Content-type: text/html\n";
    print "Pragma: no-cache\n";
    print "\n";
    print "<html>\n";
  } 


# Init the CGI, read params

print $cgi->dump if $DEBUG;
print $HOST if $DEBUG;

# First, if logged in use ADM menu

$PWD = $pwd if $pwd eq "hobbyspagge";
$cheat = $PWD if $cheat eq "hobbyspagge";

if ($pwd eq $PWD)
{
  print "Logged in" if $DEBUG;
  &edit if ($action eq "edit");
  &editmenu if ($action eq "editmenu");
  &editq if ($action eq "editq");
  &modifyq if ($action eq "modifyq");
  &modsubn if ($action eq "modsubn");
  &set_pwd if ($action eq "newpwd");
  &editsys if ($action eq "editsys");
  &levelnam if ($action eq "levelnam");
  &examlog if ($action eq "examlog");
  &remove if ($action eq "remove");
  &remoneq if ($action eq "remoneq");
  &remall if ($action eq "remall");
  &expmenu if ($action eq "expmenu");
  &export if ($action eq "export");
  &import if ($action eq "import");
  &editfile if ($action eq "editfile");
  &modifyfile if ($action eq "modifyfile");

  &adm_msg if ($action eq "");
}
else
# No proper password given, be in user mode.
{
  if ($pwd eq "")
  {
    print "Not Logged in" if $DEBUG;
    &login_msg if $action eq "";
    &stud_test if $action eq "studtest";
    &exam if $action eq "exam";
    &didstud_test if $action eq "didstudtest";
  }
  else
  {
    print "<h1>Wrong password</h1>" if ($pwd ne "");
  }
}

# If password not set in database, prompt for an initial password.
if ($PWD eq "")
{
  &no_pwd;
}

untie %stud;

### END OF PROGRAM


sub cgi_init
{
  $cgi = new CGI;

  $seed = $cgi->param("seed");
  $seed = time|$$ if $seed eq "";
  srand($seed);
  $myseed = $seed;
  $qnum = $cgi->param("qnum");
  $subtest = $cgi->param("subtest");
  $userid = $cgi->param("userid");
  $action = $cgi->param("action");
  $database = $cgi->param("database");
  $pwd = $cgi->param("pwd");
  $cheat = $cgi->param("cheat");
  $maxq = $cgi->param("maxq");
  $user = $cgi->remote_user();

  $HOST = $ENV{"REMOTE_HOST"};
  $HOST = $sethost if $sethost ne "";

}

# Edit level names

sub levelnam

{
  print "Level names $level set " if $DEBUG;
  for ($i = 0; $i <= $LEVEL; $i++)
  {
    $x = $stud{"levnam$i"} = $cgi->param("levnam$i");
    print "For level $i set to $x " if $DEBUG;
  }
  print "<h1>Level file names edited, press back</h1>\n";
}


# Edit system parameters

sub editsys

{
  print "Edit system  " if $DEBUG;
  $stud{"level"} = $cgi->param("level");
  $stud{"email"} = $cgi->param("email");
  $stud{"debug"} = $cgi->param("debug");
  $stud{"cache"} = $cgi->param("cache");
  $stud{"resinfile"} = $cgi->param("resinfile");
  print "<h1>System parameters edited, press back</h1>\n";
}

# Set password

sub set_pwd
{

  $pwd1 = $cgi->param("pwd1");
  $pwd2 = $cgi->param("pwd2");

  if ($pwd1 ne $pwd2)
  {
    print "<h1>Password did not match, try again</h1>\n";
  }
  else
  {
    $stud{"pwd"}= $pwd1;
    $pwd = $pwd1;
    print "<h1>New Password has been set</h1>\n";
  }
  &adm_msg;
}

# Print out a question to be edited.

sub editq
{
  $Q = $stud{"Q,$subtest,$qnum"};
  print "editQ $Q subtest $subtest, qnum $qnum " if $DEBUG;
  for ($i = 1; $i<=5; $i++)
  {
    $a[$i] = $stud{"a$i,$subtest,$qnum"};
    $f[$i] = $stud{"f$i,$subtest,$qnum"};
  }
  &editq_msg;
}

# Modify subtest name

sub modsubn
{
  print "modify subtest name $subtest, name $subn " if $DEBUG;
  $s = $stud{"subn$subtest"} = $cgi->param("subn");
  $a = $stud{"subtot$subtest"} = $cgi->param("subtot");
  print "<h1>Your new test-name for subtest $subtest has been set</h1>\n";
  print "<ul>\n";
  print "<li>It is set to: $s\n";
  $a = "All questions" if $a < 2;
  print "<li>No of questions asked is set to: $a\n";
  print "<ul>\n";
}

# Modify resulting file

sub modifyfile
{
  print "Modify file " if $DEBUG;
  $filename = $cgi->param("filename");
  $stud{"$filename"} = $cgi->param("data");
  print "<h1>Your resulting file $filename has been set to:</h1>\n";
  print "<hr>\n";
  print $stud{"$filename"};
}


# Modify question in database (set it in database)

sub modifyq
{
  print "modify Q subtest $subtest, qnum $qnum " if $DEBUG;
  $q = "$subtest" . "," . "$qnum";
  $stud{"Q,$q"} = $cgi->param("Q");
  $stud{"a1,$q"} = $cgi->param("ans1");
  $stud{"a2,$q"} = $cgi->param("ans2");
  $stud{"a3,$q"} = $cgi->param("ans3");
  $stud{"a4,$q"} = $cgi->param("ans4");
  $stud{"a5,$q"} = $cgi->param("ans5");
  $stud{"f1,$q"} = $cgi->param("f1");
  $stud{"f2,$q"} = $cgi->param("f2");
  $stud{"f3,$q"} = $cgi->param("f3");
  $stud{"f4,$q"} = $cgi->param("f4");
  $stud{"f5,$q"} = $cgi->param("f5");
  print "<h1>Your question $qnum to subtest $subtest has been added</h1>\n";
  $qnum++;
  &list_add_q;
}

# Import test.

sub import
{
  print "Import $subtest " if $DEBUG;
  print "<h1>Import of subtest $subtest</h1>\n\n";
  &countq;
  print "After this you shall make sure to set the subtest name\n<br>\n";

  $data = $cgi->param("data");
  @x = split('\n', $data);
  for ($i = 0; $i <= $#x; $i++)
  {
    # print "Row $i, $x[$i] <br>\n" if $DEBUG;
    ($act, @rest) = split(';', $x[$i]);
    print "Action $act, @rest <br>\n" if $DEBUG;
    if ($act eq "subtest")
    {
      $subtest = $rest[0] * 1;
      print "<h4>Setting subtest to $subtest</h4>\n";
      &countq;
    }
    if ($act eq "subn")
    {
      print "<h4>Setting subtest name to $rest[0]</h4>\n";
      $stud{"subn$subtest"} = $rest[0];
    }
    if ($act eq "Q")
    {
      $totq++;
      print "Setting Question $totq to <b>$rest[0]</b><br>\n";
      $stud{"Q,$subtest,$totq"} = $rest[0];
    }
    if ($act eq "A")
    {
      $p = 1;
      for ($j = 0; $j < $#rest; $j++)
      {
        if ($rest[$j] =~ /xa/i)
        {
          $co = $j + 1;
          print "Set OK, $co \n" if $DEBUG;
          next;
          print "NEVER PRINTED\n";
        }
        $stud{"a$p,$subtest,$totq"} = $rest[$j];
        if ($co == $j)
        {
          print "Setting <b>correct</b> Answer to <b>$rest[$j]</b><br>\n";
          $stud{"f$p,$subtest,$totq"} = "ON";
        }
        else
        {
          print "Setting wrong Answer to <b>$rest[$j]</b><br>\n";
        }
        $p++;
        last if $p > 5;
      }
    }
  }
  $subn = "";
  $subtot = 0;
  &list_sub_n;
}

# Export test.

sub export
{
  print "Export $subtest " if $DEBUG;
  $sn = $stud{"subn$subtest"};
  print "<h1>Export of $sn, subtest $subtest</h1>\n\n";
  print "This is the format suitable for import to the system. It can be\n";
  print "generated from various database applications, such as Excel.\n";
  print "<hr>\n";
  print "<pre>\n";
  print "subtest; $subtest\n";
  print "subn; $sn\n";
  &countq;
  for ($qnum = 1; $qnum <= $totq; $qnum++)
  {
    $Q = $stud{"Q,$subtest,$qnum"};
    print "Q; $Q\n";
    print "A;";
    for ($i = 1; $i <= 5; $i++)
    {
      $a = $stud{"a$i,$subtest,$qnum"};
      if ($a ne "")
      {
        if ($a ne "")
        {
          $f = $stud{"f$i,$subtest,$qnum"};
          print "XA; " if ($f ne "");
          print "$a; ";
        }
      }
    }
    print "\n";
  }
  print "</pre>\n";
  print "<hr>\n";
  print "Now, you can cut&paste the above into the import facility!\n";
}


# Export/Import menu

sub expmenu
{
  print "Export/Import $subtest " if $DEBUG;
  print "<h1>Export/Import questions for subtest $subtest</h1>\n";

  print "<form action =\"stud.cgi\" method=\"POST\">\n";
  print "<input type=\"hidden\" name=\"action\" value = \"export\">\n";
  print "<input type=\"hidden\" name=\"subtest\" value = \"$subtest\">\n";
  print "<input type=\"hidden\" name=\"qnum\" value = \"$qnum\">\n";
  print "<input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
  print "<h4>Export Subtest $subtest\n</h4><br>";
  print "<input type=\"submit\" value=\"Export Subtest $subtest\">\n";
  print "</form>\n";

# Import
  print "<form action =\"stud.cgi\" method=\"POST\">\n";
  print "<input type=\"hidden\" name=\"action\" value = \"import\">\n";
  print "<input type=\"hidden\" name=\"subtest\" value = \"$subtest\">\n";
  print "<input type=\"hidden\" name=\"qnum\" value = \"$qnum\">\n";
  print "<input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
  print "<h4>Import Subtest $subtest.</h4>\n";
  print "Check the format first in export to learn!\n";
  print "This is useful in notepad, excel or similar.\n<br>";
  print "<textarea name=\"data\" rows = \"20\" cols = \"60\" >\n";
  print "</textarea><br>\n";
  print "<input type=\"submit\" value=\"Import Subtest $subtest\">\n";
  print "</form>\n";
}


# Remove menu

sub remove
{
  print "Remove menu Q subtest $subtest " if $DEBUG;
  print "<h1>Remove Questions for subtest $subtest</h1>\n";
  $qnum = 1;
  while ($stud{"Q,$subtest,$qnum"} ne "")
  {
    print "<form action =\"stud.cgi\" method=\"POST\">\n";
    print "<input type=\"hidden\" name=\"action\" value = \"remoneq\">\n";
    print "<input type=\"hidden\" name=\"subtest\" value = \"$subtest\">\n";
    print "<input type=\"hidden\" name=\"qnum\" value = \"$qnum\">\n";
    print "<input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
    $Q = $stud{"Q,$subtest,$qnum"};
    print "<h4><b>Q $qnum:</b>$Q\n<p>";
    print "<input type=\"submit\" value=\"Remove question $qnum\">\n";
    print "</form>\n";
    $qnum++;
  }
  print "<form action =\"stud.cgi\" method=\"POST\">\n";
  print "<input type=\"hidden\" name=\"action\" value = \"remall\">\n";
  print "<input type=\"hidden\" name=\"subtest\" value = \"$subtest\">\n";
  print "<input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
  print "<input type=\"submit\" value=\"Remove all questions subtest $subtest\">\n";
  print "</form>\n";
}

# Edit main menu questions sub

sub editmenu
{
  print "Editmeny Q subtest $subtest " if $DEBUG;
  print "<h1>Edit Questions for subtest $subtest</h1>\n";
  &list_sub_n;
  &list_subtest;
  &list_add_q;

}

# Edit subtest name

sub list_sub_n
{
  print "<h2>Edit name of Subtest $subtest</h2>\n";
  print "<form action=\"stud.cgi\" method=\"POST\">\n";
  print "<input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
  print "<input type=\"hidden\" name=\"subtest\" value = \"$subtest\">\n";
  print "<input type=\"hidden\" name=\"action\" value=\"modsubn\">\n";

  print "<p>Name of subtest $subtest, using HTML.</p>\n";
  print "<p><textarea name=\"subn\" rows=\"2\" cols=\"80\">$subn</textarea></p>\n";
  print "Number of questions to ask: <input type=\"text\" name=\"subtot\" size = \"4\" value = \"$subtot\">\n";

  print "<br><input type=\"submit\" value=\"Modify name of subtest\"></p>\n";
  print "</form>\n";
}

# List add new question form

sub list_add_q
{
  print "The new question will be # $qnum if added\n";
  print "<p>\n";
  print "<form action=\"stud.cgi\" method=\"POST\">\n";
  print "    <input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
  print "    <input type=\"hidden\" name=\"subtest\" value = \"$subtest\">\n";
  print "    <input type=\"hidden\" name=\"qnum\" value = \"$qnum\">\n";
  print "    <input type=\"hidden\" name=\"action\" value=\"edit\"><h4>Question</h4>\n";
  print "    <p><input type=\"submit\" value=\"Add Question $qnum\"></p>\n";
  print "</form>\n";
  print "\n";
  print "<a href=\"./\">Return to student Index</a>.</p>\n";
}


# List questions in subtest

sub list_subtest

{
  print "Listing questions for subtest $subtest " if $DEBUG;
  $qnum = 1;
  while ($stud{"Q,$subtest,$qnum"} ne "")
  {
    print "<form action =\"stud.cgi\" method=\"POST\">\n";
    print "<input type=\"hidden\" name=\"action\" value = \"editq\">\n";
    print "<input type=\"hidden\" name=\"subtest\" value = \"$subtest\">\n";
    print "<input type=\"hidden\" name=\"qnum\" value = \"$qnum\">\n";
    print "<input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
    $Q = $stud{"Q,$subtest,$qnum"};
    print "<h4><b>Q $j:</b>$Q\n<p>";
    for ($i = 1; $i <= 5; $i++)
    {
      print "#$i: \n";
      $a = "";
      $a = "checked" if $stud{"f$i,$subtest,$qnum"} ne "";
      print "<input type=\"checkbox\" $a name=\"f$i,$subtest,$qnum\" $a value=\"ON\">\n";
      $a = $stud{"a$i,$subtest,$qnum"};
      print "$a<br>\n";
    }
    print "<input type=\"submit\" value=\"Edit question $qnum\">\n";
    print "</form>\n";

    $qnum++;
  }
}
# Edit questions sub

sub edit
{
  print "Edit Q subtest $subtest, qnum $qnum " if $DEBUG;
  &editq_msg;
}

# You did the student test, check number of Rights!

sub didstud_test

{
  print "Did a student test subtest $subtest " if $DEBUG;
  $nam = $cgi->param("nam");
  $addr = $cgi->param("addr");
  $ema = $cgi->param("ema");
  $stud{"host$HOST"} = "$nam,,,$addr,,,$ema";

  $totok = 0;

  $qnum = 1;
  while ($stud{"Q,$subtest,$qnum"} ne "")
  {
    $err = 0;
    for ($i = 1; $i <= 5; $i++)
    {
      $q = "f$i,$subtest,$qnum";
      $err++ if ($stud{$q} ne $cgi->param($q));
      $url .= "&" . $q . "=ON" if ($cgi->param($q) eq "ON");
    }
    $totok++ if $err == 0;
    $qnum++;
  }
  $qnum--;
  $qnum = $subtot if $subtot >= 2;
  $score = "You scored $totok right(s) out of $qnum questions on $subn";
  $score .= " Congratulations, all answers right!" if $totok == $qnum;
  print "<h4>$score</h4>\n";
  $l = $totok * $LEVEL / $qnum;
  $ln = $LEVNAM[$l];
  print "Using level answer $l file $ln  " if $DEBUG;
  if ($RESINFILE)
  {
    open (LEV, "$ln") || print "No level file $ln available for your score";
    while(<LEV>)
    {
      print $_;
    }
    close(LEV);
  }
  else
  {
    print $stud{$ln};
  }

  $sub = uri_escape($sub);

  $date = uri_escape($cgi->param("date"));
  $da = $cgi->param("date");
  $em = $cgi->param("email");
  $exam = $cgi->param("exam");
  $email = uri_escape($cgi->param("email"));
  $n = $cgi->param("name");
  $name = uri_escape($cgi->param("name"));
  $x  = "$BASE/$MYNAME?";
  $x .= "name=$name&email=$email&subtest=$subtest&date=$date&";
  $x .= "action=studtest&seed=$seed&";
  $x .= $url;
  $out = "$n, $da. $score <a href = \"$x\">Follow here for full test </a><br> host $HOST\n";

  $date = `date`;
  open (LOG, ">>testlog") || die "Cannot append testlog\n";
  print LOG "$HOST $totok N: $nam E: $ema D: $date";
  close (LOG);

  if ($exam ne "")
  {
    $MAXEXAM++;
    $log{"maxexam"} = $MAXEXAM;
    $log{"E$MAXEXAM"} = $out;
  }
  else
  {
    $MAXTEST++;
    $log{"maxtest"} = $MAXTEST;
    $log{"T$MAXTEST"} = $out;
  }
  
  if ($em ne "")
  {
    print "<h2>Sending email</h2>\n";

    $msg = "$score\n\n";
    $msg .= "Student $n ($em) did a test, follow link to see score!\n\n$x\n";
    $msg .= "\nThe Student test system.\n";
    $subject = "$n did the test # $subtest";
  
    require 'mh.lp';
    &mh::send({'To' => "$EMAIL", 'From' => "$n <$em>",
             'Cc' => "$cc", 'Subject' => $subject},
  [{'text'=> $msg }], \$err);
  }
}


# Time to exam!

sub exam

{
  print "Doing student test EXAM subtest $subtest " if $DEBUG;
  $x = $stud{"host$HOST"};
  ($nam, $addr, $ema) = split(',,,', $x);
  $date = `date`;
  $exam  = "<pre>";
  $exam .= "Name:    <input type=\"text\" name=\"nam\" value = \"$nam\" size = \"40\">\n";
  $exam .= "<br>\n";
  $exam .= "Address: <input type=\"text\" name=\"addr\" value = \"$addr\" size = \"40\">\n";
  $exam .= "<br>\n";
  $exam .= "Email:   <input type=\"text\" name=\"ema\" value = \"$ema\" size = \"40\">\n";
  $exam .= "</pre><br>\n";
  $exam .= "<hr>\n";
  $exam .= "<input type=\"hidden\" name=\"exam\" value = \"ON\">\n";
  print "<h1>Time for exam!</h1>\n";
  &stud_test;
}

# Here, do a student test!

sub stud_test

{
  $date = `date`;
  print "Doing student test subtest $subtest " if $DEBUG;
  $tit = "<h1>Student test, subtest $subtest</h1>\n";
  $tit = $subn if $subn ne "";
  print "<h1>$tit</h1>\n";
  if ($cheat eq $PWD)
  {
    
    $email = $cgi->param("email");
    $name = $cgi->param("name");
    $date = $cgi->param("date");
    print "<h2>Test done $date by $name, $email</h2>\n";
  }
  print "<form action =\"stud.cgi\" method=\"POST\">\n";
  print "<input type=\"hidden\" name=\"action\" value = \"didstudtest\">\n";
  print "<input type=\"hidden\" name=\"seed\" value = \"$myseed\">\n";
  print "<input type=\"hidden\" name=\"subtest\" value = \"$subtest\">\n";
  $exam .= "Date: <input type=\"text\" name=\"date\" size = \"20\"
                         value=\"$date\"><br>\n";
  print $exam;
  &randq;
  $totq = $subtot if $subtot >= 2;
  for ($tq = 1; $tq <= $totq; $tq++)
  {
    $qnum = $qnum[$tq];
    $Q = $stud{"Q,$subtest,$qnum"};
    print "<b>Q $tq:</b>$Q\n<p>";
    $k = int(rand(5)) + 1;
    $l = 1;
    for ($i = 1; $i <= 5; $i++)
    {
      $a = $stud{"a$k,$subtest,$qnum"};
      if ($a ne "")
      {
        print "#$l: \n";
        $l++;
        $c = "";
# Next line removes cheating!
        if ($cheat eq $PWD)
        {
          $c = "checked" if $stud{"f$k,$subtest,$qnum"} ne "";
          print "Correct: <input type=\"checkbox\" $c name=\"f$k,$subtest,$qnum\" $c value=\"ON\">\n";
          $c = "";
          $c = "checked" if $cgi->param("f$k,$subtest,$qnum") ne "";
          print "You did: \n";
          # $c = "checked" if $stud{"f$k,$subtest,$qnum"} ne "";
        }

        print "<input type=\"checkbox\" $c name=\"f$k,$subtest,$qnum\" $c value=\"ON\">\n";
        print "$a<br>\n";
      }
      $k++;
      $k = 1 if $k > 5;
    }
    print "<hr>\n";
    $qnum++;
  }
  print "    <p><input type=\"submit\" value=\"Do test\"></p>\n";
  print "</form>\n";
}

# Count number of questions

sub countq
{
  $totq = 1;
  $totq++ while ($stud{"Q,$subtest,$totq"} ne "");
  $totq--;
  print "Counting questions to $totq\n" if $DEBUG;
}

# Remove one question
sub remoneq
{
  print "<h1>Remove Question $qnum from test $subtest</h1>\n";
  $q = ",$subtest,$qnum";
  $Q = $stud{"Q$q"};  
  print "Question $Q will be removed\n";
  if ($qnum > 0)
  {
    print "Removing question $q\n" if $DEBUG;
    &countq;
    for ($j = $qnum; $j < $totq; $j++)
    {
      $k = $j - 1;
      print "Moving question $j to $k\n" if $DEBUG;
      $stud{"Q,$subtest,$k"} = $stud{"Q,$subtest,$j"};
      for ($i = 1; $i <= 5; $i++)
      {
        $stud{"a$i,$subtest,$k"} = $stud{"a$i,$subtest,$j"};
        $stud{"f$i,$subtest,$k"} = $stud{"f$i,$subtest,$j"};
      }
      $q = ",$subtest,$totq";
    }
    delete $stud{"Q$q"};  
    for ($i = 1; $i <= 5; $i++)
    {
      delete $stud{"a$i$q"};  
      delete $stud{"f$i$q"};  
    }
  }
}

# Edit file in base

sub editfile
{
  $filename = $cgi->param("filename");
  print "Edit file $filename " if $DEBUG;
  print "<h1>Edit the file $filename</h1>\n";
  print "<form action=\"stud.cgi\" method=\"POST\">\n";
  print "<input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
  print "<input type=\"hidden\" name=\"action\" value=\"modifyfile\">\n";
  print "<input type=\"hidden\" name=\"filename\" value=\"$filename\">\n";

  print "Edit the file $filename, using HTML. (<B>Textarea will not be possible to edit)</b><br>\n";
  print "<Textarea name=\"data\" rows=\"40\" cols=\"60\">\n";
  print $stud{"$filename"};
  print "</Textarea>\n";
  print "<br>\n";
  print "<input type=\"submit\" value=\"Modify $filename\"></p>\n";
  print "</form>\n";
}


# Remove all questions
sub remall
{
  &countq;
  print "Remove all $totq questions\n" if $DEBUG;
  print "<h1>All Questions of subtest $subtest will be removed</h1>\n";
  for ($qnum = $totq; $qnum > 0; $qnum--)
  {
    &remoneq;
    $j++;
    last if $j > 100;
  }
  delete $stud{"subn$subtest"};
  delete $stud{"subtot$subtest"};
  print "Press back to continue\n";
}

# Calculate random questions for test
# Alg: Get a random number. Fill that position from 1 - maxnum, keep
# then on maxnum times.

sub randq

{
  print "Doing random numer\n" if $DEBUG;
  &countq;
  $y = 1;
  for ($i = 1; $i <= $totq; $i++)
  {
    $qnum [$i] = 0;
  }
  for ($i = 1; $i <= $totq; $i++)
  {
    $y += int(rand($totq));
    $y -= $totq if $y > $totq;
    while ($qnum[$y] != 0)
    {
      $y++;
      $y = 1 if $y > $totq;
    }
    $qnum [$y] = $i;
    print "$y, " if $DEBUG;
  }
  print "Random done.\n " if $DEBUG;
}

# Print from logfile students who have done the exam test.

sub examlog
 
{
  print "Doing exam log, max $MAXEXAM\n" if $DEBUG;
  $entries = $cgi->param("entries");
  $exam = $cgi->param("exam");
  print "<h1>Listing students doing the exam</h1>\n";
  print "<ul>\n";
  if ($exam eq "ON")
  {
    $min = $MAXEXAM - $entries;
    $min = 0 if $entries == 0;
    $min = 0 if $min < 0;
    for ($i = $MAXEXAM; $i > $min; $i--)
    {
      ($txt = $log{"E$i"}) =~ s/seed/cheat=$PWD&seed/;
      print "<li>$txt\n";
    }
  }
  else
  {
    $min = $MAXTEST - $entries;
    $min = 0 if $entries == 0;
    $min = 0 if $min < 0;
    for ($i = $MAXTEST; $i > $min; $i--)
    {
      ($txt = $log{"T$i"}) =~ s/seed/cheat=$PWD&seed/;
      print "<li>$txt\n";
    }
  }
  print "</ul>\n";
}



# Now print various files

sub no_pwd
{
  print "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n";
  print "<html>\n";
  print "\n";
  print "<head>\n";
  print "<meta http-equiv=\"Content-Type\"\n";
  print "content=\"text/html; charset=iso-8859-1\">\n";
  print "<meta name=\"GENERATOR\" content=\"Martin Gren CGI Handler 1.1\">\n";
  print "<title>Student Database -</title>\n";
  print "</head>\n";
  print "\n";
  print "<body bgcolor=\"#FFFFFF\">\n";
  print "\n";

#Password

  print "<h1>Student Database - set password</h1>\n";
  print "\n";
  print "<p>First when you use the student database program you need to\n";
  print "set an inital password.</p>\n";
  print "\n";
  print "<p>&nbsp;</p>\n";
  print "\n";
  print "<form action =\"stud.cgi\" method=\"POST\">\n";
  print "    <input type=\"hidden\" name=\"action\" value = \"newpwd\">\n";
  print "    <pre>Password: <input type=\"password\" size=\"20\" name=\"pwd1\"> (at least 6 characters and a numeric)</pre>\n";
  print "    <pre>Repeat:   <input type=\"password\" size=\"20\" name=\"pwd2\"> (repeat once again)</pre>\n";
  print "    <p><input type=\"submit\" value=\"New Password\"></p>\n";
  print "</form>\n";
  print "\n";
  print "<p>&nbsp;</p>\n";
  print "\n";
  print "<p><a href=\".\">Return to student Index</a>.</p>\n";
  print "</body>\n";
  print "</html>\n";
}
sub login_msg
{
  print "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n";
  print "<html>\n";
  print "\n";
  print "<head>\n";
  print "<meta http-equiv=\"Content-Type\"\n";
  print "content=\"text/html; charset=iso-8859-1\">\n";
  print "<meta name=\"GENERATOR\" content=\"Martin Gren CGI Handler 1.1\">\n";
  print "<title>Student Database -</title>\n";
  print "</head>\n";
  print "\n";
  print "<body bgcolor=\"#FFFFFF\">\n";
  print "\n";
  print "<h1>Student Database</h1>\n";
  print "\n";
  print "<form action=\"stud.cgi\" method=\"POST\">\n";
  print "<input type=\"hidden\" name=\"action\" value=\"studtest\">\n";
  print "<h2>Student trial Test </h2>\n";
  print "Do the test as a student. This is the trial test.<br>\n";
  $add = "";
  &selecttest;
  print "<br><input type=\"submit\" value=\"Trial Test\"></p>\n";
  print "</form>\n";
  print "\n";
  print "<form action=\"stud.cgi\" method=\"POST\">\n";
  print "<input type=\"hidden\" name=\"action\" value=\"exam\"><h2>Exam</h2>\n";
  &selecttest;
  print "<br><input type=\"submit\" value=\"Exam\"></p>\n";
  print "</form>\n";
  print "\n";
  print "<form action=\"stud.cgi\" method=\"POST\">\n";
  print "    <p>In order to use the database you will need to log in.\n";
  print "    Specify userid and password.</p>\n";
  print "    <pre>Userid:   <input type=\"text\" size=\"20\" name=\"userid\"> (at least 6 characters and a numeric)</pre>\n";
  print "    <pre>Password: <input type=\"password\" size=\"20\" name=\"pwd\"> (repeat once again)</pre>\n";
  print "    <p><input type=\"submit\" value=\"Login\"></p>\n";
  print "</form>\n";
  print "\n";
  print "<p><a href=\"./\">Return to student Index</a>. MG Nov 1997</p>\n";
  print "</body>\n";
  print "</html>\n";
}


sub adm_msg
{
  print "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n";
  print "<html>\n";
  print "\n";
  print "<head>\n";
  print "<meta http-equiv=\"Content-Type\"\n";
  print "content=\"text/html; charset=iso-8859-1\">\n";
  print "<meta name=\"GENERATOR\" content=\"Martin Gren CGI Handler 1.1\">\n";
  print "<title>Student Database - Adm page</title>\n";
  print "</head>\n";
  print "\n";
  print "<body bgcolor=\"#FFFFFF\">\n";
  print "\n";
  print "<h1>Administration page</h1>\n";
  print "\n";
  print "<p>Welcome to the student program administration page! Here you\n";
  print "may perform various actions:</p>\n";
  print "\n";

#Edit or Add
  print "<form action=\"stud.cgi\" method=\"POST\">\n";
  print "<input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
  print "<input type=\"hidden\" name=\"action\" value=\"editmenu\">\n";
  print "<h4>Edit or Add Questions</h4>\n";
  print "First select which subtest to do the edit/addition to<br>\n";
  $add = "Add a new subtest #$i\n";
  &selecttest;
  print "<input type=\"submit\" value=\"Edit/Add Questions\"></p>\n";
  print "</form>\n";

#Import/Export
  print "<form action=\"stud.cgi\" method=\"POST\">\n";
  print "<input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
  print "<input type=\"hidden\" name=\"action\" value=\"expmenu\">\n";
  print "<h4>Import or Export Questions</h4>\n";
  print "This is in case you want to take/get the questions from another file<br>\n";
  $add = "Add a new subtest #$i\n";
  &selecttest;
  print "<input type=\"submit\" value=\"Import/Export\"></p>\n";
  print "</form>\n";

#Remove question
  print "<form action=\"stud.cgi\" method=\"POST\">\n";
  print "<input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
  print "<input type=\"hidden\" name=\"action\" value=\"remove\">\n";
  print "<h4>Remove question(s)</h4>\n";
  print "Here you can remove one or more questions<br>\n";
  $add = "";
  &selecttest;
  print "<input type=\"submit\" value=\"Remove Question(s)\"></p>\n";
  print "</form>\n";

#Edit system parameters
  print "<form action=\"stud.cgi\" method=\"POST\">\n";
  print "<input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
  print "<input type=\"hidden\" name=\"action\" value=\"editsys\">\n";
  print "<h4>Edit or Add System parameters</h4>\n";
  print "Here you can specify what will happen when a test is done. Email,";
  print "and the file printed once the test is done.<br>\n";
  print "System admin email: <input type=\"text\" size=\"20\" name=\"email\" value = \"$EMAIL\"><br>\n";
  print "Level of answers: <input type=\"text\" size=\"2\" name=\"level\" value = \"$LEVEL\"><br>\n";
  $a = "checked" if $RESINFILE;
  print "Result in file or database.: <input type=\"checkbox\" $a name=\"resinfile\" value=\"ON\">Tick if result as file (then you cannot edit it)<br>\n";
  $a = "checked" if $DEBUG;
  print "Debug mode: <input type=\"checkbox\" $a name=\"debug\" value=\"ON\">Tick if debug mode<br>\n";
  $a = "checked" if $CACHE;
  print "Cache mode: <input type=\"checkbox\" $a name=\"cache\" value=\"ON\">Tick to allow cache (enables cheating, simplifies debugging)<br>\n";
  print "<input type=\"submit\" value=\"Edit System parameters\"></p>\n";
  print "</form>\n";

# Level handling
  print "<form action=\"stud.cgi\" method=\"POST\">\n";
  print "<input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
  print "<input type=\"hidden\" name=\"action\" value=\"levelnam\">\n";
  print "<h4>Edit file name given when test done for level $LEVEL</h4>\n";

  $LEVEL = 1 if $LEVEL == 0;

  for ($i = 0; $i <= $LEVEL; $i++)
  {
    $a = $i * 100 / $LEVEL;
    $b = (1 + $i) * 100 / $LEVEL - 1;
    $a = $b = 100 if $i == $LEVEL;
    print "File name printed at exam for result $a % to $b %: ";
    print "<input type=\"text\" size=\"12\" name=\"levnam$i\" value = \"$LEVNAM[$i]\"><br>\n";
  }

# Edit file names
  print "<input type=\"submit\" value=\"Edit filenames\"></p>\n";
  print "</form>\n";

  print "<form action=\"stud.cgi\" method=\"POST\">\n";
  print "<input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
  print "<input type=\"hidden\" name=\"action\" value=\"editfile\">\n";
  print "<h4>Edit file in base</h4>\n";
  print "Here you can edit a file that is given as a result of the test.\n";
  print "<input type=\"text\" size=\"12\" name=\"filename\"\"><br>\n";
  print "<input type=\"submit\" value=\"Edit resulting file\"></p>\n";
  print "</form>\n";

# Log file
  print "<form action=\"stud.cgi\" method=\"POST\">\n";
  print "<input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
  print "<input type=\"hidden\" name=\"action\" value=\"examlog\"><h4>List\n";
  print "log of students result</h4>\n";
  print "Number of entries (0 for all) <input type=\"text\" size=\"2\" name=\"entries\"\n";
  print "value=\"10\">\n";
  print "<br>Exams (check) or Test: <input type=\"checkbox\" checked name=\"exam\" value=\"ON\">remove check for regular tests.<br>\n";
  print "<input type=\"submit\" value=\"List log\"></p>\n";
  print "</form>\n";

#Password
  print "<form action=\"stud.cgi\" method=\"POST\">\n";
  print "    <input type=\"hidden\" name=\"action\" value=\"newpwd\">\n";
  print "<h4>Change password</h4>\n";
  print "<input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
  print "<pre>Password: <input type=\"password\" size=\"20\" name=\"pwd1\"> (at least 6 characters and a numeric)</pre>\n";
  print "<pre>Password: <input type=\"password\" size=\"20\" name=\"pwd2\"> (repeat once again)</pre>\n";
  print "<br><input type=\"submit\" value=\"New Password\"></p>\n";
  print "</form>\n";
  print "\n";


  print "\n";
  print "<p>&nbsp;</p>\n";
  print "\n";
  print "<p><a href=\"./\">Return to student Index</a>.</p>\n";
  print "</body>\n";
  print "</html>\n";
}

sub editq_msg
{
  print "<head>\n";
  print "<meta http-equiv=\"Content-Type\"\n";
  print "content=\"text/html; charset=iso-8859-1\">\n";
  print "<meta name=\"GENERATOR\" content=\"Martin Gren CGI Handler 1.1\">\n";
  print "<title>Student Database - Adm page</title>\n";
  print "</head>\n";
  print "\n";
  print "<body bgcolor=\"#FFFFFF\">\n";
  print "\n";
  print "<h1>Edit/Add question subtest $subtest question $qnum</h1>\n";
  print "\n";
  print "<p>Here you may edit questions of the subtest. If there are no\n";
  print "questions right now, simply fill it in. You may use HTML, but be\n";
  print "careful! All questions are multi choice, with 0 or 5 correct\n";
  print "answers. The answers have to be answered exactly like the teacher\n";
  print "sais to be correct.</p>\n";
  print "\n";
  print "<form action=\"stud.cgi\" method=\"POST\">\n";
  print "    <input type=\"hidden\" name=\"pwd\" value = \"$pwd\">\n";
  print "    <input type=\"hidden\" name=\"subtest\" value = \"$subtest\">\n";
  print "    <input type=\"hidden\" name=\"qnum\" value = \"$qnum\">\n";
  print "    <input type=\"hidden\" name=\"action\" value=\"modifyq\"><h4>Question</h4>\n";

  print "    <p>Fill in the Questions, using HTML.</p>\n";
  print "    <p><textarea name=\"Q\" rows=\"4\" cols=\"80\">$Q</textarea></p>\n";

  for ($i = 1; $i <= 5; $i++)
  {
    print "<h4>Answer multi $i:</h4>\n";
    $a = "";
    $a = "checked" if $f[$i] eq "ON";
    print "<input type=\"checkbox\" $a name=\"f$i\" value=\"ON\">Tick if answer\n";
    print "is OK.</p>\n";
    print "Fill in this answer, you may use HTML.</p>\n";
    print "<textarea name=\"ans$i\" rows=\"2\" cols=\"40\">$a[$i]</textarea></p>\n";
  }

  print "<input type=\"submit\" value=\"Add/edit Question\"></p>\n";
  print "</form>\n";
  print "<a href=\"./\">Return to student Index</a>.</p>\n";
  print "</body>\n";
  print "</html>\n";
}

# List options to select a test.
sub selecttest
{
  print "<select name = \"subtest\">\n";
  for ($i = 1; ($s = $stud{"subn$i"}) ne ""; $i++)
  {
    print "<option value = \"$i\">$s\n";
  }
  print "<option value = \"$i\">$add\n" if ($add ne "");
  print "</select>\n";
  print "<br>\n";
}
