Location: PHPKode > projects > ConPortal > conportal/inc/weekview/display.php
<?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
 */

/*
Implemented in this file

	showWholeWeek

 */

function showWholeWeek($startDate)
{

	//get the data
	
	$weekShifts = array();
	
	$weekShifts = getShiftsForWeek($startDate);
	
	/*because the way the query works, multiple shifts for the same hour and position 
	can be returned if the shift changes hands during that period. This function 
	removes the spurious entries.  I suppose I could do it while generating the table, 
	but really, it seems cleaner this way.  Please note I'm passing by reference, so 
	there's no return value.  Given the size the the possible response (300+ rows)
	it's more efficient than copying the rows over and back.  */
	fixOverlaps($weekShifts, $startDate);
	
	//magically massage the data into a pretty picture for people who can't appreciate var_dump
	//var_dump($weekShifts);
	
    // constants
    define('HALF_HOUR', 30 * 60);          // 1800 seconds = 1/2 hour
    define('TIME_SCALE', 2 / HALF_HOUR); // ratio of ems to seconds
    define('SMALL_DIFF', 5 * 60);          // between shifts, spaces smaller
                                           // than this many seconds are not
                                           // displayed

/*
    $start_time = earliest_shift_start_time($shift_sets);
    $end_time = latest_shift_end_time($shift_sets);
    if ($start_time >= $end_time) {
        echo "There are no shifts for today.\n";
        return;
    }*/
	
	//George sez:  I'm assuming a midnight to 11:59pm day here... might FIXME someday to make this more flexible
	$start_time = 0;  
	$end_time = 86400;

?>
<div id="shifts_by_day">
<?
    // Display the legend =D
    //call_user_func("print_{$legend}_legend");
	print_week_legend();
?>
    <div style='clear:both'></div>
    <table>
<? /* php print_positions($shift_sets)  */?>
        <tr>
            <td class="time">
<?php
    // Print out the list of times, at half-hour intervals
    // FIXME: should we round down and up to the next half-hour?
    // if the first shift of the day started at 9:05 for some
    // reason, this would make the display weird...
    for ($i = $start_time; $i < $end_time; $i += HALF_HOUR)
	{
/* The old code; George is going to attempt an ugly hack that stops showing things from 
	2:30 to 6:30; since the TD is closed at those times that just leaves a big ugly
	blank space which serves no good purpose.  This change fixes the time display on
	the side; the actual printing of blocks is not done here.

        echo '<div class="time">' . date('g:i', date_part(time()) + $i) . "</div>\n"; */
	
		if($i <= 9000 || $i >= 23400)
		{ 
        	echo '<div class="time">' . date('g:i', date_part(time()) + $i) . "</div>\n";
		}
	}
?>
            </td>
<?php
    // Loop through positions
    foreach ($weekShifts as $shifts)
	{
        if (!count($shifts))
            continue;
    
	    echo "<td>\n";
		
        $current_time = $start_time;
		//echo "var_d 1: " . var_dump($shifts);
       //foreach ($shifts as $i => $shift) {
			//echo "var_d 2: " . var_dump($shift);
            $diff   = time_part($shifts['start_time']) - $current_time;
            $length = $shift['end_time'] - $shift['start_time'];

            // Check that this shift is in the correct shift group
         
		/* Sanity check temporarily disabled, probably good to re-enable it at some point
		if ($shift['shift_group'] != $_SESSION['shift_shiftgroup'])
                echo "<b>Error: Shift {$shift['pid']} returned, ".
                     "but is not in shiftgroup ".
                     "{$_SESSION['shift_shiftgroup']}</b><br>"; */

            /*
             * Check to see if there's space between the current time and the
             * start time of the next shift. If so, output a div.NoShift
             */
            if ($diff < 0) {
                echo "<b>Error: Overlapping shifts! Displayed times may " .
                     "not be correct</b>\n";
            } else if ($diff <= SMALL_DIFF) {
                /*
                 * This porridge is too small!
                 *
                 * If the diff is too small, IE would render the element too
                 * big. So we won't display a NoShift block, but we need to
                 * then compensate in the length of the next block (to assure
                 * consistency).
                 */
                $length += $diff;
            } else {
                // This porridge is just right!

		/* George sez: Not so fast, Goldilocks!  I want to skip displaying empty
		blocks from 2:30-6:30 AM.  Commenting out original code...

		 		
		*/
		if($start_time>0)
		{
			$adjusted_diff = $diff;
		}
		else
		{
			$adjusted_diff = $diff-12600;
		}

		echo "<div class='NoShift' style='height: ".
                     ($adjusted_diff * TIME_SCALE) . "em'></div>\n";  
		
                $current_time += $diff;
            }
            $current_time += $length;

            /*
             * Output the outer div that sets the shift's height. We give this
             * div an overflow:hidden style unless it has a space after it or
             * or it's the last in its column. this is a hack to get the
             * heights to display correctly in IE.  (oh IE, how i loathe thee!)
             */
            if (($i == count($shifts) - 1) ||
                ($shifts[$i+1]['start_time'] - $shift['end_time'] > SMALL_DIFF))
                $overflow = '';
            else
                $overflow = 'overflow: hidden';

            // !!!TEMP: no overflow at all (why is this again?)
			$overflow = '';
			
?>
<div style='height: <?= $length * TIME_SCALE ?>em; <?= $overflow ?>'>
<? /*
call_user_func($info_func, $shift)  */
print_week_info($shifts);
?>
</div>
<?
       //}
        echo "</td>\n";
    }
?>
        </tr>
<? /*php print_positions($shift_sets) */ ?>
    </table>

<?/*
    call_user_func("print_{$legend}_legend") */
	print_week_legend();
?>
  </div>
<?php

}

