public function get_duration($start, $end, $users = null, $customers = null, $projects = null, $activities = null, $filterCleared = null)
{
// -1 for disabled, 0 for only not cleared entries
if (!is_numeric($filterCleared)) {
$filterCleared = -1;
if ($this->kga->getSettings()->isHideClearedEntries()) {
$filterCleared = 0;
}
}
$start = MySQL::SQLValue($start, MySQL::SQLVALUE_NUMBER);
$end = MySQL::SQLValue($end, MySQL::SQLVALUE_NUMBER);
$p = $this->kga['server_prefix'];
$whereClauses = $this->timeSheet_whereClausesFromFilters($users, $customers, $projects, $activities);
if ($start) {
$whereClauses[] = "end > {$start}";
}
if ($end) {
$whereClauses[] = "start < {$end}";
}
if ($filterCleared > -1) {
$whereClauses[] = "cleared = {$filterCleared}";
}
$query = "SELECT start,end,duration FROM {$p}timeSheet\n JOIN {$p}projects USING(projectID)\n JOIN {$p}customers USING(customerID)\n JOIN {$p}users USING(userID)\n JOIN {$p}activities USING(activityID) " . (count($whereClauses) > 0 ? " WHERE " : " ") . implode(" AND ", $whereClauses);
$this->conn->Query($query);
$this->conn->MoveFirst();
$sum = 0;
$consideredStart = 0;
// Consider start of selected range if real start is before
$consideredEnd = 0;
// Consider end of selected range if real end is afterwards
while (!$this->conn->EndOfSeek()) {
$row = $this->conn->Row();
if ($row->start <= $start && $row->end < $end) {
$consideredStart = $start;
$consideredEnd = $row->end;
} elseif ($row->start <= $start && $row->end >= $end) {
$consideredStart = $start;
$consideredEnd = $end;
} elseif ($row->start > $start && $row->end < $end) {
$consideredStart = $row->start;
$consideredEnd = $row->end;
} elseif ($row->start > $start && $row->end >= $end) {
$consideredStart = $row->start;
$consideredEnd = $end;
}
$sum += (int) ($consideredEnd - $consideredStart);
}
return $sum;
}