<?php

function head($title) {
  print "<HTML><HEAD><TITLE>$title</TITLE></HEAD>
<BODY>
<H2>$title</H2>\n";
}

function tail() {
  print "<HR SIZE=1><A HREF=\"?\">start over</A>\n&nbsp; <A HREF=\"?STAT=1\">stats</A>\n";
  if ($_SERVER["SCRIPT_NAME"] != "/wmt06/shared-task/judge/index.php") {
    print "&nbsp; see also: /home/pkoehn/statmt/httpd/html/judge\n";
  }
  print "</BODY></HTML>\n";
}

function start() {
  global $task,$system,$annotator;

  head("Select Annotation Task");
  print "<P><FONT COLOR=RED>This is a demo version, using WPT 2005 submissions. Feel free to play around.</FONT><P>";
  print "<FORM ACTION=\"\" METHOD=POST>";
  print "<B>Task:</B> <SELECT NAME=TASK>";
  for ($i=0;$i<count($task);$i++) {
    print "<OPTION>$task[$i]\n";
  }
  print "</SELECT>";

  print "<P><B>Login:</B>
<TABLE BGCOLOR=lightgrey>
<TR><TD>Annotator:</TD><TD><SELECT NAME=ANNOTATOR>";
  for ($i=0;$i<count($annotator);$i++) {
    print "<OPTION>$annotator[$i]\n";
  }
  print "</SELECT></TD></TR>
<TR><TD>Password:</TD><TD><INPUT TYPE=PASSWORD SIZE=10 NAME=PWD></TD></TR>
<TR><TD><INPUT TYPE=SUBMIT VALUE=\"Login\"></TD></TR>
</TABLE>
<P>
<B>Register new annotator:</B>
<TABLE BGCOLOR=lightgrey>
<TR><TD>Login name:</TD><TD><INPUT NAME=NEW_ANNOTATOR SIZE=20></TD></TR>
<TR><TD>Password:</TD><TD><INPUT TYPE=PASSWORD SIZE=10 NAME=NEW_PWD></TD></TR>
<TR><TD>Email:</TD><TD><INPUT SIZE=30 NAME=EMAIL></TD></TR>
<TR><TD>Conflict:</TD><TD><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0><TR><TD><SELECT NAME=CONFLICT[] MULTIPLE SIZE=5>";
  for ($i=0;$i<count($system);$i++) {
    print "<OPTION>$system[$i]\n";
  }
  print "</SELECT></TD><TD>&nbsp;&nbsp;</TD><TD>Do not judge your own submission.<BR>If you have multiple conflicts,<BR>use CTRL and click.</TD></TR></TABLE></TD></TR>
<TR><TD COLSPAN=2><INPUT TYPE=SUBMIT VALUE=\"Register\"></TD></TR>
</TABLE>\n</FORM>\n";
  tail();
}

function display_sentence() {
  global $translation,$translation_file,$translation_name,$source,$reference;

  head("Judge Sentence");
  $sentence_number = get_new_sentence();

  if ($sentence_number == -1) { print "DONE"; tail(); return; }
  print "<B>Source:</B> ".display_one_line($source,$sentence_number);
  for($i=0;$i<count($reference);$i++) {
    print "<P><B>Reference:</B> <FONT COLOR=#008000>".display_one_line($reference[$i],$sentence_number)."</FONT>\n";
  }
  print "<FORM ACTION=\"\" METHOD=POST>
<INPUT TYPE=HIDDEN NAME=SENTENCE_NUMBER VALUE=$sentence_number>
<INPUT TYPE=HIDDEN NAME=TIME VALUE=".time().">
<TABLE BORDER=1>
<TR><TD><B>Translation</B></TD><TD><B>Adequacy</B></TD><TD><B>Fluency</B></TD></TR>\n";
  for($ii=0;$ii<count($translation) && $ii<$GLOBALS["max_per_page"];$ii++) {
    $i = $translation[$ii];
    print "<TR BGCOLOR=#F0F0FF><TD><FONT COLOR=#000080>".display_one_line($translation_file[$i],$sentence_number);
    print "</FONT></TD><TD NOWRAP>";
    print "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0><TR>";
    for($j=1;$j<=5;$j++) {
      print "<TD><INPUT TYPE=RADIO NAME=ADEQUACY_$translation[$i] VALUE=$j></TD>\n";
    }
    print "</TR>\n<TR>";
    for($j=1;$j<=5;$j++) {
       print "<TD ALIGN=CENTER>$j</TD>\n";
    }
    print "</TR></TABLE>";
    print "</TD><TD NOWRAP>";
    print "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0><TR>";
    for($j=1;$j<=5;$j++) {
      print "<TD><INPUT TYPE=RADIO NAME=FLUENCY_$translation[$i] VALUE=$j></TD>\n";
    }
    print "</TR>\n<TR>";
    for($j=1;$j<=5;$j++) {
       print "<TD ALIGN=CENTER>$j</TD>\n";
    }
    print "</TR></TABLE>";
    print "</TD></TR>\n";
  }
  print "
<TR><TD><P><FONT COLOR=#800000><B>Annotator:</B> $_POST[ANNOTATOR]</B>
<INPUT TYPE=HIDDEN NAME=ANNOTATOR VALUE=\"$_POST[ANNOTATOR]\">
<INPUT TYPE=HIDDEN NAME=PWD VALUE=\"$_POST[PWD]\">
<B>Task:</B> $_POST[TASK]</B>
<INPUT TYPE=HIDDEN NAME=TASK VALUE=\"$_POST[TASK]\"></FONT>
</TD><TD COLSPAN=2 ALIGN=RIGHT><INPUT TYPE=SUBMIT VALUE=\"Annotate\"></TD></TR>
<TR><TD>Instructions</TD><TD>
5= All Meaning<BR>
4= Most Meaning<BR>
3= Much Meaning<BR>
2= Little Meaning<BR>
1= None
</TD><TD>
5= Flawless English<BR>
4= Good English<BR>
3= Non-native English<BR>
2= Disfluent English<BR>
1= Incomprehensible
</TABLE>
</FORM>

";
  tail();
}

