Location: PHPKode > projects > ConPortal > conportal/inc/shift_draw/db.php
<?
/*
 *  ConPortal - Pomona College ITS/ Bucknell University ISR scheduling appplication
 *  Copyright (C) 2005-2007  Pomona College & Bucknell University
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of version 2 of the GNU General Public License
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * Can this user take this shift?  (Based on how much shift draw time they have)
 * George has modded this to check also for 8 hours/day and same hours / different 
 * position problems.  
 */
function drawCheckRemainingTime ($userPid, $shiftPid, $timestamp)
{
    $shift = getShiftDetails($shiftPid);

    // How much allowed to draw
    $allowed = drawTimeTotal($userPid, $shift['shift_group']);

    // Amount drawn in the week
    $drawn = drawTotalWeekTime($userPid, $timestamp);
	
    // Amount drawn today - added by George
    $drawnToday = drawTotalDayTime($userPid, $timestamp);

	//orginal code
    //return $drawn + shiftLength($shiftPid) <= $allowed;

	/* FIXME - checks less than 8:16, to allow people to take 7 regular hours
      and the 7:45 - 9 shift */
	return ($drawn + shiftLength($shiftPid) <= $allowed) && 
		($drawnToday + shiftLength($shiftPid) <= 29760) &&
		notAlreadyTakenTheseHours($userPid, $shift['start_time']); 

}

function drawTimeTotal($user, $shift_group)
{
	$result = safeQuery("select seconds from shift_draw where " .
		"shift_draw.group = %d and shift_group = %d and start_time <= NOW() " .
		" and seniority >= %d ORDER BY start_time DESC LIMIT 1",
		getPrimaryGroupForUser($user), $shift_group, getSeniorityForUser($user));
    $total = 0;
    while ($row = mysql_fetch_row($result))
        $total += intval($row[0]);
    return $total;
}


/* Takes in user id and shift id, and returns true if it's safe to give the hours to
 * the user, and false if they've already taken them. (Sam's Version)
 * (I removed the commented out version of George's version of this function in
 * revision 47).
 */

function notAlreadyTakenTheseHours ($userPid, $shiftPid)
{

	$isNotOverlapping = true;
	$overlappingPids = array();
	$temp_data = array();
	$shift_data = getShiftDetails($shiftPid);
	

	$overlappingPids = checkShiftsOverlapAnyPosition ($shiftPid, $shift_data['start_date'], $shift_data['end_date'], $shift_data['start_time'], $shift_data['end_time'], $shift_data['day_of_week']);


	if (count($overlappingPids) > 0)
		foreach ($overlappingPids as $Pid)
		{	
			$temp_data = getShiftDetails($Pid);
			if ($temp_data['owner'] == $userPid)
				$isNotOverlapping = false;
		}


	return $isNotOverlapping;
}



/*
 * Total seconds a user is working during a given week (identified by timestamp)
 */
function drawTotalWeekTime ($userPid, $timestamp)
{
    // Get the start/end of the week
    list($start, $end) = start_end_of_week($timestamp);

    // Get the shifts worked by user in that range
    $shifts = getShiftsForDateRangeAndUser($userPid, $start, $end);
    // This will break if a shift can occur more than once in a week.

    return shiftLengthTotal($shifts);
}

/*
 * George: Takes in user id and figures out how many hours that user has scheduled today.
 * Modeled on function above.  Yes, I suppose the above *could* be used...
 */
function drawTotalDayTime ($userPid, $timestamp)
{
	//Get the shifts the user works today
	$shiftsToday = getShiftsForDayAndUser($userPid, $timestamp);
	return shiftLengthTotal($shiftsToday);
} 

/*
 * Takes a shift associative array  and returns the length of the
 * shift in seconds.
 * Returns false on error or 0 if the shift doesn't exist.
 */
function shiftLength ($shiftPid)
{
	$shiftData = getShiftDetails($shiftPid);
    return $shiftData['end_time'] - $shiftData['start_time'];
}

/* adds up the lengths in seconds of an array of shift pids.
   if any of the pids are invalid they are simply ignored */
function shiftLengthTotal ($shiftPids)
{
    $total = 0;
    if ($shiftPids) {
        foreach ($shiftPids as $pid)
            $total += intval(shiftLength($pid));
    }
    return $total;
}

function getShiftPickStartTimes()
{
	$result = safeQuery("SELECT shift_draw.pid as pid, groups.description as usergroup, shiftgroups.name as shiftgroup, seniority_levels.description as senlevel, shift_draw.start_time as starttime, (shift_draw.seconds/3600) AS hours FROM shift_draw, seniority_levels, shiftgroups, groups where shift_draw.seniority = seniority_levels.pid AND shiftgroups.pid = shift_draw.shift_group AND shift_draw.group = groups.pid ORDER BY shift_draw.pid");
    	if (mysql_num_rows($result) > 0)
	{
        	$array = array();
       		while ($row = mysql_fetch_assoc($result))
           	{
		 $array[] = $row;
		}
        	return $array;
   	}
	else
   		return NULL;
}

/* 
//FIXME: This should now be obsolete, with the addition of the set_pick_start_time.php related code on 5/15/08

Sets the number of hours per week for a given shiftgroup and user group.  This function will both add new rows to 
the shift_draw table AND update rows that already exist.  
Overthinking this; it occurs to me that if shift_draw rows can be uniquely defined by shiftgroup + usergroup, does the table really need separate pid values?  Conceptually easier that way, I guess.  I've got a lot of other things to do before I even consider screwing around with that, however.  Potential, very low priority FIXME.
function setShiftDrawHoursPerWeek($shiftgroup, $usergroup, $seconds)
{
	//first, does it already exist?
	$exist_result = safeQuery("SELECT pid FROM shift_draw WHERE shift_group = \"" . $shiftgroup . "\" AND " .
			"shift_draw.group = \"" . $usergroup . "\"");
	$rows = mysql_num_rows($exist_result);
	
	//doesn't exist, so INSERT
	if($rows == 0)
	{
		$result = safeQuery("INSERT INTO shift_draw (shift_group, shift_draw.group, seconds) VALUES (\"" . $shiftgroup . "\" , \"" . $usergroup . "\" , \"" . $seconds . "\")");
		return $rows;
	}
	//exists, so UPDATE
	else if($rows == 1)
	{
		$result = safeQuery("UPDATE shift_draw SET seconds = \"" . $seconds . "\" WHERE shift_group = \"" . $shiftgroup . "\" AND shift_draw.group = \"" . $usergroup . "\"");
		return $rows;
	}
	// if it's not 0 or 1, something wierd has happened!
	else{
		return -1;	
	}

}  */

function setShiftDrawInfo($shiftgroup, $usergroup, $seconds, $seniority, $start_time)
{
	$result = safeQuery("INSERT INTO shift_draw (shift_group, shift_draw.group, seconds, seniority, start_time) VALUES (\"" . $shiftgroup . "\" , \"" . $usergroup . "\" , \"" . $seconds . "\" , \"" . $seniority . "\" , \"" . $start_time . "\")");
		
	return $rows;

}

function deleteShiftHoursStartByPid($pid)
{
	$result = safeQuery("DELETE FROM shift_draw WHERE pid = \"" . $pid . "\"");
	return 0;
}

?>
Return current item: ConPortal