<?php
class CONSTITUENT {
	function instructions() {
	?>
		<TR><TD VALIGN=TOP>Instructions: <BR>Rank each constituent translation from Best to Worst relative to the other choices (ties are allowed). Grade <FONT style='BACKGROUND-COLOR: yellow'><b>only the highlighted part</b></FONT> of each translation.<BR>
		<I>Please note that segments are selected automatically, and they should be taken as an approximate guide.  They might include extra words, or miss words on either end that are not in the actual alignment.</I></TD><TD>
		&nbsp;
		</TD><TD>
		
		
	<?php
	}
	
	function min_item_number() {
		return get_task_property('min_constituent_number');
	}
	
	function max_item_number() {
		return get_task_property('max_constituent_number');	
	}
	
	
	function head_text() {
		return "Rank Segments";
	}
	
	
	//return judgement string, null if no judgements scored
	function process_judgement() {
		$translation_ids = get_task_property('translation_id');
		$success = false;

		$ranks = array();
		$systems = array();
		
		while (list($key, $value) = each($_POST)) {
			if (preg_match("/C_RANK_(.+)/",$key,$reg)) {
				//at least one judgement is present, mark as a successful attempt
				$success = true;
				$ranks[] = $translation_ids[$reg[1]].":".$value;
			}
		}
		
		if ($success) {
			return join(" ",$ranks);
		} else {
			return null;
		}
	}
	
	
	function is_numerical_score() {
		return true;
	}

	function process_statistics($system_score_list,$score_total) {
		//we get system->string. We return system->{score_name->value}
		
		$processed_scores = array();
		$score_avg = $score_total / sizeof($system_score_list);
		$score_adjustment = 3 - $score_avg;
			
		foreach ($system_score_list as $system => $score) {
			$score_normalized =$score + $score_adjustment;
			$processed_scores[$system]["CONSTITUENT_NORMALIZED"] = -1*$score_normalized;	
		}
		return $processed_scores;
	}
	
	function test_info($assignment_id) {	

		list($test_type_id,$assignment_number,$assignment_info_string) = split("-",$assignment_id);			
		$system_id_list = split(",",$assignment_info_string);

		$constituent_file = get_task_property('constituent');

		$constituent_info_string = display_one_line($constituent_file,$assignment_number);
		
		list($sentence_number,$constituent_type) = split(" \|\|\| ",$constituent_info_string);
		
		//FIXED TODO: the sentence numbers stored in the constituent file start at 1, not 0
		//$sentence_number--;
		
		$constituent_map = load_constituent_locations($system_id_list,$constituent_info_string);
	
		//print_r($constituent_map);
		
		print "<B>Source:</B> ".display_one_phrase(get_task_property('source'),$sentence_number,$constituent_map["src"]["start_word"],$constituent_map["src"]["end_word"]);
	
		//for($i=0;$i<count($reference);$i++) {
		//	print "<P><B>Reference:</B> <FONT COLOR=#008000>".display_one_phrase($reference[$i],$assignment_number)."</FONT>\n";
		//}
		$reference = get_task_property('reference');

		//only 1 ref for constituents
		print "<P><B>Reference:</B> <FONT COLOR=#008000>".display_one_phrase($reference[0],$sentence_number,$constituent_map["ref"]["start_word"],$constituent_map["ref"]["end_word"])."</FONT>\n";
	
		$translation_ids = get_task_property('translation_id');
		$translation_files = get_task_property('translation_file');
		?>
		<TABLE BORDER=1>
		<TR><TD><B>Translation</B></TD><TD><B>Rank</B></TD></TR>
		<?php
		foreach($system_id_list as $system_id) {
			$system_name = $translation_ids[$system_id];
			if (file_exists($translation_files[$system_id]) && array_key_exists($system_name,$constituent_map)) {
				print "<TR BGCOLOR=#F0F0FF><TD><FONT COLOR=#000080>".display_one_phrase($translation_files[$system_id],$sentence_number,$constituent_map[$system_name]["start_word"],$constituent_map[$system_name]["end_word"]);
				//print ($GLOBALS["translation_file"][$system_id]." ".$GLOBALS["translation_id"][$system_id]);
				?>
				</FONT></TD><TD NOWRAP>
				<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0><TR>
				<?php
				for($j=1;$j<=5;$j++) {
					print "<TD WIDTH=75 ALIGN=CENTER><INPUT TYPE=RADIO NAME=C_RANK_$system_id VALUE=$j></TD>\n";
				}
				print "</TR>\n<TR>";
				for($j=1;$j<=5;$j++) {
					print "<TD TD WIDTH=75 VALIGN=TOP ALIGN=CENTER>$j";
					if ($j==1) print"<BR>Best";
					if ($j==5) print"<BR>Worst";
					print "</TD>\n";
				}
				?>
				</TR></TABLE>
				</TD>
				</TD></TR>
				<?php
			}
		}

	}
	
}
?>