function display_one_line($file,$n) {
  $line = file($file);
  return $line[$n];
}

function process_judgements() {
  global $translation_id;
  $types = array("ADEQUACY","FLUENCY");
  $f = fopen("judgements","a");
  while (list($key, $value) = each($_POST)) {
    foreach ($types as $type) {
      if (preg_match("/${type}_(.+)/",$key,$reg)) {
	fputs($f,"$_POST[TASK] ||| $_POST[ANNOTATOR] ||| $_POST[SENTENCE_NUMBER] ||| ".$translation_id[$reg[1]]." ||| $type ||| $value ||| ".(time()-$_POST[TIME])."\n");
      }
    }
  }
  fclose($f);
}

function get_new_sentence() {

  $line = file("judgements");
  for($i=0;$i<count($line);$i++) {
    list($t,$j,$number,$system,$type,$judgment,$time) = split(" \|\|{2,3} ",$line[$i]);
    if ($t == $_POST["TASK"] &&
  	$j == $_POST["ANNOTATOR"]) {
      $already[$number] = 1;
      if ($time>0) { $total_time += $time; $count_time++; }
    }
  }
  
  print "You have already judged ".count($already)." of ".$GLOBALS["max_sentence_number"]." sentences";
  if ($count_time>0) {  print ", taking ".number_format($total_time/$count_time,1)." seconds per sentence"; }
  print ".<P>\n";
  if (count($already) == $GLOBALS["max_sentence_number"]) { return -1; }
  do { 
    $n = rand(0,$GLOBALS["max_sentence_number"]-1); 
  } while($already[$n]);
  return $n;
}

function init() {
  global $task,$system,$annotator,$annotator_new,$annotator_pwd,$annotator_email,$annotator_conflict;

  // general task info
  $line = file("task");
  for($i=0;$i<count($line);$i++) {
    $line[$i] = chop($line[$i]);
    list($t,$key,$value,$value2) = split(" \|\|\| ",$line[$i]);
    if (!$already[$t]) {
      $already[$t] = 1;
      $task[] = $t;
    }
    if ($key == "translation" && !$already_system[$value2]) {
      $already_system[$value2] = 1;
      $system[] = $value2;
    }
  }

  // annotators
  $line = file("annotator");
  for($i=0;$i<count($line);$i++) {
    $line[$i] = chop($line[$i]);
    list($name,$pwd,$email,$conflict) = split(" \|\|\| ",$line[$i]);
    $annotator[] = $name;
    $annotator_pwd[$name] = $pwd;
    $annotator_email[$name] = $email;
    $c = split(" ",$conflict);
    for($j=0;$j<count($c);$j++) { 
      $annotator_conflict[$name][$c[$j]] = 1;
    }
    $annotator_registered[$name] = 1;
  }
}

function load_task_info() {
  global $annotator_conflict;

  $line = file("task");
  $index = 0;
  for($i=0;$i<count($line);$i++) {
    list($t,$key,$value,$value2) = split(" \|\|\| ",chop($line[$i]));
    if ($t == $_POST["TASK"]) {
      if ($key == "max_sentence_number" || 
	  $key == "max_per_page" ||
	  $key == "source") {
	$GLOBALS[$key] = $value;
      }
      else if ($key == "reference") {
	$GLOBALS[$key][] = $value;
      }
      else if ($key == "translation" && 
	       ! $annotator_conflict[$_POST["ANNOTATOR"]][$value2]) {
	$GLOBALS["translation_file"][] = $value;
	$GLOBALS["translation_id"][] = $value2;
	$GLOBALS["translation"][] = $index++;
      }
    }
  }
  shuffle($GLOBALS["translation"]);
}

