<?php

//cat through all tasks, get assigned units, compare with finished, show info

$annotator_tasks=array();
global $total_tasks;
global $LANG_CODES;
$total_tasks=array();

load_global_task_info();
$line = load_judgements();
$current_user = current_user();

for($i=0;$i<count($line);$i++) {
	list($t,$j,$assignmentid,$type,$judgment,$time) = split(" \|\|{2,3} ",$line[$i]);
	if ($j == $current_user) {
		if (!$annotator_tasks[$t]) {
			$annotator_tasks[$t] = array('unique_assigned'=>array(),
							'unique_completed'=>array(),
							'total_assigned'=>0,
							'total_completed'=>0,
							'total_time'=>0,
							'count_time'=>0);
		}
		$annotator_tasks[$t]['unique_completed'][$assignmentid]++;
		$annotator_tasks[$t]['total_completed']++;
		if ($time>0) { 
			$annotator_tasks[$t]['total_time']+= $time; 
			$annotator_tasks[$t]['count_time']++; 
		}
	}
	if(!$total_tasks[$t]) {
		$total_tasks[$t] = array('total_completed'=>0,
								 'num_systems'=>count(get_global_task_property("translation_id",null,false,$t)),
								 'num_tests'=>count(get_global_task_property("test_types",null,false,$t)));
	}
	$total_tasks[$t]['total_completed'] += count(split(' ',$judgment));
	//$total_tasks[$t]['total_completed']++;
}

$line = file(get_assignments_file());
for($i=0;$i<count($line);$i++) {
	list($t,$annotator,$assignment_list_string) = split(" \|\|\| ",chop($line[$i]));
	if ($annotator == $current_user) {
		$assignment_list_temp = split(";",$assignment_list_string);				
		foreach ($assignment_list_temp as $assignmentid) {
			$annotator_tasks[$t]['unique_assigned'][$assignmentid]++;
			$annotator_tasks[$t]['total_assigned']++;

		}
	}			
}


?>

<?php

function priority_sort($a,$b) {
	global $total_tasks;
	if ($a == $b) {
		return 0;
	}
//	print($total_tasks[$a]['total_completed']." ".$total_tasks[$b]['total_completed']);
	list($foo,$bar,$a_type) = split(" ",$a);
	list($foo,$bar,$b_type) = split(" ",$b);
	if ($a_type == $b_type) {
		return (($total_tasks[$a]['total_completed']/$total_tasks[$a]['num_systems']/$total_tasks[$a]['num_tests'])-($total_tasks[$b]['total_completed']/$total_tasks[$b]['num_systems']/$total_tasks[$b]['num_tests']));
	} else {
		return strcmp($b_type,$a_type);
	}
}

usort($task,priority_sort);

$annotators = file(get_annotators_file());
$language_str;
foreach ($annotators as $annotator) {
	list($name,$ignored1,$ignored2,$ignored3,$language_str) = split(' \|\|\| ',$annotator);
	if ($name == $current_user) 	break ;
}
$language_list = split(' ',trim($language_str));
$lang_hash = array();
foreach ($language_list as $lang_code) {
	$lang_hash[$LANG_CODES[trim($lang_code)]] = 1;
}

$spoken_tasks = array();
$other_tasks = array();

foreach ($task as $task_item) {
	list($setup,$pair,$corpus) = split(' ',$task_item);
	list($source_lang,$target_lang) = split('-',$pair);
	if ($lang_hash[$target_lang]) {
		$spoken_tasks[] = $task_item;
	} else {
		$other_tasks[] = $task_item;
	}
}


function print_task_entries($task,$annotator_tasks,$total_tasks) {

	for ($i=0;$i<count($task);$i++) {
		?>
		<tr><td valign="middle" style="padding:3px;"> 
		<FORM ACTION="do_task.php" METHOD=POST style="display:inline;">
		<INPUT TYPE=HIDDEN NAME=TASK VALUE="<?php print($task[$i]);?>">
		<INPUT TYPE=SUBMIT VALUE="<?php print($task[$i]);?>">
		<?php
		if ($annotator_tasks[$task[$i]]['total_assigned'] - $annotator_tasks[$task[$i]]['total_completed'] <= 0) {
			?><INPUT TYPE=HIDDEN NAME=GENERATEASSIGNMENT VALUE="TRUE"><?php
		}
		?></FORM>
		</td><td valign="middle">
	
		<?php
		if($annotator_tasks[$task[$i]]['total_assigned'] > 0) {
			print "<FONT COLOR=#800000>You have completed ".($annotator_tasks[$task[$i]]['total_completed']?$annotator_tasks[$task[$i]]['total_completed']:"0")." screens out of ".$annotator_tasks[$task[$i]]['total_assigned']." assigned";
			if ($annotator_tasks[$task[$i]]['total_completed'] > 0) {
				//print ", taking ".number_format($annotator_tasks[$task[$i]]['total_time']/$annotator_tasks[$task[$i]]['count_time'],1)." seconds per sentence";
			}
			print ".<BR></FONT>";
		}
		if ($total_tasks[$task[$i]]['num_systems'] > 0) print "<FONT COLOR=blue> All users have contributed ".$total_tasks[$task[$i]]['total_completed']." datapoints, averaging ".number_format($total_tasks[$task[$i]]['total_completed']/$total_tasks[$task[$i]]['num_systems']/$total_tasks[$task[$i]]['num_tests'],1)." per system per test type.</FONT>";
		
			
			
		?>
		</td></tr>
		<tr height="1" bgcolor="black"><td colspan=2></td></tr>
		<?php
		
	}
	
}
?>

<table cellpadding="0">
<tr><td colspan=2>
Tasks in languages you speak:
</td></tr>
<tr height="1" bgcolor="black"><td colspan=2></td></tr>
<?php print_task_entries($spoken_tasks,$annotator_tasks,$total_tasks);?>
<tr><td colspan=2>
<br><br>All other tasks:
<tr height="1" bgcolor="black"><td colspan=2></td></tr>

</td></tr>
<?php print_task_entries($other_tasks,$annotator_tasks,$total_tasks);




?>
</table>