<?php

	$requests_array = db_request(db_execute("SELECT r.COURSE_ID".CCAT."'_'".CCAT."r.COURSE_CREDIT_TITLE AS CRS,
					r.COURSE_ID,r.COURSE_CREDIT_TITLE,cp.COURSE_SECTION_ID,
					c.TITLE AS COURSE_TITLE,cp.SCHOOL_PERIOD_ID,
					(cp.TOTAL_SEATS-cp.FILLED_SEATS) AS OPEN_SEATS,s.STUDENT_ID AS SCHEDULED
				FROM STUDENT_SCHEDULE_REQUESTS r,
				COURSES c,SCHOOL_PERIODS sp,
				COURSE_SECTIONS cp LEFT OUTER JOIN STUDENT_SCHEDULE s ON 
					(s.COURSE_SECTION_ID=cp.COURSE_SECTION_ID AND s.STUDENT_ID='".get_student_id()."')
				WHERE 
					r.SCHOOL_YEAR='".get_school_year()."' AND r.SCHOOL_ID='".get_school_id()."'
					AND r.COURSE_ID=cp.COURSE_ID AND c.COURSE_ID=cp.COURSE_ID
					AND r.STUDENT_ID='".get_student_id()."'
					AND sp.SCHOOL_PERIOD_ID=cp.SCHOOL_PERIOD_ID
					ORDER BY ".db_case(array('s.STUDENT_ID',"''","NULL",'sp.SORT_ORDER'))."
				"),array(),array('CRS','SCHOOL_PERIOD_ID'));

	$periods_array = db_request(db_execute("SELECT SCHOOL_PERIOD_ID,SHORT_NAME FROM SCHOOL_PERIODS WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."' ORDER BY SORT_ORDER"));
	echo '<CENTER><TABLE style="border: 1px solid;">';
	echo '<TR><TD></TD>';
	foreach($periods_array as $period)
		echo '<TD><small><b>'.$period['SHORT_NAME'].'</b></small></TD>';
	foreach($requests_array as $course=>$periods)
	{
		echo '<TR><TD><small><b>'.$periods[key($periods)][1]['COURSE_TITLE'].'</b><small></TD>';
		foreach($periods_array as $period)
		{
			if($periods[$period['SCHOOL_PERIOD_ID']][1]['SCHEDULED'])
				$color = '0000FF';
			elseif($periods[$period['SCHOOL_PERIOD_ID']])
			{
				if($periods[$period['SCHOOL_PERIOD_ID']][1]['OPEN_SEATS']==0)
					$color = 'FFFF00';
				else
					$color = '00FF00';
			}
			else
				$color = 'CCCCCC';
			echo '<TD height=10 width=6 bgcolor=#'.$color.'></TD>';
		}
		
		echo '</TR>';
	}
	echo '</TABLE></CENTER>';
?>