#!/usr/bin/perl
use CGI ':standard';

$CSS=<<END;
TABLE  { background: #ff0; border: solid black;
       }
TABLE.gr  { background: #ddd; border:0;width:70%
       }
DIV {margin: 16px 16px 16px 16px;
    }
END

$JSCRIPT=<<END;
function check() {
   if (document.eform.login.value=="" || document.eform.matr.value==""){
      alert("Bitte Login-Namen und Matrikelnummer eintragen");
      return false;
      }
      else return true;
}
END


print header(),
      start_html(-title=>'Skriptsprachen-Prüfung, Sommer 2009',
                 -script=>$JSCRIPT,
                 -style=>{'-code'=>$CSS}),
      img {src=>"http://ag-kastens.uni-paderborn.de/lehre/material/skriptsprachen/skriptsprachen.gif",
           alt=>'Skriptsprachenlogo'};

$login="";
$matr="";
$hits=0;

if (!param()) {
  print div(h2('Informationen zu den Skriptsprachen-Projekten'));
  show_form();
  print end_html();
  }
else {
# alle csv-dateien einlesen
readdata();
# query-parameter

$login=escapeHTML(param("login"));
$matr=escapeHTML(param("matr"));
# print "Query $login $matr\n";

if ($matr && $matr != $matrs{$login}) {
  print h3("Sie haben die falsche Matrikelnummer eingegeben"),p;
  show_form();
  print end_html();
}
else {
counthits();
$email=$login.'@'.$mailservs{$login};
$project=$projects{$login};

if (!$project) {
   $project = h4("Sie sind nicht für ein Skriptsprachenprojekt angemeldet.");
}
else {
   if ($project == 999) {
      $project = h4("Sie sind für ein Skriptsprachenprojekt angemeldet.").
                 h4("Die Projekte werden am 3. Juli 2009 zugeteilt.");
     }
else {
   $project = 
table({-class=>gr},
TR([td ({-valign=>TOP},[b('Projekt:'), $project]),
   td ({-valign=>TOP},[b('Titel:'), $titles{$project}]),
   td ({-valign=>TOP},[b('Beschreibung:'), $descrs{$project}]),
   td ({-valign=>TOP},[b('Anforderungen:'), $reqs{$project}])
   ]
  )
);
}
}

$prf=readprf($matr);

print 
div(
h3("Ihre Daten"),
table(
TR([td ({-valign=>TOP},[b('Vorname:'), $vnames{$login}]),
   td ({-valign=>TOP},[b('Name:'), $nnames{$login}]),
   td ({-valign=>TOP},[b('Matrikelnummer:'), $matrs{$login}]),
   td ({-valign=>TOP},[b('Login:'), $login]),
   td ({-valign=>TOP},[b('Email:'), $email])
   ]
  )
),
p,
h3("Ihr Projekt"),
p,
$project,
p,
h3("Ihre Pr&uuml;fung, Termin 1"),
p,
$prf
),

a({-href=>"http://ag-kastens.cs.uni-paderborn.de/cgi-bin/peter/skspv2.pl"},
  "Zur&uuml;ck"),
" / $hits",
end_html();
}
}


sub readprf {
my $matr = $_[0];

open(KLERG2009, "<klerg1.csv") or die "Can't open klerg1.csv $!";
while ($line = <KLERG2009>) {
  ($m,$name,$a1,$a2,$a3,$a4,$proj,$sum,$note) = split("#", $line);
  
  if ($matr == $m) {
    last;
  }
}
close(KLERG2009);
  if ($matr == $m) {
     return table(
TR([td ({-valign=>TOP, -align=>RIGHT},[b('Aufgabe 1: '), "$a1 Pkt"]),
   td ({-valign=>TOP, -align=>RIGHT},[b('Aufgabe 2: '), "$a2 Pkt"]),
   td ({-valign=>TOP, -align=>RIGHT},[b('Aufgabe 3: '), "$a3 Pkt"]),
   td ({-valign=>TOP, -align=>RIGHT},[b('Aufgabe 4: '), "$a4 Pkt"]),
   td ({-valign=>TOP},[b('Projekt: '), "$proj Pkt"]),
   td ({-valign=>TOP},[b('Summe: '), "$sum Pkt"]),
   td ({-valign=>TOP},[b('Note: '), b($note)])
   ]
  )
)
  }
  else {
    return "";
  }

}


sub readdata {
# teilnehmer
open(TN2009, "<tn2009.csv") or die "Can't open tn2009.csv $!";
while ($line = <TN2009>) {
  ($num, $matr,$nname,$vname,$login,$mailserv) = split(",|@", $line);
  $matrs{$login} = $matr;
  $nnames{$login} = $nname;
  $vnames{$login} = $vname;
  $mailservs{$login} = $mailserv;
}
close(TN2009);

# projekte
open(PROJ2009, "<proj2009.csv") or die "Can't open proj2009.csv $!";
while ($line = <PROJ2009>) {
  ($num, $title, $descr, $req) = split("#", $line);
  $title =~ s/'(.*)'$/$1/;  # remove surrounding 's
  $descr =~ s/'(.*)'$/$1/;  # remove surrounding 's
  $req =~ s/'(.*)'$/$1/;  # remove surrounding 's
  $titles{$num} = $title;
  $descrs{$num} = $descr;
  $reqs{$num} = $req;
  
}
close(PROJ2009);

# mapping
open(PRMAP2009, "<prmap2009.csv") or die "Can't open prmap2009.csv $!";
while ($line = <PRMAP2009>) {
  ($login, $num) = split(",", $line);
  $num =~ s/(.*)\s$/$1/;  # remove trailing whitespace
  $projects{$login} = $num;
}
close(PRMAP2009);
}

sub show_form {
    print 
      div(start_form(-name=>'eform'),
      table(
      Tr([td(["Login-Name  ",textfield(-name=>'login',
                                     -default=>$login)
	    ]
           ),
         td(["Matrikelnummer  ", password_field(-name=>'matr',
                                               -default=>$matr)
	    ]
           )
	]
      ),
      ),p,
      submit(-onClick=>"return check();"),
      end_form());			      
}

sub counthits {
 $countfile = "cnt.dat";
 open (COUNTHAND, "<$countfile");
 $hits = <COUNTHAND>;
 close (COUNTHAND);
 open (COUNTHAND, ">$countfile");
 $hits += 1;
 $hits %= 5555;
 print COUNTHAND $hits;
 close (COUNTHAND);
}
