<?
 require("../common.inc.php");
 require_once("../user.inc.php");
 user_auth_required('committee', 'admin');
 require("../lpdf.php");
 require("../lcsv.php");

 if($_GET['year']) $foryear=$_GET['year'];
 else $foryear=$config['FAIRYEAR'];

 if($_GET['awardtype']=="All") $awardtype="";
 else if($_GET['awardtype']) $awardtype=" AND award_types.type='".mysql_escape_string($_GET['awardtype'])."'";
 else $awardtype="";

 if($_GET['show_unawarded_awards']=="on") $show_unawarded_awards="yes";
 else $show_unawarded_awards="no";

 if($_GET['show_unawarded_prizes']=="on") $show_unawarded_prizes="yes";
 else $show_unawarded_prizes="no";

 $show_pronunciation= ($_GET['show_pronunciation'] == 'on') ? TRUE : FALSE;

 if(is_array($_GET['show_category'])) {
	 $show_category = array();
 	foreach($_GET['show_category'] as $id=>$val) {
		$show_category[] = "projects.projectcategories_id='$id'";
	}
	$and_categories = join(' OR ', $show_category);
 } else {
 	$and_categories = '1';
 }

 $type=$_GET['type'];
 if(!$type) $type="pdf";

	if($type=="pdf")
	{

		$rep=new lpdf(	i18n($config['fairname']),
				i18n("Awards Ceremony Script"),
				$_SERVER['DOCUMENT_ROOT'].$config['SFIABDIRECTORY']."/data/logo-200.gif"
				);

		$rep->newPage();
		$rep->setFontSize(11);
	}
	else if($type=="csv")
	{
		$rep=new lcsv(i18n("Awards Ceremony Script"));
	}
	$q=mysql_query("SELECT 
				award_awards.id,
				award_awards.name,
				award_awards.presenter,
				award_awards.description,
				award_awards.order AS awards_order,
				award_types.type,
				sponsors.organization
			FROM 
				award_awards,
				award_types,
				sponsors
			WHERE 
					award_awards.year='$foryear'
				AND	award_types.year='$foryear'
				AND	award_awards.award_types_id=award_types.id
				AND	award_awards.sponsors_id=sponsors.id
				AND	award_awards.excludefromac='0'
				$awardtype
			ORDER BY awards_order");

	echo mysql_error();

	if(mysql_num_rows($q))
	{
		while($r=mysql_fetch_object($q))
		{
			$pq=mysql_query("SELECT 
						award_prizes.prize,
						award_prizes.number,
						award_prizes.id,
						award_prizes.cash,
						award_prizes.scholarship,
						winners.projects_id,
						projects.projectnumber,
						projects.title,
						projects.projectcategories_id,
						projects.registrations_id AS reg_id
					FROM 
						award_prizes 
						LEFT JOIN winners ON winners.awards_prizes_id=award_prizes.id
						LEFT JOIN projects ON projects.id=winners.projects_id
					WHERE 
						award_awards_id='$r->id' 
						AND award_prizes.year='$foryear'
						AND award_prizes.excludefromac='0'
						AND ($and_categories)
					ORDER BY 
						`order`");
					echo mysql_error();

			if($show_unawarded_awards=="no") 
			{
				$skipAward=true;
				while($pr=mysql_fetch_object($pq))
				{
					if($pr->projectnumber)
					{
						$skipAward=false;
						break;
					}
				}
				if($skipAward)
				{
					continue;
				}
				mysql_data_seek($pq, 0);
			}
			$rep->heading("$r->name  ($r->type)");
			if($r->type!="Divisional")
				$rep->addText(i18n("Sponsored by: %1",array($r->organization)));
			if($r->presenter)
				$rep->addText(i18n("Presented by: %1",array($r->presenter)));
			if($r->description)
				$rep->addText(i18n("Description: %1",array($r->description)));

			$prevprizeid=-1;
			while($pr=mysql_fetch_object($pq))
			{
				if($pr->projectnumber || $show_unawarded_prizes=="yes")
				{
					if($prevprizeid!=$pr->id)
					{
						$prizetext=$pr->prize;
	
						if($pr->cash || $pr->scholarship)
						{
							$prizetext.=" (";
							if($pr->cash && $pr->scholarship)
								$prizetext.="\$$pr->cash cash / \$$pr->scholarship scholarship";
							else if($pr->cash)
								$prizetext.= "\$$pr->cash cash";
							else if($pr->scholarship)
								$prizetext.= "\$$pr->scholarship scholarship";
							$prizetext.= ")";
					
						}
						$rep->addText($prizetext);
	
						$prevprizeid=$pr->id;
					}

					if($pr->projectnumber)
					{
						$rep->addText( "    ($pr->projectnumber) $pr->title");

						$sq=mysql_query("SELECT students.firstname,
									students.lastname,
									students.pronunciation,
									students.schools_id,
									schools.school
								FROM
									students,
									schools
								WHERE
									students.registrations_id='$pr->reg_id'
									AND students.schools_id=schools.id
								");
	
						$students="       Students: ";
						$studnum=0;
						$pronounce = "";
						while($studentinfo=mysql_fetch_object($sq))
						{
							if($studnum>0) $students.=", ";
							$students.="$studentinfo->firstname $studentinfo->lastname";

							if($studnum>0) $pronounce .= ", ";
							$pronounce .= "\"{$studentinfo->pronunciation}\"";

							$studnum++;
							
							//we will assume that they are coming from the same school, so lets just grab the last students school
							//and use it.
							$school=$studentinfo->school;
						}

						$rep->addText($students);
						if(trim($pronounce) != '' && $show_pronunciation == TRUE)
							$rep->addText("Pronunciation: $pronounce");
						$rep->addText("       School: $school");
					}
					else
					{
						$rep->addText("    Prize not awarded");
					}
				}
			}
			$rep->nextLine();
		}

	}
	$rep->output();
?>
