Location: PHPKode > projects > Online Resource Management system > storms/user/user_add_room_booking.php
<?php
/*******************************************
 *   Add Room Booking
 *   maintained by: Wing Sin
 *   Create Date: 14 Mar 20001
 *   Last Update: 6 May 2001
 *   Input: Room Booking data
 *   Output: Nil
 *   Purpose: Used to Add New Room Booking data
 ******************************************/

include("../includes/session_mysql.inc");
include("../includes/global-vars.inc");
include("../includes/command.php");

session_start();
session_register("user_class");
session_register("username");
session_register("name");
session_register("lang");

include("$lang.inc");

mysql_connect($dbhost, $dbuser, $dbpasswd);
	
?>
<html>
<head>
<title><?php print $msg_room_booking_add_title; ?></title>
<?php print $doc_encoding; ?>
<link rel="stylesheet" href="../style_<?php print $lang; ?>.css" type="text/css">
</head>
<SCRIPT LANGUAGE="JavaScript">
function isValidText(the_text, msg)
{
	if (the_text=="")
	{
		alert(msg);
		return false;
	}
	else
		return true;
}

function isValidField(the_text, msg, field)
{
	var checkpass = "false";
	for (i = 0; i < field.length; i++)
	{
		if (field[i].checked == true)
			checkpass = "true";
	}
	if (the_text == "weekly" && checkpass == "false")
	{
		alert(msg);
		return false;
	}
	else
	{
		return true;
	}
}

function frmChk(me)
{
	if ((!isValidText(me.in_class.value, "<?php print $msg_room_booking_add_error_class; ?>")))
		return false
	else
		return true
}
</script>
<body bgcolor="#FFFFFF" text="#000000" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" bottommargin="0" rightmargin="0">
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
		<td width="160" bgcolor="#FFFFFF" valign="top"> 
<?php include("menu.inc"); ?>
		</td>
		<td valign="top">
		<!--- Beginning of Add New Room Booking Code --->
		<h3 class="heading"><?php print $msg_room_booking_add_title; ?></h3>
		<span class="text10point">User Account: </span>
		<span class="text10pointbold"><?php print $username ?></span>
		<br>
