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

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

	if($type=="csv")
	{
		$rep=new lcsv(i18n("Awards Ceremony Presentation"));
	}
	$q=mysql_query("SELECT 
				award_awards.id,
				award_awards.name,
				award_awards.presenter,
				award_awards.order AS awards_order,
				award_types.type
			FROM 
				award_awards,
				award_types
			WHERE 
					award_awards.year='".$config['FAIRYEAR']."'
				AND	award_types.year='".$config['FAIRYEAR']."'
				AND	award_awards.award_types_id=award_types.id
				AND	award_awards.excludefromac='0'
			ORDER BY awards_order");

	echo mysql_error();

	$table=array();
//		$table['header']=array(i18n("Timeslot"),i18n("Judging Team"));
	$table['widths']=array(	1,1,1,1,1,1,1,1);
	$table['dataalign']=array("left","left","left","left","left","left","left","left");

	if(mysql_num_rows($q))
	{
		while($r=mysql_fetch_object($q))
		{
			$award=$r->name;
			$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.registrations_id AS reg_id,
						projectcategories.category,
						projectdivisions.division
					FROM 
						award_prizes 
						LEFT JOIN winners ON winners.awards_prizes_id=award_prizes.id
						LEFT JOIN projects ON projects.id=winners.projects_id
						LEFT JOIN projectcategories ON projects.projectcategories_id=projectcategories.id
						LEFT JOIN projectdivisions ON projects.projectdivisions_id=projectdivisions.id
					WHERE 
						award_awards_id='$r->id' 
						AND award_prizes.year='".$config['FAIRYEAR']."'
						AND award_prizes.excludefromac='0'
						AND projectcategories.year='".$config['FAIRYEAR']."'
						AND projectdivisions.year='".$config['FAIRYEAR']."'
					ORDER BY 
						projectcategories.id,projectdivisions.id,
						`order`");
					echo mysql_error();
			$prevprizeid=-1;
			while($pr=mysql_fetch_object($pq))
			{
				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.= ")";
					
					}
					$prevprizeid=$pr->id;
				}

				if($pr->projectnumber)
				{
					$sq=mysql_query("SELECT students.firstname,
								students.lastname,
								students.schools_id,
								schools.school,
								schools.board,
								schools.district,
								schools.postalcode,
								schools.city
							FROM
								students,
								schools
							WHERE
								students.registrations_id='$pr->reg_id'
								AND students.schools_id=schools.id
							");

					$students="       Students: ";
					$studnum=0;
					$student1="";
					$student2="";
					while($studentinfo=mysql_fetch_object($sq))
					{
						if($studnum==0) $student1="$studentinfo->firstname $studentinfo->lastname";
						if($studnum==1) $student2="$studentinfo->firstname $studentinfo->lastname";
						if($studnum>0) $students.=", ";
						$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;
						$schoolboard=$studentinfo->board;
						$schooldistrict=$studentinfo->district;
						$schoolpostalcode=$studentinfo->postalcode;
						$schoolcity=$studentinfo->city;
					}
				}
				else
				{
				}


/*
				$table['data'][]=array(i18n("Division"),$proj->division);
				$table['data'][]=array(i18n("Category"),$proj->category);
				$table['data'][]=array(i18n("Prize"),$prizetext);
				$table['data'][]=array(i18n("Proj #"),$pr->projectnumber);
				$table['data'][]=array(i18n("Project Title"),$pr->title);
				$table['data'][]=array(i18n("Name 1"),$student1);
				$table['data'][]=array(i18n("Name 2"),$student2);
				$table['data'][]=array(i18n("School"),$school->school);
				*/
				$table['data'][]=array($pr->division,
							i18n($pr->division,array(),array(),"fr"),
							"$pr->category / ".i18n($pr->category,array(),array(),"fr"),
							$award,
							$prizetext,
							$pr->projectnumber,
							$pr->title,
							$student1,
							$student2,
							$school,
							$schoolcity,
							$schoolboard,
							$schoolpostalcode
							);
			}
		}

	}
	$rep->addTable($table);
	$rep->output();
?>