function print_week_info ($shift_data)
{
	echo "Um: ";
	var_dump($shift_data);
		
    // Determine which buttons to display
    /*$status = getShiftStaffingStatus($shift_data['pid']);
    switch($status)
    {
        case STATUS_TEMP_TAKEN:
            $temp_drop_button = checkShowShiftDropButton($shift_data['pid'], $_SESSION['shift_timestamp']);
            $temp_take_button = false;
            $perm_drop_button = false;
            $perm_take_button = false;
            break;
        case STATUS_TEMP_OPEN:
            $temp_drop_button = false;
            $temp_take_button = checkShowShiftTakeButton($shift_data['pid'], $_SESSION['shift_timestamp']);
            $perm_drop_button = false;
            $perm_take_button = false;
            break;
        case STATUS_PERM_TAKEN:
            $temp_drop_button = checkShowShiftDropButton($shift_data['pid'], $_SESSION['shift_timestamp']);
            $temp_take_button = false;
            $perm_drop_button = $temp_drop_button;
            $perm_take_button = false;
            break;
        case STATUS_PERM_OPEN:
            $temp_drop_button = false;
            $temp_take_button = checkShowShiftTakeButton($shift_data['pid'], $_SESSION['shift_timestamp']);
            $perm_drop_button = false;
            $perm_take_button = $temp_take_button;
            break;
    }*/
	
	// Determine CSS class and open the div
	/*
	$classname = determine_shift_class(getShiftStaffingStatus($shift_data['pid']), $shift_data['owner']);
	echo "<div class=\"$classname\">\n"; 
	Hardcode to open for now */
	echo "<div class=OpenShift>\n";

    // Time
    echo date('g:i', $shift_data['start_time']) . ' - ';
    echo date('g:i', $shift_data['end_time']) . "<br />\n";
	
	echo twelveHourTime($shift_data['start_time']) . ' - ';
    echo twelveHourTime($shift_data['end_time']) . "<br />\n";
	
	echo miltoampm($shift_data['start_time']) . ' - ';
    echo miltoampm($shift_data['end_time']) . "<br />\n";

    // Names
    if (isset($shift_data['userpid']) and $shift_data['userpid'] != 0)
        echo $shift_data['first'] . " " . $shift_data['last'] . "<br />\n";
    /*elseif (isset($shift_data["dropper"]) and $shift_data["dropper"] != 0)
        echo 'Dropped by: ' . getNameForUser($shift_data["dropper"]) . "<br />\n"; */
    else
        echo "Shift open<br />\n";

    if ($DEBUG)
    {
    	$dbout = "";
    	foreach ($shift_data as $dbkey => $dbval)
		$dbout .= $dbkey . "=>" . $dbval . " -- ";
    	echo "<a href=\"javascript:alert('$dbout');\">DB</a><br />";
    }


	// Close out the containing div
	echo "</div>\n";
}
	
