<?php

$periods_array = db_request(db_execute("SELECT SCHOOL_PERIOD_ID,TITLE FROM SCHOOL_PERIODS WHERE SCHOOL_ID='".get_school_id()."' AND SCHOOL_YEAR='".get_school_year()."' ORDER BY SORT_ORDER "));

DisplayHeader(".:".ProgramTitle().":.");

$columns = array('TITLE'=>_('Room'),'SEATS'=>_('Seats'));

foreach($periods_array as $period)
{
	$select .= ',NULL AS PERIOD_'.$period['SCHOOL_PERIOD_ID'];
	$columns['PERIOD_'.$period['SCHOOL_PERIOD_ID']] = $period['TITLE'];
	$functions['PERIOD_'.$period['SCHOOL_PERIOD_ID']] = '_preparePeriods';
}

$singular = _('room');
$plural = _('rooms');

$courses_array = db_request(db_execute("SELECT cp.COURSE_SECTION_ID,cp.SCHOOL_PERIOD_ID,cp.TITLE,cp.ROOM,c.TITLE FROM COURSES c,COURSE_SECTIONS cp WHERE cp.COURSE_ID=c.COURSE_ID AND c.SCHOOL_YEAR='".get_school_year()."' AND c.SCHOOL_ID='".get_school_id()."'"),null,array('ROOM','SCHOOL_PERIOD_ID'));

$rooms_array = db_request(db_execute("SELECT ID,TITLE,SEATS,SHORT_NAME".$select." FROM RESBASE WHERE TYPE='Y'"));
foreach($rooms_array as $i=>$room)
{
	foreach($periods_array as $period)
	{
		if($courses_array[$room['ID']][$period['SCHOOL_PERIOD_ID']])
		{
			$rooms_array[$i]['PERIOD_'.$period['SCHOOL_PERIOD_ID']] = $courses_array[$room['SHORT_NAME']][$period['SCHOOL_PERIOD_ID']];
		}
	}
//		if(count($schedule_array[$student['STUDENT_ID']])!=count($periods_array))
//			$bad_students[] = $student;
}

Gridoutput($rooms_array,$columns,$singular,$plural);


function _preparePeriods($value,$name)
{	global $THIS_array,$schedule_array;

	$school_period_id = substr($name,7);
	if(!$schedule_array[$THIS_array['STUDENT_ID']][$school_period_id])
		return '<IMG SRC=images/x.gif>';
	else
		return '';
}
?>