Location: PHPKode > projects > ampCAD > functions.php
<?php

// *******************************
// functions.php
//
// Global functions
// *******************************

// *********************************************************************************************
// elapsed_time( $start, $end )
//		$start - Start time
//		$end - End time
//
// Returns
//		Difference between $start and $end
// *********************************************************************************************

function elapsed_time( $start, $end ){
	$start_sec = strtotime( $start );
	$end_sec = strtotime( $end );
	$dur = $end_sec - $start_sec;

	$hour = intval( $dur / 3600 );
	$dur = $dur - (3600 * $hour);

	$mins = intval( $dur / 60 );
	$dur = $dur - (60 * $mins );

	$secs = $dur;

	$output = sprintf("%02d:%02d:%02d", $hour, $mins, $secs);
	return $output;
}

// *********************************************************************************************
// getGlobalVariable( $dbv, $var )
//		$dbv - Database link
//		$var - Variable name
//
// Returns
//		Value of variable $var
// *********************************************************************************************

function getGlobalVariable( $dbv, $var ) {
	$sql = "select * from tblDefaults where var = '$var' limit 1";
	$result = mysql_query( $sql, $dbv );
	if( !$result ) error_process( $dbv );
	if( mysql_num_rows( $result ) != 1 )
		return 0;

	$data = mysql_fetch_object( $result );
	return $data->value;
}

// *********************************************************************************************
// setGlobalVariable( $dbv, $var, $val )
//		$dbv - Database link
//		$var - Variable name
//		$val - Value
//
// Returns
//		0 - If no error, 1 if error
// *********************************************************************************************

function setGlobalVariable( $dbv, $var, $val ) {
	$sql = "update tblDefaults set value='$val' where var='$var'";
	if( !mysql_query( $sql, $dbv ) ) {
		return 1;
	} else {
		return 0;
	}
}

// *********************************************************************************************
// createNewCall( $db, $location, $incident, $type, $channel, $units, $notes, $username )
//		$dbc - Database link
//		$location - Location ID
//		$incident - Incident Type ID
//		$type - Call Type ID
//		$channel - Channel ID
//		$units - Array of assigned units
//		$notes - Notes
//		$username - Username of person opening call
//
// Returns
//		ID of new call created or 0 for error
// *********************************************************************************************

function createNewCall( $dbc, $location, $incident, $type, $channel, $units, $notes, $username ) {
	if( $units == '' ) $assigned = 0; else $assigned = sizeof( $units );

	$sql_call = "insert into tblCall (callLocation, callIncident, callType, callChannel, callNotes, callDate, callTime) values ";
	$sql_call .= "($location, $incident, $type, $channel, '$notes', current_date(), current_time())";

	if( !mysql_query( "start transaction", $dbc ) ) error_process( $dbc );

	if( !mysql_query( $sql_call, $dbc ) ) {
		mysql_query( "rollback", $dbc );
		error_process( $dbc );
	}

	$callID = mysql_insert_id();

	if( createLogMessage( $dbc, $callID, 'Call Opened', $username ) ) {
		error_generate( 1 );
		mysql_query( "rollback", $dbc );
		return 0;
	}

	for( $x = 0; $x < $assigned; $x++ ) {
		$sql_dis = "select statusID from tblStatus where statusAdd = 1";
		$result_dis = mysql_query( $sql_dis, $dbc );
		if( !result_dis ) error_process( $dbc );
		if( mysql_num_rows( $result_dis ) <> 1 ) error_generate( 1 );
		$data_dis = mysql_fetch_object( $result_dis );
		if( $units[$x] != "null" ) {
			$sql = "select unitDescription from tblUnit where unitID=$units[$x]";
			$result_unit = mysql_query( $sql, $dbc );
			if( !$result_unit ) {
				mysql_query( "rollback", $dbc );
				error_process( $dbc );
			}

			$data_unit = mysql_fetch_object( $result_unit );
			if( addUnitToCall( $dbc, $callID, $units[$x], $data_unit->unitDescription, $data_dis->statusID, $username ) ) {
				error_generate( 1 );
				mysql_query( "rollback", $dbc );
				return 0;
			}
		}
	}

	if( !mysql_query( "commit", $dbc ) ) {
		mysql_query( "rollback", $dbc );
		error_process( $dbc );
	}

	return $callID;
}

// *********************************************************************************************
// createLogMessage( $dbc, $call, $message, $user )
//		$dbc - Database link
//		$call - Call ID
//		$message - Message to be logged
//		$username - Username of who is logging
//
// Returns
//		0 - If no error, 1 if error
// *********************************************************************************************

function createLogMessage( $dbc, $call, $message, $username ) {
	$sql_log = "insert into tblCallLog (logCall, logTime, logDate, logMessage, logUser) values ";
	$sql_log .= "($call, current_time(), current_date(), '$message', '$username')";

	if( !mysql_query( $sql_log, $dbc ) ) {
		return 1;
	}

	return 0;
}

// *********************************************************************************************
// addUnitToCall( $dbc, $call, $unit, $description, $status, $username )
//		$dbc - Database link
//		$call - Call ID
//		$unit - Unit ID
//		$description - Unit description
//		$status - Status to change to
//		$username - Username of who is adding
//
// Returns
//		0 - If no error, 1 if error
// *********************************************************************************************

function addUnitToCall( $dbc, $call, $unit, $description, $status, $username ) {

	$sql_assign = "insert into tblCallUnits (callUnitsCall, callUnitsAssigned, callUnitsActive) values ($call, $unit, 1)";
	$sql_status = "update tblUnit set unitStatus = $status where unitID = $unit";

	if( !mysql_query( $sql_assign, $dbc ) || !mysql_query( $sql_status, $dbc ) ) {
		return 1;
	}

	$sql_st = "select statusDescription from tblStatus where statusID=$status limit 1";
	$result_st = mysql_query( $sql_st, $dbc );
	if( mysql_num_rows( $result_st ) != 1 ) return 1;
	$data_st = mysql_fetch_object( $result_st );

	$message = "$description $data_st->statusDescription";

	return createlogMessage( $dbc, $call, $message, $username );
}

// *********************************************************************************************
// updateUnit( $dbc, $unit, $change, $new )
//		$dbc - Database link
//		$unit - Unit ID
//		$change - Attribute to change
//		$new - New value
//
// Returns
//		0 - If no error, 1 if error
// *********************************************************************************************

function updateUnit( $dbc, $unit, $change, $new ) {

	$sql = "update tblUnit set ";

	switch( $change ) {
		case "status":
			$sql .= "unitStatus=";
			break;
		case "department":
			$sql .= "unitDepartment=";
			break;
		case "notes":
			$sql .= "unitNotes=";
			break;
	}

	$sql .= "'$new' where unitID=$unit";

	if( !mysql_query( $sql, $dbc ) ) {
		error_process( $dbc );
		return 1;
	}

	return 0;
}
?>
Return current item: ampCAD