function new_annotator() {
  global $annotator_registered;

  head("New annotator");
  
  if ($annotator_registered[$_POST["NEW_ANNOTATOR"]]) {
    print "An annotator with the name ".$_POST["NEW_ANNOTATOR"]." already exists, please go back.";
  }
  
  else if (! $_POST["NEW_PWD"]) {
    print "No password entered, please go back.";
  }

  else {
    $f = fopen("annotator","a");
    $email = $_POST["EMAIL"];
    if ($email == "") { $email = "none"; }
    $conflict = join(" ",$_POST["CONFLICT"]);
    if ($conflict == "") { $conflict = "none"; }
    fputs($f,$_POST["NEW_ANNOTATOR"]." ||| ".$_POST["NEW_PWD"]." ||| $email ||| $conflict\n");
    fclose($f);
    print "Name and password registered. <A HREF=\"\">Please login</A>.";
  }
  tail();
}

function check_password() {
  global $annotator_pwd;
  if ($annotator_pwd[$_POST["ANNOTATOR"]] == $_POST["PWD"]) {
    return;
  }
  head("Password error");
  if (! $_POST["PWD"]) {
    print "Please go back and enter password.";
  }
  else {
    print "Wrong password, please go back.";
  }
  tail();
  exit;
}

function stats() {

  head("Statistics");

  $line = file("judgements");
  for($i=0;$i<count($line);$i++) {
    list($t,$j,$number,$system,$type,$judgment,$time) = split(" \|{2,3} ",$line[$i]);
    $COUNT_TS [$t][$system][$type] += $judgment;
    $COUNT_TSJ[$t][$system][$j][$type] += $judgment;
    $TOTAL_TS [$t][$system][$type]++;
    $TOTAL_TSJ[$t][$system][$j][$type]++;
  }

  while (list($t, $dummy) = each($COUNT_TS)) {
    print "<H3>$t</H3>\n";
    print "<TABLE BORDER=1>";
    while (list($system, $dummy) = each($COUNT_TS[$t])) {
      $sys = preg_replace("/(.+\/)([^\/]+)/","<FONT SIZE=-3 COLOR=#808080>$1</FONT>$2",$system);
      print "<TR><TD>$sys</TD><TD>Adq: ".
	number_format($COUNT_TS[$t][$system]["ADEQUACY"]/$TOTAL_TS[$t][$system]["ADEQUACY"],2)
	." (".($TOTAL_TS[$t][$system]["ADEQUACY"]).")"
	."</TD><TD>Flcy: ".
	number_format($COUNT_TS[$t][$system]["FLUENCY"]/$TOTAL_TS[$t][$system]["FLUENCY"],2)
	." (".($TOTAL_TS[$t][$system]["FLUENCY"]).")"
	."</TD></TR>\n";      
    }
    print "</TABLE>\n<P>Per annotator: <TABLE BORDER=1>";
    while (list($system, $dummy) = each($COUNT_TSJ[$t])) {
      $sys = preg_replace("/(.+\/)([^\/]+)/","<FONT SIZE=-3 COLOR=#808080>$1</FONT>$2",$system);
      while (list($annotator, $dummy) = each($COUNT_TSJ[$t][$system])) {
	print "<TR><TD>$sys</TD><TD>$annotator</TD><TD>Adq: ".
	  number_format($COUNT_TSJ[$t][$system][$annotator]["ADEQUACY"]/$TOTAL_TSJ[$t][$system][$annotator]["ADEQUACY"],2)
	  ." (".($TOTAL_TSJ[$t][$system][$annotator]["ADEQUACY"]).")"
	  ."</TD><TD>Flcy: ".
	  number_format($COUNT_TSJ[$t][$system][$annotator]["FLUENCY"]/$TOTAL_TSJ[$t][$system][$annotator]["FLUENCY"],2)
	  ." (".($TOTAL_TSJ[$t][$system][$annotator]["FLUENCY"]).")"
	."</TD></TR>\n";      
	$sys = "&nbsp;";
      }
    }
    print "</TABLE>\n";
  }
  tail();
}

init();

if ($_POST["NEW_ANNOTATOR"]) {
  new_annotator();
}
else if ($_POST["ANNOTATOR"] && $_POST["TASK"]) { 
  check_password();
  load_task_info();
  process_judgements();
  display_sentence(); 
}
else if ($_GET["STAT"]) {
  stats();
}
else { 
  start(); 
}
