Пример #1
0
/**
* Calculate the engineer working time between two timestamps for a given incident
i.e. ignore times when customer has action
* @author Ivan Lucas
@param int $incidentid - The incident ID to perform a calculation on
@param int $t1 - UNIX Timestamp. Start of range
@param int $t2 - UNIX Timestamp. End of range
@param array $states (optional) Does not count time when the incident is set to
any of the states in this array. (Default is closed, awaiting closure and awaiting customer action)
*/
function calculate_incident_working_time($incidentid, $t1, $t2, $states = array(2, 7, 8))
{
    if ($t1 > $t2) {
        $t3 = $t2;
        $t2 = $t1;
        $t1 = $t3;
    }
    $startofday = mktime(0, 0, 0, date("m", $t1), date("d", $t1), date("Y", $t1));
    $endofday = mktime(23, 59, 59, date("m", $t2), date("d", $t2), date("Y", $t2));
    $publicholidays = get_public_holidays($startofday, $endofday);
    $sql = "SELECT id, currentstatus, timestamp FROM `{$GLOBALS['dbUpdates']}` WHERE incidentid='{$incidentid}' ORDER BY id ASC";
    $result = mysql_query($sql);
    if (mysql_error()) {
        trigger_error(mysql_error(), E_USER_WARNING);
    }
    $time = 0;
    $timeptr = 0;
    $laststatus = 2;
    // closed
    while ($update = mysql_fetch_array($result)) {
        //  if ($t1<=$update['timestamp'])
        if ($t1 <= $update['timestamp']) {
            if ($timeptr == 0) {
                // This is the first update
                // If it's active, set the ptr = t1
                // otherwise set to current timestamp ???
                if (is_active_status($laststatus, $states)) {
                    $timeptr = $t1;
                } else {
                    $timeptr = $update['timestamp'];
                }
            }
            if ($t2 < $update['timestamp']) {
                // If we have reached the very end of the range, increment time to end of range, break
                if (is_active_status($laststatus, $states)) {
                    $time += calculate_working_time($timeptr, $t2, $publicholidays);
                }
                break;
            }
            // if status has changed or this is the first (active update)
            if (is_active_status($laststatus, $states) != is_active_status($update['currentstatus'], $states)) {
                // If it's active and we've not reached the end of the range, increment time
                if (is_active_status($laststatus, $states) && $t2 >= $update['timestamp']) {
                    $time += calculate_working_time($timeptr, $update['timestamp'], $publicholidays);
                } else {
                    $timeptr = $update['timestamp'];
                }
                // if it's not active set the ptr
            }
        }
        $laststatus = $update['currentstatus'];
    }
    mysql_free_result($result);
    // Calculate remainder
    if (is_active_status($laststatus, $states) && $t2 >= $update['timestamp']) {
        $time += calculate_working_time($timeptr, $t2, $publicholidays);
    }
    return $time;
}
 while ($row = mysql_fetch_object($result)) {
     $updatearray[$row->currentstatus]['name'] = $row->name;
     if ($last == -1) {
         $updatearray[$row->currentstatus]['time'] = 0;
     } else {
         $updatearray[$laststatus]['time'] += 60 * calculate_incident_working_time($row->incidentid, $last, $row->timestamp, array(2, 7));
     }
     $laststatus = $row->currentstatus;
     $last = $row->timestamp;
 }
 if ($incident->status == 7 or $incident->status == 2) {
     $end = $incident->closed;
 } else {
     $end = $now;
 }
 $publicholidays = get_public_holidays($incident->opened, $end);
 //calculate the last update
 $updatearray[$laststatus]['time'] += 60 * calculate_working_time($last, time(), $publicholidays);
 echo "<h3>{$strStatusSummary}</h3>";
 if (extension_loaded('gd')) {
     $data = array();
     $legends;
     foreach ($updatearray as $row) {
         array_push($data, $row['time']);
         $legends .= $GLOBALS[$row['name']] . "|";
     }
     $data = implode('|', $data);
     $title = urlencode($strStatusSummary);
     echo "<div style='text-align:center;'>";
     echo "<img src='chart.php?type=pie&data={$data}&legends={$legends}&title={$title}&unit=seconds' />";
     echo "</div>";