<?php
if($_REQUEST['school_term'])
{
	$mps_array = db_request(db_execute("SELECT SCHOOL_TERM_ID,TITLE,SORT_ORDER,SCHOOL_YEAR FROM ".$_REQUEST['school_term']." ORDER BY SCHOOL_YEAR DESC,SORT_ORDER ASC"),array(),array('SCHOOL_TERM_ID'));
/*
echo"<pre>";
print_r($mps_array);
echo "</pre>";
*/
	foreach($mps_array as $mp_id=>$mp)
	{
		$mp_list .= ",'$mp_id'";
	}
	$mp_list = substr($mp_list,1);
}
elseif($_REQUEST['search_servicefunction']=='list' && !$_SYSTEM['modules_search'])
	BackPrompt(_('You must choose a school term'));
//die('bb');

$addition['SELECT'] = ",sg1.SCHOOL_YEAR,sg1.GRADE_TYPE_ID,sg1.SCHOOL_TERM_ID,sg1.COURSE_TITLE,sg1.COURSE_NUM,sg1.GRADE_TITLE,sg1.PERCENT_GRADE,sg1.COMMENT,sg1.TEACHER,sg1.LOCATION_TITLE,sg1.GRADELEVEL_TITLE,sg1.CREDITS,sg1.STUDENT_ID,sgc.WEIGHTED_GPA,sgc.GPA,sgc.CLASS_RANK,sg1.CARRIES_CREDITS,sg1.GRAD_SUBJECT_ID,ssr.COURSE_RANK,ssr.LOW_SCORE".CCAT."'/'".CCAT."ssr.HIGH_SCORE AS LOW_HIGH,round(((SELECT sum(sg2.PERCENT_GRADE) FROM STUDENT_GRADES_CARD sg2 WHERE sg1.COURSE_ID=sg2.COURSE_ID)/(SELECT count(sg3.STUDENT_ID) FROM STUDENT_GRADES_CARD sg3 WHERE sg1.COURSE_ID=sg3.COURSE_ID)),2) AS COURSE_AVG";
$addition['FROM'] = ",STUDENT_GRADES_CARD sg1 LEFT OUTER JOIN STUDENT_GPA_TOTALS sgc ON (sgc.STUDENT_ID=sg1.STUDENT_ID AND CAST(sgc.SCHOOL_TERM_ID AS TEXT)=sg1.SCHOOL_TERM_ID) LEFT OUTER JOIN STUDENTS_SCORES_RATING ssr ON (sg1.STUDENT_ID=ssr.STUDENT_ID AND sg1.COURSE_ID=ssr.COURSE_ID AND sg1.SCHOOL_TERM_ID=ssr.SCHOOL_TERM_ID),".$_REQUEST['school_term'].' mp';
$addition['WHERE'] = " AND sg1.SCHOOL_TERM_ID IN (".$mp_list.") AND sg1.STUDENT_ID=ssm.STUDENT_ID AND CAST(mp.SCHOOL_TERM_ID AS TEXT)=sg1.SCHOOL_TERM_ID";
$addition['ORDER'] = ',sg1.SCHOOL_YEAR,mp.SORT_ORDER,sg1.COURSE_TITLE';
$addition['group'] = array('STUDENT_ID','SCHOOL_YEAR','SCHOOL_TERM_ID');
//die('ll');
$stu_count_array = db_request(db_execute("SELECT COUNT(*) AS COUNT,CLASSLEVEL_ID FROM STUDENT_REGISTRATION WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."' AND ('".get_db_date()."' BETWEEN START_DATE AND END_DATE OR END_DATE IS NULL) GROUP BY CLASSLEVEL_ID"),array(),array('CLASSLEVEL_ID'));
/*Widgets('course');
Widgets('gpa');
Widgets('class_rank');
Widgets('letter_grade');
*/
Widgets('all');

$addition['second_col'] .= '<fieldset style="border:1px solid gray;"><legend>'._('Include in Transcripts').'</legend><TABLE class=\'transcript\'>';
$cnt = 0;
$addition['second_col'] .= "<TR><TD colspan=2><TABLE>";
	$addition['second_col'] .= "<TR>";
	$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[grade] id=options'.$cnt.' value=Y CHECKED><label for=options'.$cnt++.'>'._('Grade').'</label></TD>';
	$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[percent] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('Percent Grade').'</label></TD>';
	$addition['second_col'] .= "</TR><TR>";
	$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[comments] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('Comments').'</label></TD>';
	$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[teacher] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('Teacher Name').'</label></TD>';
	$addition['second_col'] .= "</TR><TR>";
	$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[school] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('School Name').'</label></TD>';
	$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[grade_level] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('Class Level').'</label></TD>';
	if (SystemPreferences('USE_CREDITS')=='Y')
	{	
		$addition['second_col'] .= "</TR><TR>";
		$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[credits] id=options'.$cnt.' value=Y CHECKED><label for=options'.$cnt++.'>'._('Course Credits').'</label></TD>';
		$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[carried_credits] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('Carried Credits') .'</label></TD>';
	}
	$addition['second_col'] .= "</TR><TR>";
	$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[subject] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('Subject Name').'</label></TD>';
	$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[course_num] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('Course Number').'</label></TD>';
	$addition['second_col'] .= "</TR><TR>";
	$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[course_rank] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('Course Rank').'</label></TD>';
	$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[low_high] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('Lowest & Highest Scores').'</label></TD>';
	$addition['second_col'] .= "</TR><TR>";
	$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[grade_descr] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('Grade Description').'</label></TD>';
	$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[course_avg] id=options[course_avg] value=Y><label for=options[course_avg]>'._('Course Average').'</label></TD>';
	$addition['second_col'] .= "</TR><TR>";
//	$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[graduation_reqs] value=Y CHECKED>'._('Credits Summary').'</TD>';
//	$addition['second_col'] .= '<TD></TD>';
	$addition['second_col'] .= '<TD colspan=2><BR>'._('Term Totals').':</TD>';
	if(SystemPreferences('WEIGHTED_GPA')=='Y')
	{
		$addition['second_col'] .= "</TR><TR>";
		$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[gpa] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('GPA').'</label></TD>';
		$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[weighted_gpa] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('Weighted GPA').'</label></TD>';
		$addition['second_col'] .= "</TR><TR>";
		$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[cumulative_gpa] id=options'.$cnt.' value=Y CHECKED><label for=options'.$cnt++.'>'._('Cumulative GPA').'</label></TD>';
		$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[cumulative_weighted_gpa] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('Cumulative Weighted GPA').'</label></TD>';
		$addition['second_col'] .= "</TR><TR>";
	}
	else
	{
		$addition['second_col'] .= "</TR><TR>";
		$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[gpa] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('GPA').'</label></TD>';
		$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[cumulative_gpa] id=options'.$cnt.' value=Y CHECKED><label for=options'.$cnt++.'>'._('Cumulative GPA').'</label></TD>';
		$addition['second_col'] .= "</TR><TR>";
	}	
	if (SystemPreferences('USE_CREDITS')=='Y')
	{
		$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[term_credits] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('Credits').'</label></TD>';
		$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[cumulative_credits] id=options'.$cnt.' value=Y CHECKED><label for=options'.$cnt++.'>'._('Cumulative Credits').'</label></TD>';
	}
	$addition['second_col'] .= "</TR><TR>";
	$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[class_rank] id=options'.$cnt.' value=Y CHECKED><label for=options'.$cnt++.'>'._('Student Rating in Class').'</label></TD>';
	$addition['second_col'] .= '<TD><INPUT type=checkbox name=options[average_percent_grade] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('Average Percent Grade').'</label></TD>';
	$addition['second_col'] .= "</TR><TR>";
	$addition['second_col'] .= '<TD COLSPAN=2><INPUT type=checkbox name=options[average_letter_grade] id=options'.$cnt.' value=Y><label for=options'.$cnt++.'>'._('Average Letter Grade').'</label></TD>';
	$addition['second_col'] .= '</TR>';


$addition['second_col'] .= '</TABLE></TD></TR>';

$addition['second_col'] .= '<TR><TD align='.ALIGN_RIGHT.'>'._('School Term').'</TD><TD><TABLE>';
$addition['second_col'] .= '<TR><TD><INPUT type=radio name=school_term id=options'.$cnt.' value=SCHOOL_YEARS><label for=options'.$cnt++.'>'._('Full Year').'</label></TD><TD><INPUT type=radio name=school_term id=options'.$cnt.' value=SCHOOL_SEMESTERS CHECKED><label for=options'.$cnt++.'>'._('Semesters').'</label></TD><TD><INPUT type=radio name=school_term id=options'.$cnt.' value=SCHOOL_QUARTERS><label for=options'.$cnt++.'>'._('Quarters').'</label></TD></TR>';

$addition['second_col'] .= "</TABLE>";
$addition['secont_col'] .= "</TD>";
$addition['second_col'] .= "</TR></TABLE></fieldset>";

$addition['second_col'] .= '<CENTER><TABLE width=100%>';
$addition['second_col'] .= '<TR><TD align=left><INPUT type=radio name=columns id=options'.$cnt.' value=one><label for=options'.$cnt++.'>' . _('One Column') . '</label></TD><TD><INPUT type=radio name=columns id=options'.$cnt.' value=multiple CHECKED><label for=options'.$cnt++.'>' . _('Multiple Columns') . '</label></TD></TR>';
$addition['second_col'] .= "</TABLE><hr></CENTER>";
$addition['second_col'] .= '<TABLE align=left><TR><TD width=150 align=left>'._('Page Orienation').'</TD><TD><SELECT name=orient> <OPTION value=auto>'._('Auto').'</OPTION>';
$addition['second_col'] .= '<OPTION value=portrait>'._('Portrait').'</OPTION>';
$addition['second_col'] .= '<OPTION value=landscape>'._('Landscape').'</OPTION>';
$addition['second_col'] .= '</SELECT></TD></TR>';
//Grading scale selector
$grading_scale_array = db_request(db_execute("SELECT ID,TITLE FROM GRADES_CARD_TYPES WHERE SCHOOL_YEAR = ".get_school_year()." AND SCHOOL_ID = ".get_school_id()));
foreach($grading_scale_array as $num=>$garray)
        $goptions .= '<OPTION value='.$garray['ID'].'>'.$garray['TITLE'].'</OPTION>';
        $addition['second_col'] .= '<TR><TD width=150 align=left>'._('Grading scale').'</TD><TD><SELECT name=grading_scale> <OPTION value=auto>'._('Auto').'</OPTION>';
        $addition['second_col'] .= $goptions;
        $addition['second_col'] .= '</SELECT></TD></TR></TABLE><BR>';
Widgets('document_template');

//$addition['action'] .= "&CREATEPDF=true";
$addition['force_search'] = true;
$addition['search_button_title'] = _('Next');
//var_dump($_REQUEST);
if((!$_REQUEST['search_servicefunction'] || $_REQUEST['search_servicefunction']=='search' || $_SYSTEM['modules_search']) && $_REQUEST['CREATEPDF'] != 'true')
//if(!$_REQUEST['search_servicefunction'] || $_SYSTEM['modules_search'])
{
	DisplayHeader(".:".ProgramTitle().":.");

	$addition['new'] = true;
	Search('student_id',$addition);
}
elseif($_REQUEST['CREATEPDF'] != 'true')
{
	DisplayHeader(".:".ProgramTitle().":.");
	Widgets('all');
    
	//if($_REQUEST['servicefunction']!='gradelist')
        //        ;//$addition['action'] .= "&CREATEPDF=true";
        //            else
        //                    $addition['action'] .= '&servicefunction=gradelist';
        $addition['force_search'] = true;
        $addition['search_button_title'] = _('Next');
        $addition['link']['FULL_NAME'] = false;
        $addition['SELECT'].=",'' as CHECKBOX, CFIELD_50012";
        $addition['columns_before'] = array('CHECKBOX'=>'</A><INPUT type=checkbox value=Y name=controller onclick="checkAll(this.form,this.checked,\''.$html_field_name.'\');CheckAll_hilite(this.form,this.form.controller.checked,\''.$html_field_name.'\');"><A>');
        $addition['functions']=array('CHECKBOX'=>'_makeChooseCheckbox1');
        $addition['options']['onclick']="check_clicked(this);";
}
//die('dd');
//if(($_REQUEST['search_servicefunction']=='list' || $_REQUEST['search_servicefunction']=='search') && $_REQUEST['preview'] != 'true')
if(($_REQUEST['search_servicefunction']=='list') && $_REQUEST['preview'] != 'true' && $_REQUEST['CREATEPDF'] != 'true')
{   
//die('hh');
echo "<form name=letters_form method=post action='".SetRequestOptions()."&CREATEPDF=true' enctype=multipart/form-data>";
	DisplayHeader('','<INPUT TYPE=button value="'._('Preview').'" onclick="this.form.target=\'_blank\';this.form.preview.value=\'true\';this.form.submit();">'.' &nbsp; <INPUT type=submit value="'._('Print').'" id="top_submit_button" onclick="this.form.target=\'blank\';this.form.preview.value=\'false\';"">'/*.' &nbsp; <INPUT type=submit name=printandlog value="'._('Print Letters & Add Log Entries for Each Student').'" id="top_submit_button_1" onclick="this.form.target=\'\';this.form.preview.value=\'false\';"">'*/);//Print Letters & add Log Entries for Each Student
        echo '<HR>';
	echo '<INPUT type=hidden name=preview value="false">';
//die('mm');
//        var_dump($_REQUEST[$html_field_name]);
	if(!isset($_REQUEST[$html_field_name])) //(!$_REQUEST['search_servicefunction'] || $_REQUEST['search_servicefunction']=='search' || $_SYSTEM['modules_search']) && $_REQUEST['delete']!='true')
        {
	        $addition['new'] = true;
                Search('student_id',$addition);
        }
	echo "</FORM>";
//        var_dump($_POST);
//die();
}
//else
//die('bb');
if($_REQUEST['CREATEPDF'] == 'true' || $_REQUEST['preview'] == 'true')
{
//var_dump($_POST);
//echo "sdffsdfsdfsdfsdf";
//var_dump($_REQUEST);
//die('jj');
	$addition['functions']['CARRIES_CREDITS'] = '_makeCarriedCredits';
	$addition['functions']['GRAD_SUBJECT_ID'] = '_makeGradSubject';
	$array = get_student_list($addition);
	$grade_descr = db_request(db_execute("SELECT SCHOOL_YEAR,SCALE_ID,TITLE,DESCRIPTION FROM GRADES_CARD_SETTINGS"),array(),array('SCHOOL_YEAR','SCALE_ID','TITLE'));

	if($_REQUEST['options']['class_rank'] || $_REQUEST['options']['average_percent_grade'] || $_REQUEST['options']['term_credits'] || $_REQUEST['options']['cumulative_credits'] || $_REQUEST['options']['gpa'] || $_REQUEST['options']['weighted_gpa'] || $_REQUEST['options']['cumulative_gpa'] || $_REQUEST['options']['cumulative_weighted_gpa'])
	{
		unset($addition);
		$addition['SELECT_ONLY'] .= 'ssm.STUDENT_ID,sgc.SCHOOL_TERM_ID,sgc.GPA,sgc.WEIGHTED_GPA,sgc.CUMULATIVE_GPA,sgc.CUMULATIVE_WEIGHTED_GPA,sgc.CLASS_RANK,'."(SELECT AVG(asrcg.PERCENT_GRADE)/100 FROM STUDENT_GRADES_CARD asrcg WHERE asrcg.STUDENT_ID=ssm.STUDENT_ID AND asrcg.SCHOOL_TERM_ID=CAST(sgc.SCHOOL_TERM_ID AS TEXT)) AS AVERAGE_PERCENT_GRADE,
			    (SELECT AVG(asrcg.PERCENT_GRADE)/100 FROM STUDENT_GRADES_CARD asrcg WHERE asrcg.STUDENT_ID=ssm.STUDENT_ID AND asrcg.SCHOOL_TERM_ID=CAST(sgc.SCHOOL_TERM_ID AS TEXT)) AS AVERAGE_LETTER_GRADE,
			    (SELECT SUM(asrcg.CREDITS) FROM STUDENT_GRADES_CARD asrcg WHERE asrcg.STUDENT_ID=ssm.STUDENT_ID AND asrcg.CARRIES_CREDITS='Y' AND asrcg.SCHOOL_TERM_ID=CAST(sgc.SCHOOL_TERM_ID AS TEXT)) AS CREDITS,(SELECT SUM(asrcg.CREDITS) FROM STUDENT_GRADES_CARD asrcg WHERE asrcg.STUDENT_ID=ssm.STUDENT_ID AND asrcg.CARRIES_CREDITS='Y' AND SCHOOL_TERM_ID IN (".$mp_list.")) AS CUMULATIVE_CREDITS";
		$addition['functions']['AVERAGE_PERCENT_GRADE'] = 'Percent';
		$addition['functions']['AVERAGE_LETTER_GRADE'] = 'getLetterGrade';
		$addition['functions']['GPA'] = $addition['functions']['WEIGHTED_GPA'] = $addition['functions']['CUMULATIVE_GPA'] = $addition['functions']['CUMULATIVE_WEIGHTED_GPA'] = '_roundGPA';

		if(strpos($addition['FROM'],'STUDENT_GPA_TOTALS sgc')===false)
		{
			$addition['FROM'] .= " LEFT OUTER JOIN STUDENT_GPA_TOTALS sgc ON (sgc.STUDENT_ID=ssm.STUDENT_ID)";
		}
		$addition['group'][] = 'STUDENT_ID';
		$addition['group'][] = 'SCHOOL_TERM_ID';
		Widgets('all');
		
		$gpa_array = get_student_list($addition);
    //print_r($addition);
	}


	if(count($array))
	{
		$s_pdf_options = "--webpage --quiet -t pdf12 --no-links --footer t --header . --left 0.5in --top 0.5in ".SystemPreferences("pdf_options", "grades");
		if($_REQUEST['school_term']=='SCHOOL_QUARTERS' && $_REQUEST['columns']=='multiple')
		{
			if($_REQUEST['orient'] == 'landscape' || $_REQUEST['orient'] == 'auto')
				$s_pdf_options .= " --landscape";
			else
				$s_pdf_options .= " --portrait";
		}
		else
		{
			if($_REQUEST['orient'] == 'portrait' || $_REQUEST['orient'] == 'auto')
				$s_pdf_options .= " --portrait";
			else
				$s_pdf_options .= " --landscape";
		}
		$handle = PDFStart($s_pdf_options);

		$columns = array();
		$columns['COURSE_TITLE'] = _('Course Name');
		if($_REQUEST['options']['course_num'])
			$columns['COURSE_NUM'] = _('Course Number');

		if($_REQUEST['options']['school']=='Y')
			$columns['LOCATION_TITLE'] = _('School Name');
		if($_REQUEST['options']['grade_level']=='Y')
			$columns['GRADELEVEL_TITLE'] = _('Class Level');
		if($_REQUEST['options']['subject']=='Y')
			$columns['GRAD_SUBJECT_ID'] = _('Subject Name');
		
		if($_REQUEST['options']['grade'])
			$columns['GRADE_TITLE'] = _('Grade');
		if($_REQUEST['options']['percent'])
			$columns['PERCENT_GRADE'] = _('Percentage');
		if($_REQUEST['options']['credits'] && $_REQUEST['options']['carried_credits'])
			$columns['CREDITS'] = _('Course Credits');
		elseif($_REQUEST['options']['credits'])
			$columns['CREDITS'] = _('Credits');
		if($_REQUEST['options']['carried_credits'])
			$columns['CARRIES_CREDITS'] = _('Carried Credits');
		if($_REQUEST['options']['comments'])
			$columns['COMMENTS'] = _('Comments');
		if($_REQUEST['options']['course_rank'])
			$columns['COURSE_RANK'] = _('Course Rank');
		if($_REQUEST['options']['low_high'])
			$columns['LOW_HIGH'] = _('L/H Scores %');
		if($_REQUEST['options']['course_avg'])
			$columns['COURSE_AVG'] = _('Course Avg');

		if($_REQUEST['options']['grade_descr'])
			$columns['GRADE_DESCR'] = _('Grade Description');
			
		
		$total_width = 100;
		foreach($columns as $col=>$title)
		{
			$widths[$col] = '15%';
			$total_widths -= 15;
		}
		$widths['COURSE_TITLE'] = ($total_width+15).'%';

		$Gridoutput_options = array('print'=>false,'cellpadding'=>0,'center'=>false,'widths'=>$widths);
		if($_REQUEST['columns']=='one')
			$Gridoutput_options['width'] = '70';
		foreach($array as $student_id=>$school_years)
		{
//			$_SYSTEM['student_arrays'] = array($student_id=>'Y');
    			unset($_SYSTEM['DisplayHeader']);
			$_REQUEST['DrawAlign'] = 'ALIGN_LEFT';
			DisplayHeader(_('Student Transcript'),null,null,null,null,null,null,true);
			$student = $school_years[key($school_years)][key($school_years[key($school_years)])][1];
			DisplayHeader($student['FULL_NAME'].' ('.$student['STUDENT_ID'].')');
			DisplayHeader(get_school(get_school_id()));
			unset($_REQUEST['DrawAlign']);
			if($_REQUEST['columns']=='multiple')
				echo "<TABLE width=100%>";
			else
				echo '<BR>';
			foreach($school_years as $school_year=>$school_terms)
			{
				if($_REQUEST['columns']=='multiple')
					echo "<TR>";

				$sort_array = array();
				foreach($school_terms as $school_term_id=>$sort)
					$sort_array[] = get_school_term($school_term_id,'SORT_ORDER');
				if(count($school_terms)>1)
					array_multisort($sort_array,SORT_ASC,SORT_NUMERIC,$school_terms);
				
				foreach($school_terms as $key=>$courses)
				{
//					if($_REQUEST['columns']=='multiple')
//						echo '<TD valign=top>';
					$title = $school_year.((SystemPreferences('SCHOOL_YEAR_TITLE')=='YYYY-YYYY')?'-'.($school_year+1):'').' ';
					$school_term_id = $courses[1]['SCHOOL_TERM_ID'];
					if(get_school_term($courses[1]['SCHOOL_TERM_ID'],'MP_TABLE')!='SCHOOL_YEARS')
						$title .= get_school_term($school_term_id);
					$columns['COURSE_TITLE'] = '<i>'.$title.'</i>';
					$lines = 0;
					if($_REQUEST['options']['grade_descr'])
					{
						for($c_index=0;$c_index<count($courses);$c_index++)
							$courses[$c_index]['GRADE_DESCR'] = $grade_descr[$courses[$c_index]['SCHOOL_YEAR']][$courses[$c_index]['GRADE_TYPE_ID']][$courses[$c_index]['GRADE_TITLE']][1]['DESCRIPTION'];
					}			
					if($_REQUEST['columns']=='one')
					{
						if($_REQUEST['options']['average_percent_grade'])
							$lines++;
						if($_REQUEST['options']['cumulative_credits'] || $_REQUEST['options']['term_credits'])
							$lines++;
						if($_REQUEST['options']['gpa'] || $_REQUEST['options']['weighted_gpa'])
							$lines++;
						if($_REQUEST['options']['cumulative_gpa'] || $_REQUEST['options']['cumulative_weighted_gpa'])
							$lines++;
                      	echo '<!-- NEED '.(count($courses)+$lines).' -->';
                    

					}
					
					// Output the information for the student
					Gridoutput($courses,$columns,'','',array(),array(),$Gridoutput_options);
					
					if($_REQUEST['options']['average_percent_grade'] || $_REQUEST['options']['term_credits'] || $_REQUEST['options']['gpa'] || $_REQUEST['options']['weighted_gpa'] || $_REQUEST['options']['cumulative_gpa'] || $_REQUEST['options']['cumulative_weighted_gpa'] || $_REQUEST['options']['class_rank'])
					{
						if($_REQUEST['columns']=='one')
							echo '<TABLE width=50%>';
						else
							echo '<TABLE width=100%>';

						if($_REQUEST['options']['average_percent_grade'])
						{
							echo "<TR>";
							echo '<TD><small>'._('Average Percent Grade').': '.$gpa_array[$student_id][$school_term_id][1]['AVERAGE_PERCENT_GRADE'].'</small></TD>';
							echo '</TR>';
						}
						if($_REQUEST['options']['average_letter_grade'])
						{
							echo "<TR>";
							echo '<TD><small>'._('Average Letter Grade').': '.$gpa_array[$student_id][$school_term_id][1]['AVERAGE_LETTER_GRADE'].'</small></TD>';
							echo '</TR>';
						}
						if($_REQUEST['options']['class_rank'])
						{
							echo "<TR>";
							echo '<TD><small>'._('Student Rating in Class').': '.$gpa_array[$student_id][$school_term_id][1]['CLASS_RANK'].'</small></TD>';
							echo '</TR>';
						}
						

						if($_REQUEST['options']['cumulative_credits'] || $_REQUEST['options']['term_credits'])
						{
							echo "<TR>";
							if($_REQUEST['options']['cumulative_credits'])
								echo '<TD><small>'._('Cumulative Credits').': '.$gpa_array[$student_id][$school_term_id][1]['CUMULATIVE_CREDITS'].'</small></TD>';
							if($_REQUEST['options']['term_credits'])
								echo '<TD><small>'._('Credits this term').': '.$gpa_array[$student_id][$school_term_id][1]['CREDITS'].'</small></TD>';
							echo '</TR>';							
						}
						if($_REQUEST['options']['gpa'] || $_REQUEST['options']['weighted_gpa'])
						{
							echo "<TR>";
							if($_REQUEST['options']['gpa'])
								echo '<TD><small>'._('GPA').': '.$gpa_array[$student_id][$school_term_id][1]['GPA'].'</small></TD>';
							if($_REQUEST['options']['weighted_gpa'])
								echo '<TD><small>'._('Weighted GPA').': '.$gpa_array[$student_id][$school_term_id][1]['WEIGHTED_GPA'].'</small></TD>';
							echo '</TR>';
						}
						if($_REQUEST['options']['cumulative_gpa'] || $_REQUEST['options']['cumulative_weighted_gpa'])
						{
							echo "<TR>";
							if($_REQUEST['options']['cumulative_gpa'])
								echo '<TD><small>'._('Cumulative GPA').': '.$gpa_array[$student_id][$school_term_id][1]['CUMULATIVE_GPA'].'</small></TD>';
							if($_REQUEST['options']['cumulative_weighted_gpa'])
								echo '<TD><small>'._('Cumulative Weighted GPA').': '.$gpa_array[$student_id][$school_term_id][1]['CUMULATIVE_WEIGHTED_GPA'].'</small></TD>';
							echo '</TR>';
						}

						echo '</TABLE>';
					}
					else
						echo '<BR>';
//					if($_REQUEST['columns']=='multiple')
//							echo '</TD>';
				}
				if($_REQUEST['columns']=='multiple')
					echo '</TR>';
			}
			if($_REQUEST['columns']=='multiple')
				echo "</TABLE>";
			
			DrawTemplateHeaders($student_id,'footer');
			echo "<!-- NEW PAGE -->";
			//unset($_SYSTEM['DisplayHeader']);
		}
		if($_REQUEST['preview'] != 'true')
			PDFStop($handle);
	}
	else
		BackPrompt(_('No Students were found.'));
}

function _roundGPA($gpa,$column)
{
	return round($gpa,SystemPreferences('ROUND_GPA_POINTS'));
}

function _makeCarriedCredits($value,$column)
{	global $THIS_array;

	if($value === 'Y')
		return $THIS_array['CREDITS'];
	else
		return 0;
}

function _makeGradSubject($value,$column)
{	global $THIS_array,$grad_subjects_array;

	if(!is_array($grad_subjects_array[get_school_id()]))
		$grad_subjects_array[get_school_id()] = db_request(db_execute("SELECT ID,SHORT_NAME FROM GRADUATION_REQ_SUBJECTS WHERE SCHOOL_ID='".get_school_id()."'"),array(),array('ID'));

	return $grad_subjects_array[get_school_id()][$value][1]['SHORT_NAME'];
}
function _makeChooseCheckbox1($value,$title='')
{       global $THIS_array, $html_field_name;

        $student_id_field="STUDENT_ID";
        
        return "<INPUT onchange='hl_cb(this)' type=checkbox name=".$student_id_field."[".$THIS_array[$student_id_field]."] value=Y>";
}
                
?>