/*
 * Print the legend that occurs at the top and bottom of the shifts table
 */
function print_week_legend ()
{
    ?>
    <div class="Legend">Legend:</div>
    <div class="OpenShift" style="height: inherit; float: left">Open Shift</div>
    <div class="DroppedShift" style="height: inherit; float: left">Dropped Shift</div>
    <div class="PermTakenShift" style="height: inherit; float: left">Perm Taken Shift</div>
    <div class="TempTakenShift" style="height: inherit; float: left">Temp Taken Shift</div>
    <div class="YourPermShift" style="height: inherit; float: left">Your Perm Shift</div>
    <div class="YourTempShift" style="height: inherit; float: left">Your Temp Shift</div>
	<div style="clear:both">
	</div>
    <?php
}	

/* This fixes overlap conditions.  Note the pass by reference
	also note that since we have added indexing to this array, we
	want to use array_splice and not unset, since splice
	handles fixing the index for us.  Woot! */
function fixOverlaps(&$weekShifts, $startDate)
{
	//pre-load the first value
	
	for($i=1; $i<count($weekShifts); $i++)
	{
		/*Do we have an overlap?  I believe we could have 3 consecutive shifts 
		overlapping; this should take care of the case of 3 as well as case of 2. */
		if($weekShifts[$i-1]['start_time'] == $weekShifts[$i]['start_time'] &&
			$weekShifts[$i-1]['day_of_week'] == $weekShifts[$i]['day_of_week'] &&
			$weekShifts[$i-1]['position'] == $weekShifts[$i]['position'] )
		{
			//then fix stuff!
			$daysFromStart = "+" . DOW_to_number($weekShifts[$i]['day_of_week']) . " days";
			
			//if the end date of $i-1 is prior to that particular day, remove that element.  Otherwise, remove element at position $i
			if($weekShifts[$i-1]['end_date'] <= strtotime($daysFromStart, $startDate))
				array_slice($weekShifts, $i-1, 1);
			else
				array_slice($weekShifts, $i, 1);
		}
	}
}

//convert database day_of_week to a number 
function DOW_to_number($dow)
{
	if($dow=="monday")
		return 0;
	else if($dow=="tuesday")
		return 1;
	else if($dow=="wednesday")
		return 2;
	else if($dow=="thursday")
		return 4;
	else if($dow=="friday")
		return 5;
	else if($dow=="saturday")
		return 6;
	else 					//gotta be sunday... right? 
		return 7;
}

function miltoampm($hour) 
{  
	$ampm = ($hour >=12 && $hour <24) ? "PM" : "AM";  
	$newhour = ($hour % 12 === 0) ? 12 : $hour % 12;  
	return $newhour . ' ' . $ampm;  
}  

//Found on teh intarwebs
function twelveHourTime($twentyFour) 
{
	$ampm = ($twentyFour >=1200 && $twentyFour < 2400) ? PM : AM;
	//$hour = (floor($twentyFour / 100) % 12 === 0) ? 12 : floor($twentyFour / 100) % 12;
	$hour = ($twentyFour % 12 === 0) ? 12 : $twentyFour % 12;  
	$minute = ($twentyFour % 100 === 0) ? ‘00′ : $twentyFour % 100;
	return "$hour:$minute:$ampm";
}
?>
Return current item: ConPortal