<?php
// To input the New User Account Data
if (!isset($action))
{
	// input value, and assign value
	$in_s_yr = date("Y", strtotime($in_s_date));
	$in_s_mon = date("m", strtotime($in_s_date));
	$in_s_day = date("d", strtotime($in_s_date));
	if ($in_s_hr < 10)
	{
		$in_s_hr = "0" . $in_s_hr;
	}
	$in_s_min = "30";
?>
		<form name="form1" method="post" action="user_add_room_booking.php?action=add" onsubmit="return frmChk(this);">
		<input type="hidden" name="in_roomID" value="<?php echo $in_roomID; ?>">
		<input type="hidden" name="in_s_date" value="<?php echo $in_s_date; ?>">
		<input type="hidden" name="in_s_hr" value="<?php echo $in_s_hr; ?>">
		<input type="hidden" name="room_type" value="<?php echo $room_type; ?>">
		<table width="100%" border="0" cellspacing="0" cellpadding="2">
			<tr> 
				<td nowrap class="text10point" width="50%" align="right"> 
				<?php print $msg_room_booking_roomcode; ?>
				</td>
				<td class="textfield" width="50%"><?php echo $in_room_code; ?></td>
			</tr>
			<tr> 
				<td nowrap class="text10point" width="50%" align="right"> 
				<?php print $msg_room_booking_class; ?>
				</td>
				<td class="textfield" width="50%"> 
					<INPUT TYPE="TEXT" NAME="in_class" VALUE="" SIZE=10 ALIGN="LEFT" MAXLENGTH=10>
				</td>
			</tr>
			<tr> 
				<td nowrap class="text10point" width="50%" align="right"> 
				<?php print $msg_room_booking_purpose; ?>
				</td>
				<td class="textfield" width="50%"> 
					<select name="in_type" class="textfield">
						<option value="Lecture" selected>Lecture</option>
						<option value="Lab">Lab</option>
						<option value="Tutorial">Tutorial</option>
						<option value="Test">Test</option>
						<option value="Others">Others</option>
					</select>
				</td>
			</tr>
<?php
if (($user_class == "admin") || ($user_class == "clerk") || ($user_class == "tech"))
{
?>
			<tr> 
				<td nowrap class="text10point" width="50%" align="right"> 
				<?php print $msg_room_booking_time_start; ?>
				</td>
				<td class="textfield" width="50%"><?php echo $in_s_date; ?></td>
			</tr>
			<tr> 
				<td nowrap class="text10point" width="50%" align="right"> 
				<?php print $msg_room_booking_time_end; ?>
				</td>
				<td nowrap class="textfield" width="50%"> 
					<select name="in_e_year" class="textfield">
						<option value="<?php echo date("Y"); ?>"<?php if ($in_s_yr == date("Y")) echo " selected"; ?>><?php echo date("Y"); ?></option>
						<option value="<?php echo date("Y")+1; ?>"<?php if ($in_s_yr == date("Y")+1) echo " selected"; ?>><?php echo date("Y")+1; ?></option>
					</select>
					<select name="in_e_month" class="textfield">
						<option value="01"<?php if ($in_s_mon == "01") echo " selected"; ?>><?php print $msg_room_booking_main_jan; ?></option>
						<option value="02"<?php if ($in_s_mon == "02") echo " selected"; ?>><?php print $msg_room_booking_main_feb; ?></option>
						<option value="03"<?php if ($in_s_mon == "03") echo " selected"; ?>><?php print $msg_room_booking_main_mar; ?></option>
						<option value="04"<?php if ($in_s_mon == "04") echo " selected"; ?>><?php print $msg_room_booking_main_apr; ?></option>
						<option value="05"<?php if ($in_s_mon == "05") echo " selected"; ?>><?php print $msg_room_booking_main_may; ?></option>
						<option value="06"<?php if ($in_s_mon == "06") echo " selected"; ?>><?php print $msg_room_booking_main_jun; ?></option>
						<option value="07"<?php if ($in_s_mon == "07") echo " selected"; ?>><?php print $msg_room_booking_main_jul; ?></option>
						<option value="08"<?php if ($in_s_mon == "08") echo " selected"; ?>><?php print $msg_room_booking_main_aug; ?></option>
						<option value="09"<?php if ($in_s_mon == "09") echo " selected"; ?>><?php print $msg_room_booking_main_sep; ?></option>
						<option value="10"<?php if ($in_s_mon == "10") echo " selected"; ?>><?php print $msg_room_booking_main_oct; ?></option>
						<option value="11"<?php if ($in_s_mon == "11") echo " selected"; ?>><?php print $msg_room_booking_main_nov; ?></option>
						<option value="12"<?php if ($in_s_mon == "12") echo " selected"; ?>><?php print $msg_room_booking_main_dec; ?></option>
					</select>
					<select name="in_e_day" class="textfield">
<?php
	for ($d=1; $d<32; $d++)
	{
		if ($d<10)
		{
			if ($in_s_day == $d)
			{
				echo "\t\t\t\t\t<option value=\"0$d\" selected>$d</option>\n";
			}
			else
			{
				echo "\t\t\t\t\t<option value=\"0$d\">$d</option>\n";
			}
		}
		else
		{
			if ($in_s_day == $d)
			{
				echo "\t\t\t\t\t<option value=\"$d\" selected>$d</option>\n";
			}
			else
			{
				echo "\t\t\t\t\t<option value=\"$d\">$d</option>\n";
			}
		}
	}
?>
					</select>
				</td>
			</tr>
<?php
}
else
{
?>
			<tr> 
				<td nowrap class="text10point" width="50%" align="right"> 
				Date:
				</td>
				<td class="textfield" width="50%"><?php echo $in_s_date; ?></td>
			</tr>
			<input type="hidden" name="in_e_year" value="<?php echo date("Y", strtotime($in_s_date)); ?>">
			<input type="hidden" name="in_e_month" value="<?php echo date("m", strtotime($in_s_date)); ?>">
			<input type="hidden" name="in_e_day" value="<?php echo date("d", strtotime($in_s_date)); ?>">
<?php
}
?>
			<tr> 
				<td nowrap class="text10point" width="50%" align="right"> 
				<?php print $msg_room_booking_time_start; ?>
				</td>
				<td class="textfield" width="50%">
			  	<?php echo $in_s_hr . ":" . $in_s_min; ?>
				</td>
			</tr>
			<tr> 
				<td nowrap class="text10point" width="50%" align="right"> 
				<?php print $msg_room_booking_time_end; ?>
				</td>
				<td class="textfield" width="50%"> 
					<select name="in_e_time" class="textfield">
<?php
// Show only the time after Start Time

	for ($i=9; $i<18; $i++)
	{
		if ($in_s_hr < $i)
		{
			if ($i < 10)
			{
				echo "\t\t\t\t\t<option value=\"0".$i."\">0".$i.":30</option>\n";
			}
		else
		{
			echo "\t\t\t\t\t<option value=\"".$i."\">".$i.":30</option>\n";
		}
	}
}
?>
					</select>
				</td>
			</tr>
<?php

if (($user_class == "admin") || ($user_class == "clerk") || ($user_class == "tech"))
{
?>
			<tr> 
				<td nowrap class="text10point" width="50%" align="right"> 
				<?php print $msg_room_booking_add_repeat_type; ?>
				</td>
				<td class="textfield" width="50%"> 
					<select name="in_repeat" class="textfield" onchange=changeLoc(this.form)>
						<option value="daily"><?php print $msg_room_booking_add_repeat_daily; ?></option>
						<option value="weekly"><?php print $msg_room_booking_add_repeat_weekly; ?></option>
					</select>
				</td>
			</tr>
			<tr> 
				<td nowrap class="text10point" width="50%" align="right"> 
		        	Weekly Type:
				</td>
				<td class="textfield" width="50%"> 
					<input type="checkbox" name="in_weekly[0]" value="1"> <?php print $msg_room_booking_mon; ?><br>
					<input type="checkbox" name="in_weekly[1]" value="2"> <?php print $msg_room_booking_tue; ?><br>
					<input type="checkbox" name="in_weekly[2]" value="3"> <?php print $msg_room_booking_wed; ?><br>
					<input type="checkbox" name="in_weekly[3]" value="4"> <?php print $msg_room_booking_thu; ?><br>
					<input type="checkbox" name="in_weekly[4]" value="5"> <?php print $msg_room_booking_fri; ?><br>
					<input type="checkbox" name="in_weekly[5]" value="6"> <?php print $msg_room_booking_sat; ?>
				</td>
			</tr>
<?php
}
else
{
	echo "<input type=\"hidden\" name=\"in_repeat\" value=\"daily\">";
}
?>
			<tr> 
				<td nowrap class="text10point" width="50%" align="right"> 
				<?php print $msg_room_booking_desc; ?>
				</td>
				<td class="textfield" width="50%"> 
					<textarea name="in_desc" wrap=physical cols=30 rows=5></textarea>
				</td>
			</tr>
			<tr>
				<td class="text10white" bordercolor="0" height="25">&nbsp;</td>
			</tr>
			<tr> 
				<td nowrap class="text10point" colspan="2" align="center"> 
				<input type="image" name="Submit" value="Submit" src="../image/save.png" width="85" height="20" border="0">
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	            		<a href="user_add_room_booking.php?in_roomID=<?php echo $in_roomID; ?>&in_room_code=<?php echo $in_room_code; ?>&in_s_date=<?php echo $in_s_day; ?>&in_s_hr=<?php echo $in_s_hr; ?>"><img src="../image/reset.png" alt="<?php print $msg_reset; ?>" border="0"></a>
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	            		<a href="<?php print $HTTP_REFERER; ?>"><img src="../image/cancel.png" alt="<?php print $msg_cancel; ?>" border="0"></a>
				</td>
			</tr>
		</table>
		</form>
<?php
// To add the room booking data
}
elseif ($action=="add")
{
	// Input Value:
	// in_s_date ==> "2001-12-31"
	// in_roomID ==> RoomID
	// in_s_hr ==> "12"
	// in_class ==> text
	// in_type ==> for enum
	// in_e_year, in_e_month, in_e_day ==> "2001" , "12" , "31"
	// in_e_time ==> "12"
	// in_repeat ==> daily / weekly
	// in_weekly ==> 1 to 6
	// in_desc ==> text
	
	if ($in_repeat == "weekly")
	{
		$checkpass = FALSE;
		for ($i=1; $i<7; $i++)
		{
			if (isset($in_weekly[$i]))
				$checkpass = TRUE;
		}
		if ($checkpass == FALSE)
		{
			on_error(16);
			exit;
		}
	}
	
	// input value, and assign value
	$in_s_yr = date("Y", strtotime($in_s_date));
	$in_s_mon = date("m", strtotime($in_s_date));
	$in_s_day = date("d", strtotime($in_s_date));
	$in_s_min = "30";
	
	// Structured end date
	$in_e_date = $in_e_year . "-" . $in_e_month . "-" . $in_e_day;
	// start, end time stamp format
	$start_time = $in_s_yr . $in_s_mon . $in_s_day . $in_s_hr . $in_s_min . "00";
	$end_time = $in_e_year . $in_e_month . $in_e_day . $in_e_time . "3000";
	// pure start time
	$in_s_time = $in_s_hr . $in_s_min;
	
	// Check if the date is valid
	if (!checkdate($in_e_month, $in_e_day, $in_e_year))
	{
		on_error(13);
		exit();
	}
	
	// Check Start time must before End time
	if ($end_time < $start_time)
	{
		on_error(14);
		exit();
	}
	
	// To calculate the number of day between start and end time
	$date1 = date("z", strtotime($in_s_date));
	if ($in_e_year > $in_s_yr)
	{
		$date2 = date("z", strtotime($in_s_yr."-12-31")) + date("z", strtotime($in_e_date));
	}
	else
	{
		$date2 = date("z", strtotime($in_e_date));
	}
	$num_of_days = $date2 - $date1;
	
	// Used to find UserID
	$query_user = "SELECT userID FROM users " .
					"WHERE user_acct = '$username' ";
	$result_user = mysql_db_query($dbname, $query_user);
	if (!mysql_num_rows($result_user))
	{
		on_error(0);
		exit();
	}
	$row_user = mysql_fetch_array($result_user);
	
	// Check and Insert record for daily/weekly booking
	if ($in_repeat == "daily")
	{
		for ($i=0; $i<=$num_of_days; $i++)
		{
			// Define the date-time for start and end
			$add_date = date('YmdHis', mktime ($in_s_hr,$in_s_min,"00",$in_s_mon,$in_s_day+$i,$in_s_yr));
			$full_add_date = date('Y-m-d', mktime ($in_s_hr,$in_s_min,"00",$in_s_mon,$in_s_day+$i,$in_s_yr));
			$end_add_date = date('YmdHis', mktime ($in_e_time,$in_s_min,"00",$in_s_mon,$in_s_day+$i,$in_s_yr));
			
			// Check if room available for booking
			$query_ava = "SELECT room_bookingID, DATE_FORMAT(room_booking_start_time, '%Y%m%d') AS s_date, 
							DATE_FORMAT(room_booking_end_time, '%Y%m%d') AS e_date,
							TIME_FORMAT(room_booking_start_time, '%H%i') AS s_time,
							TIME_FORMAT(room_booking_end_time, '%H%i') AS e_time FROM bookings 
							WHERE ((room_booking_start_time >= '$add_date' AND room_booking_end_time <= '$end_add_date') 
							OR (room_booking_start_time <= '$add_date' AND room_booking_start_time >= '$add_date') 
							OR (room_booking_end_time >= '$end_add_date' AND room_booking_end_time <= '$end_add_date')) 
							AND roomID = '$in_roomID'";
			$result_ava = mysql_db_query($dbname, $query_ava);
			
			// Check if that date cannot book, that date fail, but other date possible success
			if (mysql_num_rows($result_ava) > 0)
			{
				$add_record[$i]["result"] = "Fail";
				mysql_free_result($result_ava);
			}
			else
			{
				$add_record[$i]["result"] = "Success";
				// Used to insert the room booking data
				$insertStmt = "INSERT INTO bookings (roomID, userID, class, room_booking_start_time, room_booking_end_time, room_booking_type, room_booking_desc) " . 
							"VALUES ('$in_roomID', '".$row_user["userID"]."', '$in_class', '$add_date', '$end_add_date', '$in_type', '$in_desc')";
				if (!mysql_db_query($dbname, $insertStmt))
				{
					on_error(6);
					exit();
				}
			}
			$add_record[$i]["at_date"] = $full_add_date;
			$add_record[$i]["start_time"] = $in_s_hr . ":" . $in_s_min;
			$add_record[$i]["end_time"] = $in_e_time . ":" . $in_s_min;
		}
	}
	elseif ($in_repeat == "weekly")
	{
		$temp_rec = 0;
		for ($i=0; $i<=$num_of_days; $i++)
		{
			// Define the date-time for start and end
			$add_date = date('YmdHis', mktime ($in_s_hr,$in_s_min,"00",$in_s_mon,$in_s_day+$i,$in_s_yr));
			$full_add_date = date('Y-m-d', mktime ($in_s_hr,$in_s_min,"00",$in_s_mon,$in_s_day+$i,$in_s_yr));
			$end_add_date = date('YmdHis', mktime ($in_e_time,$in_s_min,"00",$in_s_mon,$in_s_day+$i,$in_s_yr));
			// Check if the date is in the selected weekly type
			$at_week = FALSE;
			for ($j=0; $j<6; $j++)
			{
				if (isset($in_weekly[$j]))
				{
					if (date('w', mktime ($in_s_hr,$in_s_min,"00",$in_s_mon,$in_s_day+$i,$in_s_yr)) == $in_weekly[$j])
					{
						$at_week = TRUE;
					}
				}
			}
			
			if ($at_week == TRUE)
			{
				// Check if room available for booking
				$query_ava = "SELECT room_bookingID, DATE_FORMAT(room_booking_start_time, '%Y%m%d') AS s_date, 
								DATE_FORMAT(room_booking_end_time, '%Y%m%d') AS e_date,
								TIME_FORMAT(room_booking_start_time, '%H%i') AS s_time,
								TIME_FORMAT(room_booking_end_time, '%H%i') AS e_time FROM bookings 
								WHERE ((room_booking_start_time >= '$add_date' AND room_booking_end_time <= '$end_add_date') 
								OR (room_booking_start_time <= '$add_date' AND room_booking_start_time >= '$add_date') 
								OR (room_booking_end_time >= '$end_add_date' AND room_booking_end_time <= '$end_add_date')) 
								AND roomID = '$in_roomID'";
				$result_ava = mysql_db_query($dbname, $query_ava);
				
				// Check if that date cannot book, that date fail, but other date possible success
				if (mysql_num_rows($result_ava) > 0)
				{
					$add_record[$temp_rec]["result"] = "Fail";
					mysql_free_result($result_ava);
				}
				else
				{
					$add_record[$temp_rec]["result"] = "Success";
					// Used to insert the room booking data
					$insertStmt = "INSERT INTO bookings (roomID, userID, class, room_booking_start_time, room_booking_end_time, room_booking_type, room_booking_desc) " . 
								"VALUES ('$in_roomID', '".$row_user["userID"]."', '$in_class', '$add_date', '$end_add_date', '$in_type', '$in_desc')";
					if (!mysql_db_query($dbname, $insertStmt))
					{
						on_error(6);
						exit();
					}
				}
				$add_record[$temp_rec]["at_date"] = $full_add_date;
				$add_record[$temp_rec]["start_time"] = $in_s_hr . ":" . $in_s_min;
				$add_record[$temp_rec]["end_time"] = $in_e_time . ":" . $in_s_min;
				$temp_rec++;
			}
		}
	}
	mysql_free_result($result_user);
?>
	<form name="form1" method="post" action="user_room_booking_main.php<?php echo "?date=$in_s_date&room_type=$room_type"; ?>">
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
		<tr>
			<td class="text10white" colspan="3" bordercolor="0" height="25" align="center">
			<span align="center" class="heading"><?php print $msg_room_booking_add_title; ?></span>
			<br><br>
			</td>
		</tr>
		<tr>
			<td nowrap class="text10pointbold" width="10%" align="center"><?php print $msg_room_booking_add_result_date; ?></td>
			<td nowrap class="text10pointbold" width="10%" align="center"><?php print $msg_room_booking_add_result_time; ?></td>
			<td nowrap class="text10pointbold" width="10%" align="center"><?php print $msg_room_booking_add_result; ?></td>
		</tr>
<?php
	for ($i=0; $i<count($add_record); $i++)
	{
?>
		<tr> 
			<td nowrap class="text10point" width="10%" align="center"><?php echo $add_record[$i]["at_date"]; ?></td>
			<td nowrap class="text10point" width="10%" align="center"><?php echo $add_record[$i]["start_time"] . "-" . $add_record[$i]["end_time"]; ?></td>
			<td nowrap class="text10point" width="10%" align="center"><?php echo $add_record[$i]["result"]; ?></td>
		</tr>
<?php
	}
?>
		<tr>
			<td class="text10white" colspan="3" bordercolor="0" height="25" align="center">
			<br>
			<input type="image" name="Submit" value="Back" src="../image/ok.png" border="0">
			<br>
			</td>
		</tr>
	</table>
	</form>
<?php
}
?>
		<!--- End of Add New Room Booking Code --->
		</td>		
	</tr>
</table>
</body>
</html>
Return current item: Online Resource Management system