Location: PHPKode > projects > Ludwig Course Management System > ludwig/include/lw_test_schedule.php
<?php
/*
 * contains data structure and access functions for test scheduling algorith
 *
 * @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
 * @since 04/21/2004
 */
class lw_test_schedule
{
	var $LW;
	/*
	* constructor
	*
	* @param $master 
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function lw_test_schedule($master)
	{
		$this->LW = $master;
	}
	
	/*
	* Get the time and corresponding seat numbers for the test
	*
	* @param $timeArray
	* @param $seatArray
	* @param $classTestId
	* 
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/21/2004
	*/
	function GetTimesSeats(&$timeArray, &$seatArray, $classTestId)
	{
		$lwUtils = &$this->LW->UTILS;
		$query = "select vs.start, vs.seat from lw_venue_seat_schedule vs, lw_venue_schedule v, lw_classtests CT ";
		$query.= "WHERE v.id = vs.venuescheduleid AND v.id = CT.venuescheduleid ";
		$query.= "AND CT.id = ".$lwUtils->dbStringEscape($classTestId)." AND vs.status = '[not assigned]' ";
		$query.= "ORDER BY vs.start;";
		if (!$results = mysql_query($query))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}
		else if (0 == mysql_num_rows($results))
		{
			$funRetVal = false;
		}
		else
		{
			for ($x=0; $x<mysql_num_rows($results); $x++)
			{
				$timeArray[$x] = mysql_result($results, $x, "start");
				$seatArray[$x] = mysql_result($results, $x, "seat");
			}
			$funRetVal = true;
		}
		return $funRetVal;
	}

	/*
	* Get the number of seats in the specific venue
	*
	* @param $venueId
	* @param $numSeats
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function GetNumSeats($venueId, &$numSeats)
	{
		$lwUtils = &$this->LW->UTILS;
		$query = "select seats from lw_venues where id = ".$lwUtils->dbStringEscape($venueId).";";
		if (!$results = mysql_query($query))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}
		else if (0 == mysql_num_rows($results))
		{
			$funRetVal = false;
		}
		else
		{
			$numSeats = mysql_result($results, 0, "seats");
			$funRetVal = true;
		}
		return $funRetVal;
	}

	/*
	* Get venue information for test
	*
	* @param $venueScheduleId
	* @param venueId
	* @param $start
	* @param $end
	* @param $startInterval
	* @param $betweenTests
	* @param $numSeats
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function GetVenueInfo($venueScheduleId, &$venueId, &$start, &$end, &$startInterval, &$betweenTests, &$numSeats)
	{
		$lwUtils = &$this->LW->UTILS;
		$query = "select * from lw_venue_schedule where id = ".$lwUtils->dbStringEscape($venueScheduleId).";";
		if (!$results = mysql_query($query))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}
		else if (0 == mysql_num_rows($results))
		{
			$funRetVal = false;
		}
		else
		{
			$venueId = mysql_result($results, 0, "venueid");
			if (!$this->GetNumSeats($venueId, $numSeats))
			{
				$funRetVal = false;
			}
			else
			{
				$start = mysql_result($results, 0, "start");
				$end = mysql_result($results, 0, "end");
				$startInterval = mysql_result($results, 0, "start_interval");
				$betweenTests = mysql_result($results, 0, "between_tests");
				$funRetVal = true;
			}
		}

		return $funRetVal;
	}

	/*
	* Get shortest timeallowed for scheduled venue
	*
	* @param $venueScheduleId
	* @param $testId
	* @param $timeAllowed
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function GetTestLength($venueScheduleId, $testId, &$timeAllowed)
	{
		$lwUtils = &$this->LW->UTILS;
		$query = "select min(t.timeallowed) as mintime from lw_tests t, lw_classtests ct ";
		$query.= "where t.id = ct.testid and venuescheduleid =".$lwUtils->dbStringEscape($venueScheduleId).";";
		if (!$results = mysql_query($query))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}
		else if (0 == mysql_num_rows($results))
		{
			$funRetVal = false;
		}
		else
		{
			$timeAllowed = mysql_result($results, 0, "mintime");
			$funRetVal = true;
		}

		return $funRetVal;
	}
	
	/*
	* Get the time allowed for a test
	*
	* @param $venueScheduleId
	* @param $classTestId
	* @param $testLength
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function GetTimeAllowed($venueScheduleId, $classTestId, &$testLength)
	{
		$lwUtils = &$this->LW->UTILS;
		$query = "select t.timeallowed from lw_tests t, lw_classtests ct ";
		$query.= "where t.id = ct.testid and venuescheduleid =".$lwUtils->dbStringEscape($venueScheduleId)." AND ";
		$query.= "ct.id = ".$lwUtils->dbStringEscape($classTestId).";";
					 
		if (!$results = mysql_query($query))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}
		else if (0 == mysql_num_rows($results))
		{
			$funRetVal = false;
		}
		else
		{
			$testLength = mysql_result($results, 0, "t.timeallowed");
			$funRetVal = true;
		}
		
		return $funRetVal;
	}
	
	/*
	* Increment time based on time and increment
	*
	* @param $time
	* @param $increment
	*	
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function IncrementTime($time, $increment)
	{
		$seatEndHour = 0;
		$seatEndMin = 0;
		list($startHour, $startMin, $startSec) = split(":",$time);
					 
		$seatEndHour = $startHour;
		$seatEndMin = $startMin + $increment;
		while ($seatEndMin >= 60)
		{
			$seatEndMin = $seatEndMin - 60;
			$seatEndHour++;
		}

		if (($seatEndHour < 10)&&(strlen($seatEndHour)==1))
		{
			$seatEndHour = "0".$seatEndHour;
		}

		if (($seatEndMin < 10)&&(strlen($seatEndMin)==1))
		{
			$seatEndMin = "0".$seatEndMin;
		}

		$seatEndTime = $seatEndHour.":".$seatEndMin.":".$startSec;
		
		return $seatEndTime;
	}

	/* 
	* Decrement date by 7 days
	*
	* @param $date
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function DecrementDateByWeek($date)
	{
		list($startYear, $startMonth, $startDay) = split("-",$date);
		$startDay=$startDay-7;
		if($startDay<1)
		{
			$startMonth=$startMonth-1;
			if($startMonth<1)
			{
				$startMonth=12;
				$startYear=$startYear-1;
			}
			switch($startMonth)
			{
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:
					$startDay=31+$startDay;
					break;
				case 2:
					$startDay=28+$startDay;
					break;
				default:
					$startDay=30+$startDay;
			}
		}

		if (($startMonth < 10)&&(strlen($startMonth)==1))
		{
			$startMonth = "0".$startMonth;
		}
		if (($startDay < 10) &&(strlen($startDay)==1))
		{
			$startDay = "0".$startDay;
		}

		$startDate = $startYear."-".$startMonth."-".$startDay;
		return $startDate;
	}

	/*
	* Updates the time_allotted field when a test is added or deleted
	*
	* @param $testId
	* @param $venueScheduleId
	* @param $semesterClassId
	* @param $addOrDelete
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function UpdateTimeAllotted($testId, $venueScheduleId, $semesterClassId,$addOrDelete) 
	{ 
		$lwUtils = &$this->LW->UTILS; 
		$query1 = "select count(*) as num_students from lw_students where "; 
		$query1 .= "semesterclassid = ".$lwUtils->dbStringEscape($semesterClassId).";"; 
		$query2 = "select timeallowed from lw_tests where id =".$lwUtils->dbStringEscape($testId).";"; 
		$query3 = "select start_interval, between_tests, time_allotted from lw_venue_schedule where id = ".$lwUtils->dbStringEscape($venueScheduleId).";"; 
	
		if ((!$results = mysql_query($query1)) || (!$results = mysql_query($query2)) ||(!$results = mysql_query($query3))) 
		{ 
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error(); 
			reportError($msg, __FILE__, __LINE__); 
		} 
		else if (0 == mysql_num_rows($results)) 
		{ 
			$funRetVal = false; 
		} 
		else 
		{ 
			$numStudents = mysql_result($results, 0, "num_students"); 
			$test_length = mysql_result($results, 0, "timeallowed"); 

			$startInterval = mysql_result($results, 0, "start_interval"); 
			$betweenTests = mysql_result($results, 0, "between_tests"); 
			$timeAllotted = mysql_result($results, 0, "time_allotted"); 
			if($addOrDelete)
			{
				$timeAllotted += ($numStudents * ($test_length + $betweenTests + $startInterval)); 
			}
			else  
			{
				$timeAllotted -= ($numStudents * ($test_length + $betweenTests + $startInterval)); 
			}
			$query = "update lw_venue_schedule set time_allotted = "; 
			$query .= $timeAllotted; 
			$query .= " where id = "; 
			$query .= $lwUtils->dbStringEscape($venueScheduleId); 
			$query .= ";"; 

			if (!$results = mysql_query($query)) 
			{ 
				$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error(); 
				reportError($msg, __FILE__, __LINE__); 
			} 
			else 
			{
				$funRetVal = true; 
			}

		} 
		return $funRetVal;
	} 

	/*
	* Increment date by 7 days
	*
	* @param $date
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004			
	*/
	function IncrementDateByWeek($date)
	{
		list($startYear, $startMonth, $startDay) = split("-",$date);
		$startDay=$startDay+7;
		$daysInMonth=31;
		switch($startMonth)
		{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				break;
			case 2:
				$daysInMonth=28;
				break;
			default:
				$daysInMonth=30;
				break;
		}

		if($startDay > $daysInMonth)
		{
			$startDay=$startDay-$daysInMonth;

			if($startMonth!=12)
			{
				$startMonth++;
			}
			else
			{
				$startMonth=1;
				$startYear++;
			}
		}
		
		if (($startMonth < 10)&&(strlen($startMonth)==1))
		{
			$startMonth = "0".$startMonth;
		}

		if (($startDay < 10) &&(strlen($startDay)==1))
		{
			$startDay = "0".$startDay;
		}

		$startDate = $startYear."-".$startMonth."-".$startDay;
		return $startDate;
	}

	/*
	* Determines if seat end time is <= than test end time
	*
	* @param $seatEndTime
	* @param $endTime
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004	
	*/
	function IsTimeLessEqual($seatEndTime,$endTime)
	{
		list($seatEndHour, $seatEndMin) = split(":",$seatEndTime);
		list($end_hour, $end_min) = split(":",$endTime);

		if ($seatEndHour > $end_hour)
		{
			$funRetVal = false;
		}
		else
		{
			$funRetVal = true;
			if (($seatEndHour == $end_hour)&&($seatEndMin > $end_min))
			{
				$funRetVal = false;
			}
		}

		return $funRetVal;
	}

	/*
	* Create Initial Test Schedule based on inputs in lw_venue_seat_schedule
	* 
	* @param $venueScheduleId
	* @param $start
	* @param $end
	* @param $numSeats
	* @param $timeAllotted
	* @param $startInterval
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function CreateInitialTestSchedule($venueScheduleId, $start, $end, $numSeats, $timeAllotted, $startInterval)
	{
		$lwUtils = &$this->LW->UTILS;
		$seat = 1;
		list($startDate, $startTime) = split(" ",$start, 2);
		list($endDate, $endTime) = split(" ", $end, 2);

		$seatStartTime = $startTime;
		$seatEndTime = $this->IncrementTime($startTime, $timeAllotted);

		while ($this->IsTimeLessEqual($seatEndTime,$endTime))
		{
			$seatStart = $startDate." ".$seatStartTime;
			$seatEnd = $startDate." ".$seatEndTime;

			$query = "INSERT INTO lw_venue_seat_schedule (venuescheduleid, seat, start, end) VALUES (";
			$query.= $lwUtils->dbStringEscape($venueScheduleId).",";
			$query.= $lwUtils->dbStringEscape($seat).",";
			$query.= "'".$lwUtils->dbStringEscape($seatStart)."',";
			$query.= "'".$lwUtils->dbStringEscape($seatEnd)."');";

			if (!$results = mysql_query($query))
			{
				$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
				reportError($msg, __FILE__, __LINE__);
			}

			$seatStartTime = $seatEndTime;
			$seatEndTime = $this->IncrementTime($seatStartTime, $timeAllotted);
		}

		return true;
	}

	/*
	* Determine if seat schedules exist for a venue schedule
	*
	* @param $venueScheduleId
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function ExistsSchedule($venueScheduleId)
	{
		$lwUtils = &$this->LW->UTILS;
		$query = "select id from lw_venue_seat_schedule ";
		$query.= "where venuescheduleid = ".$lwUtils->dbStringEscape($venueScheduleId).";";

		if (!$results = mysql_query($query))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}
		else if (0 == mysql_num_rows($results))
		{
			$funRetVal = false;
		}
		else
		{
			$funRetVal = true;
		}
		
		return $funRetVal;
	}

	/*
	* Delete all seat schedules with venuescheduleid = $venuescheduelid
	*
	* @param $venueScheduleId
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function DeleteSchedule($venueScheduleId)
	{
		$lwUtils = &$this->LW->UTILS;
		$query = "delete from lw_venue_seat_schedule ";
		$query.= "where venuescheduleid = ".$lwUtils->dbStringEscape($venueScheduleId).";";
					 
		if (!$results = mysql_query($query))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}

		return true;
	}
	
	/*
	* Compare Start time against other seats
	*
	* @param $venueScheduleId
	* @param $start
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function CompareStartTimes($venueScheduleId, $start)
	{
		$lwUtils = &$this->LW->UTILS;
		$query = "SELECT id from lw_venue_seat_schedule ";
		$query.= "where start = '".$lwUtils->dbStringEscape($start)."';";
		       
		if (!$results = mysql_query($query))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}
		else if (0 == mysql_num_rows($results))
		{
			$funRetVal = true;
		}
		else
		{
			$funRetVal = false;
		}
		return $funRetVal;
	}

	/*
	* Determine if seat schedule will fit
	*
	* @param $venueScheduleId
	* @param $seat
	* @param $start
	* @param $end
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function TimeAvailable($venueScheduleId, $seat, $start, $end)
	{
		$lwUtils = &$this->LW->UTILS;
		$query = "SELECT id from lw_venue_seat_schedule ";
		$query.= "where ((start >= '".$lwUtils->dbStringEscape($start)."' AND start < '".$lwUtils->dbStringEscape($end)."') ";
		$query.= "OR (end > '".$lwUtils->dbStringEscape($start)."' AND end <= '".$lwUtils->dbStringEscape($end)."')) AND ";
		$query.= "seat = ".$lwUtils->dbStringEscape($seat).";";

		if (!$results = mysql_query($query))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}
		else if (0 == mysql_num_rows($results))
		{
			$funRetVal = true;
		}
		else
		{
			$funRetVal = false;
		}
		return $funRetVal;
	}
	
	/*
	* Determine if seat schedule will fit
	*
	* @param $venueScheduleId
	* @param $seat
	* @param $start
	* @param $end
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function TestTimeAvailable($venueScheduleId, $seat, $start, $end)
	{
		$lwUtils = &$this->LW->UTILS;
		$query = "SELECT id from lw_venue_seat_schedule ";
		$query.= "where ((start >= '".$lwUtils->dbStringEscape($start)."' AND start < '".$lwUtils->dbStringEscape($end)."') ";
		$query.= "OR (end > '".$lwUtils->dbStringEscape($start)."' AND end <= '".$lwUtils->dbStringEscape($end)."')) AND ";
		$query.= "seat = ".$lwUtils->dbStringEscape($seat)." AND status = '[assigned]';";

		if (!$results = mysql_query($query))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}
		else if (0 == mysql_num_rows($results))
		{
			$funRetVal = true;
		}
		else
		{
			$funRetVal = false;
		}
		return $funRetVal;
	}

	/*
	* Add seat schedule to database
	*
	* @param $venueScheduleId
	* @param $seat
	* @param $start
	* @param $end
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function AddSeatSchedule($venueScheduleId, $seat, $start, $end)
	{
		$lwUtils = &$this->LW->UTILS;
		$query = "INSERT INTO lw_venue_seat_schedule (venuescheduleid, seat, start, end) VALUES (";
		$query.= $lwUtils->dbStringEscape($venueScheduleId).",";
		$query.= $lwUtils->dbStringEscape($seat).",";
		$query.= "'".$lwUtils->dbStringEscape($start)."',";
		$query.= "'".$lwUtils->dbStringEscape($end)."');";

		if (!$results = mysql_query($query))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}

		return true;
	}
	
	/*
	* Get id of a venue given a venue schedule
	* 
	* @param $venueScheduleId
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function GetVenueId($venueScheduleId)
	{
		$lwUtils = &$this->LW->UTILS;
		$query = "select venueid from lw_venue_schedule where id=".$lwUtils->dbStringEscape($venueScheduleId).";";
		if (!$results = mysql_query($query))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}
		else if (0 == mysql_num_rows($results))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}
		else
		{
			return mysql_result($results, 0, "venueid");
		}
	}

	/*
	* Create time on next seat
	*
	* @param $venueScheduleId
	* @param $seat
	* @param $start
	* @param $timeAllotted
	* @param $startInterval
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function CreateTimeOnNextSeat($venueScheduleId, &$seat, $start, $timeAllotted, $startInterval)
	{
		$scheduled = false;
						
		$venueId = $this->GetVenueId($venueScheduleId);
		$this->GetNumSeats($venueId, $numSeats);
		while(!$scheduled)
		{
			list($startDate, $startTime) = split(" ",$start, 2);
			$conflict = false;
			if ($seat != $numSeats)
			{
				$seat++;

				while(!$conflict)
				{
					$startTime = $this->IncrementTime($startTime, $startInterval);
					$nextStart = $startDate." ".$startTime;
					$conflict = $this->CompareStartTimes($venueScheduleId, $nextStart);
				}

				$endTime = $this->IncrementTime($startTime, $timeAllotted);
				$end = $startDate." ".$endTime;

				list($venueEndDate, $venueEndTime) = split(" ", $this->TestEnd($venueScheduleId), 2);
				$endTimeValid = $this->IsTimeLessEqual($endTime, $venueEndTime);

				if (($this->TimeAvailable($venueScheduleId, $seat, $nextStart, $end))&&$endTimeValid)
				{
					if(!$this->AddSeatSchedule($venueScheduleId, $seat, $nextStart, $end))
					{
						$funRetVal = false;
					}
					else
					{
						$scheduled = true;
						$funRetVal = true;
					}
				}

			}
			else 
			{
				$funRetVal = false;
				$scheduled = true;
			}
		}

		return $funRetVal;
	}

	/* 
	* Check schedule for overlaps
	*
	* @param $testLength
	* @param $shortestTestLength
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function CheckForOverlaps($testLength, $shortestTestLength)
	{
		if($testLength > $shortestTestLength)
		{
			$funRetVal = true;
		}
		else
		{
			$funRetVal = false;
		}
		return $funRetVal;
	}

	/*
	* Compute the ending time of a test
	* 
	* @param $start
	* @param $testLength
	* @param $betweenTests
	* 
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function ComputeEndTime($start, $testLength, $betweenTests)
	{
		$increment = $testLength + $betweenTests;
	
		list($startDate, $startTime) = split(" ",$start, 2);
		$endTime = $this->IncrementTime($startTime, $increment);

		$end = $startDate." ".$endTime;
	
		return $end;
	}
	
	/*
	* Update the times in the schedule
	*
	* @param $start
	* @param $end
	* @param $venueScheduleId
	* @param $seat
	* @param $testLength
	* @param $betweenTests
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since  04/05/2004
	*/
	function TimeUpdater($start, $end, $venueScheduleId, $seat, $testLength, $betweenTests)
	{
		$lwUtils = &$this->LW->UTILS;
		$query = "select id from lw_venue_seat_schedule ";
		$query .= "where ((start > '$start' and start < '$end') or ";
		$query .= "(end > '$start' and end <= '$end')) and ";
		$query .= "status = '[not assigned]' and seat = $seat and venuescheduleid = ".$lwUtils->dbStringEscape($venueScheduleId)." order by start;";
		
		$updatedStart = $start;
	
		if (!$results = mysql_query($query))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}
		else if (0 == mysql_num_rows($results))
		{
			$funRetVal = false;
		}
		else
		{
			for ($x=0; $x < mysql_num_rows($results); $x++)
			{
				$venueSeatScheduleId = mysql_result($results, $x, "id");

				$updatedEnd = $this->ComputeEndTime($updatedStart, $testLength, $betweenTests);
				list($updatedEnddate,$updatedEndtime) = split(" ", $updatedEnd, 2);
				list($venueEndDate, $venueEndTime) = split(" ", $this->TestEnd($venueScheduleId), 2);

				if ($this->IsTimeLessEqual($updatedEndtime, $venueEndTime ))
				{
					$query2 = "update lw_venue_seat_schedule ";
					$query2 .= "set start = '".$lwUtils->dbStringEscape($updatedStart)."', end = '".$lwUtils->dbStringEscape($updatedEnd)."' ";
					$query2 .= "where id = ".$lwUtils->dbStringEscape($venueSeatScheduleId).";";
				}
				else
				{
					$query2 = "delete from lw_venue_seat_schedule ";
					$query2 .= "where id = ".$lwUtils->dbStringEscape($venueSeatScheduleId).";";
				}

				if (!$results2 = mysql_query($query2))
				{
                    $msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
					reportError($msg, __FILE__, __LINE__);
				}
  
				$updatedStart = $updatedEnd;
			}
			$funRetVal = true;
		}
		
		return $funRetVal;
	}
	
	/*
	* Get the ending time of the venue schedule
	*
	* @param $venueScheduleId
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function TestEnd($venueScheduleId)
	{
		$lwUtils = &$this->LW->UTILS;
		$query = "select end from lw_venue_schedule ";
		$query .= "where id = ".$lwUtils->dbStringEscape($venueScheduleId).";";
	
		if (!$results = mysql_query($query))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}
		else if (0 == mysql_num_rows($results))
		{
			return false;
		}
		else
		{
			return mysql_result($results, 0, "end");
		}
	}

	/*
	* Get the ending time of the update range
	*
	* @param $start
	* @param $seat
	* @param $venueScheduleId
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function FindEnd($start, $seat, $venueScheduleId)
	{
		$lwUtils = &$this->LW->UTILS;
		$query = "select min(start) as nextstart from lw_venue_seat_schedule ";
		$query .= "where start > '$start' and ";
		$query .= "status = '[assigned]' and seat = ".$lwUtils->dbStringEscape($seat)." and venuescheduleid = ".$lwUtils->dbStringEscape($venueScheduleId).";";
	
		if (!$results = mysql_query($query))
		{
			$msg = 'MySQL error #' . mysql_errno() . ": " . mysql_error();
			reportError($msg, __FILE__, __LINE__);
		}
		else if (0 == mysql_num_rows($results))
		{
			$nextStart = $this->TestEnd($venueScheduleId);
		}
		else
		{
			$nextStart = mysql_result($results, 0, "nextstart");
			if ($nextStart == NULL)
			{
				$nextStart = $this->TestEnd($venueScheduleId);
			}
		}
		return $nextStart;
	}
	
	/*
	* Adjust The Schedule if overlaps exist
	*
	* @param $registeredEnd
	* @param $testLength
	* @param $betweenTests
	* @param $venueScheduleId
	* @param $seat
	* @param $shortestTestLength
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/

	function AdjustScheduleTimes($registeredEnd, $testLength, $betweenTests, $venueScheduleId, $seat, $shortestTestLength)
	{
		if($this->CheckForOverlaps($testLength, $shortestTestLength))
		{
			$start = $registeredEnd;
			$end = $this->FindEnd($start, $seat, $venueScheduleId);
			$this->TimeUpdater($start, $end, $venueScheduleId, $seat, $shortestTestLength, $betweenTests);
		}
		
		return true;
	}

	/*
	* Insert or update initial test schedule in lw_venue_seat_schedule
	*
	* @param $venueScheduleId
	* @param $testId
	*
	* @author Brian Dunkin, Thomas Macie, Ethan Roseborough, Greg Richardson
	* @since 04/05/2004
	*/
	function AddInitialTestSchedule($venueScheduleId, $testId)
	{
		$numSeats = 0;
		$venueId = 0;
		$start = '';
		$end = '';
		$startInterval = 0;
		$betweenTests = 0;
		$timeAllowed = 0;
		
		if (!$this->GetVenueInfo($venueScheduleId, $venueId, $start, $end, $startInterval, $betweenTests, $numSeats))
		{
			$funRetVal = false;
		}
		else if (!$this->GetTestLength($venueScheduleId, $testId, $timeAllowed))
		{
			$funRetVal = false;
		}
		else
		{
			$timeAllotted = $timeAllowed + $betweenTests;
		
			if ($this->ExistsSchedule($venueScheduleId))
			{
				$this->DeleteSchedule($venueScheduleId);
			}
		
			if (!$this->CreateInitialTestSchedule($venueScheduleId, $start, $end, $numSeats, $timeAllotted, $startInterval))
			{
				$funRetVal = false;
			}
			else
			{
				$funRetVal = true;
			}
		}
		return $funRetVal;

	}
}
?>
Return current item: Ludwig Course Management System