<?

function judges_scheduler_check_timeslots()
{
	global $config;

	$q=mysql_query("SELECT * FROM judges_timeslots WHERE ".
		" year='".$config['FAIRYEAR']."'".
		" AND allowdivisional='yes'" );
	$rows = mysql_num_rows($q);

	return $rows;
}

function judges_scheduler_check_awards()
{
	global $config;

	$q=mysql_query("SELECT * FROM projectdivisions WHERE year='".$config['FAIRYEAR']."' ORDER BY id");
	while($r=mysql_fetch_object($q))
		$div[$r->id]=$r->division;

	$q=mysql_query("SELECT * FROM projectcategories WHERE year='".$config['FAIRYEAR']."' ORDER BY id");
	while($r=mysql_fetch_object($q))
		$cat[$r->id]=$r->category;

	$dkeys = array_keys($div);
	$ckeys = array_keys($cat);

	if($config['filterdivisionbycategory']=="yes") {
		$q=mysql_query("SELECT * FROM projectcategoriesdivisions_link WHERE year='".$config['FAIRYEAR']."' ORDER BY projectdivisions_id,projectcategories_id");
		$divcat=array();
		while($r=mysql_fetch_object($q)) {
			$divcat[]=array("c"=>$r->projectcategories_id,"d"=>$r->projectdivisions_id);
		}

	}
	else {
		$divcat=array();
		foreach($dkeys AS $d) {
			foreach($ckeys AS $c) {
				$divcat[]=array("c"=>$c,"d"=>$d);
			}
		}
	}


	$missing_awards = array();
	foreach($divcat AS $dc) {
		$d=$dc['d'];
		$c=$dc['c'];
			$q=mysql_query("SELECT award_awards.id FROM 
							award_awards,
							award_awards_projectcategories,
							award_awards_projectdivisions
						WHERE 
							award_awards.year='{$config['FAIRYEAR']}'
							AND award_awards_projectcategories.year='{$config['FAIRYEAR']}'
							AND award_awards_projectdivisions.year='{$config['FAIRYEAR']}'
							AND award_awards.id=award_awards_projectcategories.award_awards_id
							AND award_awards.id=award_awards_projectdivisions.award_awards_id
							AND award_awards_projectcategories.projectcategories_id='$c'
							AND award_awards_projectdivisions.projectdivisions_id='$d'
							AND award_awards.award_types_id='1'
						");
						echo mysql_error();
			if(mysql_num_rows($q)!=1) {
				$missing_awards[] = "{$cat[$c]} - {$div[$d]} (".i18n("%1 found",array(mysql_num_rows($q))).")";
			}
	}
	return $missing_awards;
}


function judges_scheduler_check_jdivs()
{
	global $config;

	$q=mysql_query("SELECT DISTINCT jdiv_id FROM judges_jdiv ");
	$rows = mysql_num_rows($q);

	return $rows;
}


function judges_scheduler_check_judges()
{
	global $config;
	$ok = 1;

	$jdiv = array();
	$q=mysql_query("SELECT * FROM judges_jdiv ORDER BY jdiv_id");
	while($r=mysql_fetch_object($q)) {
		/* Ignore any div/cat with jdiv 0 */
		if($r->jdiv_id == 0) continue;

		$d = $r->projectdivisions_id;
		$c = $r->projectcategories_id;
		$l = $r->lang;

		$qp = mysql_query("SELECT COUNT(projects.id) as cnt FROM projects, registrations WHERE ".
						" projects.year='".$config['FAIRYEAR']."' AND ".
						" projectdivisions_id='$d' AND ".
						" projectcategories_id='$c' AND ".
						" language='$l' AND " .
						" registrations.id = projects.registrations_id " .
						getJudgingEligibilityCode()
				);
		$qr = mysql_fetch_object($qp);
		
		$jdiv[$r->jdiv_id]['num_projects'] += $qr->cnt;
		
	}

	$totalteams = 0;
	print("<table width=75%><tr><th></th>".
		"<th>".i18n("Projects")."</th>".
		"<th>".i18n("Required Teams")."</th></tr>");
	$keys = array_keys($jdiv);
	for($k=0; $k<count($keys); $k++) {
		$jdiv_id = $keys[$k];
		$c = $jdiv[$jdiv_id]['num_projects'];
		$t=ceil($c/$config['max_projects_per_team']*$config['times_judged']);
		if($t < $config['times_judged'] && $c>0) $t = $config['times_judged'];
		$jdiv[$jdiv_id]['num_jteams'] = $t;
		$totalteams+=$t;

		print("<tr><td>Judging Division Group $jdiv_id</td><td align=center>$c</td><td align=center>$t</td></tr>");
	}
	print("</table>");

	echo "<b>";
	echo "Total judging teams required: $totalteams";
	echo "<br />";
	$minjudges=($totalteams*$config['min_judges_per_team']);
	$maxjudges=($totalteams*$config['max_judges_per_team']);
	echo "Minimum number of judges required: $minjudges";
	echo "<br />";
	echo "Maximum number of judges acceptable: $maxjudges";
	echo "<br />";

/*	$jq=mysql_query("SELECT COUNT(judges.id) AS num FROM judges,judges_years WHERE complete='yes' AND deleted='no' AND judges_years.year='{$config['FAIRYEAR']}' AND judges_years.judges_id=judges.id");
	$jr=mysql_fetch_object($jq);
	$currentjudges=$jr->num;*/
	/* FIXME: this his highly inefficient :), but won't be done very often */
	$judges = judges_load_all();
	$currentjudges = count($judges);
	echo "Current number of registered judges: $currentjudges";
	echo "<br />";
	echo "<br />";
	echo "</b>";
	if($currentjudges<$minjudges)
	{
		echo error(i18n("You do not have sufficient number of judges based on your parameters"));
		echo "&nbsp;&nbsp;";
		echo "<a href=\"judges_schedulerconfig.php\">".i18n("Update Scheduler Configuration")."</a> (".i18n("or get more judges!").")";
		$ok=0;
	}
	else
		echo happy(i18n("You have a sufficient number of judges based on your parameters"));

	//now check if we can find a divisional award for each division and category
	return $ok;
}

?>
